0
votes

I have created one IOT Hub in Azure and added two devices in that. I am sending same message from both of the device. I have also created one function app in Azure. The function is triggering with Azure IoT Hub and it saving data to azure storage table. All things are working as per expectation. Now I want to filter message using device id. I meant my azure function app should recognize the message is came from which device.

Here is my function app java script code

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

Thanks!

2
Hi Jignesh, have you tried my solution? Is it working for you?Rita Han

2 Answers

1
votes

In JavaScript, you can use the following method to get the device id:

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

The index.js looks like this:

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

The log is:

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)
0
votes

In the 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}"))}");
}

and the telemetry sample in the Log:

    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)

as you can see the above Log, the deviceId is in the SystemProperties.