Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Import packages
const dgram = require('dgram');
const net = require('net');
const timeout = require('p-timeout');
const retry = require('retry');
const debug = require('debug')('TuyAPI-ext');
// Helpers
const Cipher = require('tuyapi/lib/cipher');
const Parser = require('tuyapi/lib/message-parser')
TuyaDevice.prototype.getDevice = function () {
return this.device;
}
TuyaDevice.prototype.get = function (options) {
// Set empty object as default
options = options ? options : {};
const payload = {
gwId: this.device.id,
devId: this.device.id
};
debug('Payload: ', payload);
// Create byte buffer
const buffer = Parser.encode({
data: payload,
commandByte: '0a'
});
// // eslint-disable-next-line max-len
// err.message = 'Error communicating with device. Make sure nothing else is trying to control it or connected to it.';
// reject(err);
// });
// });
// };
TuyaDevice.prototype.getStatus = function (callback) {
var tuya = this;
tuya.get().then(status => {
debug('Current Status: ' + status);
callback.call(this, status);
});
}
TuyaDevice.prototype.setStatus = function (options, callback) {
var tuya = this;
tuya.set(options).then(result => {
tuya.get().then(status => {
debug('New status: ' + status);
if (callback != undefined) {
callback.call(this, status);
} else {
debug(status);
}
return;
});
});
}
TuyaDevice.prototype.toggle = function (callback) {
var tuya = this;
const TuyaDevice = require('tuyapi');
const TuyaColor = require('./tuya-color');
// Import packages
const dgram = require('dgram');
const net = require('net');
const timeout = require('p-timeout');
const retry = require('retry');
const debug = require('debug')('TuyAPI-ext');
// Helpers
const Cipher = require('tuyapi/lib/cipher');
const Parser = require('tuyapi/lib/message-parser')
TuyaDevice.prototype.getDevice = function () {
return this.device;
}
TuyaDevice.prototype.get = function (options) {
// Set empty object as default
options = options ? options : {};
const payload = {
gwId: this.device.id,
devId: this.device.id
};
debug('Payload: ', payload);
// Create byte buffer
const buffer = Parser.encode({
// resolve(data);
// });
// // Handle errors
// client.on('error', err => {
// debug('Error event from socket.');
// client.destroy(); // kill client after server's response
// // eslint-disable-next-line max-len
// err.message = 'Error communicating with device. Make sure nothing else is trying to control it or connected to it.';
// reject(err);
// });
// });
// };
TuyaDevice.prototype.getStatus = function (callback) {
var tuya = this;
tuya.get().then(status => {
debug('Current Status: ' + status);
callback.call(this, status);
});
}
TuyaDevice.prototype.setStatus = function (options, callback) {
var tuya = this;
tuya.set(options).then(result => {
tuya.get().then(status => {
debug('New status: ' + status);
if (callback != undefined) {
callback.call(this, status);
} else {
debug(status);
});
});
}
TuyaDevice.prototype.toggle = function (callback) {
var tuya = this;
//tuya.resolveId().then(() => {
tuya.get().then(status => {
tuya.setStatus({
set: !status
}, callback);
});
//});
}
TuyaDevice.prototype.onoff = function (newStatus, callback) {
newStatus = newStatus.toLowerCase();
debug("onoff: " + newStatus);
if (newStatus == "on") {
this.on(callback);
}
if (newStatus == "off") {
this.off(callback);
}
if (newStatus == "toggle") {
this.toggle(callback);
}
}
TuyaDevice.prototype.setColor = function (hexColor, callback) {
debug("Set color to", hexColor);
var tuya = this;
}
TuyaDevice.prototype.setColor = function (hexColor, callback) {
debug("Set color to", hexColor);
var tuya = this;
var color = new TuyaColor(tuya);
var dps = color.setColor(hexColor);
//tuya.resolveId().then(() => {
tuya.get().then(status => {
tuya.setStatus(dps, callback);
});
//});
}
TuyaDevice.prototype.on = function (callback) {
var tuya = this;
//tuya.resolveId().then(() => {
tuya.get().then(status => {
tuya.setStatus({
set: true
}, callback);
});
//});
}
TuyaDevice.prototype.off = function (callback) {
debug("off: ");
var tuya = this;
//tuya.resolveId().then(() => {
tuya.get().then(status => {
tuya.setStatus({
TuyaDevice.prototype.onoff = function (newStatus, callback) {
newStatus = newStatus.toLowerCase();
debug("onoff: " + newStatus);
if (newStatus == "on") {
this.on(callback);
}
if (newStatus == "off") {
this.off(callback);
}
if (newStatus == "toggle") {
this.toggle(callback);
}
}
TuyaDevice.prototype.setColor = function (hexColor, callback) {
debug("Set color to", hexColor);
var tuya = this;
var color = new TuyaColor(tuya);
var dps = color.setColor(hexColor);
//tuya.resolveId().then(() => {
tuya.get().then(status => {
tuya.setStatus(dps, callback);
});
//});
}
TuyaDevice.prototype.on = function (callback) {
var tuya = this;
//tuya.resolveId().then(() => {
tuya.get().then(status => {
} else if (options.dps) {
resolve(dps[options.dps]);
} else {
if (dps != undefined && dps["1"] != undefined) {
resolve(dps['1']);
} else {
resolve(dps);
}
}
}).catch(err => {
reject(err);
});
});
};
TuyaDevice.prototype.set = function (options) {
let dps = {};
var count = Object.keys(options).length;
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;
}
TuyaDevice.prototype.setStatus = function (options, callback) {
var tuya = this;
tuya.set(options).then(result => {
tuya.get().then(status => {
debug('New status: ' + status);
if (callback != undefined) {
callback.call(this, status);
} else {
debug(status);
}
return;
});
});
}
TuyaDevice.prototype.toggle = function (callback) {
var tuya = this;
//tuya.resolveId().then(() => {
tuya.get().then(status => {
tuya.setStatus({
set: !status
}, callback);
});
//});
}
TuyaDevice.prototype.onoff = function (newStatus, callback) {
newStatus = newStatus.toLowerCase();
debug("onoff: " + newStatus);
if (newStatus == "on") {
this.on(callback);
}
});
//});
}
TuyaDevice.prototype.on = function (callback) {
var tuya = this;
//tuya.resolveId().then(() => {
tuya.get().then(status => {
tuya.setStatus({
set: true
}, callback);
});
//});
}
TuyaDevice.prototype.off = function (callback) {
debug("off: ");
var tuya = this;
//tuya.resolveId().then(() => {
tuya.get().then(status => {
tuya.setStatus({
set: false
}, callback);
});
//});
}
module.exports = TuyaDevice;