merge web socket again :)
This commit is contained in:
commit
e5cb66c572
1 changed files with 32 additions and 8 deletions
|
|
@ -172,15 +172,25 @@ export default function DevicePage() {
|
||||||
// Streams all component changes for this device.
|
// Streams all component changes for this device.
|
||||||
// When the backend receives a new reading and updates a component,
|
// When the backend receives a new reading and updates a component,
|
||||||
// this fires and updates the component in state without a page refresh.
|
// this fires and updates the component in state without a page refresh.
|
||||||
useWebSocket<{ key: string; component: Component }>({
|
useWebSocket<{ key: string; component: Component } | null>({
|
||||||
path: `/live/devices/${deviceID}/components`,
|
path: `/live/devices/${deviceID}/components`,
|
||||||
parse: (e) => {
|
parse: (e) => {
|
||||||
|
try {
|
||||||
const raw = JSON.parse(e.data);
|
const raw = JSON.parse(e.data);
|
||||||
const comp = Component.any(raw);
|
const comp = Component.any(raw);
|
||||||
|
if (!comp?.settings) {
|
||||||
|
console.warn("[ws] received component without settings:", raw);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
return { key: comp.key(), component: comp };
|
return { key: comp.key(), component: comp };
|
||||||
|
} catch (err) {
|
||||||
|
console.warn("[ws] failed to parse component:", err);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
onMessage: ({ key, component }) => {
|
onMessage: (data) => {
|
||||||
// Functional update so we always work with the latest state
|
if (!data) return;
|
||||||
|
const { key, component } = data;
|
||||||
setComponents((prev) => {
|
setComponents((prev) => {
|
||||||
const updated = new Map(prev);
|
const updated = new Map(prev);
|
||||||
updated.set(key, component);
|
updated.set(key, component);
|
||||||
|
|
@ -193,10 +203,24 @@ export default function DevicePage() {
|
||||||
|
|
||||||
// --- Real-time device updates (optional) ---
|
// --- Real-time device updates (optional) ---
|
||||||
// Updates device-level info (name, status, lastActive, etc.)
|
// Updates device-level info (name, status, lastActive, etc.)
|
||||||
useWebSocket<Device>({
|
useWebSocket<Device | null>({
|
||||||
path: `/live/devices/${deviceID}`,
|
path: `/live/devices/${deviceID}`,
|
||||||
parse: (e) => Device.any(JSON.parse(e.data)),
|
parse: (e) => {
|
||||||
|
try {
|
||||||
|
const raw = JSON.parse(e.data);
|
||||||
|
const dev = Device.any(raw);
|
||||||
|
if (!dev?.settings) {
|
||||||
|
console.warn("[ws] received device without settings:", raw);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return dev;
|
||||||
|
} catch (err) {
|
||||||
|
console.warn("[ws] failed to parse device:", err);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
},
|
||||||
onMessage: (updatedDevice) => {
|
onMessage: (updatedDevice) => {
|
||||||
|
if (!updatedDevice) return;
|
||||||
setDevice(updatedDevice);
|
setDevice(updatedDevice);
|
||||||
},
|
},
|
||||||
token,
|
token,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue