2024-10-19 09:47:56 +00:00
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"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-25 22:01:07 +00:00
|
|
|
"github.com/charmbracelet/log"
|
2024-10-19 09:47:56 +00:00
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
)
|
|
|
|
|
2024-10-21 07:37:07 +00:00
|
|
|
// handleClientCommands listens for commands from WebSocket clients
|
2024-10-19 09:47:56 +00:00
|
|
|
func handleClientCommands(ws *websocket.Conn) {
|
|
|
|
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-21 07:20:58 +00:00
|
|
|
delete(Clients, ws)
|
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-23 15:35:16 +00:00
|
|
|
if clientCommand.PomodoroSettings != (models.GoTomatoPomodoroConfig{}) {
|
2024-10-22 06:51:22 +00:00
|
|
|
shared.Message.PomodoroSettings = clientCommand.PomodoroSettings
|
|
|
|
shared.Message.TimeLeft = clientCommand.PomodoroSettings.Work
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2024-10-19 09:47:56 +00:00
|
|
|
}
|
|
|
|
}
|
2024-10-19 16:15:16 +00:00
|
|
|
|
2024-10-19 09:47:56 +00:00
|
|
|
}
|
|
|
|
}
|