Skip to content

Commit

Permalink
API cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
toresbe committed Jul 6, 2023
1 parent 39e2e41 commit 5cdf1be
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 54 deletions.
50 changes: 0 additions & 50 deletions src/api.ts

This file was deleted.

58 changes: 58 additions & 0 deletions src/api/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { WebSocket, WebSocketServer } from "ws";
import { log } from "../log.js";
import { timeline } from "../scheduling/Timeline.js";
import { getRemoteAddress } from "./getRemoteAddress.js";
import type { IncomingMessage } from "http";

const UPDATE_INTERVAL_MS = 200 as const;
let updateTimer: NodeJS.Timer | null = null;
let wsServer: WebSocketServer | null = null;

const sendUpdate = () => {
if (!wsServer) {
log.warn(`sendUpdate called with no wsServer - shouldn't happen`);
if (updateTimer) {
log.warn(`update timer still running - shouldn't happen - clearing it`);
clearInterval(updateTimer);
}
return;
}

if (!wsServer.clients.size) {
if (updateTimer) {
log.debug(`No websocket clients connected, clearing update timer`);
clearInterval(updateTimer);
}
return;
}

const now = new Date();
const message = JSON.stringify({
time: now.toISOString(),
timeline: timeline.getItems(),
});

wsServer.clients.forEach((sock) => sock.send(message));
};
const handleConnection = (sock: WebSocket, incoming: IncomingMessage) => {
log.info(`Websocket client connected from ${getRemoteAddress(incoming)}`);

sendUpdate();
if (!updateTimer) updateTimer = setInterval(sendUpdate, UPDATE_INTERVAL_MS);

sock.on("error", log.error);

sock.on("close", (code, reason) => {
log.info(`Websocket client disconnected (${code}, reason: "${reason}")`);
});

sock.on("message", (data: any) => {
log.warn(`Received message ${data} on read-only socket`);
});
};

export const startWebsocketServer = (port: number) => {
log.info(`Starting websocket server on port ${port}`);
wsServer = new WebSocketServer({ port: port });
wsServer.on("connection", handleConnection);
};
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { connection } from "./connection.js";
import { endOfDay, startOfDay } from "date-fns";
import { timeline } from "./scheduling/Timeline.js";
import { makeTestSchedule } from "./scheduling/testUtils.js";
import { startWebsocketServer } from "./api.js";
import { startWebsocketServer } from "./api/server.js";

OpenAPI.BASE = FK_API;

Expand Down Expand Up @@ -60,7 +60,7 @@ const runPlayout = async () => {
(async () => {
try {
log.info(`Starting playout at ${new Date().toLocaleString()}`);
await startWebsocketServer();
await startWebsocketServer(8080);
await runPlayout();
} catch (e) {
log.error(e);
Expand Down
4 changes: 2 additions & 2 deletions src/scheduling/InterstitialGraphics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ export class InterstitialGraphics implements ScheduleItem {
async arm() {
const now = new Date();
const { startsAt, endsAt, load, play, stop, clear } = this;
const loadAt = sub(startsAt, { seconds: 1 });
const loadAt = sub(startsAt, { seconds: 10 });
const playAt = subMilliseconds(startsAt, 500);
const clearAt = add(endsAt, { seconds: 2 });
const clearAt = add(endsAt, { seconds: 10 });

if (endsAt <= now) {
log.debug(
Expand Down

0 comments on commit 5cdf1be

Please sign in to comment.