/** Decoder **/// decode payload to stringvarpayloadStr=decodeToString(payload);vardata=JSON.parse(payloadStr);vardeviceName=metadata.topic.split("/")[3];// decode payload to JSONvardeviceType='sensor';// Result object with device attributes/telemetry datavarresult={deviceName:deviceName,deviceType:deviceType,attributes:{integrationName:metadata['integrationName'],},telemetry:{temperature:data.value,}};/** Helper functions 'decodeToString' and 'decodeToJson' are already built-in **/returnresult;
/** Decoder **/// decode payload to stringvarpayloadStr=decodeToString(payload);vardata=JSON.parse(payloadStr);vardeviceName=metadata.topic.split("/")[3];// decode payload to JSONvardeviceType='sensor';// Result object with device attributes/telemetry datavarresult={deviceName:deviceName,deviceType:deviceType,attributes:{integrationName:metadata['integrationName'],},telemetry:{temperature:data.value,}};/** Helper functions **/functiondecodeToString(payload){returnString.fromCharCode.apply(String,payload);}functiondecodeToJson(payload){// convert payload to string.varstr=decodeToString(payload);// parse string to JSONvardata=JSON.parse(str);returndata;}returnresult;
// Encode downlink data from incoming Rule Engine message// msg - JSON message payload downlink message json// msgType - type of message, for ex. 'ATTRIBUTES_UPDATED', 'POST_TELEMETRY_REQUEST', etc.// metadata - list of key-value pairs with additional data about the message// integrationMetadata - list of key-value pairs with additional data defined in Integration executing this converter/** Encoder **/vardata={};// Process data from incoming message and metadatadata.tempFreq=msg.temperatureUploadFrequency;data.humFreq=msg.humidityUploadFrequency;data.devSerialNumber=metadata['ss_serialNumber'];// Result object with encoded downlink payloadvarresult={// downlink data content type: JSON, TEXT or BINARY (base64 format)contentType:"JSON",// downlink datadata:JSON.stringify(data),// Optional metadata object presented in key/value formatmetadata:{topic:metadata['deviceType']+'/'+metadata['deviceName']+'/upload'}};returnresult;
// Encode downlink data from incoming Rule Engine message// msg - JSON message payload downlink message json// msgType - type of message, for ex. 'ATTRIBUTES_UPDATED', 'POST_TELEMETRY_REQUEST', etc.// metadata - list of key-value pairs with additional data about the message// integrationMetadata - list of key-value pairs with additional data defined in Integration executing this converter/** Encoder **/vardata={};// Process data from incoming message and metadatadata.tempFreq=msg.temperatureUploadFrequency;data.humFreq=msg.humidityUploadFrequency;data.devSerialNumber=metadata['ss_serialNumber'];// Result object with encoded downlink payloadvarresult={// downlink data content type: JSON, TEXT or BINARY (base64 format)contentType:"JSON",// downlink datadata:JSON.stringify(data),// Optional metadata object presented in key/value formatmetadata:{topic:metadata['deviceType']+'/'+metadata['deviceName']+'/upload'}};returnresult;
MQTT集成配置
进入Integrations center -> Integrations页面,点击“plus”添加新集成。命名为“MQTT Integration”,选择类型MQTT;
Incoming message
Topic: tb/mqtt-integration-tutorial/sensors/SN-001/rx/twoway
Message: {"value":40}
This is a Two-way RPC call. Going to reply now!
Sending a response message: {"rpcReceived":"OK"}