This repository has been archived by the owner on Dec 15, 2019. It is now read-only.
forked from EdJoPaTo/homebridge-fake-rgb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
102 lines (88 loc) · 2.61 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
var colorsys = require('colorsys');
var jspack = require('jspack').jspack;
var netcat = require('node-netcat');
var Service, Characteristic;
module.exports = function( homebridge ) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory( "homebridge-struct-rgb", "Struct-RGB", RgbAccessory );
};
function RgbAccessory( log, config ) {
this.log = log;
this.config = config;
this.name = config.name;
this.ip = config.ip;
this.port = config.port;
this.power = 0;
this.brightness = 100;
this.saturation = 0;
this.hue = 0;
this.log( "Initialized '" + this.name + " - " + this.ip + ":" + this.port + "'" );
}
RgbAccessory.prototype.setColor = function() {
var color = colorsys.hsv_to_rgb( {
h: this.hue,
s: this.saturation,
v: this.brightness
} );
if ( !this.power ) {
color.r = 0;
color.g = 0;
color.b = 0;
}
this.log( "set color to", color.r, color.g, color.b );
var data = jspack.Pack(">HHH", [color.r, color.g, color.b]);
var cl = netcat.client(this.port, this.ip); // Open connection to LED controller
cl.start();
cl.send(data, true); // 'true' closes connection after sending
this.log(data);
};
RgbAccessory.prototype.getServices = function() {
var lightbulbService = new Service.Lightbulb( this.name );
var bulb = this;
lightbulbService
.getCharacteristic( Characteristic.On )
.on( 'get', function( callback ) {
callback( null, bulb.power );
} )
.on( 'set', function( value, callback ) {
bulb.power = value;
bulb.log( "power to " + value );
bulb.setColor();
callback();
} );
lightbulbService
.addCharacteristic( Characteristic.Brightness )
.on( 'get', function( callback ) {
callback( null, bulb.brightness );
} )
.on( 'set', function( value, callback ) {
bulb.brightness = value;
bulb.log( "brightness to " + value );
bulb.setColor();
callback();
} );
lightbulbService
.addCharacteristic( Characteristic.Hue )
.on( 'get', function( callback ) {
callback( null, bulb.hue );
} )
.on( 'set', function( value, callback ) {
bulb.hue = value;
bulb.log( "hue to " + value );
bulb.setColor();
callback();
} );
lightbulbService
.addCharacteristic( Characteristic.Saturation )
.on( 'get', function( callback ) {
callback( null, bulb.saturation );
} )
.on( 'set', function( value, callback ) {
bulb.saturation = value;
bulb.log( "saturation to " + value );
bulb.setColor();
callback();
} );
return [ lightbulbService ];
};