0

我在 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.");
    }

在此先感谢您在此问题上的一些帮助:)

4

0 回答 0