【问题标题】:How to filter IOT messages from different devices in azure function app?如何在 azure function app 中过滤来自不同设备的 IOT 消息?
【发布时间】:2018-07-18 17:01:24
【问题描述】:

我在 Azure 中创建了一个 IOT Hub 并在其中添加了两个设备。我从两个设备发送相同的消息。我还在 Azure 中创建了一个function app。该功能由 Azure IoT Hub 触发,并将数据保存到 azure 存储表。一切都按预期工作。现在我想使用设备 ID 过滤消息。我的意思是我的 azure 函数应用程序应该能够识别消息来自哪个设备。

这是我的函数应用 java 脚本代码

module.exports = function (context, iotHubMessage) {

context.log(iotHubMessage.length + ' Message received');

var date = Date.now();
var partitionKey = Math.floor(date / (24 * 60 * 60 * 1000)) + '';
var rowKey = date;

context.bindings.messageLog1 = {
    "partitionKey": partitionKey,
    "rowKey": rowKey + '',
    "MsgCount": iotHubMessage.length,
    "data": JSON.stringify(iotHubMessage)
};

var defaultData = [];

for (var i = 0; i < iotHubMessage.length; i++) {

    var iotMsgObj = iotHubMessage[i];

    iotMsgObj.CreatedDate = new Date();

    defaultData.push({ "partitionKey": partitionKey, "rowKey": (rowKey+i) + '', "data": JSON.stringify(iotMsgObj) });
}

context.bindings.pbDefaultPara1 = defaultData;

context.done();
};

谢谢!

【问题讨论】:

  • 嗨,Jignesh,你试过我的解决方案了吗?它对你有用吗?

标签: azure azure-functions azure-iot-hub


【解决方案1】:

JavaScript中,可以使用以下方法获取设备id:

context.bindingData.systemProperties["iothub-connection-device-id"]

index.js 看起来像这样:

    module.exports = function (context, IoTHubMessages) {
    context.log('systemProperties', context.bindingData.systemProperties, 'of type', (typeof context.bindingData.systemProperties));
    context.log('DeviceId: ', context.bindingData.systemProperties["iothub-connection-device-id"]);
    context.done();
};

日志是:

2018-07-18T08:55:17.122 [Info] systemProperties { 'iothub-connection-device-id': 'device1',
  'iothub-connection-auth-method': '{"scope":"device","type":"sas","issuer":"iothub","acceptingIpFilterRule":null}',
  'iothub-connection-auth-generation-id': '636627421237258770',
  'iothub-enqueuedtime': 2018-07-18T08:55:16.193Z,
  'iothub-message-source': 'Telemetry',
  'x-opt-sequence-number': 48574,
  'x-opt-offset': '28334352',
  'x-opt-enqueued-time': 2018-07-18T08:55:17.100Z,
  enqueuedTimeUtc: 2018-07-18T08:55:17.100Z,
  sequenceNumber: 48574,
  offset: '28334352',
  'content-type': 'JSON' } of type object
2018-07-18T08:55:17.122 [Info] DeviceId:  device1
2018-07-18T08:55:17.122 [Info] Function completed (Success, Id=709739ed-7b14-4ac2-821f-df2a68c601ef, Duration=2ms)

【讨论】:

    【解决方案2】:

    在c#中:

    using System;
    
    public static void Run(string myIoTHubMessage, IDictionary<string, object> properties, IDictionary<string, object> systemproperties, TraceWriter log)
    {
        log.Info($"C# IoT Hub trigger function processed a message: \n\t{myIoTHubMessage}");
    
        log.Info($"\nSystemProperties:\n\t{string.Join("\n\t", systemproperties.Select(i => $"{i.Key}={i.Value}"))}");
    
        log.Info($"\nProperties:\n\t{string.Join("\n\t", properties.Select(i => $"{i.Key}={i.Value}"))}");
    }
    

    以及日志中的遥测样本:

        2018-07-17T14:34:32.521 [Info] C# IoT Hub trigger function processed a message: 
            {"counter":0,"time":"2018-07-17T14:34:31.9949651Z","deviceId":"Device02","windSpeed":11.9509,"temperature":16.58,"humidity":79.94}
        2018-07-17T14:34:32.521 [Info] SystemProperties:
            iothub-connection-device-id=Device02
            iothub-connection-auth-method=   {"scope":"device","type":"sas","issuer":"iothub","acceptingIpFilterRule":null}
            iothub-connection-auth-generation-id=636652663042146012
            iothub-enqueuedtime=7/17/2018 2:34:31 PM
            iothub-message-source=Telemetry
            x-opt-sequence-number=1542
            x-opt-offset=769928
            x-opt-enqueued-time=7/17/2018 2:34:32 PM
            EnqueuedTimeUtc=7/17/2018 2:34:32 PM
            SequenceNumber=1542
            Offset=769928
        2018-07-17T14:34:32.521 [Info] Properties:
        2018-07-17T14:34:32.521 [Info] Function completed (Success, Id=24df604d-40a4-4509-8631-ddaba9f56d4b, Duration=242ms)
    

    从上面的Log中可以看到,deviceId在SystemProperties中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-10
      • 2018-11-05
      • 1970-01-01
      • 1970-01-01
      • 2019-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多