From a0dba673a2e08631a9abf54cd0df286b4e52ac09 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Tue, 22 Oct 2024 07:46:46 +0200 Subject: [PATCH] break: enhance server message structure and settings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - add explicit server messages for start and end - include pomodoro setttings in server messages - update README 🤖 --- README.md | 29 ++++++++++++++++------------- index.html | 2 +- internal/pomodoro/pomodoro.go | 8 ++++++-- internal/shared/state.go | 12 ++++++------ pkg/models/server.go | 12 ++++++------ 5 files changed, 35 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index b04ff29..b651eb1 100644 --- a/README.md +++ b/README.md @@ -55,23 +55,26 @@ Example: ### 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. -These messages contain the following fields: +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: -- 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). -- 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). -- ongoing: Wether a pomodoro is currently ongoing -- paused: Wether the timer is paused +- ongoing: Whether a Pomodoro session is currently ongoing. +- paused: Whether the timer is paused. -| Message Type | Example | -| --------------------- | --------------------------------------------------- | -| Welcome Message | `{"mode": "", "session":0, "total_sessions":0, "time_left":0, "ongoing": false, "paused": false}` | -| Session Running | `{"mode": "Work", "session": 1, "total_sessions": 4, "time_left": 900, "ongoing": true, "paused": false}` | -| Session Running | `{"mode": "ShortBreak", "session": 2, "total_sessions": 4, "time_left": 50, "ongoing": true, "paused": false}` | -| Session Paused | `{"mode": "Work", "session": 2, "total_sessions": 4, "time_left": 456, "ongoing": true, "paused": true}` | -| Session End/Reset | `{"mode": "", "session": 0, "total_sessions": 0, "time_left": 0, "ongoing": false, "paused": false}` | +| Message Type | Example | +| --- | --- | +| 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", "settings":{"work":1500, "shortBreak":300, "longBreak":900, "sessions":4}, "session":1, "time_left":900, "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", "settings":{"work":1500, "shortBreak":300, "longBreak":900, "sessions":4}, "session":2, "time_left":456, "ongoing":true, "paused":true} | +| Session End/Reset | {"mode":"End", "settings":{"work":1500, "shortBreak":300, "longBreak":900, "sessions":4}, "session":0, "time_left":0, "ongoing":false, "paused":false} | ## Testing diff --git a/index.html b/index.html index 421cae4..24aaaf7 100644 --- a/index.html +++ b/index.html @@ -65,7 +65,7 @@ var data = JSON.parse(event.data); var mode = data.mode; var session = data.session; - var totalSession = data.total_sessions; + var totalSession = data.settings.sessions; var timeLeft = data.time_left; document.getElementById("timer").innerText = diff --git a/internal/pomodoro/pomodoro.go b/internal/pomodoro/pomodoro.go index 702d0f7..1b994b9 100644 --- a/internal/pomodoro/pomodoro.go +++ b/internal/pomodoro/pomodoro.go @@ -19,7 +19,7 @@ func RunPomodoro(config models.GoTomatoPomodoroConfig) { shared.Message.Paused = false mu.Unlock() - shared.Message.TotalSession = config.Sessions + shared.Message.PomodoroSettings = config for session := 1; session <= config.Sessions; session++ { shared.Message.Session = session @@ -38,9 +38,13 @@ func RunPomodoro(config models.GoTomatoPomodoroConfig) { 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. diff --git a/internal/shared/state.go b/internal/shared/state.go index 813ddba..0c1beb6 100644 --- a/internal/shared/state.go +++ b/internal/shared/state.go @@ -8,12 +8,12 @@ var Message = ResetToDefault() func ResetToDefault() models.ServerMessage { return models.ServerMessage{ - Mode: "", - Session: 0, - TotalSession: 0, - TimeLeft: 0, - Ongoing: false, - Paused: false, + Mode: "Idle", + PomodoroSettings: DefaultPomodoroConfig, + Session: 0, + TimeLeft: DefaultPomodoroConfig.Work, + Ongoing: false, + Paused: false, } } diff --git a/pkg/models/server.go b/pkg/models/server.go index 7ebe4c3..29780f0 100644 --- a/pkg/models/server.go +++ b/pkg/models/server.go @@ -2,10 +2,10 @@ package models // ServerMessage represents the data sent to the client via WebSocket. type ServerMessage struct { - Mode string `json:"mode"` // "Work", "ShortBreak", or "LongBreak" - 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 - Ongoing bool `json:"ongoing"` // Ongoing pomodoro - Paused bool `json:"paused"` // Is timer paused + 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 + TimeLeft int `json:"time_left"` // Remaining time in seconds + Ongoing bool `json:"ongoing"` // Ongoing pomodoro + Paused bool `json:"paused"` // Is timer paused }