Files

98 lines
1.8 KiB
TypeScript
Raw Permalink Normal View History

2025-10-22 06:12:25 -04:00
import { Server } from "socket.io";
2025-10-22 15:20:13 -04:00
import { PrismaClient } from "./generated/prisma";
2025-10-22 15:20:13 -04:00
/**
* Server interfaces
*/
2025-10-22 15:01:08 -04:00
interface ServerToClientEvents {
}
2025-10-22 15:01:08 -04:00
interface ClientToServerEvents {
2025-10-22 15:20:13 -04:00
create: (cmd:CreateAlarmCommand) => void;
ls: (cmd:ListCommand) => void;
mkgrp: (cmd:MakeGroupCommand) => void;
rm: (cmd:RemoveCommand) => void;
snooze: (cmd:SnoozeCommand) => void;
shutoff: (cmd:ShutoffCommand) => void;
2025-10-22 06:12:25 -04:00
}
2025-10-22 15:01:08 -04:00
interface InterServerEvents {
}
2025-10-22 06:12:25 -04:00
2025-10-22 15:01:08 -04:00
interface SocketData {
token: string;
}
2025-10-22 06:12:25 -04:00
2025-10-22 15:20:13 -04:00
/**
* Command interfaces
*/
2025-10-22 15:01:08 -04:00
interface CreateAlarmCommand {
2025-10-22 15:20:13 -04:00
time:string // HH:MM or HH:MM:SS
groupId:number
2025-10-22 15:01:08 -04:00
}
2025-10-22 06:12:25 -04:00
2025-10-22 15:01:08 -04:00
interface ListCommand {
groupId?:number
}
2025-10-22 06:12:25 -04:00
2025-10-22 15:01:08 -04:00
interface MakeGroupCommand {
name:string
}
2025-10-22 06:12:25 -04:00
2025-10-22 15:01:08 -04:00
interface RemoveCommand {
groupId:number
2025-10-22 15:20:13 -04:00
alarmId?:number
2025-10-22 15:01:08 -04:00
}
2025-10-22 06:12:25 -04:00
2025-10-22 15:01:08 -04:00
interface SnoozeCommand {
groupId:number
}
interface ShutoffCommand {
groupId:number
}
2025-10-22 06:12:25 -04:00
2025-10-22 15:20:13 -04:00
// Start program
2025-10-22 15:01:08 -04:00
const key = process.env.SECRET_KEY;
if (!key) {
throw new Error("Secret key not defined");
}
const io = new Server<
ClientToServerEvents,
2025-10-22 15:20:13 -04:00
ServerToClientEvents,
2025-10-22 15:01:08 -04:00
InterServerEvents,
SocketData
>(3300);
2025-10-22 15:20:13 -04:00
const prisma = new PrismaClient();
io.on("connection", async (socket) => {
2025-10-22 15:01:08 -04:00
if (socket.data.token !== key) {
2025-10-22 15:20:13 -04:00
socket.disconnect();
2025-10-22 15:01:08 -04:00
return;
}
2025-10-22 15:20:13 -04:00
socket.on("create", async (cmd:CreateAlarmCommand) => {
});
socket.on("ls", async (cmd:ListCommand) => {
});
socket.on("mkgrp", async (cmd:MakeGroupCommand) => {
2025-10-22 15:01:08 -04:00
});
2025-10-22 15:20:13 -04:00
socket.on("rm", async (cmd:RemoveCommand) => {
});
socket.on("snooze", async (cmd:SnoozeCommand) => {
});
socket.on("shutoff", async (cmd:ShutoffCommand) => {
});
});
setInterval(async () => {
// Process alarms constantly to announce alarm state to "groupstate-alert" on a change or when the client reports a differing state
}, 1);