简体   繁体   English

使用Stream Analytics将Azure IoT中心云到设备消息

[英]Azure IoT hub cloud to device message using Stream Analytics

I am sending data through the path as below. 我通过以下路径发送数据。

Android -> IoT Hub -> Stream Analytics -> SQL Android - > IoT Hub - >流分析 - > SQL

I am calling machine learning function at the query of Stream Analytics. 我在Stream Analytics的查询中调用机器学习功能。 Now, I want to return the result of machine learning to the Android device. 现在,我想将机器学习的结果返回给Android设备。 For receiving cloud-to-device message at Android device, I have done and tested with Device Explorer. 为了在Android设备上接收云到设备消息,我已经完成并使用Device Explorer进行了测试。 I am looking at official tutorials, 1 , 2 but still no clue on how to send cloud to device message using the Stream Analytics. 我在看官方教程, 12 ,但仍然对如何发送云使用流分析设备的消息毫无头绪。 He said using the service bus and function app but did not give the details. 说使用服务总线和功能应用程序,但没有提供详细信息。 I am new to Azure. 我是Azure的新手。 Hoping someone will give me some guidance or any link so I can understand more on how to implement it. 希望有人会给我一些指导或任何链接,以便我可以更多地了解如何实现它。 Thanks in advance. 提前致谢。

You can use an Azure Function (Preview) to output an ASA job for sending a cloud-to-device message via the Azure IoT Hub service-facing endpoint. 您可以使用Azure功能(预览)输出ASA作业, 通过Azure IoT Hub面向服务的端点发送云到设备消息

The following is an example of this function. 以下是此功能的示例。

run.csx: run.csx:

#r "Newtonsoft.Json"

using System.Configuration;
using System.Text;
using System.Net;
using Microsoft.Azure.Devices;
using Newtonsoft.Json;

// create proxy
static Microsoft.Azure.Devices.ServiceClient client = ServiceClient.CreateFromConnectionString(ConfigurationManager.AppSettings["myIoTHub"]);

public static async Task<HttpResponseMessage> Run(string input, HttpRequestMessage req, TraceWriter log)
{
    log.Info($"ASA Job: {input}");

    var data = JsonConvert.DeserializeAnonymousType(input, new[] { new { xyz = "", IoTHub = new { ConnectionDeviceId = ""}}});

    if(!string.IsNullOrEmpty(data[0]?.IoTHub?.ConnectionDeviceId))
    {
        string deviceId = data[0].IoTHub.ConnectionDeviceId;
        log.Info($"Device: {deviceId}");

        // cloud-to-device message 
        var msg = JsonConvert.SerializeObject(new { temp = 20.5 });
        var c2dmsg = new Microsoft.Azure.Devices.Message(Encoding.ASCII.GetBytes(msg));

       // send AMQP message
       await client.SendAsync(deviceId, c2dmsg);
    }

   return req.CreateResponse(HttpStatusCode.NoContent);
}

function.json: function.json:

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "input",
      "type": "httpTrigger",
      "direction": "in"
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    }
  ],
  "disabled": false
}

project.json: project.json:

{
   "frameworks": {
   "net46":{
      "dependencies": {
        "Microsoft.Azure.Devices": "1.3.2"
      }
    }
   }
}

Appendix A 附录A.

For the test purpose, make the following steps: 出于测试目的,请执行以下步骤:

  1. Add the setting myIoTHub in your Azure Function App for your connection string to the Azure IoT Hub. 在Azure功能应用程序中添加myIoTHub设置,以获取连接到Azure IoT Hub的连接字符串。
  2. Create HttpTrigger function, let call it as HttpASA_1 创建HttpTrigger函数,我们将其称为HttpASA_1
  3. Update run.csx, function.json and project.json files with the above contents 使用上述内容更新run.csx,function.json和project.json文件
  4. Use the following sample for Test Request Body: 将以下示例用于测试请求正文:

[ { "time": "2017-11-26T12:52:23.4292501Z", "counter": 57, "windSpeed": 8.7358, "temperature": 16.63, "humidity": 79.42, "EventProcessedUtcTime": "2017-11-26T12:52:21.3568252Z", "PartitionId": 2, "EventEnqueuedUtcTime": "2017-11-26T12:52:22.435Z", "IoTHub": { "MessageId": null, "CorrelationId": null, "ConnectionDeviceId": " Device1 ", "ConnectionDeviceGenerationId": "636189812948967054", "EnqueuedTime": "2017-11-26T12:52:21.562Z", "StreamId": null } } ] [{“time”:“2017-11-26T12:52:23.4292501Z”,“counter”:57,“windSpeed”:8.7358,“temperature”:16.63,“湿度”:79.42,“EventProcessedUtcTime”:“2017- 11-26T12:52:21.3568252Z“,”PartitionId“:2,”EventEnqueuedUtcTime“:”2017-11-26T12:52:22.435Z“,”IoTHub“:{”MessageId“:null,”CorrelationId“:null, “ConnectionDeviceId”:“ Device1 ”,“ConnectionDeviceGenerationId”:“636189812948967054”,“EnqueuedTime”:“2017-11-26T12:52:21.562Z”,“StreamId”:null}}]

Change the value Device1 for your actually deviceId. 更改您的实际设备ID值Device1的

  1. Now, the AF is ready to test it. 现在,AF已准备好进行测试。 Press the button Run to run the sample and look at the Logs progress. 按“ 运行 ”按钮运行示例并查看“日志”进度。
  2. You should see a C2D Message sent by AF on your device, or you can download a small tester Azure IoT Hub Tester to simulate your MQTT Devices. 您应该在设备上看到AF发送的C2D消息,或者您可以下载小型测试仪Azure IoT Hub Tester来模拟您的MQTT设备。 The following screen snippet shows this tester: 以下屏幕截图显示了此测试人员:

测试仪

  1. Now, in this step we can go to the ASA job for invoking this HttpASA_1 function. 现在,在此步骤中,我们可以转到ASA作业来调用此HttpASA_1函数。 Note, that the ASA job invoking only the HttpTrigger function. 请注意,ASA作业仅调用HttpTrigger函数。 The following screen snippet shows adding an output for our Azure Function: 以下屏幕截图显示为Azure功能添加输出:

输出

You should see this function in the combobox when you selected your subscription in the Import option combobox. 当您在导入选项组合框中选择订阅时,您应该在组合框中看到此功能。 Once you done, press the Save button and watch the notification message on the screen. 完成后,按“ 保存”按钮,在屏幕上观看通知消息。 The ASA will send a validation message to your AF and its response status should be 20x code. ASA将向您的AF发送验证消息,其响应状态应为20x代码。

  1. Finally, you can go to the Query to generate an output for your AF. 最后,您可以转到查询以生成AF的输出。 The following screen shows a simple output for all telemetry data to the AF: 以下屏幕显示了AF的所有遥测数据的简单输出:

SQL

Note, the inpsim is my iothub input. 注意, inpsim是我的iothub输入。

The ASA outputs payload for HttpTrigger Function in the following format, see my example: ASA以下列格式输出HttpTrigger函数的有效负载,请参阅我的示例:

[
  {
    "time": "2017-11-26T12:52:23.4292501Z",
    "counter": 57,
    "windSpeed": 8.7358,
    "temperature": 16.63,
    "humidity": 79.42,

    "EventProcessedUtcTime": "2017-11-26T12:52:21.3568252Z",
    "PartitionId": 2,
    "EventEnqueuedUtcTime": "2017-11-26T12:52:22.435Z",
    "IoTHub": {
      "MessageId": null,
      "CorrelationId": null,
      "ConnectionDeviceId": "Device1",
      "ConnectionDeviceGenerationId": "636189812948967054",
      "EnqueuedTime": "2017-11-26T12:52:21.562Z",
      "StreamId": null
    }
  }
]

Note, that my telemetry data (*) are counter , temperature , humidity and timestamp time , so the other properties are created implicitly by ASA job. 请注意,我的遥测数据(*)是计数器温度湿度和时间戳时间 ,因此其他属性由ASA作业隐式创建。 Based on the query, you can create any business properties for C2D message. 根据查询,您可以为C2D消息创建任何业务属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM