Azure / azure-iot-sdk-node

A Node.js SDK for connecting devices to Microsoft Azure IoT services
https://docs.microsoft.com/en-us/azure/iot-hub/
Other
261 stars 226 forks source link

Azure IoT Edge - route messages between modules: custom message properties are no propagated #1194

Open valeriaArvika opened 1 year ago

valeriaArvika commented 1 year ago

Context

Description of the issue

I'm using IoT Edge to connect Modbus devices to IoT Hub and I'm trying to implement protocol + identity translation modules.

I started with the example here

I have two modules:

  1. ModbusClient, for the protocol translation, in C#

  2. IdentityTranslation, in NodeJS

Here is how routes are defined in the deployment file:

"ModbusClientToIdentityTranslation": "FROM /messages/modules/ModbusClient/outputs/modbusOutput INTO BrokeredEndpoint(\"/modules/IdentityTranslation/inputs/input1\")",

"IdentityTranslationToIoTHub": "FROM /messages/modules/IdentityTranslation/outputs/* INTO $upstream"

I would like to add a property with the deviceId value in the message transformed by the ModbusClient and read this property in the IdentityTranslation module.

Code sample exhibiting the issue

Here are the snippets of the code in the two modules:

Message message = new Message(Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(out_message)));
message.Properties.Add("content-type", "application/edge-modbus-json");
message.Properties.Add("deviceId", deviceId);

Console.WriteLine("deviceId " + message.Properties["deviceId"]);
await ioTHubModuleClient.SendEventAsync("modbusOutput", message);
moduleClient.on('inputMessage', function (inputName, msg) {
  console.log("new message received", msg);
  if (modbusClient) {
    // Identity Translation configured, wrap message for IoT device
    const message = msg.getBytes().toString('utf8');
    const sensorMsg = new Message(message);
    console.log("sensorMsg ", sensorMsg);
    if(sensorMsg.Keys.Contains("deviceId")){
       console.log("deviceId ", sensorMsg.Properties["deviceId"]);
    }
    modbusClient.sendEvent(sensorMsg);
}

When the message arrives at the IdentityTranslation module, Properties are empty and I cannot get the deviceId value.

Console log of the issue

This is the content of the received message

new message received Message {
  data:
   <Buffer 7b 22 50 75 62 6c 69 73 68 54 69 6d 65 73 74 61 6d 70 22 3a 22 32 30 32 33 2d 30 36 2d 32 37 20 31 34 3a 34 39 3a 33 30 22 2c 22 43 6f 6e 74 65 6e 74 ... >,
  properties:
   Properties { propertyList: [ [Object], [Object], [Object] ] },
  messageId: '',
  to: '',
  expiryTimeUtc: undefined,
  lockToken: '',
  correlationId: '',
  userId: '',
  contentEncoding: 'utf-8',
  contentType: 'application/json' }

Other references

I found this old issue Message properties seem to get lost after routing through edgeHub related to the python sdk that has been fixed. Maybe is there a similar problem on nodejs (or .Net)?