feat: refactor client handling and introduce websocket client type

- add new websocket.Client type
- change Connect and Disconnect functions to use the new Client type
- implement methods for sending commands and updating settings on Client
- update keyhandler to use websocket.Client instead of ChronoTomato.Client
- modify UpdateLoop to accept websocket.Client
- refactor ProcessServerMessages to accept the new Client type

🤖
This commit is contained in:
Sebastian Mark 2024-10-30 08:54:58 +01:00
parent b943c9d6eb
commit 0ca90b50a5
6 changed files with 25 additions and 18 deletions

View file

@ -9,35 +9,37 @@ import (
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
func sendClientCommand(client ChronoTomato.Client, msg GoTomato.ClientCommand) {
type Client ChronoTomato.PomodoroClient // New websocket client
func (c Client) sendClientCommand(msg GoTomato.ClientCommand) {
messageBytes, err := json.Marshal(msg)
if err != nil {
log.Error("Error marshalling!", "reason", err)
return
}
err = client.Conn.WriteMessage(websocket.TextMessage, messageBytes)
err = c.Conn.WriteMessage(websocket.TextMessage, messageBytes)
if err != nil {
log.Error("Write error!", "reason", err)
return
}
}
func SendCmd(client ChronoTomato.Client, pwd string, cmd string) {
func (c Client) SendCmd(pwd string, cmd string) {
message := GoTomato.ClientCommand{
Command: cmd,
Password: pwd,
}
sendClientCommand(client, message)
c.sendClientCommand(message)
}
func Send_updateSettings(client ChronoTomato.Client, pwd string, settings GoTomato.PomodoroConfig) {
func (c Client) SendSettingsUpdate(pwd string, settings GoTomato.PomodoroConfig) {
message := GoTomato.ClientCommand{
Command: "updateSettings",
Password: pwd,
Settings: settings,
}
sendClientCommand(client, message)
c.sendClientCommand(message)
}