-
Notifications
You must be signed in to change notification settings - Fork 2
/
GoveeDirectConnect.js
301 lines (245 loc) · 8.91 KB
/
GoveeDirectConnect.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import udp from "@SignalRGB/udp";
import goveeProducts from "./govee-products.test.js";
import GoveeDevice from "./GoveeDevice.test.js";
import GoveeController from "./GoveeController.test.js";
import GoveeDeviceUI from "./GoveeDeviceUI.test.js";
export function Name() { return "Govee Direct Connect"; }
export function Version() { return "2.0.0"; }
export function Type() { return "network"; }
export function Publisher() { return "RickOfficial"; }
export function Size() { return [1, 1]; }
export function DefaultPosition() {return [0, 70]; }
export function DefaultScale(){return 1.0;}
export function DefaultComponentBrand() { return "Govee";}
export function ControllableParameters()
{
return [
{"property":"lightingMode", "group":"lighting", "label":"Lighting Mode", "type":"combobox", "values":["Canvas", "Forced"], "default":"Canvas"},
{"property":"forcedColor", "group":"lighting", "label":"Forced Color", "min":"0", "max":"360", "type":"color", "default":"#009bde"},
{"property":"turnOff", "group":"lighting", "label":"On shutdown", "type":"combobox", "values":["Do nothing", "Single color", "Turn device off"], "default":"Turn device off"},
{"property":"shutDownColor", "group":"lighting", "label":"Shutdown Color", "min":"0", "max":"360", "type":"color", "default":"#8000FF"},
{"property":"frameDelay", "group":"settings", "label":"Delay between frames", "type":"combobox", "values":["0", "10", "50", "100"], "default":"0"}
];
}
export function SubdeviceController() { return false; }
let goveeUI;
let lastRender = 0;
export function Initialize()
{
device.log('Creating Govee Device UI');
goveeUI = new GoveeDeviceUI(device, controller);
}
export function Render()
{
let now = Date.now();
goveeUI.render(lightingMode, forcedColor, now, frameDelay);
}
export function Shutdown()
{
device.log('Shutting down');
goveeUI.shutDown(turnOff, shutDownColor);
}
export function Validate()
{
return true;
}
export function DiscoveryService()
{
this.IconUrl = getGoveeLogo();
this.lastPollTime = -5000;
this.PollInterval = 5000;
this.lastPort = null;
// Disabled so we don't use the built in broadcasting
// this.UdpBroadcastPort = 4003;
// this.UdpListenPort = 4002;
this.discoveredDeviceData = {};
this.GoveeDeviceControllers = {};
this.Initialize = function() {
this.lastPort = service.getSetting('ipCache', 'lastUniquePort');
if (!this.lastPort) this.getUniquePort();
this.convertSettings();
this.lastPollTime = Date.now();
this.devicesLoaded = false;
this.startSocketServer();
}
this.startSocketServer = function()
{
// Start the udp server
this.udpServer = udp.createSocket();
this.udpServer.on('message', this.handleSocketMessage.bind(this));
this.udpServer.on('error', this.handleSocketError.bind(this));
this.udpServer.bind(4002);
}
this.convertSettings = function()
{
let oldSettingsData = service.getSetting('GoveeDirectConnect', 'devices');
let newSettingsData = service.getSetting('ipCache', 'cache');
if(oldSettingsData !== undefined && !newSettingsData)
{
service.log('Found old settings');
let oldSettings = JSON.parse(oldSettingsData);
for(let ip of Object.keys(oldSettings))
{
// We found the ip, let's find the device id
let deviceData = oldSettings[ip];
deviceData.id = deviceData.device;
//Create a new Govee device so we can save the data
let goveeDevice = new GoveeDevice(deviceData);
goveeDevice.save();
}
this.saveCache();
// service.removeSetting('GoveeDirectConnect', 'devices');
}
}
this.forceDiscover = function(ip, leds, type, split)
{
let goveeLightData = {
ip: ip,
leds: parseInt(leds),
type: parseInt(type),
split: split,
uniquePort: this.getUniquePort()
};
this.GoveeDeviceControllers[ip] = this.createController(goveeLightData);
this.saveCache();
this.Update(true);
}
this.loadForcedDevices = function()
{
// Load the cached ips
let ipCacheJSON = service.getSetting('ipCache', 'cache');
let ipCache = {};
if (ipCacheJSON) ipCache = JSON.parse(ipCacheJSON);
// Get all cached ips
let cachedIps = Object.keys(ipCache);
for(let cachedIp of cachedIps)
{
// If Controller is not yet created
if (!this.GoveeDeviceControllers.hasOwnProperty(cachedIp))
{
// Create the controller and add it
this.GoveeDeviceControllers[cachedIp] = this.createController(ipCache[cachedIp]);
}
let goveeController = this.GoveeDeviceControllers[cachedIp];
if (!service.hasController(cachedIp))
{
service.addController(goveeController);
// Announce the controller as a device
service.announceController(goveeController);
} else
{
service.updateController(goveeController);
}
}
this.devicesLoaded = true;
}
this.Update = function(force)
{
let diff = Date.now() - discovery.lastPollTime;
if(diff > discovery.PollInterval || force === true)
{
discovery.lastPollTime = Date.now();
if (!this.devicesLoaded || force === true)
{
this.loadForcedDevices();
}
}
}
this.handleSocketError = function(err, message)
{
service.log(message);
}
this.handleSocketMessage = function(value)
{
if (!value) return;
const ip = this.getIPv4(value.address);
if (this.GoveeDeviceControllers.hasOwnProperty(ip))
{
let goveeController = this.GoveeDeviceControllers[ip];
goveeController.relaySocketMessage(value);
} else
{
service.log(`Cannot find controller for ${ip}`);
}
};
this.getIPv4 = function(address)
{
const ipv4Pattern = /(\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)\.(\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)\.(\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)\.(\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)/;
const match = address.match(ipv4Pattern);
return match ? match[0] : null;
}
this.Delete = function(ip)
{
service.log('Deleting controller ' + ip);
this.removeController(ip);
this.saveCache();
this.Update(true);
}
this.saveCache = function()
{
let ipCache = {};
for(let ip of Object.keys(this.GoveeDeviceControllers))
{
let goveeController = this.GoveeDeviceControllers[ip];
ipCache[goveeController.id] = goveeController.toCacheJSON();
}
service.saveSetting('ipCache', 'cache', JSON.stringify(ipCache));
}
this.removeController = function(ip)
{
let goveeController = this.GoveeDeviceControllers[ip];
service.removeController(goveeController);
delete this.GoveeDeviceControllers[ip];
}
this.getUniquePort = function()
{
if (!this.lastPort || this.lastPort < 46920)
{
this.lastPort = 46920;
} else
{
this.lastPort++;
}
// Save the new port:
service.saveSetting('ipCache', 'lastUniquePort', this.lastPort);
return this.lastPort;
}
this.createController = function(cacheData)
{
service.log('Creating controller: ' + cacheData.ip);
let goveeDevice;
if (cacheData.id)
{
goveeDevice = (new GoveeDevice).load(cacheData.id);
// Add this for devices with the old settings
if (!goveeDevice.uniquePort)
{
goveeDevice.uniquePort = this.getUniquePort();
goveeDevice.save();
}
} else
{
goveeDevice = new GoveeDevice(cacheData);
}
// Create and store controller for network tab
let goveeController = new GoveeController(goveeDevice, this);
// Start the udp socket?
goveeController.setupUDPSocket();
return goveeController;
}
this.updatedController = function(goveeController)
{
service.log(`Controller ${goveeController.id} data updated`);
this.saveCache();
service.removeController(goveeController);
service.addController(goveeController);
service.log('Re-announcing the controller');
service.announceController(goveeController);
service.log('Restart our socket server');
this.startSocketServer();
}
}
function getGoveeLogo()
{
return 'data:image/png;base64,' + goveeProducts['default'].base64Image;
}