update package and code cleanup

This commit is contained in:
KarstenSiedentopp
2018-11-26 16:12:39 +01:00
parent debe4968d2
commit e1be123c0a
5 changed files with 62 additions and 1536 deletions

View File

@@ -1,143 +1,78 @@
const mqtt = require('mqtt');
const TuyaDevice = require('./tuyaapi-extended');
const CronJob = require('cron').CronJob;
const crypto = require('crypto');
const debug = require('debug')('TuyAPI-mqtt');
const autoUpdate = {};
/**
* MQTT Settings
*/
var options = {
clientId: 'tuya_mqtt',
port: 1883,
keepalive: 60
};
const client = mqtt.connect({
host: 'localhost',
port: options.port
});
const TuyaDevice = require('./tuya-device');
const debug = require('debug')('tuya-mqtt');
var cleanup = require('./cleanup').Cleanup(onExit);
function bmap(istate) {
return istate ? 'ON' : "OFF";
}
client.on('connect', function () {
var topic = 'tuya/#';
client.subscribe(topic);
debug("MQTT Subscribed");
updateDeviceStatus();
})
function createHash(tuyaID, tuyaKey, tuyaIP) {
try {
return crypto.createHmac('sha256', "")
.update(tuyaID + tuyaKey + tuyaIP)
.digest('hex');
} catch (e) {
debug(e);
}
return tuyaID + tuyaKey + tuyaIP;
function bmap(istate) {
return istate ? 'ON' : "OFF";
}
function isKnowDevice(tuyaID, tuyaKey, tuyaIP) {
try {
var isKnown = false;
var searchKey = createHash(tuyaID, tuyaKey, tuyaIP);
if (autoUpdate[searchKey] != undefined) {
isKnown = true;
const CONFIG = {
host: 'localhost',
port: 1883,
topic: "tuya/"
}
const mqtt_client = mqtt.connect({
host: CONFIG.host,
port: CONFIG.port
});
mqtt_client.on('connect', function () {
var topic = CONFIG.topic + '#';
mqtt_client.subscribe(topic, function (err) {
if (!err) {
//mqtt_client.publish(CONFIG.topic + 'presence', 'Hello mqtt')
}
return isKnown;
} catch (e) {
debug(e);
}
}
});
});
function getKnownDevice(tuyaID, tuyaKey, tuyaIP) {
try {
var searchKey = createHash(tuyaID, tuyaKey, tuyaIP);
return autoUpdate[searchKey];
} catch (e) {
debug(e);
}
}
function addDevice(device) {
try {
var infos = device.getDevice();
var tuyaID = infos.id;
var tuyaKey = infos.key;
var tuyaIP = infos.ip;
var key = createHash(tuyaID, tuyaKey, tuyaIP);
autoUpdate[key] = device;
} catch (e) {
debug(e);
}
}
function createDevice(tuyaID, tuyaKey, tuyaIP, tuyaType) {
try {
if (tuyaID != undefined && tuyaKey != undefined) {
var tuya = undefined;
if (isKnowDevice(tuyaID, tuyaKey, tuyaIP)) {
tuya = getKnownDevice(tuyaID, tuyaKey, tuyaIP);
} else {
var tuya = new TuyaDevice({
id: tuyaID,
key: tuyaKey,
ip: tuyaIP,
type: tuyaType
});
addDevice(tuya);
}
return tuya;
}
} catch (e) {
debug(e);
}
return undefined;
};
client.on('message', function (topic, message) {
mqtt_client.on('message', function (topic, message) {
try {
message = message.toString();
message = message.toLowerCase();
var topic = topic.split("/");
var type = topic[1];
var options = {
type: topic[1],
id: topic[2],
key: topic[3],
ip: topic[4],
};
var exec = topic[5];
if ((type == "socket" || type == "lightbulb") && exec == "command" && topic.length == 7) {
var tuya = createDevice(topic[2], topic[3], topic[4], type);
tuya.onoff(topic[6], function (status) {
publishStatus(tuya, bmap(status));
debug("Device status updated to: " + bmap(status));
});
}
if (type == "lightbulb" && exec == "color" && topic.length == 6) {
message = message.toString();
message = message.toLowerCase();
debug("Recevied color: " + message);
var tuya = createDevice(topic[2], topic[3], topic[4], type);
tuya.setColor(message, function (status) {
publishStatus(tuya, bmap(status));
debug("Color is updated: " + bmap(status));
});
if (options.type == "socket" || options.type == "lightbulb") {
debug("device", options);
debug("message", message);
var device = new TuyaDevice(options);
if (exec == "command") {
var status = topic[6];
device.onoff(status);
}
if (exec == "color") {
var color = message;
device.setColor(color);
}
}
} catch (e) {
debug(e);
}
});
function publishStatus(tuya, status) {
function publishStatus(device, status) {
try {
var device = tuya.getDevice();
var type = device.type;
var tuyaID = device.id;
var tuyaKey = device.key;
var tuyaIP = device.ip;
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";
client.publish(topic, status, {
mqtt_client.publish(topic, status, {
retain: true,
qos: 2
});
@@ -150,19 +85,16 @@ function publishStatus(tuya, status) {
}
}
function updateDeviceStatus() {
try {
Object.keys(autoUpdate).forEach(function (k) {
var tuya = autoUpdate[k];
tuya.getStatus(function (status) {
publishStatus(tuya, bmap(status));
})
});
} catch (e) {
debug(e);
TuyaDevice.onAll('data', function (data) {
debug('Data from device ' + this.type + ' :', data);
var status = data.dps['1'];
if (this.type == "lightbulb" && status == undefined) {
status = true;
}
}
publishStatus(this, bmap(status));
});
new CronJob('0 */10 * * * *', function () {
//updateDeviceStatus();
}, null, true, 'America/Los_Angeles');
// defines app specific callback...
function onExit() {
TuyaDevice.disconnectAll();
};