Skip to content

Commit

Permalink
* (bluefox) Corrected error with array comparison
Browse files Browse the repository at this point in the history
* (bluefox) added tests
  • Loading branch information
GermanBluefox committed Sep 6, 2024
1 parent 453ece5 commit 3af0bef
Show file tree
Hide file tree
Showing 8 changed files with 418 additions and 57 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ For example, `red = 0`, blue and green will stay unchanged.
Placeholder for the next version (at the beginning of the line):
### **WORK IN PROGRESS**
-->
### **WORK IN PROGRESS**
* (bluefox) Corrected error with array comparison

### 2.0.0 (2024-09-05)
* (bluefox) The adapter was completely refactored
* (bluefox) Added compact mode
Expand Down
66 changes: 36 additions & 30 deletions lib/devices.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ const LW12 = {
responseLen2: 11,
on: [0xCC, 0x23, 0x33],
off: [0xCC, 0x24, 0x33],
rgb: [0x56, VARS.red, VARS.green, VARS.blue, 0xAA],
progOn: [0xCC, 0x21, 0x33],
progOff: [0xCC, 0x20, 0x33],
progNo: [0xBB, VARS.prog, VARS.speed, 0x44],
statusRequest: [0xEF, 0x01, 0x77],
programNames: programNames,
programNames,
progNo: [0xBB, VARS.prog, VARS.speed, 0x44],
rgb: [0x56, VARS.red, VARS.green, VARS.blue, 0xAA],
decodeResponse: data => {
if (data[0] !== 0x66 || data[1] !== 0x01) {
return [11, null];
Expand Down Expand Up @@ -89,15 +89,15 @@ const LD382A = {
responseLen2: 4,
on: [0x71, 0x23, 0x0f],
off: [0x71, 0x24, 0x0f],
rgb: [0x31, VARS.red, VARS.green, VARS.blue, 0xff /*VARS.white*/, 0x00, 0x0f],
rgbw: [0x31, VARS.red, VARS.green, VARS.blue, VARS.white, 0x00, 0x0f],
progOn: [0x71, 0x21, 0x0f],
progOff: [0x71, 0x20, 0x0f],
progNo: [97, VARS.prog, VARS.speed, 0x0f],
statusRequest: [0x81, 0x8A, 0x8B],
programNames: programNames,
programNames,
progNo: [97, VARS.prog, VARS.speed, 0x0f],
rgbw: [0x31, VARS.red, VARS.green, VARS.blue, VARS.white, 0x00, 0x0f],
rgb: [0x31, VARS.red, VARS.green, VARS.blue, 0xff /*VARS.white*/, 0x00, 0x0f],

decodeResponse: function(data) {
decodeResponse: data => {
// If power on / off request, the result is 4 bytes long and the second byte is 0x71
if (data[1] === 0x71) {
return [4, null];
Expand All @@ -106,20 +106,23 @@ const LD382A = {
return [0, null];
}
//[129, 4, 35, 97, 33, 9, 11, 22, 33, 255, 3, 0, 0, 119]
return [14, {
//power: ((data[2] === 0x23) ? true : false),
on: (data[2] === 0x23),
//power: ((data[13] & 0x01) ? true : false),
//power: ((data[13] & 0x01) ? false : true),
progNo: data[3],//mode
progOn: data[4] === 33, //modeRun
progSpeed: data[5], //modeSpeed
red: data[6],
green: data[7],
blue: data[8],
white: data[9],
}];
}
return [
14,
{
// power: ((data[2] === 0x23) ? true : false),
on: data[2] === 0x23,
// power: ((data[13] & 0x01) ? true : false),
// power: ((data[13] & 0x01) ? false : true),
progNo: data[3],// mode
progOn: data[4] === 33, // modeRun
progSpeed: data[5], // modeSpeed
red: data[6],
green: data[7],
blue: data[8],
white: data[9],
},
];
},
};

const LD686 = {
Expand All @@ -139,9 +142,9 @@ const LD686 = {
progOff: [0x71, 0x20, 0x0f],
progNo: [97, VARS.prog, VARS.speed, 0x0f],
statusRequest: [0x81, 0x8A, 0x8B],
programNames: programNames,
programNames,

decodeResponse: function (data) {
decodeResponse: data => {
if (data[0] !== 129) {
return [0, null];
}
Expand All @@ -168,11 +171,11 @@ const LD382 = Object.assign({}, LD382A, { // not tested
const UFO = Object.assign({}, LD382A, { // not tested
on: [0x71, 0x23],
off: [0x71, 0x24],
rgb: [0x31, VARS.red, VARS.green, VARS.blue, 0x00, 0x00, 0x00],
rgbw: [0x31, VARS.red, VARS.green, VARS.blue, VARS.white, 0x00],
progOn: [0x71, 0x21],
progOff: [0x71, 0x20],
progNo: [0x61, VARS.prog, VARS.speed],
rgbw: [0x31, VARS.red, VARS.green, VARS.blue, VARS.white, 0x00],
rgb: [0x31, VARS.red, VARS.green, VARS.blue, 0x00, 0x00, 0x00],
statusRequest: [0x81, 0x8A, 0x8B],
});

Expand All @@ -188,6 +191,7 @@ const MiLight = {
colorSteps: 255,
delay: 100,

// this function cannot be lambda (=>) because it is used with "this"
setZone: function (zone) {
const self = this;
if (zone > 4) {
Expand Down Expand Up @@ -255,12 +259,10 @@ const MiLight = {
wtmaxBright: [0xB5, 0x00, 0x55],
wtBrightUp: [0x3C, 0x00, 0x55],
wtBrightDown: [0x34, 0x00, 0x55],
wtWarmer: [0x3E, 0x00,0x55],
wtWarmer: [0x3E, 0x00, 0x55],
wtCooler: [0x3F, 0x00, 0x55],

progNo: [0x4D, 0x00, 0x55],
bri: [0x4E, VARS.bright, 0x55],
hue: [0x40, VARS.red, 0x55],

pair: [0x25, 0x00, 0x55], // send 3 x with 1 sec delay
unPair: [0x25, 0x00, 0x55], // send 15 x with 200 ms delay
Expand All @@ -269,6 +271,9 @@ const MiLight = {

rgb: [0x00],
rgbw: [0x00],
bri: [0x4E, VARS.bright, 0x55],
hue: [0x40, VARS.red, 0x55],

programNames: {
0: '[Off]',
1: 'Regenbogen',
Expand Down Expand Up @@ -342,8 +347,9 @@ const MiLightW = Object.assign({}, MiLight, {

module.exports = {
VARS,
MiLightW,
knownDeviceNames,

MiLightW,
LW12,
LD382A,
LD686,
Expand Down
13 changes: 13 additions & 0 deletions lib/dontBeSoSoef.js
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,18 @@ function Timer(func, timeout, v1) {
}
}

function compareArrays(arr1, arr2) {
if (arr1.length !== arr2.length) {
return false;
}
for (let i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) {
return false;
}
}
return true;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

async function forEachInSystemObjectView(type, id, callback) {
Expand Down Expand Up @@ -662,4 +674,5 @@ module.exports = {
devices,
fullExtend,
CDevice,
compareArrays,
};
73 changes: 47 additions & 26 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const {
devices,
fullExtend,
CDevice,
compareArrays,
} = require('./lib/dontBeSoSoef');

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -494,7 +495,11 @@ class WifiLight {

function doIt() {
if (self.queue.length > 0) {
setTimeout(doIt, self.queue.length * 2);
this.doItTimeout && clearTimeout(this.doItTimeout);
this.doItTimeout = setTimeout(() => {
this.doItTimeout = null;
doIt();
}, self.queue.length * 2);
return;
}
if (++i >= cmds.length) {
Expand Down Expand Up @@ -532,10 +537,13 @@ class WifiLight {
timeout = cb;
cb = undefined;
}
if (this.client) {
this.destroyClient();
setTimeout(() => this.start(cb), timeout === undefined ? 5000 : timeout);
}

this.destroyClient();

this.reconnectTimeout = this.reconnectTimeout || setTimeout(() => {
this.reconnectTimeout = null;
this.start(cb);
}, timeout === undefined ? 5000 : timeout);
}

_write(data, cb) {
Expand All @@ -559,16 +567,16 @@ class WifiLight {
this.log(`onClose ${ts}hasError=${hasError} client=${this.config.ip}:${this.config.port}`);
});
this.client.on('error', error => {
const ts = debug ? `(${Math.round((Date.now() - this.ts) / 1000)} sec) ` : '';

this.log(`onError: ${ts}${error.code !== undefined ? error.code : ''} ${error.message}`);

switch (error.code) { //error.code
switch (error.code) {
case 'ECONNRESET':
case 'ETIMEDOUT':
case 'EPIPE':
adapter.log.warn(`[${this.config.ip}] onError: ${error.code} ${error.message} - reconnecting in 5 sec...`);
this.reconnect(5000);
break;
default:
adapter.log.error(`[${this.config.ip}] onError: ${error.code} ${error.message} - will not be reconnected`);
break;
}
this.setOnline(false);
});
Expand All @@ -591,6 +599,34 @@ class WifiLight {
clearTimeout(this.updateTimer);
this.updateTimer = null;
}

if (this.doItTimeout) {
clearTimeout(this.doItTimeout);
this.doItTimeout = null;
}

if (this.writeTimeout) {
clearTimeout(this.writeTimeout);
this.writeTimeout = null;
}

if (this.writeTimer) {
clearTimeout(this.writeTimer);
this.writeTimer = null;
}

if (this.onTimerObject) {
clearTimeout(this.onTimerObject);
this.onTimerObject = null;
}

if (this.reconnectTimeout) {
clearTimeout(this.reconnectTimeout);
this.reconnectTimeout = null;
}

this.prgTimer.clear();

if (this.client) {
this.client.destroy();
this.client = null;
Expand Down Expand Up @@ -630,21 +666,6 @@ class WifiLight {
close() {
this.clearQueue();
this.destroyClient();
if (this.writeTimeout) {
clearTimeout(this.writeTimeout);
this.writeTimeout = null;
}

if (this.writeTimer) {
clearTimeout(this.writeTimer);
this.writeTimer = null;
}

if (this.onTimerObject) {
clearTimeout(this.onTimerObject);
this.onTimerObject = null;
}
this.prgTimer.clear();
}

runUpdateTimer() {
Expand Down Expand Up @@ -781,7 +802,7 @@ class WifiLight {
if (!(akt.inProcess || (!akt.ctrl && akt.ts && akt.ts < Date.now()))) {
break;
}
if (this.queue.length <= 1 && !akt.cmd.eq(this.cmds.statusRequest)) {
if (this.queue.length <= 1 && !compareArrays(akt.cmd, this.cmds.statusRequest)) {
this.directRefresh(akt.channel);
}
this.queue.shift();
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@alcalzone/release-script-plugin-license": "^3.7.0",
"@alcalzone/release-script-plugin-manual-review": "^3.7.0",
"@iobroker/adapter-dev": "^1.3.0",
"@iobroker/legacy-testing": "^1.0.13",
"@iobroker/testing": "^4.1.3",
"@tsconfig/node14": "^14.1.2",
"@types/chai": "^4.3.19",
Expand Down Expand Up @@ -67,6 +68,7 @@
"test:js": "mocha --config test/mocharc.custom.json \"{!(node_modules|test)/**/*.test.js,*.test.js,test/**/test!(PackageFiles|Startup).js}\"",
"test:package": "mocha test/package --exit",
"test:integration": "mocha test/integration --exit",
"test:legacy": "mocha test/legacy --exit",
"test": "npm run test:js && npm run test:package",
"check": "tsc --noEmit -p tsconfig.check.json",
"lint": "eslint .",
Expand Down
Loading

0 comments on commit 3af0bef

Please sign in to comment.