mirror of
https://github.com/lehanspb/tuya-mqtt.git
synced 2025-12-16 17:54:36 +00:00
removed custom set function
This commit is contained in:
@@ -1,80 +1,9 @@
|
||||
const TuyAPI = require('tuyapi');
|
||||
const TuyColor = require('./tuya-color');
|
||||
const debug = require('debug')('TuyAPI-device');
|
||||
const debugTuya = require('debug')('TuyAPI-ext');
|
||||
const debugColor = require('debug')('TuyAPI-device-color');
|
||||
const debugTimer = require('debug')('TuyAPI-device-timer');
|
||||
|
||||
/**
|
||||
* Sets a property on a device.
|
||||
* @param {Object} options
|
||||
* @param {Number} [options.dps=1] DPS index to set
|
||||
* @param {*} options.set value to set
|
||||
* @example
|
||||
* // set default property
|
||||
* tuya.set({set: true}).then(() => console.log('device was changed'))
|
||||
* @example
|
||||
* // set custom property
|
||||
* tuya.set({dps: 2, set: true}).then(() => console.log('device was changed'))
|
||||
* @returns {Promise<Boolean>} - returns `true` if the command succeeded
|
||||
*/
|
||||
const Parser = require('tuyapi/lib/message-parser')
|
||||
TuyAPI.prototype.set = function (options) {
|
||||
let dps = {};
|
||||
|
||||
if (options.dps != undefined || options.set != undefined) {
|
||||
if (options.dps === undefined) {
|
||||
dps = {
|
||||
1: options.set
|
||||
};
|
||||
} else {
|
||||
dps = {
|
||||
[options.dps.toString()]: options.set
|
||||
};
|
||||
}
|
||||
} else {
|
||||
dps = options;
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
const timeStamp = (parseInt(now.getTime() / 1000, 10)).toString();
|
||||
|
||||
const payload = {
|
||||
devId: this.device.id,
|
||||
uid: '',
|
||||
t: timeStamp,
|
||||
dps
|
||||
};
|
||||
|
||||
debugTuya('Payload:', this.device.ip);
|
||||
debugTuya(payload);
|
||||
|
||||
// Encrypt data
|
||||
const data = this.device.cipher.encrypt({
|
||||
data: JSON.stringify(payload)
|
||||
});
|
||||
|
||||
// Create MD5 signature
|
||||
const md5 = this.device.cipher.md5('data=' + data +
|
||||
'||lpv=' + this.device.version +
|
||||
'||' + this.device.key);
|
||||
|
||||
// Create byte buffer from hex data
|
||||
const thisData = Buffer.from(this.device.version + md5 + data);
|
||||
const buffer = Parser.encode({
|
||||
data: thisData,
|
||||
commandByte: 7 // 0x07
|
||||
});
|
||||
|
||||
// Send request to change status
|
||||
return new Promise((resolve, reject) => {
|
||||
this._send(buffer, 7, false).then(() => {
|
||||
resolve(true);
|
||||
}).catch(error => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
var steckdose = new TuyaDevice({
|
||||
@@ -200,22 +129,23 @@ var TuyaDevice = (function () {
|
||||
resetTimer();
|
||||
}
|
||||
|
||||
TuyaDevice.prototype.onoff = function (newStatus, callback) {
|
||||
TuyaDevice.prototype.switch = function (newStatus, callback) {
|
||||
newStatus = newStatus.toLowerCase();
|
||||
debug("onoff: " + newStatus);
|
||||
debug("switch: " + newStatus);
|
||||
if (newStatus == "on") {
|
||||
this.on(callback);
|
||||
this.switchOn(callback);
|
||||
}
|
||||
if (newStatus == "off") {
|
||||
this.off(callback);
|
||||
this.switchOff(callback);
|
||||
}
|
||||
if (newStatus == "toggle") {
|
||||
this.toggle(callback);
|
||||
}
|
||||
}
|
||||
|
||||
TuyaDevice.prototype.on = function (callback) {
|
||||
TuyaDevice.prototype.switchOn = function (callback) {
|
||||
var device = this;
|
||||
debug("switch -> ON");
|
||||
device.get().then(status => {
|
||||
device.set({
|
||||
set: true
|
||||
@@ -223,8 +153,9 @@ var TuyaDevice = (function () {
|
||||
});
|
||||
}
|
||||
|
||||
TuyaDevice.prototype.off = function (callback) {
|
||||
TuyaDevice.prototype.switchOff = function (callback) {
|
||||
var device = this;
|
||||
debug("switch -> OFF");
|
||||
device.get().then(status => {
|
||||
device.set({
|
||||
set: false
|
||||
@@ -242,14 +173,18 @@ var TuyaDevice = (function () {
|
||||
}
|
||||
|
||||
TuyaDevice.prototype.setColor = function (hexColor, callback) {
|
||||
debug("Set color to", hexColor);
|
||||
debugColor("Set color to: ", hexColor);
|
||||
var device = this;
|
||||
var tuya = this.device;
|
||||
var color = new TuyColor(tuya);
|
||||
var dps = color.setColor(hexColor);
|
||||
debugColor("dps values:", dps);
|
||||
|
||||
device.get().then(status => {
|
||||
device.set(dps, callback);
|
||||
device.set({
|
||||
multiple: true,
|
||||
data: dps
|
||||
}, callback);
|
||||
});
|
||||
resetTimer();
|
||||
}
|
||||
|
||||
40
tuya-mqtt.js
40
tuya-mqtt.js
@@ -1,6 +1,7 @@
|
||||
const mqtt = require('mqtt');
|
||||
const TuyaDevice = require('./tuya-device');
|
||||
const debug = require('debug')('tuya-mqtt');
|
||||
const debugColor = require('debug')('color');
|
||||
const debugMqtt = require('debug')('mqtt');
|
||||
const debugTuya = require('debug')('tuyAPI-Events');
|
||||
const debugError = require('debug')('error');
|
||||
@@ -28,6 +29,9 @@ try {
|
||||
if (typeof CONFIG.qos == "undefined") {
|
||||
CONFIG.qos = 2;
|
||||
}
|
||||
if (typeof CONFIG.retain == "undefined") {
|
||||
CONFIG.retain = false;
|
||||
}
|
||||
|
||||
const mqtt_client = mqtt.connect({
|
||||
host: CONFIG.host,
|
||||
@@ -41,6 +45,7 @@ mqtt_client.on('connect', function (err) {
|
||||
connected = true;
|
||||
var topic = CONFIG.topic + '#';
|
||||
mqtt_client.subscribe(topic, {
|
||||
retain: CONFIG.retain,
|
||||
qos: CONFIG.qos
|
||||
});
|
||||
});
|
||||
@@ -62,10 +67,20 @@ mqtt_client.on("error", function (error) {
|
||||
/**
|
||||
* execute function on topic message
|
||||
*/
|
||||
function boolToString(istate) {
|
||||
return istate == 1 ? 'on' : "off";
|
||||
}
|
||||
|
||||
function convertMessage(message) {
|
||||
var status = message.toString();
|
||||
status = boolToString(status);
|
||||
status = status.toLowerCase();
|
||||
return status;
|
||||
}
|
||||
|
||||
mqtt_client.on('message', function (topic, message) {
|
||||
try {
|
||||
message = message.toString();
|
||||
message = message.toLowerCase();
|
||||
var cMessage = convertMessage(message);
|
||||
var topic = topic.split("/");
|
||||
var options = {
|
||||
type: topic[1],
|
||||
@@ -77,19 +92,22 @@ mqtt_client.on('message', function (topic, message) {
|
||||
|
||||
if (options.type == "socket" || options.type == "lightbulb") {
|
||||
debug("device", options);
|
||||
debug("message", message);
|
||||
debug("message", cMessage);
|
||||
var device = new TuyaDevice(options);
|
||||
|
||||
if (exec == "command") {
|
||||
var status = topic[6];
|
||||
if (status == null) {
|
||||
device.onoff(message);
|
||||
device.switch(cMessage);
|
||||
} else {
|
||||
device.onoff(status);
|
||||
device.switch(status);
|
||||
}
|
||||
}
|
||||
if (exec == "color") {
|
||||
var color = message;
|
||||
var color = message.toString();
|
||||
color = color.toLowerCase();
|
||||
debugColor("topic: ", topic);
|
||||
debugColor("onColor: ", color);
|
||||
device.setColor(color);
|
||||
}
|
||||
}
|
||||
@@ -114,7 +132,7 @@ function publishStatus(device, status) {
|
||||
if (typeof tuyaID != "undefined" && typeof tuyaKey != "undefined" && typeof tuyaIP != "undefined") {
|
||||
var topic = CONFIG.topic + type + "/" + tuyaID + "/" + tuyaKey + "/" + tuyaIP + "/state";
|
||||
mqtt_client.publish(topic, status, {
|
||||
retain: true,
|
||||
retain: CONFIG.retain,
|
||||
qos: CONFIG.qos
|
||||
});
|
||||
debugTuya("mqtt status updated to:" + topic + " -> " + status);
|
||||
@@ -127,6 +145,10 @@ function publishStatus(device, status) {
|
||||
}
|
||||
}
|
||||
|
||||
function publishColorState(device, state) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* publish all dps-values to topic
|
||||
* @param {TuyaDevice} device
|
||||
@@ -145,7 +167,7 @@ function publishDPS(device, dps) {
|
||||
var data = JSON.stringify(dps);
|
||||
debugTuya("mqtt dps updated to:" + topic + " -> ", data);
|
||||
mqtt_client.publish(topic, data, {
|
||||
retain: true,
|
||||
retain: CONFIG.retain,
|
||||
qos: CONFIG.qos
|
||||
});
|
||||
|
||||
@@ -154,7 +176,7 @@ function publishDPS(device, dps) {
|
||||
var data = JSON.stringify(dps[key]);
|
||||
debugTuya("mqtt dps updated to:" + topic + " -> dps[" + key + "]", data);
|
||||
mqtt_client.publish(topic, data, {
|
||||
retain: true,
|
||||
retain: CONFIG.retain,
|
||||
qos: CONFIG.qos
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user