break: enhance server message structure and settings

- add explicit server messages for start and end
- include pomodoro setttings in server messages
- update README

🤖
This commit is contained in:
Sebastian Mark 2024-10-22 07:46:46 +02:00
parent b60df1c025
commit a0dba673a2
5 changed files with 35 additions and 28 deletions

View file

@ -55,23 +55,26 @@ Example:
### Server Messages ### Server Messages
The server periodically (every second) sends JSON-encoded messages to all connected clients to update them on the current state of the Pomodoro session. The server periodically (every second) sends JSON-encoded messages to all connected clients to update them on the current state of the Pomodoro session. These messages contain the following fields:
These messages contain the following fields:
- mode: Indicates the current phase of the Pomodoro session ("Work", "ShortBreak", "LongBreak", or empty if no session is running). - mode: Indicates the current phase of the Pomodoro session ("Work", "ShortBreak", "LongBreak", "End" or "Idle").
- settings: Contains the current Pomodoro settings:
- work: Length of the work session in seconds (e.g., 1500 for 25 minutes).
- shortBreak: Length of the short break in seconds (e.g., 300 for 5 minutes).
- longBreak: Length of the long break in seconds (e.g., 900 for 15 minutes).
- sessions: The total number of work/break sessions (e.g., 4).
- session: The current session number (e.g., 1 for the first work session). - session: The current session number (e.g., 1 for the first work session).
- total_sessions: The total number of sessions for the current Pomodoro cycle (e.g., 4 if the cycle consists of 4 work/break sessions).
- time_left: The remaining time for the current mode, in seconds (e.g., 900 for 15 minutes). - time_left: The remaining time for the current mode, in seconds (e.g., 900 for 15 minutes).
- ongoing: Wether a pomodoro is currently ongoing - ongoing: Whether a Pomodoro session is currently ongoing.
- paused: Wether the timer is paused - paused: Whether the timer is paused.
| Message Type | Example | | Message Type | Example |
| --------------------- | --------------------------------------------------- | | --- | --- |
| Welcome Message | `{"mode": "", "session":0, "total_sessions":0, "time_left":0, "ongoing": false, "paused": false}` | | Welcome Message | {"mode":"Idle", "settings":{"work":1500, "shortBreak":300, "longBreak":900, "sessions":4}, "session":0, "time_left":1500, "ongoing":false, "paused":false} |
| Session Running | `{"mode": "Work", "session": 1, "total_sessions": 4, "time_left": 900, "ongoing": true, "paused": false}` | | Session Running | {"mode":"Work", "settings":{"work":1500, "shortBreak":300, "longBreak":900, "sessions":4}, "session":1, "time_left":900, "ongoing":true, "paused":false} |
| Session Running | `{"mode": "ShortBreak", "session": 2, "total_sessions": 4, "time_left": 50, "ongoing": true, "paused": false}` | | Session Running | {"mode":"ShortBreak", "settings":{"work":1500, "shortBreak":300, "longBreak":900, "sessions":4}, "session":2, "time_left":50, "ongoing":true, "paused":false} |
| Session Paused | `{"mode": "Work", "session": 2, "total_sessions": 4, "time_left": 456, "ongoing": true, "paused": true}` | | Session Paused | {"mode":"Work", "settings":{"work":1500, "shortBreak":300, "longBreak":900, "sessions":4}, "session":2, "time_left":456, "ongoing":true, "paused":true} |
| Session End/Reset | `{"mode": "", "session": 0, "total_sessions": 0, "time_left": 0, "ongoing": false, "paused": false}` | | Session End/Reset | {"mode":"End", "settings":{"work":1500, "shortBreak":300, "longBreak":900, "sessions":4}, "session":0, "time_left":0, "ongoing":false, "paused":false} |
## Testing ## Testing

View file

@ -65,7 +65,7 @@
var data = JSON.parse(event.data); var data = JSON.parse(event.data);
var mode = data.mode; var mode = data.mode;
var session = data.session; var session = data.session;
var totalSession = data.total_sessions; var totalSession = data.settings.sessions;
var timeLeft = data.time_left; var timeLeft = data.time_left;
document.getElementById("timer").innerText = document.getElementById("timer").innerText =

View file

@ -19,7 +19,7 @@ func RunPomodoro(config models.GoTomatoPomodoroConfig) {
shared.Message.Paused = false shared.Message.Paused = false
mu.Unlock() mu.Unlock()
shared.Message.TotalSession = config.Sessions shared.Message.PomodoroSettings = config
for session := 1; session <= config.Sessions; session++ { for session := 1; session <= config.Sessions; session++ {
shared.Message.Session = session shared.Message.Session = session
@ -38,9 +38,13 @@ func RunPomodoro(config models.GoTomatoPomodoroConfig) {
break break
} }
} }
shared.Message.Mode = "End"
} }
shared.Message = shared.ResetToDefault() mu.Lock()
shared.Message.Ongoing = false
shared.Message.Paused = false
mu.Unlock()
} }
// ResetPomodoro resets the running Pomodoro timer. // ResetPomodoro resets the running Pomodoro timer.

View file

@ -8,10 +8,10 @@ var Message = ResetToDefault()
func ResetToDefault() models.ServerMessage { func ResetToDefault() models.ServerMessage {
return models.ServerMessage{ return models.ServerMessage{
Mode: "", Mode: "Idle",
PomodoroSettings: DefaultPomodoroConfig,
Session: 0, Session: 0,
TotalSession: 0, TimeLeft: DefaultPomodoroConfig.Work,
TimeLeft: 0,
Ongoing: false, Ongoing: false,
Paused: false, Paused: false,
} }

View file

@ -2,9 +2,9 @@ package models
// ServerMessage represents the data sent to the client via WebSocket. // ServerMessage represents the data sent to the client via WebSocket.
type ServerMessage struct { type ServerMessage struct {
Mode string `json:"mode"` // "Work", "ShortBreak", or "LongBreak" Mode string `json:"mode"` // "Idle", "Work", "ShortBreak", "LongBreak" or "End"
PomodoroSettings GoTomatoPomodoroConfig `json:"settings"` // The currrent pomodoro settings
Session int `json:"session"` // Current session number Session int `json:"session"` // Current session number
TotalSession int `json:"total_sessions"` // Total number of sessions
TimeLeft int `json:"time_left"` // Remaining time in seconds TimeLeft int `json:"time_left"` // Remaining time in seconds
Ongoing bool `json:"ongoing"` // Ongoing pomodoro Ongoing bool `json:"ongoing"` // Ongoing pomodoro
Paused bool `json:"paused"` // Is timer paused Paused bool `json:"paused"` // Is timer paused