mirror of
https://github.com/lehanspb/tuya-mqtt.git
synced 2025-12-16 17:54:36 +00:00
package updated & added more error messages
This commit is contained in:
111
tuya-mqtt.js
111
tuya-mqtt.js
@@ -1,6 +1,7 @@
|
||||
const mqtt = require('mqtt');
|
||||
const TuyaDevice = require('./tuya-device');
|
||||
const debug = require('debug')('tuya-mqtt');
|
||||
const debugMqtt = require('debug')('mqtt');
|
||||
var cleanup = require('./cleanup').Cleanup(onExit);
|
||||
|
||||
function bmap(istate) {
|
||||
@@ -11,6 +12,8 @@ function bmap(istate) {
|
||||
return istate ? 'ON' : "OFF";
|
||||
}
|
||||
|
||||
var connected = undefined;
|
||||
|
||||
const CONFIG = {
|
||||
host: 'localhost',
|
||||
port: 1883,
|
||||
@@ -22,15 +25,30 @@ const mqtt_client = mqtt.connect({
|
||||
port: CONFIG.port
|
||||
});
|
||||
|
||||
mqtt_client.on('connect', function () {
|
||||
mqtt_client.on('connect', function (err) {
|
||||
debugMqtt("Verbindung mit MQTT-Server hergestellt");
|
||||
connected = true;
|
||||
var topic = CONFIG.topic + '#';
|
||||
mqtt_client.subscribe(topic, function (err) {
|
||||
if (!err) {
|
||||
//mqtt_client.publish(CONFIG.topic + 'presence', 'Hello mqtt')
|
||||
}
|
||||
});
|
||||
mqtt_client.subscribe(topic);
|
||||
});
|
||||
|
||||
mqtt_client.on("reconnect", function (error) {
|
||||
if (connected) {
|
||||
debugMqtt("Verbindung mit MQTT-Server wurde unterbrochen. Erneuter Verbindungsversuch!");
|
||||
} else {
|
||||
debugMqtt("Verbindung mit MQTT-Server konnte nicht herrgestellt werden.");
|
||||
}
|
||||
connected = false;
|
||||
});
|
||||
|
||||
mqtt_client.on("error", function (error) {
|
||||
debugMqtt("Verbindung mit MQTT-Server konnte nicht herrgestellt werden.", error);
|
||||
connected = false;
|
||||
});
|
||||
|
||||
/**
|
||||
* execute function on topic message
|
||||
*/
|
||||
mqtt_client.on('message', function (topic, message) {
|
||||
try {
|
||||
message = message.toString();
|
||||
@@ -63,28 +81,39 @@ mqtt_client.on('message', function (topic, message) {
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Publish current TuyaDevice state to MQTT-Topic
|
||||
* @param {TuyaDevice} device
|
||||
* @param {boolean} status
|
||||
*/
|
||||
function publishStatus(device, status) {
|
||||
try {
|
||||
var type = device.type;
|
||||
var tuyaID = device.options.id;
|
||||
var tuyaKey = device.options.key;
|
||||
var tuyaIP = device.options.ip;
|
||||
if (mqtt_client.connected == true) {
|
||||
try {
|
||||
var type = device.type;
|
||||
var tuyaID = device.options.id;
|
||||
var tuyaKey = device.options.key;
|
||||
var tuyaIP = device.options.ip;
|
||||
|
||||
if (tuyaID != undefined && tuyaKey != undefined && tuyaIP != undefined) {
|
||||
var topic = "tuya/" + type + "/" + tuyaID + "/" + tuyaKey + "/" + tuyaIP + "/state";
|
||||
mqtt_client.publish(topic, status, {
|
||||
retain: true,
|
||||
qos: 2
|
||||
});
|
||||
debug("mqtt status updated to:" + topic + " -> " + status);
|
||||
} else {
|
||||
debug("mqtt status not updated");
|
||||
if (tuyaID != undefined && tuyaKey != undefined && tuyaIP != undefined) {
|
||||
var topic = "tuya/" + type + "/" + tuyaID + "/" + tuyaKey + "/" + tuyaIP + "/state";
|
||||
mqtt_client.publish(topic, status, {
|
||||
retain: true,
|
||||
qos: 2
|
||||
});
|
||||
debug("mqtt status updated to:" + topic + " -> " + status);
|
||||
} else {
|
||||
debug("mqtt status not updated");
|
||||
}
|
||||
} catch (e) {
|
||||
debug(e);
|
||||
}
|
||||
} catch (e) {
|
||||
debug(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* event fires if TuyaDevice sends data
|
||||
* @see TuyAPI (https://github.com/codetheweb/tuyapi)
|
||||
*/
|
||||
TuyaDevice.onAll('data', function (data) {
|
||||
debug('Data from device ' + this.type + ' :', data);
|
||||
var status = data.dps['1'];
|
||||
@@ -94,7 +123,43 @@ TuyaDevice.onAll('data', function (data) {
|
||||
publishStatus(this, bmap(status));
|
||||
});
|
||||
|
||||
// defines app specific callback...
|
||||
/**
|
||||
* MQTT connection tester
|
||||
*/
|
||||
function MQTT_Tester() {
|
||||
this.interval = null;
|
||||
|
||||
function mqttConnectionTest() {
|
||||
if (mqtt_client.connected != connected) {
|
||||
connected = mqtt_client.connected;
|
||||
if (connected) {
|
||||
debugMqtt('MQTT-Server verbunden.');
|
||||
} else {
|
||||
debugMqtt('MQTT-Server nicht verbunden.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.destroy = function () {
|
||||
clearInterval(this.interval);
|
||||
this.interval = undefined;
|
||||
}
|
||||
|
||||
this.connect = function () {
|
||||
this.interval = setInterval(mqttConnectionTest, 1500);
|
||||
mqttConnectionTest();
|
||||
}
|
||||
|
||||
var constructor = (function (that) {
|
||||
that.connect.call(that);
|
||||
})(this);
|
||||
}
|
||||
var tester = new MQTT_Tester();
|
||||
|
||||
/**
|
||||
* Function call on script exit
|
||||
*/
|
||||
function onExit() {
|
||||
TuyaDevice.disconnectAll();
|
||||
tester.destroy();
|
||||
};
|
||||
Reference in New Issue
Block a user