Skip to content
This repository has been archived by the owner on Jun 14, 2024. It is now read-only.

Commit

Permalink
#22 Adding deviceId to REC content in MQTT
Browse files Browse the repository at this point in the history
  • Loading branch information
baardl committed Jan 16, 2020
1 parent 51bcddc commit 368cb5d
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 14 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@
<version>3.2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.5.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
48 changes: 37 additions & 11 deletions src/main/java/no/entra/bacnet/agent/mqtt/AzureIoTMqttClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import com.microsoft.azure.sdk.iot.device.DeviceClient;
import com.microsoft.azure.sdk.iot.device.IotHubClientProtocol;
import com.microsoft.azure.sdk.iot.device.Message;
import no.entra.bacnet.agent.devices.DeviceId;
import no.entra.bacnet.agent.mqtt.azureiot.Observation;
import no.entra.bacnet.agent.mqtt.azureiot.SendReceive;
import no.entra.bacnet.rec.ConfigurationRequest;
import no.entra.bacnet.rec.RealEstateCore;
import org.slf4j.Logger;

Expand Down Expand Up @@ -35,23 +38,46 @@ public AzureIoTMqttClient(String deviceConnectionString) throws URISyntaxExcepti
connect();
}

/**
* Used for testing
*/
protected AzureIoTMqttClient() {
log.warn("Creating AzureIoTMqttClient for offline testing only.");
}

@Override
public void publishRealEstateCore(RealEstateCore message, Optional<InetAddress> senderAddress) {
if (message != null) {
String msgStr = message.toJson();
Message msg = new Message(msgStr);
msg.setContentTypeFinal("application/json");
if (senderAddress.isPresent()) {
msg.setProperty(MESSAGE_FROM, senderAddress.get().toString());
}
msg.setProperty(MESSAGE_TYPE, REAL_ESTATE_CORE);
msg.setMessageId(java.util.UUID.randomUUID().toString());
msg.setExpiryTime(D2C_MESSAGE_TIMEOUT);
public void publishRealEstateCore(RealEstateCore recMessage, DeviceId recDeviceId, Optional<InetAddress> senderAddress) {
if (recMessage != null) {
Message msg = buildMqttMessage(recMessage, recDeviceId, senderAddress);
sendMessage(msg);
}

}

public Message buildMqttMessage(RealEstateCore recMessage, DeviceId recDeviceId, Optional<InetAddress> senderAddress) {
String msgStr = "{ \n";
if (recDeviceId != null) {
msgStr += "\"deviceId\": \"" + recDeviceId.getId() + "\",\n";
}
if (recMessage != null) {
if (recMessage instanceof ConfigurationRequest) {
msgStr += "\"configurations\": [" + recMessage.toJson() + "]\n";
} else if (recMessage instanceof Observation) {
msgStr += "\"observations\": [" + recMessage.toJson() + "]\n";
}
}
msgStr += "}";
Message msg = new Message(msgStr);
msg.setContentTypeFinal("application/json");
if (senderAddress.isPresent()) {
msg.setProperty(MESSAGE_FROM, senderAddress.get().toString());
}
msg.setProperty(MESSAGE_TYPE, REAL_ESTATE_CORE);
msg.setMessageId(java.util.UUID.randomUUID().toString());
msg.setExpiryTime(D2C_MESSAGE_TIMEOUT);
return msg;
}

@Override
public void publishUnknownHexString(String hexString) {
Message msg = new Message(hexString);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/no/entra/bacnet/agent/mqtt/MqttClient.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package no.entra.bacnet.agent.mqtt;

import no.entra.bacnet.agent.devices.DeviceId;
import no.entra.bacnet.rec.RealEstateCore;

import java.net.InetAddress;
import java.util.Optional;

public interface MqttClient {

void publishRealEstateCore(RealEstateCore message, Optional<InetAddress> sourceAddress);
void publishRealEstateCore(RealEstateCore message, DeviceId recDeviceId, Optional<InetAddress> sourceAddress);
void publishUnknownHexString(String hexString);
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ public void bacnetHexStringReceived(InetAddress sourceAddress, String hexString)
if (bacnetJson != null) {
RealEstateCore message = null;
try {
DeviceId deviceId = findDeviceId(bacnetJson);
DeviceId recDeviceId = findDeviceId(bacnetJson);
message = Bacnet2Rec.bacnetToRec(bacnetJson);
if (message != null) {
message.setSenderAddress(sourceAddress.toString());
mqttClient.publishRealEstateCore(message, Optional.of(sourceAddress));
mqttClient.publishRealEstateCore(message, recDeviceId, Optional.of(sourceAddress));
log.info("Message is published from bacnetJson: {}", bacnetJson);
} else {
log.trace("Could not send empty message from bacnetJson: {}", bacnetJson);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package no.entra.bacnet.agent.mqtt;

import com.microsoft.azure.sdk.iot.device.Message;
import no.entra.bacnet.agent.devices.DeviceId;
import no.entra.bacnet.rec.ConfigurationRequest;
import no.entra.bacnet.rec.helpers.DateTimeHelper;
import org.junit.Before;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;

import java.time.LocalDateTime;
import java.util.Optional;

import static org.junit.Assert.assertNotNull;

public class AzureIoTMqttClientTest {

private LocalDateTime observationTime;
private AzureIoTMqttClient mqttClient;
private String expectedJson = "{\n" +
" \"deviceId\": \"id1234\",\n" +
" \"configurations\": [{\n" +
" \"sender\": \"sendt from\",\n" +
" \"recipient\": \"recip\",\n" +
" \"observationTime\": \"2019-12-09T20:57:17.776468\",\n" +
" \"id\": \"123\",\n" +
" \"type\": \"IHave\",\n" +
" \"properties\": {\n" +
" \"ObjectName\": \"TFM434\"\n" +
" }\n" +
" }]\n" +
"}";

@Before
public void setUp() throws Exception {
String localDateTime = "2019-12-09T20:57:17.776468";
observationTime = DateTimeHelper.fromIsoString(localDateTime);
mqttClient = new AzureIoTMqttClient();
}

@Test
public void buildMqttMessage() {
ConfigurationRequest recMessage = new ConfigurationRequest();
recMessage.setObservationTime(observationTime);
recMessage.setId("123");
recMessage.setRecipient("recip");
recMessage.setSender("sendt from");
recMessage.setType("IHave");
recMessage.addProperty("ObjectName", "TFM434");
DeviceId recDeviceId = new DeviceId("id1234");
recDeviceId.setTfmTag("TFM767");
Message mqttMessage = mqttClient.buildMqttMessage(recMessage, recDeviceId, Optional.empty());
assertNotNull(mqttMessage);
byte[] bodyBytes = mqttMessage.getBytes();
String bodyJson = new String(bodyBytes);
assertNotNull(bodyJson);
JSONAssert.assertEquals(expectedJson, bodyJson, true);
}
}

0 comments on commit 368cb5d

Please sign in to comment.