Skip to content

Commit 2de4bdb

Browse files
committed
Modified to send custom formatted RGB values
1 parent bae048d commit 2de4bdb

File tree

1 file changed

+31
-60
lines changed

1 file changed

+31
-60
lines changed

index.js

+31-60
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ HTTP_RGB.prototype = {
247247
this.log('getPowerState() failed: %s', error.message);
248248
callback(error);
249249
} else {
250-
var powerOn = parseInt(responseBody) > 0;
250+
var stateObj = JSON.parse(responseBody);
251+
var powerOn = stateObj.channel.red > 0 || stateObj.channel.green > 0 || stateObj.channel.blue > 0;
251252
this.log('power is currently %s', powerOn ? 'ON' : 'OFF');
252253
callback(null, powerOn);
253254
}
@@ -269,11 +270,12 @@ HTTP_RGB.prototype = {
269270
return;
270271
}
271272

273+
var rgb = this._hsvToRgb(this.cache.hue, this.cache.saturation, this.cache.brightness)
272274
if (state) {
273-
url = this.switch.powerOn.set_url;
275+
url = this.switch.powerOn.set_url.replace('{{RED}}', rgb.r).replace('{{GREEN}}', rgb.g).replace('{{BLUE}}', rgb.b);
274276
body = this.switch.powerOn.body;
275277
} else {
276-
url = this.switch.powerOff.set_url;
278+
url = this.switch.powerOn.set_url.replace('{{RED}}', 0).replace('{{GREEN}}', 0).replace('{{BLUE}}', 0);
277279
body = this.switch.powerOff.body;
278280
}
279281

@@ -306,7 +308,9 @@ HTTP_RGB.prototype = {
306308
this.log('getBrightness() failed: %s', error.message);
307309
callback(error);
308310
} else {
309-
var level = parseInt(responseBody);
311+
var responseObj = JSON.parse(responseBody);
312+
var hsl = this._rgbToHsl(responseObj.channel.red, responseObj.channel.green, responseObj.channel.blue);
313+
var level = hsl.l;
310314
this.log('brightness is currently at %s %', level);
311315
callback(null, level);
312316
}
@@ -365,14 +369,14 @@ HTTP_RGB.prototype = {
365369
this.log('... getHue() failed: %s', error.message);
366370
callback(error);
367371
} else {
368-
var rgb = responseBody;
369-
var levels = this._rgbToHsl(
370-
parseInt(rgb.substr(0,2),16),
371-
parseInt(rgb.substr(2,2),16),
372-
parseInt(rgb.substr(4,2),16)
373-
);
372+
var stateObj = JSON.parse(responseBody);
373+
var hsl = this._rgbToHsl(
374+
stateObj.channel.red,
375+
stateObj.channel.green,
376+
stateObj.channel.blue
377+
);
374378

375-
var hue = levels[0];
379+
var hue = hsl.h;
376380

377381
this.log('... hue is currently %s', hue);
378382
this.cache.hue = hue;
@@ -420,14 +424,14 @@ HTTP_RGB.prototype = {
420424
this.log('... getSaturation() failed: %s', error.message);
421425
callback(error);
422426
} else {
423-
var rgb = responseBody;
424-
var levels = this._rgbToHsl(
425-
parseInt(rgb.substr(0,2),16),
426-
parseInt(rgb.substr(2,2),16),
427-
parseInt(rgb.substr(4,2),16)
428-
);
427+
var stateObj = JSON.parse(responseBody);
428+
var hsl = this._rgbToHsl(
429+
stateObj.channel.red,
430+
stateObj.channel.green,
431+
stateObj.channel.blue
432+
);
429433

430-
var saturation = levels[1];
434+
var saturation = hsl.s;
431435

432436
this.log('... saturation is currently %s', saturation);
433437
this.cache.saturation = saturation;
@@ -465,24 +469,13 @@ HTTP_RGB.prototype = {
465469
*/
466470
_setRGB: function(callback) {
467471
var rgb = this._hsvToRgb(this.cache.hue, this.cache.saturation, this.cache.brightness);
468-
var r = this._decToHex(rgb.r);
469-
var g = this._decToHex(rgb.g);
470-
var b = this._decToHex(rgb.b);
472+
var r = rgb.r;
473+
var g = rgb.g;
474+
var b = rgb.b;
471475

472-
var url = this.color.set_url.replace('%s', r + g + b);
476+
var url = this.color.set_url.replace('{{RED}}', r).replace('{{GREEN}}', g).replace('{{BLUE}}', b);
473477
this.cacheUpdated = false;
474-
475-
this.log('_setRGB converting H:%s S:%s B:%s to RGB:%s ...', this.cache.hue, this.cache.saturation, this.cache.brightness, r + g + b);
476-
477-
this._httpRequest(url, '', this.color.http_method, function(error, response, body) {
478-
if (error) {
479-
this.log('... _setRGB() failed: %s', error);
480-
callback(error);
481-
} else {
482-
this.log('... _setRGB() successfully set to #%s', r + g + b);
483-
callback();
484-
}
485-
}.bind(this));
478+
this._httpRequest(url, '', 'GET', callback);
486479
},
487480

488481
/** Utility Functions **/
@@ -514,7 +507,7 @@ HTTP_RGB.prototype = {
514507
* Converts an HSV color value to RGB. Conversion formula
515508
* adapted from http://stackoverflow.com/a/17243070/2061684
516509
* Assumes h in [0..360], and s and l in [0..100] and
517-
* returns r, g, and b in [0..255].
510+
* returns r, g, and b in [0..1].
518511
*
519512
* @param {Number} h The hue
520513
* @param {Number} s The saturation
@@ -541,14 +534,14 @@ HTTP_RGB.prototype = {
541534
case 4: r = t; g = p; b = v; break;
542535
case 5: r = v; g = p; b = q; break;
543536
}
544-
var rgb = { r: Math.round(r * 255), g: Math.round(g * 255), b: Math.round(b * 255) };
537+
var rgb = { r: r, g: g, b: b };
545538
return rgb;
546539
},
547540

548541
/**
549542
* Converts an RGB color value to HSL. Conversion formula
550543
* adapted from http://en.wikipedia.org/wiki/HSL_color_space.
551-
* Assumes r, g, and b are in [0..255] and
544+
* Assumes r, g, and b are in [0..1] and
552545
* returns h in [0..360], and s and l in [0..100].
553546
*
554547
* @param {Number} r The red color value
@@ -557,9 +550,6 @@ HTTP_RGB.prototype = {
557550
* @return {Array} The HSL representation
558551
*/
559552
_rgbToHsl: function(r, g, b){
560-
r /= 255;
561-
g /= 255;
562-
b /= 255;
563553
var max = Math.max(r, g, b), min = Math.min(r, g, b);
564554
var h, s, l = (max + min) / 2;
565555

@@ -579,26 +569,7 @@ HTTP_RGB.prototype = {
579569
h *= 360; // return degrees [0..360]
580570
s *= 100; // return percent [0..100]
581571
l *= 100; // return percent [0..100]
582-
return [parseInt(h), parseInt(s), parseInt(l)];
572+
return {h: parseInt(h), s: parseInt(s), l: parseInt(l)};
583573
},
584574

585-
/**
586-
* Converts a decimal number into a hexidecimal string, with optional
587-
* padding (default 2 characters).
588-
*
589-
* @param {Number} d Decimal number
590-
* @param {String} padding Padding for the string
591-
* @return {String} '0' padded hexidecimal number
592-
*/
593-
_decToHex: function(d, padding) {
594-
var hex = Number(d).toString(16).toUpperCase();
595-
padding = typeof (padding) === 'undefined' || padding === null ? padding = 2 : padding;
596-
597-
while (hex.length < padding) {
598-
hex = '0' + hex;
599-
}
600-
601-
return hex;
602-
}
603-
604575
};

0 commit comments

Comments
 (0)