mirror of
https://github.com/lehanspb/tuya-mqtt.git
synced 2025-12-18 16:17:30 +00:00
Bump dependency to tuyapi 5.1.2
tuyapi 5.1.2 includes fix for memory leak so also removed hack for disconnect/reconnecting devices.
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "tuya-mqtt",
|
"name": "tuya-mqtt",
|
||||||
"version": "2.0.2",
|
"version": "2.0.3",
|
||||||
"description": "",
|
"description": "",
|
||||||
"homepage": "https://github.com/TheAgentK/tuya-mqtt#readme",
|
"homepage": "https://github.com/TheAgentK/tuya-mqtt#readme",
|
||||||
"main": "tuya-mqtt.js",
|
"main": "tuya-mqtt.js",
|
||||||
@@ -16,7 +16,7 @@
|
|||||||
"color-convert": "^1.9.3",
|
"color-convert": "^1.9.3",
|
||||||
"debug": "^4.1.1",
|
"debug": "^4.1.1",
|
||||||
"mqtt": "^3.0.0",
|
"mqtt": "^3.0.0",
|
||||||
"tuyapi": "^5.1.1"
|
"tuyapi": "^5.1.2"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
47
tuya-mqtt.js
47
tuya-mqtt.js
@@ -10,11 +10,6 @@ var cleanup = require('./cleanup').Cleanup(onExit);
|
|||||||
var CONFIG = undefined;
|
var CONFIG = undefined;
|
||||||
var mqtt_client = undefined;
|
var mqtt_client = undefined;
|
||||||
|
|
||||||
// Gloabal variable to track all registered Tuya devices
|
|
||||||
// Used to disconnect/reconnect devices every 60 minutes
|
|
||||||
// due to memory leak in tuyapi >5.1.x
|
|
||||||
const tuyaDevices = new Array();
|
|
||||||
|
|
||||||
function bmap(istate) {
|
function bmap(istate) {
|
||||||
return istate ? 'ON' : "OFF";
|
return istate ? 'ON' : "OFF";
|
||||||
}
|
}
|
||||||
@@ -270,22 +265,6 @@ function onExit() {
|
|||||||
TuyaDevice.disconnectAll();
|
TuyaDevice.disconnectAll();
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Function to check if devices has previously been created
|
|
||||||
* Used for memory leak hack for tuyapi >5.1.x
|
|
||||||
*/
|
|
||||||
function existingTuyaDevice(device) {
|
|
||||||
var existing = false;
|
|
||||||
tuyaDevices.forEach(tuyaDev => {
|
|
||||||
if (tuyaDev.hasOwnProperty("options")) {
|
|
||||||
if (tuyaDev.options.id === device.options.id) {
|
|
||||||
existing = true;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
});
|
|
||||||
return existing;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Simple sleep to pause in async functions
|
// Simple sleep to pause in async functions
|
||||||
function sleep(sec) {
|
function sleep(sec) {
|
||||||
return new Promise(res => setTimeout(res, sec*1000));
|
return new Promise(res => setTimeout(res, sec*1000));
|
||||||
@@ -355,12 +334,6 @@ const main = async() => {
|
|||||||
device.then(function (params) {
|
device.then(function (params) {
|
||||||
var device = params.device;
|
var device = params.device;
|
||||||
|
|
||||||
// If new device add to registered device list
|
|
||||||
// Used only for reconnecting devices due to tuyapi 5.1.1 memory leak
|
|
||||||
if (!existingTuyaDevice(device)) {
|
|
||||||
tuyaDevices.push(device);
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case "command":
|
case "command":
|
||||||
var command = getCommandFromTopic(topic, message);
|
var command = getCommandFromTopic(topic, message);
|
||||||
@@ -396,23 +369,3 @@ const main = async() => {
|
|||||||
|
|
||||||
// Call the main code
|
// Call the main code
|
||||||
main()
|
main()
|
||||||
|
|
||||||
// Hack for memory leak in Tuyapi > 5.1.x
|
|
||||||
// Disconnect and reconnect all devices every 60 minutes
|
|
||||||
setInterval(async function() {
|
|
||||||
tuyaDevices.forEach(tuyaDev => {
|
|
||||||
var device = new TuyaDevice(tuyaDev.options);
|
|
||||||
device.then(function (params) {
|
|
||||||
device = params.device;
|
|
||||||
device.disconnect();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
await sleep(1);
|
|
||||||
tuyaDevices.forEach(tuyaDev => {
|
|
||||||
var device = new TuyaDevice(tuyaDev.options);
|
|
||||||
device.then(function (params) {
|
|
||||||
device = params.device;
|
|
||||||
device.connect();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}, 3600000);
|
|
||||||
|
|||||||
Reference in New Issue
Block a user