98 lines
1.8 KiB
TypeScript
98 lines
1.8 KiB
TypeScript
import { Server } from "socket.io";
|
|
import { PrismaClient } from "./generated/prisma";
|
|
|
|
/**
|
|
* Server interfaces
|
|
*/
|
|
interface ServerToClientEvents {
|
|
}
|
|
|
|
interface ClientToServerEvents {
|
|
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 {
|
|
}
|
|
|
|
interface SocketData {
|
|
token: string;
|
|
}
|
|
|
|
/**
|
|
* Command interfaces
|
|
*/
|
|
interface CreateAlarmCommand {
|
|
time:string // HH:MM or HH:MM:SS
|
|
groupId:number
|
|
}
|
|
|
|
interface ListCommand {
|
|
groupId?:number
|
|
}
|
|
|
|
interface MakeGroupCommand {
|
|
name:string
|
|
}
|
|
|
|
interface RemoveCommand {
|
|
groupId:number
|
|
alarmId?:number
|
|
}
|
|
|
|
interface SnoozeCommand {
|
|
groupId:number
|
|
}
|
|
|
|
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<
|
|
ClientToServerEvents,
|
|
ServerToClientEvents,
|
|
InterServerEvents,
|
|
SocketData
|
|
>(3300);
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
io.on("connection", async (socket) => {
|
|
if (socket.data.token !== key) {
|
|
socket.disconnect();
|
|
return;
|
|
}
|
|
|
|
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); |