Azure Iot发送命令:反馈接收器在反馈未收到时显示空指针

时间:2016-06-01 06:42:41

标签: java azure iot azure-iot-hub azure-iot-suite

我正在开发Azure Iot平台,使用Java进行编码,并在向设备发送命令时遇到问题。当我收到我发送的命令的反馈时,我在feedbackReceiver.receiveAsync()方法中收到空指针异常。以下是我的代码段。

   public void sendCommandToDevice(String data, String deviceId, boolean gladiusChildFlag) throws Exception {
        LOGGER.info("********* Starting ServiceClient sample...");

        openServiceClient();
        openFeedbackReceiver(deviceId);

        // String commandMessage = data;
        CommandData commandData = new CommandData();
        commandData.setName("ChangeLEDState");
        commandData.setMessageId("123223");
        commandData.setCreatedTime(new Date().toString());
        if(gladiusChildFlag==false)
        {
        HashMap<String, Object> parameter = new HashMap<String, Object>();
        parameter.put("LEDState", data);
        commandData.setParameters(parameter);
        }
        else
        {
        commandData.setGladiusParameters(data); 
        }
        LOGGER.info(commandData.serialize());
        LOGGER.info("********* Sending message to device...");
        Message messageToSend = new Message(commandData.serialize());
        messageToSend.setDeliveryAcknowledgement(DeliveryAcknowledgement.Full);

        // Setting standard properties
        messageToSend.setMessageId(java.util.UUID.randomUUID().toString());
        Date now = new Date();
        messageToSend.setExpiryTimeUtc(new Date(now.getTime() + 60 * 1000));
        messageToSend.setCorrelationId(java.util.UUID.randomUUID().toString());
        messageToSend.setUserId(java.util.UUID.randomUUID().toString());
        messageToSend.clearCustomProperties();

        // Setting user properties
        Map<String, String> propertiesToSend = new HashMap<String, String>();
        propertiesToSend.put("mycustomKey1", "mycustomValue1");
        propertiesToSend.put("mycustomKey2", "mycustomValue2");
        propertiesToSend.put("mycustomKey3", "mycustomValue3");
        propertiesToSend.put("mycustomKey4", "mycustomValue4");
        propertiesToSend.put("mycustomKey5", "mycustomValue5");
        messageToSend.setProperties(propertiesToSend);

        CompletableFuture<Void> completableFuture = serviceClient.sendAsync(deviceId, messageToSend);
        completableFuture.get();

        LOGGER.info("********* Waiting for feedback...");
//recieving null pointer in the line below
        CompletableFuture<FeedbackBatch> future = feedbackReceiver.receiveAsync();
        FeedbackBatch feedbackBatch = future.get();
        LOGGER.info(
                "********* Feedback received, feedback time: " + feedbackBatch.getEnqueuedTimeUtc().toString());

        closeFeedbackReceiver();
        closeServiceClient();

        LOGGER.info("********* Shutting down ServiceClient sample...");
        // System.exit(0);

    }

    protected static void openServiceClient() throws Exception {
        LOGGER.info("Creating ServiceClient...");
        serviceClient = ServiceClient.createFromConnectionString(connectionString, protocol_Service);

        CompletableFuture<Void> future = serviceClient.openAsync();
        future.get();
        LOGGER.info("********* Successfully created an ServiceClient.");
    }

    protected static void closeServiceClient() throws ExecutionException, InterruptedException, IOException {
        serviceClient.close();

        CompletableFuture<Void> future = serviceClient.closeAsync();
        future.get();
        serviceClient = null;
        System.out.println("********* Successfully closed ServiceClient.");
    }

    protected static void openFeedbackReceiver(String deviceId) throws ExecutionException, InterruptedException {
        if (serviceClient != null) {
            feedbackReceiver = serviceClient.getFeedbackReceiver(deviceId);
            if (feedbackReceiver != null) {
                CompletableFuture<Void> future = feedbackReceiver.openAsync();
                future.get();
                LOGGER.info("********* Successfully opened FeedbackReceiver...");
            }
        }
    }

    protected static void closeFeedbackReceiver() throws ExecutionException, InterruptedException {
        CompletableFuture<Void> future = feedbackReceiver.closeAsync();
        future.get();
        feedbackReceiver = null;
        LOGGER.info("********* Successfully closed FeedbackReceiver.");
    }

在此问题上提前感谢您的帮助:)

0 个答案:

没有答案