Created
October 23, 2019 08:21
-
-
Save AlexDoanTB/e05e478f28d2a4209a7ffebfdfdc9464 to your computer and use it in GitHub Desktop.
This is an uplink converter for ThingsBoard Remote integration tutorial
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Decode an uplink message from a buffer | |
| // payload - array of bytes | |
| // metadata - key/value object | |
| /** Decoder **/ | |
| // decode payload to JSON | |
| var data = decodeToString(payload); | |
| var deviceName = metadata['topic'].replace("/devices/", ""); | |
| var deviceType = 'thermostat'; | |
| var dataArray = data.split(","); | |
| // Result object with device attributes/telemetry data | |
| var result = { | |
| deviceName: deviceName, | |
| deviceType: deviceType, | |
| telemetry: { | |
| temperature: parseFloat(dataArray[0]), | |
| humidity: parseFloat(dataArray[1]), | |
| batteryLevel: parseInt(dataArray[2]) | |
| } | |
| }; | |
| /** Helper functions **/ | |
| function decodeToString(payload) { | |
| return String.fromCharCode.apply(String, payload); | |
| } | |
| function decodeToJson(payload) { | |
| // covert payload to string. | |
| var str = decodeToString(payload); | |
| // parse string to JSON | |
| var data = JSON.parse(str); | |
| return data; | |
| } | |
| return result; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment