Search in sources :

Example 66 with IotHubTransportMessage

use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.

the class Mqtt method constructMessage.

/**
 * Converts the provided data and topic string into an instance of Message
 * @param data the payload from the topic
 * @param topic the topic string for this message
 * @return a new instance of Message containing the payload and all the properties in the topic string
 */
private IotHubTransportMessage constructMessage(byte[] data, String topic) {
    IotHubTransportMessage message = new IotHubTransportMessage(data, MessageType.DEVICE_TELEMETRY);
    int propertiesStringStartingIndex = topic.indexOf(MESSAGE_SYSTEM_PROPERTY_IDENTIFIER_ENCODED);
    if (propertiesStringStartingIndex != -1) {
        String propertiesString = topic.substring(propertiesStringStartingIndex);
        assignPropertiesToMessage(message, propertiesString);
        String routeString = topic.substring(0, propertiesStringStartingIndex);
        String[] routeComponents = routeString.split("/");
        if (routeComponents.length > 2 && routeComponents[2].equals(MODULES_PATH_STRING)) {
            message.setConnectionModuleId(routeComponents[3]);
        }
        if (routeComponents.length > 4 && routeComponents[4].equals(INPUTS_PATH_STRING)) {
            message.setInputName(routeComponents[5]);
        }
    }
    return message;
}
Also used : IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)

Example 67 with IotHubTransportMessage

use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.

the class Mqtt method deliveryComplete.

/**
 * Event fired when the message arrived on the MQTT broker.
 * @param iMqttDeliveryToken the MqttDeliveryToken for which the message was successfully sent.
 */
@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
    Message deliveredMessage = null;
    log.trace("Mqtt message with message id {} was acknowledge by service", iMqttDeliveryToken.getMessageId());
    synchronized (this.unacknowledgedSentMessagesLock) {
        if (unacknowledgedSentMessages.containsKey(iMqttDeliveryToken.getMessageId())) {
            log.trace("Mqtt message with message id {} that was acknowledge by service was sent by this client", iMqttDeliveryToken.getMessageId());
            deliveredMessage = unacknowledgedSentMessages.remove(iMqttDeliveryToken.getMessageId());
        } else {
            log.warn("Mqtt message with message id {} that was acknowledge by service was not sent by this client, will be ignored", iMqttDeliveryToken.getMessageId());
        }
    }
    if (deliveredMessage instanceof IotHubTransportMessage) {
        DeviceOperations deviceOperation = ((IotHubTransportMessage) deliveredMessage).getDeviceOperationType();
        if (deviceOperation == DeviceOperations.DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_REQUEST || deviceOperation == DeviceOperations.DEVICE_OPERATION_METHOD_SUBSCRIBE_REQUEST || deviceOperation == DeviceOperations.DEVICE_OPERATION_TWIN_UNSUBSCRIBE_DESIRED_PROPERTIES_REQUEST) {
            // no need to alert the IotHubTransport layer about these messages as they are not tracked in the inProgressQueue
            return;
        }
    }
    if (this.listener != null) {
        this.listener.onMessageSent(deliveredMessage, this.deviceId, null);
    } else {
        log.warn("Message sent, but no listener set");
    }
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) DeviceOperations(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)

Example 68 with IotHubTransportMessage

use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.

the class AmqpsSessionHandler method sendMessage.

SendResult sendMessage(Message message) {
    if (!this.deviceClientConfig.getDeviceId().equals(message.getConnectionDeviceId())) {
        // This should never happen since this session handler was chosen from a map of device Id -> session handler
        // so it should have the same device Id as in the map it was grabbed from.
        log.warn("Failed to send the message because this session belongs to a different device");
        return SendResult.WRONG_DEVICE;
    }
    MessageType messageType = message.getMessageType();
    if (messageType == null) {
        // Twin and method messages have a message type assigned to them when they are constructed by this SDK
        // (users can't construct twin/method messages directly), but telemetry messages don't necessarily have this
        // type assigned since users may create telemetry messages. By default, assume any messages with an
        // unassigned type are telemetry messages.
        messageType = DEVICE_TELEMETRY;
    }
    // Check if the message being sent is a subscription change message. If so, open the corresponding links.
    if (message instanceof IotHubTransportMessage) {
        IotHubTransportMessage transportMessage = (IotHubTransportMessage) message;
        DeviceOperations subscriptionType = transportMessage.getDeviceOperationType();
        if (subscriptionType == DEVICE_OPERATION_METHOD_SUBSCRIBE_REQUEST) {
            return handleMethodSubscriptionRequest(transportMessage);
        }
        if (subscriptionType == DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_REQUEST) {
            return handleTwinSubscriptionRequest(transportMessage);
        }
    }
    AmqpsSenderLinkHandler senderLinkHandler = this.senderLinkHandlers.get(messageType);
    if (senderLinkHandler == null) {
        // before their respective subscription messages have already opened their links.
        return SendResult.LINKS_NOT_OPEN;
    }
    if (messageType == DEVICE_TWIN) {
        if (explicitInProgressTwinSubscriptionMessage != null) {
            // has been acknowledged by the service before sending it.
            return SendResult.SUBSCRIPTION_IN_PROGRESS;
        }
        for (SubscriptionType subscriptionType : this.implicitInProgressSubscriptionMessages.values()) {
            if (subscriptionType == SubscriptionType.DESIRED_PROPERTIES_SUBSCRIPTION) {
                // executes again.
                return SendResult.SUBSCRIPTION_IN_PROGRESS;
            }
        }
    }
    AmqpsSendResult amqpsSendResult = senderLinkHandler.sendMessageAndGetDeliveryTag(message);
    if (amqpsSendResult.isDeliverySuccessful()) {
        return SendResult.SUCCESS;
    }
    return SendResult.UNKNOWN_FAILURE;
}
Also used : DeviceOperations(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) MessageType(com.microsoft.azure.sdk.iot.device.MessageType)

Example 69 with IotHubTransportMessage

use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage 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)

Example 70 with IotHubTransportMessage

use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.

the class DeviceMethod method subscribeToDeviceMethod.

/**
 * A method which subscribes to receive device method invocation for the user with the IotHub.
 * @param deviceMethodCallback Callback where upon receiving the request the
 *                             invoke a method shall be triggered.
 * @param deviceMethodCallbackContext Context to be passed on when invoking the
 *                                    callback.
 * @throws IllegalArgumentException This exception is thrown when deviceMethodCallback is provided null.
 */
public void subscribeToDeviceMethod(DeviceMethodCallback deviceMethodCallback, Object deviceMethodCallbackContext) throws IllegalArgumentException {
    if (deviceMethodCallback == null) {
        throw new IllegalArgumentException("Callback cannot be null");
    }
    this.deviceMethodCallback = deviceMethodCallback;
    this.deviceMethodCallbackContext = deviceMethodCallbackContext;
    if (!isSubscribed) {
        IotHubTransportMessage subscribeMessage = new IotHubTransportMessage(new byte[0], MessageType.DEVICE_METHODS);
        subscribeMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_METHOD_SUBSCRIBE_REQUEST);
        subscribeMessage.setConnectionDeviceId(this.config.getDeviceId());
        this.deviceIO.sendEventAsync(subscribeMessage, new deviceMethodRequestMessageCallback(), null, this.config.getDeviceId());
    }
}
Also used : IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)

Aggregations

IotHubTransportMessage (com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)129 Test (org.junit.Test)108 Message (com.microsoft.azure.sdk.iot.device.Message)37 MutablePair (org.apache.commons.lang3.tuple.MutablePair)33 Pair (org.apache.commons.lang3.tuple.Pair)33 DeviceTwin (com.microsoft.azure.sdk.iot.device.DeviceTwin)30 MqttDeviceTwin (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin)25 HashMap (java.util.HashMap)24 DeviceOperations (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations)16 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)16 MessageType (com.microsoft.azure.sdk.iot.device.MessageType)14 Verifications (mockit.Verifications)10 MqttDeviceMethod (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod)8 NonStrictExpectations (mockit.NonStrictExpectations)8 IOException (java.io.IOException)5 FileUploadTask (com.microsoft.azure.sdk.iot.device.fileupload.FileUploadTask)4 MessageCallback (com.microsoft.azure.sdk.iot.device.MessageCallback)3 MethodResult (com.microsoft.azure.sdk.iot.device.edge.MethodResult)3 HttpsTransportManager (com.microsoft.azure.sdk.iot.device.transport.https.HttpsTransportManager)3 HashSet (java.util.HashSet)3