2024-10-22 11:13:41 +00:00
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/charmbracelet/log"
|
|
|
|
"github.com/gorilla/websocket"
|
2024-10-27 20:34:53 +00:00
|
|
|
|
|
|
|
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
2024-10-22 11:13:41 +00:00
|
|
|
)
|
|
|
|
|
2024-10-30 10:03:18 +00:00
|
|
|
// Sends to ClientCommand to the clients websocket
|
2024-10-30 07:54:58 +00:00
|
|
|
func (c Client) sendClientCommand(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
|
|
|
|
}
|
|
|
|
|
2024-10-30 07:54:58 +00:00
|
|
|
err = c.Conn.WriteMessage(websocket.TextMessage, messageBytes)
|
2024-10-22 11:13:41 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Write error!", "reason", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-30 10:03:18 +00:00
|
|
|
// Sends the passed command to the server
|
2024-10-30 08:00:29 +00:00
|
|
|
func (c Client) SendCmd(cmd string) {
|
2024-10-27 09:41:31 +00:00
|
|
|
message := GoTomato.ClientCommand{
|
2024-10-22 11:13:41 +00:00
|
|
|
Command: cmd,
|
2024-10-30 08:00:29 +00:00
|
|
|
Password: c.Password,
|
2024-10-22 11:13:41 +00:00
|
|
|
}
|
|
|
|
|
2024-10-30 07:54:58 +00:00
|
|
|
c.sendClientCommand(message)
|
2024-10-22 11:13:41 +00:00
|
|
|
}
|
|
|
|
|
2024-10-30 10:03:18 +00:00
|
|
|
// Sends the new PomodoroConfig to the server
|
2024-10-30 08:00:29 +00:00
|
|
|
func (c Client) SendSettingsUpdate(settings GoTomato.PomodoroConfig) {
|
2024-10-27 09:41:31 +00:00
|
|
|
message := GoTomato.ClientCommand{
|
2024-10-28 06:56:37 +00:00
|
|
|
Command: "updateSettings",
|
2024-10-30 08:00:29 +00:00
|
|
|
Password: c.Password,
|
2024-10-28 06:56:37 +00:00
|
|
|
Settings: settings,
|
2024-10-22 11:13:41 +00:00
|
|
|
}
|
|
|
|
|
2024-10-30 07:54:58 +00:00
|
|
|
c.sendClientCommand(message)
|
2024-10-22 11:13:41 +00:00
|
|
|
}
|