listen on GET_STATE messages and send the current matrix state

This commit is contained in:
StarAppeal
2024-11-24 19:11:30 +01:00
parent 552c6d4136
commit 8d951887fa
3 changed files with 119 additions and 30 deletions
+77 -1
View File
@@ -8,7 +8,8 @@ export interface IUser {
name: string,
uuid: string,
id: ObjectId,
config: UserConfig
config: UserConfig,
lastState: MatrixState
}
export interface UserConfig {
@@ -17,6 +18,29 @@ export interface UserConfig {
isAdmin: boolean
}
export interface MatrixState {
global: {
mode: 'image' | 'text' | "idle" | "music" | "clock";
brightness: number;
};
text: {
text: string;
align: 'left' | 'center' | 'right'; // Annahme: Mögliche Ausrichtungen sind 'left', 'center', 'right'
speed: number;
size: number;
color: [number, number, number]; // RGB-Werte
};
image: {
image: string; // Der Name der Bilddatei
};
clock: {
color: [number, number, number]; // RGB-Werte
};
music: {
fullscreen: boolean;
};
}
const userSchema = new mongoose.Schema<IUser>({
name: {
type: String,
@@ -40,6 +64,58 @@ const userSchema = new mongoose.Schema<IUser>({
required: true,
},
},
lastState: {
global: {
mode: {
type: String,
required: true,
},
brightness: {
type: Number,
required: true,
},
},
text: {
text: {
type: String,
required: true,
},
align: {
type: String,
required: true,
},
speed: {
type: Number,
required: true,
},
size: {
type: Number,
required: true,
},
color: {
type: [Number],
required: true,
},
},
image: {
image: {
type: String,
required: true,
},
},
clock: {
color: {
type: [Number],
required: true,
},
},
music: {
fullscreen: {
type: Boolean,
required: true,
}
}
},
});
+8 -3
View File
@@ -1,10 +1,11 @@
import { UserModel, IUser } from "../models/user";
import {IUser, UserModel} from "../models/user";
import {connectToDatabase} from "./database.service";
export class UserService {
private static _instance: UserService;
private constructor() {}
private constructor() {
}
public static async create(): Promise<UserService> {
if (!this._instance) {
@@ -15,7 +16,7 @@ export class UserService {
}
public async updateUser(id: string, user: Partial<IUser>): Promise<IUser | null> {
return await UserModel.findByIdAndUpdate(id, user, { new: true }).exec();
return await UserModel.findByIdAndUpdate(id, user, {new: true}).exec();
}
public async getAllUsers(): Promise<IUser[]> {
@@ -25,4 +26,8 @@ export class UserService {
public async getUserById(id: string): Promise<IUser | null> {
return await UserModel.findById(id).exec();
}
public async getUserByUUID(uuid: string): Promise<IUser | null> {
return await UserModel.findOne({uuid}).exec();
}
}
+34 -26
View File
@@ -1,33 +1,41 @@
import { ExtendedWebSocket } from "../../interfaces/extendedWebsocket";
import {ExtendedWebSocket} from "../../interfaces/extendedWebsocket";
import {UserService} from "../../db/services/UserService";
export class WebsocketEventHandler {
constructor(private webSocket: ExtendedWebSocket) {}
constructor(private webSocket: ExtendedWebSocket) {
}
public enableErrorEvent() {
this.webSocket.on("error", console.error);
}
public enableErrorEvent() {
this.webSocket.on("error", console.error);
}
//needed for the heartbeat mechanism
public enablePongEvent() {
this.webSocket.on("pong", () => {
this.webSocket.isAlive = true;
console.log("Pong received");
});
}
//needed for the heartbeat mechanism
public enablePongEvent() {
this.webSocket.on("pong", () => {
this.webSocket.isAlive = true;
console.log("Pong received");
});
}
public enableDisconnectEvent() {
this.webSocket.onclose = (event) => {
console.log("WebSocket closed:", event.code, event.reason, event.wasClean, event.type);
console.log(`User: ${this.webSocket.payload.name} disconnected`);
};
}
public enableDisconnectEvent() {
this.webSocket.onclose = (event) => {
console.log("WebSocket closed:", event.code, event.reason, event.wasClean, event.type);
console.log(`User: ${this.webSocket.payload.name} disconnected`);
};
}
public enableMessageEvent() {
this.webSocket.on("message", (data) => {
const message = data.toString();
console.log("Received message:", message);
// just echo the message back to the client
this.webSocket.send(message);
});
}
public enableMessageEvent() {
this.webSocket.on("message", async (data) => {
const message = data.toString();
console.log("Received message:", message);
if (message === "GET_STATE") {
const userService = await UserService.create();
const user = await userService.getUserByUUID(this.webSocket.payload._id);
this.webSocket.send(JSON.stringify(user?.lastState), {binary: false});
}
});
}
}