Skip to content

Commit

Permalink
fixed switch list sync, new device/svg events
Browse files Browse the repository at this point in the history
persist lights view
single map of switches in lights ui
  • Loading branch information
xyven1 committed Nov 25, 2023
1 parent 5f23424 commit 5fa1ccf
Show file tree
Hide file tree
Showing 4 changed files with 300 additions and 147 deletions.
5 changes: 3 additions & 2 deletions lib/types/socket.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Irrigation from "./irrigationConfig.js";
import { type Branded } from "./brand.js";
import * as Irrigation from "./irrigationConfig.js";

export type SerialNumber = Branded<string, "SerialNumber">;

Expand All @@ -22,7 +22,6 @@ export type Region = {
title: string;
sn: SerialNumber;
stroke?: number;
sw?: Switch;
};

export type SvgLayer = {
Expand Down Expand Up @@ -118,6 +117,8 @@ export type ServerToClientEvents = {
// for lights
stateChange: (sn: SerialNumber, state: number) => void;
brightnessChange: (sn: SerialNumber, brightness: number) => void;
newDevice: (newDevice: Switch) => void;
newSvg: (svg: Svg) => void;
// for irrigation
irrigationConfigChange: (config: Irrigation.Config) => void;
irrigationStateChange: (state: Irrigation.State) => void;
Expand Down
24 changes: 15 additions & 9 deletions server/src/manageLights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ export default (io: AppServer): void => {
function manageClient(deviceInfo: DeviceInfo): void {
const client = wemo.client(deviceInfo);
devices.set(client.device.serialNumber, client);
io.emit("newDevice", {
name: client.device.friendlyName,
serialNumber: client.device.serialNumber,
state: client.device.binaryState,
brightness: client.device.brightness,
});

client.on("error", (err) => {
console.error("Client error: %s", err);
Expand Down Expand Up @@ -120,16 +126,16 @@ export default (io: AppServer): void => {
setInterval(discover, 10000);

io.on("connection", (socket) => {
// returns switches from devices object to a callback function
socket.on("getSwitches", (callback = () => {}) => {
console.log("getting switches");
callback(
[...devices.values()].map((device) => ({
name: device.device.friendlyName,
serialNumber: device.device.serialNumber,
state: device.device.binaryState,
brightness: device.device.brightness,
}))
[...devices.values()].map((device) => {
return {
name: device.device.friendlyName,
serialNumber: device.device.serialNumber,
state: -1,
brightness: undefined,
};
})
);
});

Expand Down Expand Up @@ -174,7 +180,6 @@ export default (io: AppServer): void => {
// allows client to toggle switches using serial number
socket.on("toggleSwitch", async (serialNumber, wsCallback = () => {}) => {
console.log("Toggling switch with serial number: " + serialNumber);
// io.emit("stateChange", serialNumber, 2);
const device = devices.get(serialNumber);
if (device !== undefined) {
try {
Expand Down Expand Up @@ -311,6 +316,7 @@ export default (io: AppServer): void => {
region.sn = data.sn;
fs.writeFileSync(svgPath, JSON.stringify(svg, null, 2));
wsCallback({ ok: true });
socket.broadcast.emit("newSvg", svg);
} catch (err: any) {
console.error(err);
if (err instanceof Error)
Expand Down
6 changes: 6 additions & 0 deletions ui/src/store/lights.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { defineStore } from "pinia";
import { ref } from "vue";

export enum View {
List,
FloorPlan,
}

export const useLightsStore = defineStore(
"lights",
() => {
return {
layer: ref(0),
view: ref(View.FloorPlan),
};
},
{
Expand Down
Loading

0 comments on commit 5fa1ccf

Please sign in to comment.