如何在azure功能应用中过滤来自不同设备的物联网消息?

时间:2018-07-17 13:52:55

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

我在Azure中创建了一个IOT Hub,并在其中添加了两个设备。我正在从两个设备发送相同的消息。我还在Azure中创建了一个function app。该功能由Azure IoT中心触发,并将数据保存到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();
};

谢谢!

2 个答案:

答案 0 :(得分:1)

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

rvMovies = (RecyclerView)findViewById(R.id.rvMovies);

index.js看起来像这样:

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

日志为:

    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();
};

答案 1 :(得分:0)

在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)

如您所见,上面的日志,deviceId在SystemProperties中。