chore: move neopattern tests, add new pixelstream scripts
This commit is contained in:
52
test/neopattern/http-cluster-broadcast-color.js
Normal file
52
test/neopattern/http-cluster-broadcast-color.js
Normal file
@@ -0,0 +1,52 @@
|
||||
// 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: 80 };
|
||||
console.log('Broadcasting color:', payload);
|
||||
postClusterEvent('api/neopattern/color', payload);
|
||||
}, 5000);
|
||||
|
||||
|
||||
46
test/neopattern/ws-cluster-broadcast-color.js
Normal file
46
test/neopattern/ws-cluster-broadcast-color.js
Normal file
@@ -0,0 +1,46 @@
|
||||
// WebSocket client to broadcast neopattern color changes across the cluster
|
||||
// Usage: node ws-cluster-broadcast-color.js ws://<device-ip>/ws
|
||||
|
||||
const WebSocket = require('ws');
|
||||
|
||||
const url = process.argv[2] || 'ws://127.0.0.1/ws';
|
||||
const ws = new WebSocket(url);
|
||||
|
||||
const colors = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#FF00FF', '#00FFFF'];
|
||||
let idx = 0;
|
||||
|
||||
ws.on('open', () => {
|
||||
console.log('Connected to', url);
|
||||
// Broadcast color change every 5 seconds via cluster/broadcast
|
||||
setInterval(() => {
|
||||
const color = colors[idx % colors.length];
|
||||
idx++;
|
||||
const payload = { color, brightness: 80 };
|
||||
const envelope = {
|
||||
event: 'api/neopattern/color',
|
||||
data: payload // server will serialize object payloads
|
||||
};
|
||||
const msg = { event: 'cluster/broadcast', payload: envelope };
|
||||
ws.send(JSON.stringify(msg));
|
||||
console.log('Broadcasted color event', payload);
|
||||
}, 5000);
|
||||
});
|
||||
|
||||
ws.on('message', (data) => {
|
||||
try {
|
||||
const msg = JSON.parse(data.toString());
|
||||
console.log('Received:', msg);
|
||||
} catch (e) {
|
||||
console.log('Received raw:', data.toString());
|
||||
}
|
||||
});
|
||||
|
||||
ws.on('error', (err) => {
|
||||
console.error('WebSocket error:', err.message);
|
||||
});
|
||||
|
||||
ws.on('close', () => {
|
||||
console.log('WebSocket closed');
|
||||
});
|
||||
|
||||
|
||||
71
test/neopattern/ws-cluster-broadcast-rainbow.js
Normal file
71
test/neopattern/ws-cluster-broadcast-rainbow.js
Normal file
@@ -0,0 +1,71 @@
|
||||
// WebSocket client to broadcast smooth rainbow color changes across the cluster
|
||||
// Usage: node ws-cluster-broadcast-rainbow.js ws://<device-ip>/ws
|
||||
|
||||
const WebSocket = require('ws');
|
||||
|
||||
const url = process.argv[2] || 'ws://127.0.0.1/ws';
|
||||
const ws = new WebSocket(url);
|
||||
|
||||
function hsvToRgb(h, s, v) {
|
||||
const c = v * s;
|
||||
const x = c * (1 - Math.abs(((h / 60) % 2) - 1));
|
||||
const m = v - c;
|
||||
let r = 0, g = 0, b = 0;
|
||||
if (h < 60) { r = c; g = x; b = 0; }
|
||||
else if (h < 120) { r = x; g = c; b = 0; }
|
||||
else if (h < 180) { r = 0; g = c; b = x; }
|
||||
else if (h < 240) { r = 0; g = x; b = c; }
|
||||
else if (h < 300) { r = x; g = 0; b = c; }
|
||||
else { r = c; g = 0; b = x; }
|
||||
const R = Math.round((r + m) * 255);
|
||||
const G = Math.round((g + m) * 255);
|
||||
const B = Math.round((b + m) * 255);
|
||||
return { r: R, g: G, b: B };
|
||||
}
|
||||
|
||||
function toHex({ r, g, b }) {
|
||||
const h = (n) => n.toString(16).padStart(2, '0').toUpperCase();
|
||||
return `#${h(r)}${h(g)}${h(b)}`;
|
||||
}
|
||||
|
||||
let hue = 0;
|
||||
const SAT = 1.0; // full saturation
|
||||
const VAL = 1.0; // full value
|
||||
const BRIGHTNESS = 80;
|
||||
const UPDATE_RATE = 100; // ms
|
||||
|
||||
let timer = null;
|
||||
|
||||
ws.on('open', () => {
|
||||
console.log('Connected to', url);
|
||||
// UPDATE_RATE ms updates (10 Hz). Be aware this can saturate slow links.
|
||||
timer = setInterval(() => {
|
||||
const rgb = hsvToRgb(hue, SAT, VAL);
|
||||
const color = toHex(rgb);
|
||||
const envelope = {
|
||||
event: 'api/neopattern/color',
|
||||
data: { color, brightness: BRIGHTNESS }
|
||||
};
|
||||
const msg = { event: 'cluster/broadcast', payload: envelope };
|
||||
try {
|
||||
ws.send(JSON.stringify(msg));
|
||||
} catch (_) {}
|
||||
hue = (hue + 2) % 360; // advance hue (adjust for speed)
|
||||
}, UPDATE_RATE);
|
||||
});
|
||||
|
||||
ws.on('message', (data) => {
|
||||
// Optionally throttle logs: comment out for quieter output
|
||||
// console.log('WS:', data.toString());
|
||||
});
|
||||
|
||||
ws.on('error', (err) => {
|
||||
console.error('WebSocket error:', err.message);
|
||||
});
|
||||
|
||||
ws.on('close', () => {
|
||||
if (timer) clearInterval(timer);
|
||||
console.log('WebSocket closed');
|
||||
});
|
||||
|
||||
|
||||
48
test/neopattern/ws-color-client.js
Normal file
48
test/neopattern/ws-color-client.js
Normal file
@@ -0,0 +1,48 @@
|
||||
// Simple WebSocket client to test streaming API color changes
|
||||
// Usage: node ws-color-client.js ws://<device-ip>/ws
|
||||
|
||||
const WebSocket = require('ws');
|
||||
|
||||
const url = process.argv[2] || 'ws://127.0.0.1/ws';
|
||||
const ws = new WebSocket(url);
|
||||
|
||||
const colors = [
|
||||
'#FF0000', // red
|
||||
'#00FF00', // green
|
||||
'#0000FF', // blue
|
||||
'#FFFF00', // yellow
|
||||
'#FF00FF', // magenta
|
||||
'#00FFFF' // cyan
|
||||
];
|
||||
let idx = 0;
|
||||
|
||||
ws.on('open', () => {
|
||||
console.log('Connected to', url);
|
||||
// Send a message every 5 seconds to set solid color
|
||||
setInterval(() => {
|
||||
const color = colors[idx % colors.length];
|
||||
idx++;
|
||||
const payload = { color, brightness: 80 };
|
||||
// Send payload as an object (server supports string or object)
|
||||
const msg = { event: 'api/neopattern/color', payload };
|
||||
ws.send(JSON.stringify(msg));
|
||||
console.log('Sent color event', payload);
|
||||
}, 5000);
|
||||
});
|
||||
|
||||
ws.on('message', (data) => {
|
||||
try {
|
||||
const msg = JSON.parse(data.toString());
|
||||
console.log('Received:', msg);
|
||||
} catch (e) {
|
||||
console.log('Received raw:', data.toString());
|
||||
}
|
||||
});
|
||||
|
||||
ws.on('error', (err) => {
|
||||
console.error('WebSocket error:', err.message);
|
||||
});
|
||||
|
||||
ws.on('close', () => {
|
||||
console.log('WebSocket closed');
|
||||
});
|
||||
Reference in New Issue
Block a user