use of com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations in project azure-iot-sdk-java by Azure.
the class AmqpsTwinReceiverLinkHandler method protonMessageToIoTHubMessage.
@Override
protected IotHubTransportMessage protonMessageToIoTHubMessage(AmqpsMessage protonMsg) {
IotHubTransportMessage iotHubTransportMessage = super.protonMessageToIoTHubMessage(protonMsg);
MessageCallback messageCallback = deviceClientConfig.getDeviceTwinMessageCallback();
Object messageContext = deviceClientConfig.getDeviceTwinMessageContext();
iotHubTransportMessage.setMessageCallback(messageCallback);
iotHubTransportMessage.setMessageCallbackContext(messageContext);
iotHubTransportMessage.setMessageType(MessageType.DEVICE_TWIN);
iotHubTransportMessage.setDeviceOperationType(DEVICE_OPERATION_UNKNOWN);
MessageAnnotations messageAnnotations = protonMsg.getMessageAnnotations();
if (messageAnnotations != null) {
for (Map.Entry<Symbol, Object> entry : messageAnnotations.getValue().entrySet()) {
Symbol key = entry.getKey();
Object value = entry.getValue();
if (key.toString().equals(MESSAGE_ANNOTATION_FIELD_KEY_STATUS)) {
iotHubTransportMessage.setStatus(value.toString());
} else if (key.toString().equals(MESSAGE_ANNOTATION_FIELD_KEY_VERSION)) {
iotHubTransportMessage.setVersion(value.toString());
} else if (key.toString().equals(MESSAGE_ANNOTATION_FIELD_KEY_RESOURCE) && value.toString().equals(MESSAGE_ANNOTATION_FIELD_VALUE_PROPERTIES_DESIRED)) {
iotHubTransportMessage.setDeviceOperationType(DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_RESPONSE);
}
}
}
Properties properties = protonMsg.getProperties();
if (properties != null && properties.getCorrelationId() != null) {
iotHubTransportMessage.setCorrelationId(properties.getCorrelationId().toString());
if (twinOperationCorrelationMap.containsKey(properties.getCorrelationId().toString())) {
DeviceOperations deviceOperations = twinOperationCorrelationMap.get(properties.getCorrelationId().toString());
switch(deviceOperations) {
case DEVICE_OPERATION_TWIN_GET_REQUEST:
iotHubTransportMessage.setDeviceOperationType(DEVICE_OPERATION_TWIN_GET_RESPONSE);
break;
case DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_REQUEST:
iotHubTransportMessage.setDeviceOperationType(DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_RESPONSE);
break;
case DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_REQUEST:
iotHubTransportMessage.setDeviceOperationType(DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_RESPONSE);
break;
case DEVICE_OPERATION_TWIN_UNSUBSCRIBE_DESIRED_PROPERTIES_REQUEST:
iotHubTransportMessage.setDeviceOperationType(DEVICE_OPERATION_TWIN_UNSUBSCRIBE_DESIRED_PROPERTIES_RESPONSE);
break;
default:
log.error("Unrecognized device operation type during conversion of proton message into an iothub message");
}
this.twinOperationCorrelationMap.remove(properties.getCorrelationId().toString());
}
} else if (iotHubTransportMessage.getDeviceOperationType() == DEVICE_OPERATION_UNKNOWN) {
iotHubTransportMessage.setDeviceOperationType(DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_RESPONSE);
if (iotHubTransportMessage.getStatus() == null || iotHubTransportMessage.getStatus().isEmpty()) {
iotHubTransportMessage.setStatus(DEFAULT_STATUS_CODE);
}
}
return iotHubTransportMessage;
}
use of com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwinTest method receiveSetsReqIdOnResTopic.
/*
**Tests_SRS_MQTTDEVICETWIN_25_040: [If the topic is of type response topic then this method shall parse further to look for request id which if found is set by calling setRequestId]
*/
@Test
public void receiveSetsReqIdOnResTopic() throws TransportException {
final byte[] actualPayload = "GetTwinResponseDataContainingDesiredAndReportedPropertiesDocument".getBytes(StandardCharsets.UTF_8);
final String expectedTopic = "$iothub/twin/res/" + "200" + "/?$rid=" + mockReqId;
IotHubTransportMessage receivedMessage = null;
try {
// arrange
MqttDeviceTwin testTwin = new MqttDeviceTwin("", mockedConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
Queue<Pair<String, byte[]>> testreceivedMessages = new ConcurrentLinkedQueue<>();
testreceivedMessages.add(new MutablePair<>(expectedTopic, actualPayload));
Deencapsulation.setField(testTwin, "receivedMessages", testreceivedMessages);
Deencapsulation.setField(testTwin, "stateLock", new Object());
Map<String, DeviceOperations> requestMap = new HashMap<>();
requestMap.put(mockReqId, DEVICE_OPERATION_TWIN_GET_REQUEST);
Deencapsulation.setField(testTwin, "requestMap", requestMap);
// act
receivedMessage = testTwin.receive();
} finally {
// assert
assertNotNull(receivedMessage);
assertSame(receivedMessage.getMessageType(), MessageType.DEVICE_TWIN);
assertSame(receivedMessage.getDeviceOperationType(), DEVICE_OPERATION_TWIN_GET_RESPONSE);
assertEquals(receivedMessage.getRequestId(), mockReqId);
assertEquals("200", receivedMessage.getStatus());
assertNull(receivedMessage.getVersion());
}
}
use of com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwinTest method receiveDoesNotSetReqIdOnResTopicIfNotFound.
@Test
public void receiveDoesNotSetReqIdOnResTopicIfNotFound() throws TransportException {
final byte[] actualPayload = "GetTwinResponseDataContainingDesiredAndReportedPropertiesDocument".getBytes(StandardCharsets.UTF_8);
final String expectedTopic = "$iothub/twin/res/" + "200";
IotHubTransportMessage receivedMessage = null;
try {
// arrange
MqttDeviceTwin testTwin = new MqttDeviceTwin("", mockedConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
Queue<Pair<String, byte[]>> testreceivedMessages = new ConcurrentLinkedQueue<>();
testreceivedMessages.add(new MutablePair<>(expectedTopic, actualPayload));
Deencapsulation.setField(testTwin, "receivedMessages", testreceivedMessages);
Map<String, DeviceOperations> requestMap = new HashMap<>();
requestMap.put(mockReqId, DEVICE_OPERATION_TWIN_GET_REQUEST);
Deencapsulation.setField(testTwin, "requestMap", requestMap);
Deencapsulation.setField(testTwin, "stateLock", new Object());
// act
receivedMessage = testTwin.receive();
} finally {
// assert
assertNotNull(receivedMessage);
assertSame(receivedMessage.getMessageType(), MessageType.DEVICE_TWIN);
assertSame(receivedMessage.getDeviceOperationType(), DEVICE_OPERATION_UNKNOWN);
assertNull(receivedMessage.getRequestId());
assertEquals("200", receivedMessage.getStatus());
assertNull(receivedMessage.getVersion());
}
}
use of com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwinTest method receiveSetsDataForGetTwinResp.
/*
**Tests_SRS_MQTTDEVICETWIN_25_044: [If the topic is of type response then this method shall set data and operation type as DEVICE_OPERATION_TWIN_GET_RESPONSE if data is not null]
*/
@Test
public void receiveSetsDataForGetTwinResp() throws TransportException {
final byte[] actualPayload = "GetTwinResponseDataContainingDesiredAndReportedPropertiesDocument".getBytes(StandardCharsets.UTF_8);
final String expectedTopic = "$iothub/twin/res/" + "200" + "/?$rid=" + mockReqId;
IotHubTransportMessage receivedMessage = null;
try {
// arrange
MqttDeviceTwin testTwin = new MqttDeviceTwin("", mockedConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
Queue<Pair<String, byte[]>> testreceivedMessages = new ConcurrentLinkedQueue<>();
testreceivedMessages.add(new MutablePair<>(expectedTopic, actualPayload));
Deencapsulation.setField(testTwin, "receivedMessages", testreceivedMessages);
Deencapsulation.setField(testTwin, "stateLock", new Object());
Map<String, DeviceOperations> requestMap = new HashMap<>();
requestMap.put(mockReqId, DEVICE_OPERATION_TWIN_GET_REQUEST);
Deencapsulation.setField(testTwin, "requestMap", requestMap);
// act
receivedMessage = testTwin.receive();
} finally {
// assert
assertNotNull(receivedMessage);
assertSame(receivedMessage.getMessageType(), MessageType.DEVICE_TWIN);
assertSame(receivedMessage.getDeviceOperationType(), DEVICE_OPERATION_TWIN_GET_RESPONSE);
assertEquals(receivedMessage.getRequestId(), mockReqId);
assertEquals("200", receivedMessage.getStatus());
assertNull(receivedMessage.getVersion());
byte[] receivedMessageBytes = receivedMessage.getBytes();
assertEquals(receivedMessageBytes.length, actualPayload.length);
for (int i = 0; i < receivedMessageBytes.length; i++) {
assertEquals(receivedMessageBytes[i], actualPayload[i]);
}
}
}
Aggregations