public IotHubMessageResult onCloudToDeviceMessageReceived()

in iothub/device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/twin/DirectMethod.java [37:124]


        public IotHubMessageResult onCloudToDeviceMessageReceived(Message message, Object callbackContext)
        {
            synchronized (DEVICE_METHOD_LOCK)
            {
                IotHubStatusCode iotHubStatus = IotHubStatusCode.ERROR;
                IotHubMessageResult result = IotHubMessageResult.ABANDON;

                if (message.getMessageType() != MessageType.DEVICE_METHODS)
                {
                    /*
                    **Codes_SRS_DEVICEMETHOD_25_009: [**If the received message is not of type DirectMethod and DEVICE_OPERATION_METHOD_RECEIVE_REQUEST then user shall be notified on the status callback registered by the user as ERROR before marking the status of the sent message as Abandon **]**
                     */
                    log.error("Unexpected message type received {}", message.getMessageType());
                    deviceMethodStatusCallback.onMessageSent(message, IotHubStatusCode.toException(iotHubStatus), deviceMethodStatusCallbackContext);
                    return IotHubMessageResult.ABANDON;
                }

                IotHubTransportMessage methodMessage = (IotHubTransportMessage) message;

                if (methodMessage.getDeviceOperationType() == DeviceOperations.DEVICE_OPERATION_METHOD_RECEIVE_REQUEST)
                {
                    if (methodCallback != null)
                    {
                        if (!isSubscribed)
                        {
                            isSubscribed = true;
                        }
                        try
                        {
                            /*
                             **Codes_SRS_DEVICEMETHOD_25_008: [**If the message is of type DirectMethod and DEVICE_OPERATION_METHOD_RECEIVE_REQUEST then user registered device method callback gets invoked providing the user with method name and payload along with the user context. **]**
                             */
                            log.trace("Executing method invocation callback for method name {} for message {}", methodMessage.getMethodName(), methodMessage);

                            MethodParser methodParser = new MethodParser();
                            JsonElement jsonElement = methodParser.getPayloadFromJson(new String(methodMessage.getBytes(), StandardCharsets.UTF_8));
                            DirectMethodResponse responseData = methodCallback.onMethodInvoked(methodMessage.getMethodName(), new DirectMethodPayload(jsonElement), deviceMethodCallbackContext);
                            log.trace("Method invocation callback returned for method name {} for message {}", methodMessage.getMethodName(), methodMessage);

                            /*
                             **Codes_SRS_DEVICEMETHOD_25_010: [**User is expected to provide response message and status upon invoking the device method callback.**]**
                             */
                            if (responseData != null)
                            {
                                /*
                                 **Codes_SRS_DEVICEMETHOD_25_011: [**If the user callback is successful and user has successfully provided the response message and status, then this method shall build a device method message of type DEVICE_OPERATION_METHOD_SEND_RESPONSE, serilize the user data by invoking MethodParser from serializer and save the user data as payload in the message before sending it to IotHub via sendeventAsync before marking the result as complete**]**
                                 **Codes_SRS_DEVICEMETHOD_25_015: [**User can provide null response message upon invoking the device method callback which will be serialized as is, before sending it to IotHub.**]**
                                 */
                                JsonElement payload = new GsonBuilder().create().toJsonTree(responseData.getPayload());
                                MethodParser methodParserObject = new MethodParser(payload);
                                IotHubTransportMessage responseMessage = new IotHubTransportMessage(methodParserObject.toJson().getBytes(StandardCharsets.UTF_8), MessageType.DEVICE_METHODS);
                                responseMessage.setRequestId(methodMessage.getRequestId());
                                responseMessage.setConnectionDeviceId(this.nestedConfig.getDeviceId());
                                responseMessage.setStatus(String.valueOf(responseData.getStatus()));
                                responseMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_METHOD_SEND_RESPONSE);

                                client.sendEventAsync(responseMessage, new DirectMethodRequestMessageCallback(), null);
                                result = IotHubMessageResult.COMPLETE;
                            }
                            else
                            {
                                log.info("User callback did not send any data for response");
                                result = IotHubMessageResult.REJECT;
                                deviceMethodStatusCallback.onMessageSent(message, IotHubStatusCode.toException(iotHubStatus), deviceMethodStatusCallbackContext);
                            }
                        } catch (Exception e)
                        {
                            log.info("User callback did not succeed");
                            result = IotHubMessageResult.REJECT;
                            /*
                             **Codes_SRS_DEVICEMETHOD_25_014: [**If the user invoked callback failed for any reason then the user shall be notified on the status callback registered by the user as ERROR before marking the status of the sent message as Rejected.**]**
                             */
                            deviceMethodStatusCallback.onMessageSent(message, IotHubStatusCode.toException(iotHubStatus), deviceMethodStatusCallbackContext);
                        }
                    }
                    else
                    {
                        log.warn("Received device method request, but device has not setup device method");
                    }
                }
                else
                {
                    log.warn("Received unknown type message for device methods");
                }

                return result;
            }
        }