This commit is contained in:
hzrd149
2025-02-06 16:11:00 -06:00
parent c3216e43a9
commit 9efd5019e3
66 changed files with 3759 additions and 3201 deletions

View File

@@ -1,49 +1,49 @@
import { WebSocket } from 'ws';
import { ConfigMessage, ConfigResponse } from '@satellite-earth/core/types/control-api/config.js';
import { WebSocket } from "ws";
import { ConfigMessage, ConfigResponse } from "@satellite-earth/core/types/control-api/config.js";
import type App from '../../app/index.js';
import { type ControlMessageHandler } from './control-api.js';
import type App from "../../app/index.js";
import { type ControlMessageHandler } from "./control-api.js";
/** handles ['CONTROL', 'CONFIG', ...] messages */
export default class ConfigActions implements ControlMessageHandler {
app: App;
name = 'CONFIG';
app: App;
name = "CONFIG";
private subscribed = new Set<WebSocket | NodeJS.Process>();
private subscribed = new Set<WebSocket | NodeJS.Process>();
constructor(app: App) {
this.app = app;
constructor(app: App) {
this.app = app;
// when config changes send it to the subscribed sockets
this.app.config.on('changed', (config) => {
for (const sock of this.subscribed) {
this.send(sock, ['CONTROL', 'CONFIG', 'CHANGED', config]);
}
});
}
// when config changes send it to the subscribed sockets
this.app.config.on("changed", (config) => {
for (const sock of this.subscribed) {
this.send(sock, ["CONTROL", "CONFIG", "CHANGED", config]);
}
});
}
handleMessage(sock: WebSocket | NodeJS.Process, message: ConfigMessage) {
const method = message[2];
switch (method) {
case 'SUBSCRIBE':
this.subscribed.add(sock);
sock.once('close', () => this.subscribed.delete(sock));
this.send(sock, ['CONTROL', 'CONFIG', 'CHANGED', this.app.config.data]);
return true;
handleMessage(sock: WebSocket | NodeJS.Process, message: ConfigMessage) {
const method = message[2];
switch (method) {
case "SUBSCRIBE":
this.subscribed.add(sock);
sock.once("close", () => this.subscribed.delete(sock));
this.send(sock, ["CONTROL", "CONFIG", "CHANGED", this.app.config.data]);
return true;
case 'SET':
const field = message[3];
const value = message[4];
case "SET":
const field = message[3];
const value = message[4];
this.app.config.setField(field, value);
return true;
this.app.config.setField(field, value);
return true;
default:
return false;
}
}
default:
return false;
}
}
send(sock: WebSocket | NodeJS.Process, response: ConfigResponse) {
sock.send?.(JSON.stringify(response));
}
send(sock: WebSocket | NodeJS.Process, response: ConfigResponse) {
sock.send?.(JSON.stringify(response));
}
}