-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
147 lines (131 loc) · 6.19 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const colorsys = require('colorsys');
const DreamscreenClient = require("dreamscreen-node").Client;
const client = new DreamscreenClient();
let Service, Characteristic, UUIDGen;
client.init();
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid;
homebridge.registerAccessory("homebridge-dreamscreen", "Dreamscreen", DreamscreenAccessory);
};
function DreamscreenAccessory(log, config) {
this.ipadress = config["ipadress"];
this.name = config["name"]
this.ambilightService = new Service.Switch("Ambilight mode");
this.ambilightService.subtype = "Amibilight mode"
this.lightService = new Service.Lightbulb("Brightness");
this.lightService.subtype = "Brightness";
this.infoService = new Service.AccessoryInformation();
this.log = log;
this.log(`Initialized ${this.name}`)
}
DreamscreenAccessory.prototype.setcolor = function() {
client.light(this.ipadress).setMode(3)
this.ambilightService.getCharacteristic(Characteristic.On).updateValue(0);
const color = colorsys.hsv_to_rgb({
h: global.hue,
s: global.saturation,
v: global.brightness
});
client.light(this.ipadress).setAmbientColor([color.r, color.g, color.b])
this.log(`Set ${this.name} ambientcolor: ${color.r} ${color.g} ${color.b}`);
}
DreamscreenAccessory.prototype.getcolor = function() {
ambientcolor = client.light(this.ipadress).getAmbientColor()
const hsv = colorsys.rgb_to_hsv({
r: ambientcolor[0],
g: ambientcolor[1],
b: ambientcolor[2]
});
global.hue = hsv.h
global.saturation = hsv.s
this.log(`Getting ${this.name} ambientcolor: ${hsv.h} ${hsv.s} ${hsv.v}`);
}
DreamscreenAccessory.prototype.getServices = function() {
let services = [];
this.lightService
.addCharacteristic(Characteristic.Brightness)
.on('get', callback => {
global.brightness = client.light("192.168.178.65").getBrightness()
this.log(`Getting ${this.name} current brightness: ${global.brightness} `)
this.lightService.getCharacteristic(Characteristic.Brightness).updateValue(global.brightness);
callback(null, global.brightness);
})
.on('set', (value, callback) => {
global.brightness = value
this.log(`Set ${this.name} brightness to: ${global.brightness}`)
client.light(this.ipadress).setBrightness(global.brightness)
if (global.brightness == 0) {
client.light(this.ipadress).setMode(0) //brightness = 0 so can be turned off dreamscreen anyway
this.ambilightService.getCharacteristic(Characteristic.On).updateValue(0);
} else if (global.saturation && global.hue && global.brightness !== 0) {
client.light(this.ipadress).setMode(3) //brightness is > 0 en kleur is niet wit dus ambient mode
}
callback();
})
this.lightService
.addCharacteristic(Characteristic.Hue)
.on('get', callback => {
this.getcolor();
this.lightService.getCharacteristic(Characteristic.Hue).updateValue(global.hue);
this.log(`Getting ${this.name} current ambientcolor hue: ${global.hue}`)
callback(null, global.hue);
})
.on('set', (value, callback) => {
global.hue = value
this.setcolor(this.ipadress);
this.log(`Set ${this.name} hue to: ${global.hue}`)
callback();
})
this.lightService
.addCharacteristic(Characteristic.Saturation)
.on('get', callback => {
this.getcolor(this.ipadress);
this.lightService.getCharacteristic(Characteristic.Saturation).updateValue(global.saturation);
this.log(`Getting ${this.name} current ambientcolor saturation: ${global.saturation}`)
callback(null, global.saturation);
})
.on('set', (value, callback) => {
global.saturation = value
this.log(`Set ${this.name} saturation to: ${global.saturation}`)
this.setcolor(this.ipadress);
callback();
})
this.ambilightService
.getCharacteristic(Characteristic.On)
.on('get', (callback) => {
this.mode = client.light(this.ipadress).getMode()
this.ambilightService
.getCharacteristic(Characteristic.On)
.updateValue(this.mode);
if (this.mode == 1) {
this.log(`Getting ${this.name} current mode: video`)
callback(null, 1);
} else if (this.mode !== 1) {
this.log(`Getting ${this.name} current mode: ${this.mode}`) //wip
callback(null, 0)
}
})
.on('set', (value, callback) => {
this.mode = Number(value)
client.light(this.ipadress).setMode(this.mode)
if (this.mode == 1) {
this.log(`Set ${this.name} mode to video mode`)
this.lightService.getCharacteristic(Characteristic.Hue).updateValue(0);
this.lightService.getCharacteristic(Characteristic.Saturation).updateValue(0);
} else {
this.log(`Set ${this.name} mode to sleep mode`)
}
callback();
})
services.push(this.lightService);
services.push(this.ambilightService);
services.push(this.infoService);
this.infoService
.setCharacteristic(Characteristic.Manufacturer, "DreamScreen")
.setCharacteristic(Characteristic.Model, "DreamScreen 4K")
.setCharacteristic(Characteristic.SerialNumber, this.ipadress)
.setCharacteristic(Characteristic.FirmwareRevision, "1.6.17");
return services;
};