diff --git a/package-lock.json b/package-lock.json index 9a4d42f..529bf9b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "tuya-mqtt", - "version": "2.1.0", + "version": "3.0.0-beta1", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -378,6 +378,14 @@ "resolved": "https://registry.npmjs.org/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz", "integrity": "sha1-nbe1lJatPzz+8wp1FC0tkwrXJlE=" }, + "json5": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/json5/-/json5-2.1.3.tgz", + "integrity": "sha512-KXPvOm8K9IJKFM0bmdn8QXh7udDh1g/giieX0NLCaMnb4hEiVFqnop2ImTXCc5e0/oHz3LTqmHGtExn5hfMkOA==", + "requires": { + "minimist": "^1.2.5" + } + }, "leven": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/leven/-/leven-2.1.0.tgz", @@ -617,9 +625,9 @@ } }, "tuyapi": { - "version": "5.3.1", - "resolved": "https://registry.npmjs.org/tuyapi/-/tuyapi-5.3.1.tgz", - "integrity": "sha512-l0bbWxe4L8J7/bAQn0bJtBVbVDAEglC1T3a/YKYM3UvDXaKgFQUDVKhfQfHFAt0bzXVq1TeqU0zG4WIrxgiTHg==", + "version": "5.3.2", + "resolved": "https://registry.npmjs.org/tuyapi/-/tuyapi-5.3.2.tgz", + "integrity": "sha512-RKdTTnWVK+DDq3iRUTMh5DVd8coIwoulHntB+HvcDLuakDgSoNUc0Pzd69mw0CTTP7HTC6x6S9Ztg5pJIlYE8g==", "requires": { "debug": "4.1.1", "p-retry": "4.2.0", diff --git a/package.json b/package.json index ed89737..7e02573 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "color-convert": "^2.0.1", "debug": "^4.1.1", "mqtt": "^4.2.1", - "tuyapi": "^5.3.1", + "tuyapi": "^5.3.2", "json5": "^2.1.3" }, "repository": { diff --git a/tuya-device.js b/tuya-device.js index e788c35..be67d02 100644 --- a/tuya-device.js +++ b/tuya-device.js @@ -6,11 +6,11 @@ const debugColor = require('debug')('TuyAPI:device:color'); /** * - var steckdose = new TuyaDevice({ + var device = new TuyaDevice({ id: '03200240600194781244', key: 'b8bdebab418f5b55', ip: '192.168.178.45', - type: "ver33" + version: "3.3" }); */ @@ -65,65 +65,31 @@ var TuyaDevice = (function () { this.topicLevel = this.options.id; } - if (!this.options.ip) { - const findOptions = { - id: this.options.id, - key: "yGAdlopoPVldABfn" + Object.defineProperty(this, 'device', { + value: new TuyAPI(JSON.parse(JSON.stringify(this.options))) + }); + + this.device.on('data', data => { + if (typeof data == "string") { + debugError('Data from device not encrypted:', data.replace(/[^a-zA-Z0-9 ]/g, "")); + } else { + debug('Data from device:', data); + device.triggerAll('data', data); } - findDevice = new TuyAPI(JSON.parse(JSON.stringify(findOptions))) - findDevice.find().then(() => { - this.options.ip = findDevice.device.ip - this.options.version = findDevice.device.version - Object.defineProperty(this, 'device', { - value: new TuyAPI(JSON.parse(JSON.stringify(this.options))) - }); - - this.device.on('data', data => { - if (typeof data == "string") { - debugError('Data from device not encrypted:', data.replace(/[^a-zA-Z0-9 ]/g, "")); - } else { - debug('Data from device:', data); - device.triggerAll('data', data); - } - }); - - devices.push(this); - - // Find device on network - debug("Search device in network"); - this.find().then(() => { - debug("Device found in network"); - // Connect to device - this.device.connect(); - }); - }); - } else { - Object.defineProperty(this, 'device', { - value: new TuyAPI(JSON.parse(JSON.stringify(this.options))) - }); + }); - this.device.on('data', data => { - if (typeof data == "string") { - debugError('Data from device not encrypted:', data.replace(/[^a-zA-Z0-9 ]/g, "")); - } else { - debug('Data from device:', data); - device.triggerAll('data', data); - } - }); + devices.push(this); - devices.push(this); - - // Find device on network - debug("Search device in network"); - this.find().then(() => { - debug("Device found in network"); - // Connect to device - this.device.connect(); - }); - } + // Find device on network + debug("Search device in network"); + this.find().then(() => { + debug("Device found in network"); + // Connect to device + this.device.connect(); + }); /** - * @return promis to wait for connection + * @return Promise to wait for connection */ return new Promise((resolve, reject) => { this.device.on('connected', () => { @@ -158,11 +124,7 @@ var TuyaDevice = (function () { } TuyaDevice.prototype.toString = function () { - if (typeof this.type != "undefined") { - return this.type + " (" + this.options.ip + ", " + this.options.id + ", " + this.options.key + ")"; - } else { - return " (" + this.options.ip + ", " + this.options.id + ", " + this.options.key + ")"; - } + return this.name + " (" + this.options.ip + ", " + this.options.id + ", " + this.options.key + ")"; } TuyaDevice.prototype.triggerAll = function (name, argument) { @@ -194,7 +156,7 @@ var TuyaDevice = (function () { return new Promise((resolve, reject) => { this.device.set(options).then((result) => { this.get().then(() => { - debug("set completed "); + debug("Set completed "); resolve(result); }); }); diff --git a/tuya-mqtt.js b/tuya-mqtt.js index fc49295..c98eb27 100644 --- a/tuya-mqtt.js +++ b/tuya-mqtt.js @@ -180,7 +180,7 @@ const main = async() => { } try { - tuyaDevices = fs.readFileSync('./devices.json', 'utf8'); + tuyaDevices = fs.readFileSync('./devices.conf', 'utf8'); tuyaDevices = json5.parse(tuyaDevices) } catch (e) { console.error("Devices file not found!")