ApiServer: add AsyncWebSocket at /ws; accept JSON {event, payload} (string or object) and dispatch via ctx.fire; mirror all local events to clients using NodeContext::onAny.\nNodeContext: add onAny subscriber API.\nNeoPatternService: add api/neopattern/color event to set solid color.\nCluster: centralize cluster/broadcast sending in core; services delegate.\nAPI: add generic /api/node/event and /api/cluster/event endpoints in respective services.\nTests: add ws-color-client, ws-cluster-broadcast-color, http-cluster-broadcast-color.\nDocs: add StreamingAPI.md; update README and test/README.\nFixes: robust WS JSON parsing on ESP8266 and payload handling.
53 lines
1.3 KiB
JavaScript
53 lines
1.3 KiB
JavaScript
// Simple HTTP client to broadcast a neopattern color change to the cluster
|
|
// Usage: node cluster-broadcast-color.js 10.0.1.53
|
|
|
|
const http = require('http');
|
|
|
|
const host = process.argv[2] || '127.0.0.1';
|
|
const port = 80;
|
|
|
|
const colors = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#FF00FF', '#00FFFF'];
|
|
let idx = 0;
|
|
|
|
function postClusterEvent(event, payloadObj) {
|
|
const payload = encodeURIComponent(JSON.stringify(payloadObj));
|
|
const body = `event=${encodeURIComponent(event)}&payload=${payload}`;
|
|
|
|
const options = {
|
|
host,
|
|
port,
|
|
path: '/api/cluster/event',
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
'Content-Length': Buffer.byteLength(body)
|
|
}
|
|
};
|
|
|
|
const req = http.request(options, (res) => {
|
|
let data = '';
|
|
res.on('data', (chunk) => (data += chunk));
|
|
res.on('end', () => {
|
|
console.log('Response:', res.statusCode, data);
|
|
});
|
|
});
|
|
|
|
req.on('error', (err) => {
|
|
console.error('Request error:', err.message);
|
|
});
|
|
|
|
req.write(body);
|
|
req.end();
|
|
}
|
|
|
|
console.log(`Broadcasting color changes to http://${host}/api/cluster/event ...`);
|
|
setInterval(() => {
|
|
const color = colors[idx % colors.length];
|
|
idx++;
|
|
const payload = { color, brightness: 128 };
|
|
console.log('Broadcasting color:', payload);
|
|
postClusterEvent('api/neopattern/color', payload);
|
|
}, 5000);
|
|
|
|
|