mirror of
https://github.com/lehanspb/tuya-mqtt.git
synced 2025-12-18 00:10:20 +00:00
Use device.conf
This commit is contained in:
16
package-lock.json
generated
16
package-lock.json
generated
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "tuya-mqtt",
|
"name": "tuya-mqtt",
|
||||||
"version": "2.1.0",
|
"version": "3.0.0-beta1",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@@ -378,6 +378,14 @@
|
|||||||
"resolved": "https://registry.npmjs.org/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz",
|
"resolved": "https://registry.npmjs.org/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz",
|
||||||
"integrity": "sha1-nbe1lJatPzz+8wp1FC0tkwrXJlE="
|
"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": {
|
"leven": {
|
||||||
"version": "2.1.0",
|
"version": "2.1.0",
|
||||||
"resolved": "https://registry.npmjs.org/leven/-/leven-2.1.0.tgz",
|
"resolved": "https://registry.npmjs.org/leven/-/leven-2.1.0.tgz",
|
||||||
@@ -617,9 +625,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"tuyapi": {
|
"tuyapi": {
|
||||||
"version": "5.3.1",
|
"version": "5.3.2",
|
||||||
"resolved": "https://registry.npmjs.org/tuyapi/-/tuyapi-5.3.1.tgz",
|
"resolved": "https://registry.npmjs.org/tuyapi/-/tuyapi-5.3.2.tgz",
|
||||||
"integrity": "sha512-l0bbWxe4L8J7/bAQn0bJtBVbVDAEglC1T3a/YKYM3UvDXaKgFQUDVKhfQfHFAt0bzXVq1TeqU0zG4WIrxgiTHg==",
|
"integrity": "sha512-RKdTTnWVK+DDq3iRUTMh5DVd8coIwoulHntB+HvcDLuakDgSoNUc0Pzd69mw0CTTP7HTC6x6S9Ztg5pJIlYE8g==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"debug": "4.1.1",
|
"debug": "4.1.1",
|
||||||
"p-retry": "4.2.0",
|
"p-retry": "4.2.0",
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
"color-convert": "^2.0.1",
|
"color-convert": "^2.0.1",
|
||||||
"debug": "^4.1.1",
|
"debug": "^4.1.1",
|
||||||
"mqtt": "^4.2.1",
|
"mqtt": "^4.2.1",
|
||||||
"tuyapi": "^5.3.1",
|
"tuyapi": "^5.3.2",
|
||||||
"json5": "^2.1.3"
|
"json5": "^2.1.3"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
|
|||||||
@@ -6,11 +6,11 @@ const debugColor = require('debug')('TuyAPI:device:color');
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
var steckdose = new TuyaDevice({
|
var device = new TuyaDevice({
|
||||||
id: '03200240600194781244',
|
id: '03200240600194781244',
|
||||||
key: 'b8bdebab418f5b55',
|
key: 'b8bdebab418f5b55',
|
||||||
ip: '192.168.178.45',
|
ip: '192.168.178.45',
|
||||||
type: "ver33"
|
version: "3.3"
|
||||||
});
|
});
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -65,65 +65,31 @@ var TuyaDevice = (function () {
|
|||||||
this.topicLevel = this.options.id;
|
this.topicLevel = this.options.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this.options.ip) {
|
Object.defineProperty(this, 'device', {
|
||||||
const findOptions = {
|
value: new TuyAPI(JSON.parse(JSON.stringify(this.options)))
|
||||||
id: this.options.id,
|
});
|
||||||
key: "yGAdlopoPVldABfn"
|
|
||||||
|
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 => {
|
devices.push(this);
|
||||||
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");
|
||||||
// Find device on network
|
this.find().then(() => {
|
||||||
debug("Search device in network");
|
debug("Device found in network");
|
||||||
this.find().then(() => {
|
// Connect to device
|
||||||
debug("Device found in network");
|
this.device.connect();
|
||||||
// Connect to device
|
});
|
||||||
this.device.connect();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return promis to wait for connection
|
* @return Promise to wait for connection
|
||||||
*/
|
*/
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.device.on('connected', () => {
|
this.device.on('connected', () => {
|
||||||
@@ -158,11 +124,7 @@ var TuyaDevice = (function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TuyaDevice.prototype.toString = function () {
|
TuyaDevice.prototype.toString = function () {
|
||||||
if (typeof this.type != "undefined") {
|
return this.name + " (" + this.options.ip + ", " + this.options.id + ", " + this.options.key + ")";
|
||||||
return this.type + " (" + this.options.ip + ", " + this.options.id + ", " + this.options.key + ")";
|
|
||||||
} else {
|
|
||||||
return " (" + this.options.ip + ", " + this.options.id + ", " + this.options.key + ")";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TuyaDevice.prototype.triggerAll = function (name, argument) {
|
TuyaDevice.prototype.triggerAll = function (name, argument) {
|
||||||
@@ -194,7 +156,7 @@ var TuyaDevice = (function () {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.device.set(options).then((result) => {
|
this.device.set(options).then((result) => {
|
||||||
this.get().then(() => {
|
this.get().then(() => {
|
||||||
debug("set completed ");
|
debug("Set completed ");
|
||||||
resolve(result);
|
resolve(result);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -180,7 +180,7 @@ const main = async() => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
tuyaDevices = fs.readFileSync('./devices.json', 'utf8');
|
tuyaDevices = fs.readFileSync('./devices.conf', 'utf8');
|
||||||
tuyaDevices = json5.parse(tuyaDevices)
|
tuyaDevices = json5.parse(tuyaDevices)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("Devices file not found!")
|
console.error("Devices file not found!")
|
||||||
|
|||||||
Reference in New Issue
Block a user