Create decent server skeleton

This commit is contained in:
2025-10-22 15:20:13 -04:00
parent 390f637c48
commit 520c0a8280
4 changed files with 53 additions and 10 deletions

View File

@@ -1,10 +1,19 @@
import { Server } from "socket.io";
import { PrismaClient } from "./generated/prisma";
/**
* Server interfaces
*/
interface ServerToClientEvents {
}
interface ClientToServerEvents {
createAlarm: (cmd:CreateAlarmCommand) => void;
create: (cmd:CreateAlarmCommand) => void;
ls: (cmd:ListCommand) => void;
mkgrp: (cmd:MakeGroupCommand) => void;
rm: (cmd:RemoveCommand) => void;
snooze: (cmd:SnoozeCommand) => void;
shutoff: (cmd:ShutoffCommand) => void;
}
interface InterServerEvents {
@@ -14,8 +23,12 @@ interface SocketData {
token: string;
}
/**
* Command interfaces
*/
interface CreateAlarmCommand {
time:string
time:string // HH:MM or HH:MM:SS
groupId:number
}
interface ListCommand {
@@ -28,7 +41,7 @@ interface MakeGroupCommand {
interface RemoveCommand {
groupId:number
alarmId:number
alarmId?:number
}
interface SnoozeCommand {
@@ -39,24 +52,47 @@ interface ShutoffCommand {
groupId:number
}
// Start program
const key = process.env.SECRET_KEY;
if (!key) {
throw new Error("Secret key not defined");
}
const io = new Server<
ServerToClientEvents,
ClientToServerEvents,
ServerToClientEvents,
InterServerEvents,
SocketData
>(3300);
io.on("connection", (socket) => {
const prisma = new PrismaClient();
io.on("connection", async (socket) => {
if (socket.data.token !== key) {
socket.disconnect();
return;
}
socket.on("createAlarm", () => {
socket.on("create", async (cmd:CreateAlarmCommand) => {
});
});
socket.on("ls", async (cmd:ListCommand) => {
});
socket.on("mkgrp", async (cmd:MakeGroupCommand) => {
});
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);

View File

@@ -39,11 +39,12 @@ enum AlarmGroupState {
}
model Alarm {
id Int @id @default(autoincrement())
time String
nextRing DateTime
secondsSinceMidnight Int
nextRing DateTime
// AlarmGroup FK
AlarmGroupId Int
AlarmGroup AlarmGroup @relation(fields: [AlarmGroupId], references: [id])
@@id([secondsSinceMidnight, AlarmGroupId])
}