129 lines
3 KiB
JavaScript
129 lines
3 KiB
JavaScript
exports.id = 'mqtt_subscribe_temperature';
|
|
exports.title = 'MQTT subscribe temperature';
|
|
exports.group = 'MQTT';
|
|
exports.color = '#888600';
|
|
exports.version = '1.0.2';
|
|
exports.icon = 'sign-out';
|
|
exports.output = 2;
|
|
exports.options = { host: 'tb-stage.worksys.io', port: 1883, tbname: "", username: "" };
|
|
exports.npm = ['mqtt'];
|
|
exports.html = `<div class="padding">
|
|
<div class="row"><div class="col-md-6">
|
|
<div data-jc="textbox" data-jc-path="host" data-jc-config="placeholder:test.mosquitto.org;required:false" class="m">Hostname or IP address (if not empty - setting will override db setting)</div>
|
|
</div>
|
|
<div class="col-md-6">
|
|
<div data-jc="textbox" data-jc-path="port" data-jc-config="placeholder:1883;required:true" class="m">Port</div>
|
|
</div>
|
|
</div>
|
|
<div class="row">
|
|
<div class="col-md-6">
|
|
<div data-jc="textbox" data-jc-path="tbname">@(SBS thingsboard name)</div>
|
|
</div>
|
|
<div class="col-md-6">
|
|
<div data-jc="textbox" data-jc-path="topic" class="m">@(topic)</div>
|
|
</div>
|
|
</div>
|
|
</div>`;
|
|
|
|
|
|
exports.install = function(instance) {
|
|
|
|
var mqtt = require('mqtt');
|
|
var client;
|
|
var opts;
|
|
|
|
|
|
//set opts according to db settings
|
|
instance.reconfigure = function() {
|
|
|
|
if (instance.options.host !== "") {
|
|
//override settings from database
|
|
var o = instance.options;
|
|
opts = {
|
|
host: o.host,
|
|
port: o.port,
|
|
tbname: o.tbname,
|
|
topic: o.topic,
|
|
rejectUnauthorized: false,
|
|
resubscribe: false
|
|
};
|
|
|
|
console.log("wsmqttpublich -> loadSettings from instance.options", instance.options);
|
|
}
|
|
|
|
connectToTbServer();
|
|
}
|
|
|
|
function connectToTbServer() {
|
|
var url = "mqtt://" + opts.host + ":" + opts.port;
|
|
console.log("MQTT URL: ", url);
|
|
|
|
client = mqtt.connect(url, opts);
|
|
|
|
client.on('connect', function() {
|
|
client.subscribe(`${opts.topic}`, (err) => {
|
|
if (!err) {
|
|
console.log("MQTT subscribed");
|
|
}
|
|
});
|
|
instance.status("Connected", "green");
|
|
});
|
|
|
|
client.on('reconnect', function() {
|
|
instance.status("Reconnecting", "yellow");
|
|
});
|
|
|
|
client.on('message', function(topic, message) {
|
|
// message is type of buffer
|
|
message = message.toString();
|
|
//console.log('messageeee', message, typeof message);
|
|
if (message[0] === '{') {
|
|
try {
|
|
message = JSON.parse(message);
|
|
instance.send(0, message);
|
|
sendToTb({ temperature_out: message["temperature"] }, opts.tbname);
|
|
//console.log("teplota sokolov z cloudu: ", message);
|
|
} catch (error) {
|
|
console.log("Mqtt_subscribe_temperature: unable to parse mqtt temperature message");
|
|
}
|
|
}
|
|
|
|
});
|
|
|
|
client.on('close', function(err) {
|
|
if (err) console.log('client na mqtt teplotu sa odpojil')
|
|
client.reconnect();
|
|
});
|
|
|
|
client.on('error', function(err) {
|
|
instance.status("Err: " + err.code, "red");
|
|
});
|
|
|
|
}
|
|
|
|
instance.close = function() {
|
|
client.end();
|
|
};
|
|
|
|
instance.on('options', instance.reconfigure);
|
|
instance.reconfigure();
|
|
|
|
function sendToTb(values, tbName) {
|
|
|
|
const dataToTb = {
|
|
[tbName]: [
|
|
|
|
{
|
|
ts: Date.now(),
|
|
values: values
|
|
}
|
|
]
|
|
};
|
|
|
|
instance.send(1, dataToTb);
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|