如何通过 C# 中的 Azure 函数在设备中执行方法?
How can I execute a method in a device from an Azure function in C#?
我有一个流分析,它根据接收到的内容将一些数据输出到 C# 中的 Azure 函数。此函数必须向设备发送 JSON,当它读取 JSON 时,它会根据接收到的数据运行本地方法。我的问题是我无法找到执行此操作的方法。
我已经在 Java 中完成了此操作,但我找不到在 C# 中执行此操作的方法。我这样做的方法是使用 HTTP 触发器和下面的代码。
这是我反序列化接收到的数据的方式:
String body = request.getBody().get().toString().replace("[", "").replace("\"", "\'");
JSONObject bodyJson = new JSONObject(body);
String deviceMac = bodyJson.getString("deviceid");
deviceId = bodyJson.getString("receiveruuid");
这就是我将结果发送到设备的方式:
DeviceMethod methodClient = DeviceMethod.createFromConnectionString(iotHubConnectionString);
Map<String, Object> payload = new HashMap<String, Object>() {
{
//PAYLOAD DATA
}};
MethodResult result = methodClient.invoke(deviceId, methodName, responseTimeout, connectTimeout, payload);
感谢您的帮助
更新
我终于通过这种方式解决了问题。
var serviceClient = ServiceClient.CreateFromConnectionString(iotHubConnectionString);
var methodname = new CloudToDeviceMethod("method_defatult").SetPayloadJson(" {\"message\": \""+ message +"\"}");
await serviceClient.InvokeDeviceMethodAsync("moviltest", methodname);
在第一行,您创建了到 iotHub 的连接字符串。第二行是创建 CloudToDeviceMethod 并设置负载。确保 JSON 正确。 最后调用设备中的方法。
您需要从消息中获取字节并将其发送到设备:
private async static Task SendCloudToDeviceMessageAsync()
{
var serviceClient = ServiceClient.CreateFromConnectionString(iotHubConnectionString);
var commandMessage = new
Message(Encoding.ASCII.GetBytes("Cloud to device message."));
await serviceClient.SendAsync("myFirstDevice", commandMessage);
}
更多信息在这里:https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-csharp-csharp-c2d
我终于这样解决了问题
var serviceClient = ServiceClient.CreateFromConnectionString(iotHubConnectionString);
var methodname = new CloudToDeviceMethod("method_defatult").SetPayloadJson(" {\"message\": \""+ message +"\"}");
await serviceClient.InvokeDeviceMethodAsync("moviltest", methodname);
在第一行中,您创建了到 iotHub 的连接字符串。第二行是创建 CloudToDeviceMethod 并设置负载。确保 JSON 正确。最后它调用设备中的方法。
我有一个流分析,它根据接收到的内容将一些数据输出到 C# 中的 Azure 函数。此函数必须向设备发送 JSON,当它读取 JSON 时,它会根据接收到的数据运行本地方法。我的问题是我无法找到执行此操作的方法。
我已经在 Java 中完成了此操作,但我找不到在 C# 中执行此操作的方法。我这样做的方法是使用 HTTP 触发器和下面的代码。
这是我反序列化接收到的数据的方式:
String body = request.getBody().get().toString().replace("[", "").replace("\"", "\'");
JSONObject bodyJson = new JSONObject(body);
String deviceMac = bodyJson.getString("deviceid");
deviceId = bodyJson.getString("receiveruuid");
这就是我将结果发送到设备的方式:
DeviceMethod methodClient = DeviceMethod.createFromConnectionString(iotHubConnectionString);
Map<String, Object> payload = new HashMap<String, Object>() {
{
//PAYLOAD DATA
}};
MethodResult result = methodClient.invoke(deviceId, methodName, responseTimeout, connectTimeout, payload);
感谢您的帮助
更新
我终于通过这种方式解决了问题。
var serviceClient = ServiceClient.CreateFromConnectionString(iotHubConnectionString);
var methodname = new CloudToDeviceMethod("method_defatult").SetPayloadJson(" {\"message\": \""+ message +"\"}");
await serviceClient.InvokeDeviceMethodAsync("moviltest", methodname);
在第一行,您创建了到 iotHub 的连接字符串。第二行是创建 CloudToDeviceMethod 并设置负载。确保 JSON 正确。 最后调用设备中的方法。
您需要从消息中获取字节并将其发送到设备:
private async static Task SendCloudToDeviceMessageAsync()
{
var serviceClient = ServiceClient.CreateFromConnectionString(iotHubConnectionString);
var commandMessage = new
Message(Encoding.ASCII.GetBytes("Cloud to device message."));
await serviceClient.SendAsync("myFirstDevice", commandMessage);
}
更多信息在这里:https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-csharp-csharp-c2d
我终于这样解决了问题
var serviceClient = ServiceClient.CreateFromConnectionString(iotHubConnectionString);
var methodname = new CloudToDeviceMethod("method_defatult").SetPayloadJson(" {\"message\": \""+ message +"\"}");
await serviceClient.InvokeDeviceMethodAsync("moviltest", methodname);
在第一行中,您创建了到 iotHub 的连接字符串。第二行是创建 CloudToDeviceMethod 并设置负载。确保 JSON 正确。最后它调用设备中的方法。