const { Analysis, Services } = require("@tago-io/sdk");
const DEVICE_ID = "8fb6cc7d-4abd-4582-8bd4-05ae0aac04fb";
async function mqttPushExample(context, scope) {
console.log(context, scope)
if (!scope.length) {
return context.log("Missing values");
}
const myData = scope.find((x) => x.variable === "payload") || scope[0];
if (!myData) {
return context.log("Couldn't find any variable in the scope.");
}
// Create your data object to push to MQTT
// In this case we're sending a JSON object.
// You can send anything you want.
// Example:
// const myDataObject = 'This is a string';
const myDataObject = {
variable: "payload",
value: "30000,30000,30000,30000,30000,60000",
unit: "C",
};
// Create a object with the options you chooses
const options = {
retain: false,
qos: 0,
};
// Publishing to MQTT
const mqtt = new Services().mqtt;
context.log("Will send to:");
context.log(DEVICE_ID);
context.log( mqtt.publish);
mqtt.publish({
bucket: DEVICE_ID,
message: JSON.stringify(myDataObject),
topic: "GUIMEL/SENSORS/GCUBE01/SETTINGS",
options,
}).then(context.log, context.log)
}
[2024-06-19 21:38:45] Duration: 1.1s Billed Duration: 2s.
[2024-06-19 21:38:45] Analysis finished.
[2024-06-19 21:38:45] Message sent
[2024-06-19 21:38:45] Will send 8fb6cc7d-4abd-4582-8bd4-05ae0aac04fb [AsyncFunction: publish]
[2024-06-19 21:38:45] { log: [Function: log], token: 'a-ecad0d81-3516-4e05-8c78-fe3e3df0d252', environment: [ { key: '', value: '' }, { key: '_action_id', value: '66721df7864a2b00081d6290' }, { key: '_action_type', value: 'mqtt_topic' }, { key: '_action_state', value: 'unlocked' }, { key: '_device_id', value: '6669ed3894c0e80009a715c5' } ], analysis_id: '66722187ed404a0009e7d9d5' } [ { variable: 'payload', value: 'GCUBE01 189.00 159.00 20.00 40.00 141.00 107.00 false 90.00', metadata: { mqtt_topic: 'GUIMEL/SENSORS/GCUBE01/DATA' } } ]
[2024-06-19 21:38:44] Starting Analysis...