· Zen HuiFer · Operation Documentation · 2 min read
MQTT Parsing Script
In this article, we detail how to write an MQTT client parsing script in the Go IoT development platform. Through specific case programs, you will learn how to handle data reported by the MQTT client and ensure that the return value contains the necessary data fields. This article also provides a detailed explanation of each field to help you better understand and apply these concepts.
The Go IoT development platform allows users to write parsing scripts for different MQTT clients, and these scripts need to be able to execute correctly. The example program is as follows
function main(nc) {
var dataRows = [
{ "Name": "Temperature", "Value": "23" },
{ "Name": "Humidity", "Value": "30" },
{ "Name": "A", "Value": nc },
];
var result = {
"Time": Math.floor(Date.now() / 1000),
"DataRows": dataRows,
"DeviceUid": "5",
"IdentificationCode":"5",
"Nc": nc
};
return [result];
}
In the above program, the data reported by the MQTT client will be transmitted to the main
function in the form of a string (nc is the parameter name). Do not adjust this content.
Regarding the return value, it must contain the data fields Time, DataRows, DeviceUid, and IdentificationCode. Their meanings are as follows:
- Time: Device data production time, in seconds timestamp.
- DataRows: Core signal data points. It is an array structure where the keys are Name and Value, Name represents the signal name, and Value represents the signal data value.
- DeviceUid: MQTT client ID, please correspond to the unique code of the current row of data. Please check carefully
- IdentificationCode: Device identification code, if your subscription topic does not have a wildcard, please keep this data information consistent with DeviceUid. If a wildcard appears, please differentiate it from DeviceUid.
Note:
- DeviceUid is hardcoded in the return value. Do not generate it dynamically.
- IdentificationCode must be differentiated from DeviceUid in principle, i.e., the two cannot be the same.
Data Link
When writing to Influxdb, assemble according to the following data assembly rules.
bucket: Project fixed configuration.
Measurement: Dynamically calculated as
${protocol}_${DeviceUid}_${IdentificationCode}
func genMeasurement(dt DataRowList, protocol string) string {
return protocol + "_" + dt.DeviceUid + "_" + dt.IdentificationCode
}