2024-10-19 09:47:56 +00:00
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2024-10-28 08:33:05 +00:00
|
|
|
"github.com/charmbracelet/log"
|
|
|
|
|
2024-10-19 09:47:56 +00:00
|
|
|
"git.smsvc.net/pomodoro/GoTomato/internal/pomodoro"
|
2024-10-21 07:46:16 +00:00
|
|
|
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
|
2024-10-19 09:47:56 +00:00
|
|
|
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
|
|
|
)
|
|
|
|
|
2024-10-30 06:37:14 +00:00
|
|
|
// Listens for commands from a client and handles them
|
2024-10-30 08:57:09 +00:00
|
|
|
func handleClientCommands(c models.WebsocketClient) {
|
2024-10-30 07:09:41 +00:00
|
|
|
ws := c.Conn
|
2024-10-19 09:47:56 +00:00
|
|
|
for {
|
2024-10-20 21:09:30 +00:00
|
|
|
var clientCommand models.ClientCommand
|
|
|
|
|
2024-10-19 09:47:56 +00:00
|
|
|
_, message, err := ws.ReadMessage()
|
|
|
|
if err != nil {
|
2024-10-25 22:01:07 +00:00
|
|
|
log.Info("Client disconnected:", "msg", err, "host", ws.NetConn().RemoteAddr(), "clients", len(Clients)-1)
|
2024-10-30 09:17:24 +00:00
|
|
|
delete(Clients, ws.LocalAddr())
|
2024-10-19 09:47:56 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle incoming commands
|
2024-10-20 21:09:30 +00:00
|
|
|
err = json.Unmarshal(message, &clientCommand)
|
2024-10-19 09:47:56 +00:00
|
|
|
if err != nil {
|
2024-10-25 22:01:07 +00:00
|
|
|
log.Error("Error unmarshalling command:", "msg", err)
|
2024-10-19 09:47:56 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-10-21 13:36:26 +00:00
|
|
|
// Process the command if pomodoro password matches
|
|
|
|
if clientCommand.Password == shared.PomodoroPassword {
|
|
|
|
switch clientCommand.Command {
|
|
|
|
case "start":
|
|
|
|
if !pomodoro.IsPomodoroOngoing() {
|
2024-10-22 06:51:22 +00:00
|
|
|
go pomodoro.RunPomodoro() // Start the timer with the list of clients
|
2024-10-21 13:36:26 +00:00
|
|
|
}
|
|
|
|
case "stop":
|
|
|
|
if pomodoro.IsPomodoroOngoing() {
|
|
|
|
pomodoro.ResetPomodoro() // Reset Pomodoro
|
|
|
|
}
|
|
|
|
case "pause":
|
|
|
|
if pomodoro.IsPomodoroOngoing() && !pomodoro.IsPomodoroPaused() {
|
|
|
|
pomodoro.PausePomodoro() // Pause the timer
|
|
|
|
}
|
|
|
|
case "resume":
|
|
|
|
if pomodoro.IsPomodoroOngoing() && pomodoro.IsPomodoroPaused() {
|
|
|
|
pomodoro.ResumePomodoro() // Resume the timer
|
2024-10-20 21:09:30 +00:00
|
|
|
}
|
2024-10-22 06:51:22 +00:00
|
|
|
case "updateSettings":
|
|
|
|
if !pomodoro.IsPomodoroOngoing() {
|
2024-10-28 08:30:00 +00:00
|
|
|
pomodoro.UpdateSettings(clientCommand.Settings)
|
2024-10-22 06:51:22 +00:00
|
|
|
}
|
2024-10-19 09:47:56 +00:00
|
|
|
}
|
|
|
|
}
|
2024-10-19 16:15:16 +00:00
|
|
|
|
2024-10-19 09:47:56 +00:00
|
|
|
}
|
|
|
|
}
|