-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
359 lines (315 loc) · 13.4 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
const express = require('express');
const mdns = require('mdns-js');
const axios = require('axios');
const path = require('path');
const fs = require('fs').promises;
const app = express();
app.use(express.json()); // Add this line to parse JSON request bodies
const port = 3000;
// Create directory for sync settings if it doesn't exist
fs.mkdir(path.join(__dirname, 'sync-settings')).catch(() => {});
// Serve static files from public directory
app.use(express.static('public'));
// Store discovered devices
let discoveredDevices = new Map();
// Create mDNS browser
const browser = mdns.createBrowser(mdns.tcp('http'));
// Handle device discovery
browser.on('ready', function () {
browser.discover();
});
browser.on('update', async function (data) {
try {
const ip = data.addresses[0];
if (!ip || discoveredDevices.has(ip)) return;
// Try to verify if it's a WLED device by calling its API
try {
const response = await axios.get(`http://${ip}/json/info`, { timeout: 2000 });
if (response.data && response.data.ver) {
discoveredDevices.set(ip, {
name: data.hostname || 'Unknown',
ip: ip,
version: response.data.ver
});
}
} catch (error) {
// Not a WLED device or not responding
console.log(`Device at ${ip} is not a WLED device`);
}
} catch (error) {
console.error('Error processing device:', error);
}
});
// Parse the JavaScript response to extract form values
function parseWLEDSyncSettings(jsCode) {
const settings = {};
// Extract values using regex
const extractValue = (key) => {
const match = jsCode.match(new RegExp(`d\\.Sf\\.${key}\\.value=(\\d+|"[^"]*")`));
return match ? match[1].replace(/"/g, '') : null;
};
const extractChecked = (key) => {
const match = jsCode.match(new RegExp(`d\\.Sf\\.${key}\\.checked=(\\d)`));
return match ? match[1] === '1' : null;
};
// UDP Settings
settings.udp = {
UDPPort: extractValue('UP'),
secondaryPort: extractValue('U2'),
sendGroup: extractValue('GS'), //GS is the sum of binary powers example: If boxes 1, 4, 5 are ticked, GS will be 2⁰ + 2³ + 2⁴ = 25 . G1-G8 are the ticked boxes
receiveGroup: extractValue('GR'), //GR follows same logic R1-R8
sendGroup1: extractChecked('G1'),
sendGroup2: extractChecked('G2'),
sendGroup3: extractChecked('G3'),
sendGroup4: extractChecked('G4'),
sendGroup5: extractChecked('G5'),
sendGroup6: extractChecked('G6'),
sendGroup7: extractChecked('G7'),
sendGroup8: extractChecked('G8'),
receiveGroup1: extractChecked('R1'),
receiveGroup2: extractChecked('R2'),
receiveGroup3: extractChecked('R3'),
receiveGroup4: extractChecked('R4'),
receiveGroup5: extractChecked('R5'),
receiveGroup6: extractChecked('R6'),
receiveGroup7: extractChecked('R7'),
receiveGroup8: extractChecked('R8')
};
// Sync Options
settings.sync = {
receiveBrightness: extractChecked('RB'),
receiveColor: extractChecked('RC'),
receiveEffects: extractChecked('RX'),
receiveSegmentOptions: extractChecked('SO'),
notifyDirect: extractChecked('SG'),
notifyButton: extractChecked('SD'),
notifyAlexa: extractChecked('SB'),
notifyHue: extractChecked('SH'),
notifyMacro: extractChecked('SM'),
udpRetransmit: extractValue('UR')
};
// Instance Settings
settings.instance = {
enableList: extractChecked('NL'),
discoverable: extractChecked('NB')
};
// Realtime Settings
settings.realtime = {
receiveUDP: extractChecked('RD'),
useMainSegment: extractChecked('MO'),
dmxStartAddress: extractValue('DA'),
dmxMode: extractValue('DM'),
dmxTimeout: extractValue('ET'),
dmxSegmentSpacing: extractValue('XX'),
e131portPriority: extractValue('PY'),
e131Multicast: extractChecked('ES'),
e131SkipOutOfSequence: extractChecked('EM'),
startUniverse: extractValue('EU'),
forceBrightness: extractChecked('FB'),
disableGammaCorrection: extractChecked('RG'),
ledOffset: extractValue('WO')
};
// MQTT Settings
settings.mqtt = {
enabled: extractChecked('MQ'),
broker: extractValue('MS'),
port: extractValue('MQPORT'),
username: extractValue('MQUSER'),
password: extractValue('MQPASS'),
clientId: extractValue('MQCID'),
deviceTopic: extractValue('MD'),
groupTopic: extractValue('MG'),
buttonPublish: extractChecked('BM')
};
// Hue Settings
settings.hue = {
pollEnabled: extractChecked('HP'),
onOff: extractChecked('HO'),
brightness: extractChecked('HB'),
color: extractChecked('HC'),
pollHueLight: extractValue('HL'),
pollInterval: extractValue('HI'),
ip: `${extractValue('H0')}.${extractValue('H1')}.${extractValue('H2')}.${extractValue('H3')}`
};
// Additional Settings
settings.additional = {
g1: extractChecked('G1'),
r1: extractChecked('R1'),
di: extractValue('DI'), //In custom port mode this is 0. In E1.31 mode this is set to 5568. In artnet mode this is set to 6454
ai: extractValue('AI'), //Alexa invocation name
ap: extractValue('AP'), //"Also emulate devices to call the first x presets"
bd: extractValue('BD') //Baud rate
ep: extractValue('EP'), //In custom port mode this is the number used. In E1.31 mode this is set to 5568. In artnet mode this is set to 6454
};
return settings;
}
// API endpoint to get discovered devices
app.get('/api/devices', (req, res) => {
res.json(Array.from(discoveredDevices.values()));
});
// API endpoint to fetch and parse sync settings from a specific device
app.get('/api/sync-settings/:ip', async (req, res) => {
try {
const ip = req.params.ip;
const response = await axios.get(`http://${ip}/settings/s.js?p=4`, { timeout: 5000 });
// Parse the JavaScript response
const settings = parseWLEDSyncSettings(response.data);
// Save to a JSON file
const filename = `wled-sync-${ip.replace(/\./g, '-')}.json`;
await fs.writeFile(
path.join(__dirname, 'sync-settings', filename),
JSON.stringify(settings, null, 2)
);
res.json({
settings: settings,
savedTo: filename
});
} catch (error) {
res.status(500).json({
error: 'Failed to fetch sync settings',
message: error.message
});
}
});
// Serve the main page
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// Start server
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
// Add this helper function to convert JSON settings to form data
function convertSettingsToFormData(settings) {
const params = new URLSearchParams();
// Helper function to append only if the value exists
const appendIfExists = (key, value) => {
if (value !== null && value !== undefined) {
params.append(key, value === true ? 'on' : value === false ? 'off' : value);
}
};
// UDP Settings
appendIfExists('UP', settings.udp.primaryPort);
appendIfExists('U2', settings.udp.secondaryPort);
appendIfExists('GS', settings.udp.sendGroup);
appendIfExists('GR', settings.udp.receiveGroup);
appendIfExists('G1', 'on'); // These seem to be always on in the example
appendIfExists('R1', 'on'); // These seem to be always on in the example
// Sync Options
appendIfExists('RB', settings.sync.receiveBrightness);
appendIfExists('RC', settings.sync.receiveColor);
appendIfExists('RX', settings.sync.receiveEffects);
appendIfExists('SO', settings.sync.receiveSegmentOptions);
appendIfExists('SG', settings.sync.notifyDirect);
appendIfExists('SD', settings.sync.notifyButton);
appendIfExists('SB', settings.sync.notifyAlexa);
appendIfExists('SH', settings.sync.notifyHue);
appendIfExists('SM', settings.sync.notifyMacro);
appendIfExists('UR', settings.sync.udpRetransmit);
// Instance Settings
appendIfExists('NL', settings.instance.enableList);
appendIfExists('NB', settings.instance.discoverable);
// Realtime Settings
appendIfExists('RD', settings.realtime.receiveUDP);
appendIfExists('MO', settings.realtime.useMainSegment);
appendIfExists('DI', settings.realtime.e131Port); // This seems to be a duplicate of EP
appendIfExists('EP', settings.realtime.e131Port);
appendIfExists('EM', settings.realtime.e131SkipOutOfSequence);
appendIfExists('EU', settings.realtime.e131Universe);
appendIfExists('ES', settings.realtime.e131Multicast);
appendIfExists('DA', settings.realtime.dmxAddress);
appendIfExists('XX', settings.realtime.dmxSegmentSpacing); // Not sure what this is, but it's in the example
appendIfExists('PY', '0'); // Not sure what this is, but it's in the example
appendIfExists('DM', settings.realtime.dmxMode);
appendIfExists('ET', settings.realtime.dmxTimeout);
appendIfExists('FB', settings.realtime.forceBrightness);
appendIfExists('RG', settings.realtime.disableGammaCorrection);
appendIfExists('WO', settings.realtime.ledOffset);
// Additional settings
appendIfExists('AI', ''); // Not sure what this is, but it's in the example
appendIfExists('AP', '0'); // Not sure what this is, but it's in the example
// MQTT Settings
appendIfExists('MQ', settings.mqtt.enabled);
appendIfExists('MS', settings.mqtt.broker);
appendIfExists('MQPORT', settings.mqtt.port);
appendIfExists('MQUSER', settings.mqtt.username);
appendIfExists('MQPASS', settings.mqtt.password); // We probably don't want to send the password
appendIfExists('MQCID', settings.mqtt.clientId);
appendIfExists('MD', settings.mqtt.deviceTopic);
appendIfExists('MG', settings.mqtt.groupTopic);
appendIfExists('BM', settings.mqtt.buttonPublish);
// Hue Settings
appendIfExists('HL', '2'); // Not sure what this is, but it's in the example
appendIfExists('HI', settings.hue.pollInterval);
appendIfExists('HP', settings.hue.pollEnabled);
appendIfExists('HO', settings.hue.onOff);
appendIfExists('HB', settings.hue.brightness);
appendIfExists('HC', settings.hue.color);
if (settings.hue && settings.hue.ip) {
const [h0, h1, h2, h3] = settings.hue.ip.split('.');
appendIfExists('H0', h0);
appendIfExists('H1', h1);
appendIfExists('H2', h2);
appendIfExists('H3', h3);
}
// Additional setting at the end
appendIfExists('BD', '10000'); // Not sure what this is, but it's in the example
return params.toString();
}
// Get list of available presets
app.get('/api/presets', async (req, res) => {
try {
const files = await fs.readdir(path.join(__dirname, 'presets'));
const presets = files.filter(file => file.endsWith('.json'));
res.json(presets);
} catch (error) {
res.status(500).json({ error: 'Failed to read presets directory' });
}
});
app.post('/api/apply-preset/:ip', async (req, res) => {
try {
const { ip } = req.params;
const { preset } = req.body;
console.log(`Applying preset ${preset} to device ${ip}`);
// Read the preset file and convert settings to form data
const presetPath = path.join(__dirname, 'presets', preset);
const presetData = JSON.parse(await fs.readFile(presetPath, 'utf8'));
const formData = convertSettingsToFormData(presetData);
console.log('Form data to be sent:', formData);
try {
const response = await axios.post(`http://${ip}/settings/sync`, formData, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
maxRedirects: 0,
validateStatus: function (status) {
return status >= 200 && status < 300;
},
});
console.log('Preset applied successfully');
res.json({ success: true, message: 'Preset applied successfully' });
} catch (error) {
if (error.message.includes('Invalid character in chunk size')) {
// Ignore this specific error and treat the request as successful
console.log('Ignored "Invalid character in chunk size" error. Preset likely applied successfully.');
res.json({ success: true, message: 'Preset likely applied successfully (ignored parsing error)' });
} else if (error.response) {
// Handle other response errors
console.error('Error response:', error.response.status, error.response.data);
res.status(error.response.status).json({
error: 'Failed to apply preset',
message: error.response.data
});
} else {
// Handle network errors or other issues
throw error;
}
}
} catch (error) {
console.error('Error applying preset:', error);
res.status(500).json({
error: 'Failed to apply preset',
message: error.message,
});
}
});