use of com.microsoft.azure.sdk.iot.device.MessageCallback in project azure-iot-sdk-java by Azure.
the class DeviceClientConfigTest method getAndSetMessageCallbackMatch.
// Tests_SRS_DEVICECLIENTCONFIG_11_006: [The function shall set the message callback, with its associated context.]
// Tests_SRS_DEVICECLIENTCONFIG_11_010: [The function shall return the current message callback.]
@Test
public void getAndSetMessageCallbackMatch(@Mocked final MessageCallback mockCallback) throws URISyntaxException {
final String iotHubHostname = "test.iothubhostname";
final String deviceId = "test-deviceid";
final String deviceKey = "test-devicekey";
final String sharedAccessToken = null;
final IotHubConnectionString iotHubConnectionString = Deencapsulation.newInstance(IotHubConnectionString.class, new Class[] { String.class, String.class, String.class, String.class }, iotHubHostname, deviceId, deviceKey, sharedAccessToken);
DeviceClientConfig config = new DeviceClientConfig(iotHubConnectionString);
Object context = new Object();
config.setMessageCallback(mockCallback, context);
MessageCallback testCallback = config.getMessageCallback();
final MessageCallback expectedCallback = mockCallback;
assertThat(testCallback, is(expectedCallback));
}
use of com.microsoft.azure.sdk.iot.device.MessageCallback in project azure-iot-sdk-java by Azure.
the class AmqpsTelemetryReceiverLinkHandler method protonMessageToIoTHubMessage.
@Override
protected IotHubTransportMessage protonMessageToIoTHubMessage(AmqpsMessage protonMsg) {
IotHubTransportMessage iotHubTransportMessage = super.protonMessageToIoTHubMessage(protonMsg);
iotHubTransportMessage.setMessageType(MessageType.DEVICE_TELEMETRY);
iotHubTransportMessage.setDeviceOperationType(DEVICE_OPERATION_UNKNOWN);
if (protonMsg.getMessageAnnotations() != null && protonMsg.getMessageAnnotations().getValue() != null) {
Map<Symbol, Object> applicationProperties = protonMsg.getMessageAnnotations().getValue();
for (Map.Entry<Symbol, Object> entry : applicationProperties.entrySet()) {
String propertyKey = entry.getKey().toString();
if (propertyKey.equals(INPUT_NAME_PROPERTY_KEY)) {
iotHubTransportMessage.setInputName(entry.getValue().toString());
}
}
}
// inputName may be null, and if it is, then the default callback and default callback context will be used from config
String inputName = iotHubTransportMessage.getInputName();
MessageCallback messageCallback = deviceClientConfig.getDeviceTelemetryMessageCallback(inputName);
Object messageContext = deviceClientConfig.getDeviceTelemetryMessageContext(inputName);
iotHubTransportMessage.setMessageCallback(messageCallback);
iotHubTransportMessage.setMessageCallbackContext(messageContext);
return iotHubTransportMessage;
}
use of com.microsoft.azure.sdk.iot.device.MessageCallback in project azure-iot-sdk-java by Azure.
the class AmqpsMethodsReceiverLinkHandler method protonMessageToIoTHubMessage.
@Override
protected IotHubTransportMessage protonMessageToIoTHubMessage(AmqpsMessage protonMsg) {
IotHubTransportMessage iotHubTransportMessage = super.protonMessageToIoTHubMessage(protonMsg);
iotHubTransportMessage.setMessageType(MessageType.DEVICE_METHODS);
iotHubTransportMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_METHOD_RECEIVE_REQUEST);
MessageCallback messageCallback = deviceClientConfig.getDeviceMethodsMessageCallback();
Object messageContext = deviceClientConfig.getDeviceMethodsMessageContext();
iotHubTransportMessage.setMessageCallback(messageCallback);
iotHubTransportMessage.setMessageCallbackContext(messageContext);
if (protonMsg.getApplicationProperties() != null && protonMsg.getApplicationProperties().getValue() != null) {
Map<String, Object> applicationProperties = protonMsg.getApplicationProperties().getValue();
if (applicationProperties.containsKey(APPLICATION_PROPERTY_KEY_IOTHUB_METHOD_NAME)) {
iotHubTransportMessage.setMethodName(applicationProperties.get(APPLICATION_PROPERTY_KEY_IOTHUB_METHOD_NAME).toString());
}
}
if (protonMsg.getProperties() != null && protonMsg.getProperties().getCorrelationId() != null) {
iotHubTransportMessage.setRequestId(protonMsg.getProperties().getCorrelationId().toString());
}
return iotHubTransportMessage;
}
use of com.microsoft.azure.sdk.iot.device.MessageCallback 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;
}
Aggregations