ChronoTomato/internal/websocket/send.go

44 lines
1,003 B
Go
Raw Normal View History

2024-10-22 11:13:41 +00:00
package websocket
import (
"encoding/json"
"github.com/charmbracelet/log"
"github.com/gorilla/websocket"
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
2024-10-22 11:13:41 +00:00
)
func sendClientCommand(client ChronoTomato.Client, msg GoTomato.ClientCommand) {
2024-10-22 11:13:41 +00:00
messageBytes, err := json.Marshal(msg)
if err != nil {
log.Error("Error marshalling!", "reason", err)
return
}
err = client.Conn.WriteMessage(websocket.TextMessage, messageBytes)
2024-10-22 11:13:41 +00:00
if err != nil {
log.Error("Write error!", "reason", err)
return
}
}
func SendCmd(client ChronoTomato.Client, pwd string, cmd string) {
message := GoTomato.ClientCommand{
2024-10-22 11:13:41 +00:00
Command: cmd,
Password: pwd,
}
sendClientCommand(client, message)
2024-10-22 11:13:41 +00:00
}
func Send_updateSettings(client ChronoTomato.Client, pwd string, settings GoTomato.PomodoroConfig) {
message := GoTomato.ClientCommand{
Command: "updateSettings",
Password: pwd,
Settings: settings,
2024-10-22 11:13:41 +00:00
}
sendClientCommand(client, message)
2024-10-22 11:13:41 +00:00
}