none
Digital Twins User Defined Functions: How to get the spaceId of the device which I am sending telemetry messages to? RRS feed

  • Question

  • Hello all,

    As of now, I have an external simulator running on my computer that sends simulated telemetry messages to the IoT Hub component of my Azure Digital Twins. I have a user defined function (UDF): myUDF(telemetry, executionContext)

    deployed in my Digital Twins resource that takes in said telemetry messages, performs some processing on them, and pass it on to an endpoint outside of Azure Digital Twins. I am wondering if there is some sort of property within the telemetry messages that I am consuming that shows the spaceid of the device which the telemetry messages belong to/is being sent to? If there is, how would I go about modifying my UDF such that I can extract said property?

    Thank you in advance!

    Monday, June 17, 2019 2:32 PM

All replies

  • Hello Synovia,

    In your user defined function you can leverage the defined helper methods for commonly used operations.

    I believe what you are looking for is getSensorMetadata(id) 

    In the udfs used for occupancy-quickstart you have several examples : for example you can get the sensor metadata by the telemetry message ID:

    // Get sensor metadata
           var sensor = getSensorMetadata(telemetry.SensorId);

    Hope it helps.

    Thanks!


    Wednesday, June 19, 2019 10:33 AM
    Moderator
  • Hello Antonio, 

    Thank you for the response and for the helper methods link! I'm still a bit lost. I think I can retrieve the parent space of the telemetry's device, however, I am wondering how would I including that spaceId as an additional property in the telemetry message? Is this a correct way of approaching the problem?

    function process(telemetry, executionContext) {
        try {
            var deviceParentSpace = getDeviceParentSpace(telemetry.deviceId); //Retrieves parent space
            telemetry['SpaceId'] = deviceParentSpace.id; //Makes new property in telemetry & assigns parent space id
            return telemetry; //Passes telemetry onto Event Hubs Endpoint
        } catch (error){
            log(error.message);
        }
    }

    Thanks!


    Wednesday, June 19, 2019 1:46 PM
  • Hello Synovial,

    I believe this is related with your other thread. As soon as you create an UDF the eventType will be "UdfCustom" and in order to be routed through the endpoint defined the event JSON format should look something like:

    {
      "id": "568fd394-380b-46fa-925a-ebb96f658cce",
      "subject": "UdfCustom",
      "data": {
        "THIS IS WHERE YOUR MODIFIED TELEMETRY IS": "IN JSON FORMAT"
      },
      "eventType": "UdfCustom",
      "eventTime": "2018-10-02T06:50:15.198Z",
      "dataVersion": "1.0",
      "metadataVersion": "1",
      "topic": "/subscriptions/YOUR_TOPIC_NAME"
    }
    Thanks!


    Tuesday, July 2, 2019 11:53 AM
    Moderator