IotEdge 从设备向中心调用方法

huangapple go评论96阅读模式
英文:

IotEdge Invoke a method from a device to the hub

问题

这是我从设备上的中心调用方法的方式,它可以正常工作。

 DeviceMethod methodClient = DeviceMethod.createFromConnectionString(iotHubConnectionString);
 payload = ...
 String methodName = "insertUser"; 
 result = methodClient.invoke(deviceId, methodName, responseTimeout, connectTimeout, payload);

我如何从设备向中心调用方法?(设备 -> 中心)

由于 deviceId 是必填项,我应该放入什么值?
result = methodClient.invoke(deviceId, methodName, responseTimeout, connectTimeout, payload);

我正在使用这两个 Java 库

        <dependencies>
            <dependency>
                <groupId>com.microsoft.azure.sdk.iot</groupId>
                <artifactId>iot-device-client</artifactId>
                <version>1.17.0</version>
            </dependency>

            <dependency>
                <groupId>com.microsoft.azure.sdk.iot</groupId>
                <artifactId>iot-service-client</artifactId>
                <version>1.17.5</version>
            </dependency>
        </dependencies>
英文:

This way I invoke the methods, from the hub on a device and it works fine.

 DeviceMethod methodClient = DeviceMethod.createFromConnectionString(iotHubConnectionString);
 payload = ...
 String methodName = &quot;insertUser&quot;; 
 result = methodClient.invoke(deviceId, methodName, responseTimeout, connectTimeout, payload);

How can I invoke a method from the device to the hub? (device -> hub)

As deviceId is mandatory, what values shall I put?
result = methodClient.invoke(deviceId, methodName, responseTimeout, connectTimeout, payload);

I am using these 2 Java libraries

        &lt;dependencies&gt;
            &lt;dependency&gt;
                &lt;groupId&gt;com.microsoft.azure.sdk.iot&lt;/groupId&gt;
                &lt;artifactId&gt;iot-device-client&lt;/artifactId&gt;
                &lt;version&gt;1.17.0&lt;/version&gt;
            &lt;/dependency&gt;

            &lt;dependency&gt;
                &lt;groupId&gt;com.microsoft.azure.sdk.iot&lt;/groupId&gt;
                &lt;artifactId&gt;iot-service-client&lt;/artifactId&gt;
                &lt;version&gt;1.17.5&lt;/version&gt;
            &lt;/dependency&gt;
        &lt;/dependencies&gt;

答案1

得分: 1

直接方法是在您希望从服务端对设备执行特定功能时使用的方法。

根据了解并从IoT Hub调用直接方法中的定义:

> IoT Hub使您能够在云端对设备调用直接方法。直接方法代表与设备之间的请求-响应交互,类似于HTTP调用,它们会立即成功或失败(在用户指定的超时后)。这种方法对于根据设备是否能够响应而决定即时操作的情景非常有用。

因此,不打算从设备本身调用直接方法。

英文:

Direct Methods are used when you want to invoke a specific function on your Device from the Service Side.

From the definition in Understand and invoke direct methods from IoT Hub

> IoT Hub gives you the ability to invoke direct methods on devices from the cloud. Direct methods represent a request-reply interaction with a device similar to an HTTP call in that they succeed or fail immediately (after a user-specified timeout). This approach is useful for scenarios where the course of immediate action is different depending on whether the device was able to respond.

Therefore it is not intended that you call a direct method from the device itself.

huangapple
  • 本文由 发表于 2020年7月23日 19:59:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63053738.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定