コーディングに Java を使用して Azure IoT Platform に取り組んでおり、コマンドをデバイスに送信する際に問題に直面しています。送信したコマンドのフィードバックを受信しない場合、feedbackReceiver.receiveAsync() メソッドで null ポインター例外を受信します。以下は私のコードスニペットです。
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.");
}
この問題で助けてくれてありがとう:)