Search in sources :

Example 1 with MessageCallback

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));
}
Also used : DeviceClientConfig(com.microsoft.azure.sdk.iot.device.DeviceClientConfig) IotHubConnectionString(com.microsoft.azure.sdk.iot.device.IotHubConnectionString) IotHubConnectionString(com.microsoft.azure.sdk.iot.device.IotHubConnectionString) MessageCallback(com.microsoft.azure.sdk.iot.device.MessageCallback) Test(org.junit.Test)

Example 2 with MessageCallback

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;
}
Also used : Symbol(org.apache.qpid.proton.amqp.Symbol) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) Map(java.util.Map) MessageCallback(com.microsoft.azure.sdk.iot.device.MessageCallback)

Example 3 with MessageCallback

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;
}
Also used : IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) MessageCallback(com.microsoft.azure.sdk.iot.device.MessageCallback)

Example 4 with MessageCallback

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;
}
Also used : DeviceOperations(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations) MessageAnnotations(org.apache.qpid.proton.amqp.messaging.MessageAnnotations) Symbol(org.apache.qpid.proton.amqp.Symbol) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) Properties(org.apache.qpid.proton.amqp.messaging.Properties) Map(java.util.Map) MessageCallback(com.microsoft.azure.sdk.iot.device.MessageCallback)

Aggregations

MessageCallback (com.microsoft.azure.sdk.iot.device.MessageCallback)4 IotHubTransportMessage (com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)3 Map (java.util.Map)2 Symbol (org.apache.qpid.proton.amqp.Symbol)2 DeviceClientConfig (com.microsoft.azure.sdk.iot.device.DeviceClientConfig)1 DeviceOperations (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations)1 IotHubConnectionString (com.microsoft.azure.sdk.iot.device.IotHubConnectionString)1 MessageAnnotations (org.apache.qpid.proton.amqp.messaging.MessageAnnotations)1 Properties (org.apache.qpid.proton.amqp.messaging.Properties)1 Test (org.junit.Test)1