mirror of
https://github.com/lehanspb/tuya-mqtt.git
synced 2025-12-18 00:10:20 +00:00
tuya extended class integrated and updated mqtt client
This commit is contained in:
66
tuya-mqtt.js
66
tuya-mqtt.js
@@ -24,6 +24,7 @@ function bmap(istate) {
|
||||
client.on('connect', function () {
|
||||
var topic = 'tuya/#';
|
||||
client.subscribe(topic);
|
||||
console.log("MQTT Subscribed");
|
||||
})
|
||||
|
||||
var knowDevice = function (tuyaID, tuyaKey, tuyaIP) {
|
||||
@@ -35,18 +36,19 @@ var knowDevice = function (tuyaID, tuyaKey, tuyaIP) {
|
||||
});
|
||||
return isKnown;
|
||||
}
|
||||
var addDevice = function (tuyaID, tuyaKey, tuyaIP) {
|
||||
var addDevice = function (type, tuyaID, tuyaKey, tuyaIP) {
|
||||
var newDevice = {
|
||||
id: tuyaID,
|
||||
key: tuyaKey,
|
||||
ip: tuyaIP
|
||||
ip: tuyaIP,
|
||||
type: type
|
||||
};
|
||||
autoUpdate.push(newDevice);
|
||||
}
|
||||
|
||||
exports.publishStatus = function (tuyaID, tuyaKey, tuyaIP, status) {
|
||||
exports.publishStatus = function (tuyaID, tuyaKey, tuyaIP, type, status) {
|
||||
if (tuyaID != undefined && tuyaKey != undefined && tuyaIP != undefined) {
|
||||
var topic = "tuya/socket/" + tuyaID + "/" + tuyaKey + "/" + tuyaIP + "/state";
|
||||
var topic = "tuya/" + type + "/" + tuyaID + "/" + tuyaKey + "/" + tuyaIP + "/state";
|
||||
client.publish(topic, status, {
|
||||
retain: true,
|
||||
qos: 2
|
||||
@@ -54,38 +56,80 @@ exports.publishStatus = function (tuyaID, tuyaKey, tuyaIP, status) {
|
||||
}
|
||||
}
|
||||
|
||||
exports.setStatus = function (tuyaID, tuyaKey, tuyaIP, status) {
|
||||
exports.setStatus = function (type, tuyaID, tuyaKey, tuyaIP, status) {
|
||||
if (tuyaID != undefined && tuyaKey != undefined && tuyaIP != undefined) {
|
||||
if (!knowDevice(tuyaID, tuyaKey, tuyaIP)) {
|
||||
addDevice(tuyaID, tuyaKey, tuyaIP);
|
||||
addDevice(type, tuyaID, tuyaKey, tuyaIP);
|
||||
}
|
||||
TuyaDevice.createDevice(tuyaID, tuyaKey, tuyaIP);
|
||||
if (TuyaDevice.hasDevice()) {
|
||||
TuyaDevice.setStatus(status, function (newStatus) {
|
||||
module.exports.publishStatus(tuyaID, tuyaKey, tuyaIP, newStatus);
|
||||
module.exports[type].publishStatus(tuyaID, tuyaKey, tuyaIP, newStatus);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exports.getStatus = function (tuyaID, tuyaKey, tuyaIP) {
|
||||
exports.getStatus = function (type, tuyaID, tuyaKey, tuyaIP) {
|
||||
if (tuyaID != undefined && tuyaKey != undefined && tuyaIP != undefined) {
|
||||
TuyaDevice.createDevice(tuyaID, tuyaKey, tuyaIP);
|
||||
if (TuyaDevice.hasDevice()) {
|
||||
TuyaDevice.getStatus(function (status) {
|
||||
module.exports.publishStatus(tuyaID, tuyaKey, tuyaIP, bmap(status));
|
||||
module.exports[type].publishStatus(tuyaID, tuyaKey, tuyaIP, bmap(status));
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exports.socket = {};
|
||||
exports.socket.publishStatus = function (tuyaID, tuyaKey, tuyaIP, status) {
|
||||
return module.exports.publishStatus(tuyaID, tuyaKey, tuyaIP, "socket", status);
|
||||
}
|
||||
|
||||
exports.lightbulb = {};
|
||||
exports.lightbulb.publishStatus = function (tuyaID, tuyaKey, tuyaIP, status) {
|
||||
return module.exports.publishStatus(tuyaID, tuyaKey, tuyaIP, "lightbulb", status);
|
||||
}
|
||||
exports.lightbulb.publishColor = function (tuyaID, tuyaKey, tuyaIP, color) {
|
||||
if (tuyaID != undefined && tuyaKey != undefined && tuyaIP != undefined) {
|
||||
var topic = "tuya/lightbulb/" + tuyaID + "/" + tuyaKey + "/" + tuyaIP + "/state/color";
|
||||
client.publish(topic, color, {
|
||||
retain: true,
|
||||
qos: 2
|
||||
});
|
||||
}
|
||||
}
|
||||
exports.lightbulb.setColor = function (tuyaID, tuyaKey, tuyaIP, color) {
|
||||
if (tuyaID != undefined && tuyaKey != undefined && tuyaIP != undefined) {
|
||||
if (!knowDevice(tuyaID, tuyaKey, tuyaIP)) {
|
||||
//addDevice("lightbulb", tuyaID, tuyaKey, tuyaIP);
|
||||
}
|
||||
TuyaDevice.createDevice(tuyaID, tuyaKey, tuyaIP);
|
||||
if (TuyaDevice.hasDevice()) {
|
||||
console.log("tuya-mqtt.lightbulb.setColor: " + color);
|
||||
TuyaDevice.setColor(color, function (newStatus) {
|
||||
console.log(newStatus);
|
||||
module.exports.lightbulb.publishColor(tuyaID, tuyaKey, tuyaIP, newStatus);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
client.on('message', function (topic, message) {
|
||||
try {
|
||||
var topic = topic.split("/");
|
||||
var type = topic[1];
|
||||
var exec = topic[5];
|
||||
if (type == "socket" && exec == "command" && topic.length == 7) {
|
||||
module.exports.setStatus(topic[2], topic[3], topic[4], topic[6]);
|
||||
module.exports.setStatus(type, topic[2], topic[3], topic[4], topic[6]);
|
||||
}
|
||||
if (type == "lightbulb" && exec == "command" && topic.length == 7) {
|
||||
module.exports.setStatus(type, topic[2], topic[3], topic[4], topic[6]);
|
||||
}
|
||||
if (type == "lightbulb" && exec == "color" && topic.length == 6) {
|
||||
message = message.toString();
|
||||
message = message.toLowerCase();
|
||||
module.exports.lightbulb.setColor(topic[2], topic[3], topic[4], message);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
@@ -95,7 +139,7 @@ client.on('message', function (topic, message) {
|
||||
new CronJob('0 */1 * * * *', function () {
|
||||
try {
|
||||
autoUpdate.forEach(function (entry) {
|
||||
module.exports.getStatus(entry.id, entry.key, entry.ip);
|
||||
module.exports.getStatus(entry.type, entry.id, entry.key, entry.ip);
|
||||
});
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
|
||||
Reference in New Issue
Block a user