updated magic-home to new version 2.0
This commit is contained in:
parent
c56caf3de8
commit
c650a60703
154
HCMagicHome.js
154
HCMagicHome.js
@ -72,10 +72,16 @@ class HCMagicHome extends HCColorLamp {
|
||||
futureState.on = true;
|
||||
let suid = this._sumanager.registerUpdate(futureState);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this._control.turnOn((err) => {
|
||||
updateHelper(this, err, suid, resolve, reject);
|
||||
});
|
||||
return this._control.setPower(true).then(success => {
|
||||
this._sumanager.confirmUpdate(suid);
|
||||
|
||||
if (this._sumanager.highestConfirmedId == suid) {
|
||||
this.emit("state change", this.state);
|
||||
}
|
||||
return success;
|
||||
}).catch(err => {
|
||||
this._sumanager.rejectUpdate(suid);
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
|
||||
@ -84,10 +90,16 @@ class HCMagicHome extends HCColorLamp {
|
||||
futureState.on = false;
|
||||
let suid = this._sumanager.registerUpdate(futureState);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this._control.turnOff((err) => {
|
||||
updateHelper(this, err, suid, resolve, reject);
|
||||
});
|
||||
return this._control.setPower(false).then(success => {
|
||||
this._sumanager.confirmUpdate(suid);
|
||||
|
||||
if (this._sumanager.highestConfirmedId == suid) {
|
||||
this.emit("state change", this.state);
|
||||
}
|
||||
return success;
|
||||
}).catch(err => {
|
||||
this._sumanager.rejectUpdate(suid);
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
|
||||
@ -104,12 +116,18 @@ class HCMagicHome extends HCColorLamp {
|
||||
futureState.brightness = brightness;
|
||||
let suid = this._sumanager.registerUpdate(futureState);
|
||||
|
||||
let rgbColor = HSL_to_RGB(futureState.color);
|
||||
let { red, green, blue } = HSL_to_RGB(futureState.color);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this._control.setColorWithBrightness(rgbColor.red, rgbColor.green, rgbColor.blue, futureState.brightness, (err, success) => {
|
||||
updateHelper(this, err, suid, resolve, reject, success);
|
||||
});
|
||||
return this._control.setColorWithBrightness(red, green, blue, futureState.brightness).then(success => {
|
||||
this._sumanager.confirmUpdate(suid);
|
||||
|
||||
if (this._sumanager.highestConfirmedId == suid) {
|
||||
this.emit("state change", this.state);
|
||||
}
|
||||
return success;
|
||||
}).catch(err => {
|
||||
this._sumanager.rejectUpdate(suid);
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
|
||||
@ -118,12 +136,18 @@ class HCMagicHome extends HCColorLamp {
|
||||
futureState.color = fillPartialHSL(color, futureState.color);
|
||||
let suid = this._sumanager.registerUpdate(futureState);
|
||||
|
||||
let rgbColor = HSL_to_RGB(futureState.color);
|
||||
let { red, green, blue } = HSL_to_RGB(futureState.color);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this._control.setColorWithBrightness(rgbColor.red, rgbColor.green, rgbColor.blue, futureState.brightness, (err, success) => {
|
||||
updateHelper(this, err, suid, resolve, reject, success);
|
||||
});
|
||||
return this._control.setColorWithBrightness(red, green, blue, futureState.brightness).then(success => {
|
||||
this._sumanager.confirmUpdate(suid);
|
||||
|
||||
if (this._sumanager.highestConfirmedId == suid) {
|
||||
this.emit("state change", this.state);
|
||||
}
|
||||
return success;
|
||||
}).catch(err => {
|
||||
this._sumanager.rejectUpdate(suid);
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
|
||||
@ -149,84 +173,68 @@ class HCMagicHome extends HCColorLamp {
|
||||
}
|
||||
|
||||
let futureState = this.state;
|
||||
let promise;
|
||||
|
||||
if(effect == 'none') {
|
||||
futureState.effect = "none";
|
||||
let suid = this._sumanager.registerUpdate(futureState);
|
||||
|
||||
let rgbColor = HSL_to_RGB(futureState.color);
|
||||
let { red, green, blue } = HSL_to_RGB(futureState.color);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this._control.setColorWithBrightness(rgbColor.red, rgbColor.green, rgbColor.blue, futureState.brightness, (err, success) => {
|
||||
updateHelper(this, err, suid, resolve, reject, success);
|
||||
});
|
||||
});
|
||||
promise = this._control.setColorWithBrightness(red, green, blue, futureState.brightness);
|
||||
} else {
|
||||
futureState.effect = id; // id is "valid" / can be parsed
|
||||
let suid = this._sumanager.registerUpdate(futureState);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this._control.setPattern(effect, speed, (err, success) => {
|
||||
updateHelper(this, err, suid, resolve, reject, success);
|
||||
});
|
||||
});
|
||||
promise = this._control.setPattern(effect, speed);
|
||||
}
|
||||
|
||||
let suid = this._sumanager.registerUpdate(futureState);
|
||||
|
||||
return promise.then(success => {
|
||||
this._sumanager.confirmUpdate(suid);
|
||||
|
||||
if (this._sumanager.highestConfirmedId == suid) {
|
||||
this.emit("state change", this.state);
|
||||
}
|
||||
return success;
|
||||
}).catch(err => {
|
||||
this._sumanager.rejectUpdate(suid);
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
|
||||
pullState() {
|
||||
return new Promise((resolve, reject) => {
|
||||
this._control.queryState((err, status) => {
|
||||
if(err) return reject(err);
|
||||
return this._control.queryState().then(status => {
|
||||
let currentState = this.state;
|
||||
let futureState = currentState.clone();
|
||||
|
||||
let currentState = this.state;
|
||||
let futureState = currentState.clone();
|
||||
//console.log(status);
|
||||
|
||||
//console.log(status);
|
||||
futureState.on = status.on;
|
||||
|
||||
futureState.on = status.on;
|
||||
let effect = (status.mode != 'color' && status.mode != 'warm_white' && status.mode != 'special') ? status.mode : 'none';
|
||||
if(effect != 'none') {
|
||||
let speed = (status.speed > 80) ? 'fast' : (status.speed > 30) ? 'medium' : 'slow';
|
||||
futureState.effect = speed + '-' + effect;
|
||||
} else {
|
||||
futureState.effect = 'none';
|
||||
}
|
||||
|
||||
let effect = (status.mode != 'color' && status.mode != 'warm_white' && status.mode != 'special') ? status.mode : 'none';
|
||||
if(effect != 'none') {
|
||||
let speed = (status.speed > 80) ? 'fast' : (status.speed > 30) ? 'medium' : 'slow';
|
||||
futureState.effect = speed + '-' + effect;
|
||||
} else {
|
||||
futureState.effect = 'none';
|
||||
}
|
||||
if (futureState.effect == 'none') { // only update color when no effects are playing
|
||||
futureState.brightness = extractBrightness(status.color);
|
||||
futureState.color = RGB_to_HSL(removeBrightness(status.color));
|
||||
}
|
||||
|
||||
if (futureState.effect == 'none') { // only update color when no effects are playing
|
||||
futureState.brightness = extractBrightness(status.color);
|
||||
futureState.color = RGB_to_HSL(removeBrightness(status.color));
|
||||
}
|
||||
|
||||
if (currentState.hash != futureState.hash) {
|
||||
// the state of the controller has changed externally
|
||||
this._sumanager.insertConfirmedState(futureState);
|
||||
this.emit("state change", this.state);
|
||||
}
|
||||
|
||||
return resolve();
|
||||
});
|
||||
if (currentState.hash != futureState.hash) {
|
||||
// the state of the controller has changed externally
|
||||
this._sumanager.insertConfirmedState(futureState);
|
||||
this.emit("state change", this.state);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = HCMagicHome;
|
||||
|
||||
function updateHelper(self, err, suid, resolve, reject, resolveVal) {
|
||||
if (err) {
|
||||
self._sumanager.rejectUpdate(suid);
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
self._sumanager.confirmUpdate(suid);
|
||||
|
||||
if (self._sumanager.highestConfirmedId == suid) {
|
||||
self.emit("state change", self.state);
|
||||
}
|
||||
|
||||
return resolve(resolveVal);
|
||||
}
|
||||
|
||||
function HSL_to_RGB(hsl) {
|
||||
var h = hsl.hue / 360;
|
||||
var s = hsl.sat / 100;
|
||||
|
||||
16
package-lock.json
generated
16
package-lock.json
generated
@ -1,11 +1,11 @@
|
||||
{
|
||||
"name": "hc-magichome",
|
||||
"version": "1.0.0",
|
||||
"version": "1.0.1",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
"homecontrol-control-base": {
|
||||
"version": "git+https://git.literalchaos.de/jan/homecontrol-control-base.git#96b0c33583b893e236567e11082b1d093382a79e",
|
||||
"version": "git+https://git.literalchaos.de/jan/homecontrol-control-base.git#b7ed807affe888d61efd9dbff3d55bcc128215b7",
|
||||
"from": "git+https://git.literalchaos.de/jan/homecontrol-control-base.git",
|
||||
"requires": {
|
||||
"merge-options": "^1.0.1",
|
||||
@ -18,9 +18,9 @@
|
||||
"integrity": "sha1-caUMhCnfync8kqOQpKA7OfzVHT4="
|
||||
},
|
||||
"magic-home": {
|
||||
"version": "1.4.0",
|
||||
"resolved": "https://registry.npmjs.org/magic-home/-/magic-home-1.4.0.tgz",
|
||||
"integrity": "sha512-hSq3C/9rbEH3RB2t2rG/LsSb2vpalzIGh/mLKJyjaki3Wbvt9ss+2OUJ1JsAOAjjgkvxDEfbQ044yFsHZUS75A=="
|
||||
"version": "2.0.1",
|
||||
"resolved": "https://registry.npmjs.org/magic-home/-/magic-home-2.0.1.tgz",
|
||||
"integrity": "sha512-nC2XB9ENZf4afGJFvTnKKqxx+xuFOE1TjKVYiSY9B5aSkE88Z4Q5bYeVihoTEG8r8pOEZSYHnuLIi2tPXwsCwg=="
|
||||
},
|
||||
"merge-options": {
|
||||
"version": "1.0.1",
|
||||
@ -31,9 +31,9 @@
|
||||
}
|
||||
},
|
||||
"node-object-hash": {
|
||||
"version": "1.4.1",
|
||||
"resolved": "https://registry.npmjs.org/node-object-hash/-/node-object-hash-1.4.1.tgz",
|
||||
"integrity": "sha512-JQVqSM5/mOaUoUhCYR0t1vgm8RFo7qpJtPvnoFCLeqQh1xrfmr3BCD3nGBnACzpIEF7F7EVgqGD3O4lao/BY/A=="
|
||||
"version": "1.4.2",
|
||||
"resolved": "https://registry.npmjs.org/node-object-hash/-/node-object-hash-1.4.2.tgz",
|
||||
"integrity": "sha512-UdS4swXs85fCGWWf6t6DMGgpN/vnlKeSGEQ7hJcrs7PBFoxoKLmibc3QRb7fwiYsjdL7PX8iI/TMSlZ90dgHhQ=="
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "hc-magichome",
|
||||
"version": "1.0.1",
|
||||
"version": "1.0.2",
|
||||
"description": "Magic Home Plugin for Homecontrol",
|
||||
"main": "HCMagicHome.js",
|
||||
"scripts": {
|
||||
@ -14,7 +14,7 @@
|
||||
"license": "UNLICENSED",
|
||||
"dependencies": {
|
||||
"homecontrol-control-base": "git+https://git.literalchaos.de/jan/homecontrol-control-base.git",
|
||||
"magic-home": "^1.4.0",
|
||||
"magic-home": "^2.0.1",
|
||||
"merge-options": "^1.0.1"
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user