Compare commits
No commits in common. "cb6616f400db3d0c294a25da16c89a8ac030ac3d" and "b60df1c025867b9b8df4e2504dec5626cbfe22a3" have entirely different histories.
cb6616f400
...
b60df1c025
7 changed files with 86 additions and 109 deletions
66
README.md
66
README.md
|
@ -28,48 +28,50 @@ If the client sends a command with an incorrect password it will not be allowed
|
|||
|
||||
Here are the available commands:
|
||||
|
||||
| Command | Action | Example Sent by Client |
|
||||
| ---------- | ------------------------------------- | ----------------------------------- |
|
||||
| `start` | Starts a new Pomodoro session | `{"command": "start", "password": ""}` |
|
||||
| `pause` | Pauses the current session | `{"command": "pause", "password": ""}` |
|
||||
| `resume` | Resumes a paused session | `{"command": "resume", "password": ""}` |
|
||||
| `reset` | Stops and resets the current session | `{"command": "reset", "password": ""}` |
|
||||
| `updateSettings` | Update Pomodoro settings | `{"command": "updateSettings", "password": "", "settings": {"work": 600, "shortBreak": 60, "longBreak": 300, "sessions": 2}}` |
|
||||
| Command | Action | Example Sent by Client |
|
||||
|----------|-------------------------------------|-----------------------------------|
|
||||
| `start` | Starts a new Pomodoro session | `{"command": "start", "password": ""}` |
|
||||
| `pause` | Pauses the current session | `{"command": "pause", "password": ""}` |
|
||||
| `resume` | Resumes a paused session | `{"command": "resume", "password": ""}` |
|
||||
| `reset` | Stops and resets the current session| `{"command": "reset", "password": ""}` |
|
||||
|
||||
#### Update Settings Command (`updateSettings`)
|
||||
#### Optional Start Parameters
|
||||
|
||||
The `updateSettings` command allows clients to modify the Pomodoro timer configuration, including the length of work sessions, short breaks, long breaks, and the total number of sessions in a cycle. This command must include a valid password to be accepted by the server. The updateSettings command can be used when the timer is stopped.
|
||||
The Start-Command may contain an optional Pomodoro-Config, which allows you to customize the length of the work session, short break, long break, and the number of sessions. If no configuration is provided, the server will use default values.
|
||||
|
||||
- Fields in the `settings` Object:
|
||||
- `work`: Length of the work session (in seconds).
|
||||
- `shortBreak`: Length of the short break (in seconds).
|
||||
- `longBreak`: Length of the long break (in seconds).
|
||||
- `sessions`: Total number of work/break sessions in the Pomodoro cycle.
|
||||
|
||||
All fields are mandatory and may not be ommitted.
|
||||
Example:
|
||||
```json
|
||||
{
|
||||
command: "start",
|
||||
password: "foobar",
|
||||
config: {
|
||||
"work": 10, // Length of the work session in seconds
|
||||
"shortBreak": 5, // Length of the short break in seconds
|
||||
"longBreak": 10, // Length of the long break in seconds
|
||||
"sessions": 2 // Number of total sessions
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 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", "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).
|
||||
- mode: Indicates the current phase of the Pomodoro session ("Work", "ShortBreak", "LongBreak", or empty if no session is running).
|
||||
- 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: Whether a Pomodoro session is currently ongoing.
|
||||
- paused: Whether the timer is paused.
|
||||
- ongoing: Wether a pomodoro is currently ongoing
|
||||
- paused: Wether the timer is paused
|
||||
|
||||
| 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} |
|
||||
| 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}` |
|
||||
|
||||
## Testing
|
||||
|
||||
|
|
62
index.html
62
index.html
|
@ -20,13 +20,6 @@
|
|||
input {
|
||||
width: 75px;
|
||||
}
|
||||
|
||||
.settings {
|
||||
border: 2px solid black;
|
||||
background: #e7e7e7;
|
||||
padding: 10px;
|
||||
display: inline-block;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
|
@ -34,27 +27,23 @@
|
|||
<h1>Pomodoro Timer</h1>
|
||||
|
||||
<!-- Input fields for custom Pomodoro config and control password -->
|
||||
<div class="settings">
|
||||
<p>
|
||||
<label for="password">Control Password:</label>
|
||||
<input type="text" id="password" placeholder="Password" />
|
||||
</p>
|
||||
<p>
|
||||
<label for="workDuration">Work Duration (seconds):</label>
|
||||
<input type="number" id="workDuration" placeholder="Work time in seconds" value="900" />
|
||||
<br />
|
||||
<label for="shortBreakDuration">Short Break Duration (seconds):</label>
|
||||
<input type="number" id="shortBreakDuration" placeholder="Short break in seconds" value="300" />
|
||||
<br />
|
||||
<label for="longBreakDuration">Long Break Duration (seconds):</label>
|
||||
<input type="number" id="longBreakDuration" placeholder="Long break in seconds" value="600" />
|
||||
<br />
|
||||
<label for="sessions">Number of Sessions:</label>
|
||||
<input type="number" id="sessions" placeholder="Number of sessions" value="4" />
|
||||
<br />
|
||||
<button id="saveButton">Save Settings</button>
|
||||
</p>
|
||||
</div>
|
||||
<p>
|
||||
<label for="password">Control Password:</label>
|
||||
<input type="text" id="password" placeholder="Password" />
|
||||
</p>
|
||||
<p>
|
||||
<label for="workDuration">Work Duration (seconds):</label>
|
||||
<input type="number" id="workDuration" placeholder="Work time in seconds" value="900" />
|
||||
<br />
|
||||
<label for="shortBreakDuration">Short Break Duration (seconds):</label>
|
||||
<input type="number" id="shortBreakDuration" placeholder="Short break in seconds" value="300" />
|
||||
<br />
|
||||
<label for="longBreakDuration">Long Break Duration (seconds):</label>
|
||||
<input type="number" id="longBreakDuration" placeholder="Long break in seconds" value="600" />
|
||||
<br />
|
||||
<label for="sessions">Number of Sessions:</label>
|
||||
<input type="number" id="sessions" placeholder="Number of sessions" value="4" />
|
||||
</p>
|
||||
|
||||
<div id="timer">Connting to server...</div>
|
||||
|
||||
|
@ -76,7 +65,7 @@
|
|||
var data = JSON.parse(event.data);
|
||||
var mode = data.mode;
|
||||
var session = data.session;
|
||||
var totalSession = data.settings.sessions;
|
||||
var totalSession = data.total_sessions;
|
||||
var timeLeft = data.time_left;
|
||||
|
||||
document.getElementById("timer").innerText =
|
||||
|
@ -94,8 +83,8 @@
|
|||
return minutes.toString().padStart(2, '0') + ":" + remainingSeconds.toString().padStart(2, '0');
|
||||
}
|
||||
|
||||
// saveButton Click Event
|
||||
document.getElementById("saveButton").addEventListener("click", function () {
|
||||
// Start Button Click Event
|
||||
document.getElementById("startButton").addEventListener("click", function () {
|
||||
// Get the values from the input fields
|
||||
var password = document.getElementById("password").value;
|
||||
var work = parseInt(document.getElementById("workDuration").value);
|
||||
|
@ -105,22 +94,15 @@
|
|||
|
||||
// Send the start command with the custom config and password
|
||||
ws.send(JSON.stringify({
|
||||
command: "updateSettings",
|
||||
command: "start",
|
||||
password: password,
|
||||
settings: {
|
||||
config: {
|
||||
work: work,
|
||||
shortBreak: shortBreak,
|
||||
longBreak: longBreak,
|
||||
sessions: sessions
|
||||
}
|
||||
}));
|
||||
});
|
||||
|
||||
// Start Button Click Event
|
||||
document.getElementById("startButton").addEventListener("click", function () {
|
||||
var password = document.getElementById("password").value;
|
||||
// Send the start command with the custom config and password
|
||||
ws.send(JSON.stringify({command: "start", password: password}));
|
||||
|
||||
// Hide start button and show pause/resume and reset buttons
|
||||
document.getElementById("startButton").style.display = "none";
|
||||
|
|
|
@ -2,7 +2,7 @@ package pomodoro
|
|||
|
||||
import (
|
||||
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
|
||||
// "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
"sync"
|
||||
)
|
||||
|
||||
|
@ -13,38 +13,34 @@ var pomodoroResumeChannel = make(chan bool, 1)
|
|||
var mu sync.Mutex // to synchronize access to shared state
|
||||
|
||||
// RunPomodoro iterates the Pomodoro work/break sessions.
|
||||
func RunPomodoro() {
|
||||
func RunPomodoro(config models.GoTomatoPomodoroConfig) {
|
||||
mu.Lock()
|
||||
shared.Message.Ongoing = true
|
||||
shared.Message.Paused = false
|
||||
mu.Unlock()
|
||||
|
||||
pomodoroConfig := shared.Message.PomodoroSettings
|
||||
shared.Message.TotalSession = config.Sessions
|
||||
|
||||
for session := 1; session <= pomodoroConfig.Sessions; session++ {
|
||||
for session := 1; session <= config.Sessions; session++ {
|
||||
shared.Message.Session = session
|
||||
shared.Message.Mode = "Work"
|
||||
if !startTimer(pomodoroConfig.Work) {
|
||||
if !startTimer(config.Work) {
|
||||
break
|
||||
}
|
||||
if session == pomodoroConfig.Sessions {
|
||||
if session == config.Sessions {
|
||||
shared.Message.Mode = "LongBreak"
|
||||
if !startTimer(pomodoroConfig.LongBreak) {
|
||||
if !startTimer(config.LongBreak) {
|
||||
break
|
||||
}
|
||||
} else {
|
||||
shared.Message.Mode = "ShortBreak"
|
||||
if !startTimer(pomodoroConfig.ShortBreak) {
|
||||
if !startTimer(config.ShortBreak) {
|
||||
break
|
||||
}
|
||||
}
|
||||
shared.Message.Mode = "End"
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
shared.Message.Ongoing = false
|
||||
shared.Message.Paused = false
|
||||
mu.Unlock()
|
||||
shared.Message = shared.ResetToDefault()
|
||||
}
|
||||
|
||||
// ResetPomodoro resets the running Pomodoro timer.
|
||||
|
|
|
@ -8,12 +8,12 @@ var Message = ResetToDefault()
|
|||
|
||||
func ResetToDefault() models.ServerMessage {
|
||||
return models.ServerMessage{
|
||||
Mode: "Idle",
|
||||
PomodoroSettings: DefaultPomodoroConfig,
|
||||
Session: 0,
|
||||
TimeLeft: DefaultPomodoroConfig.Work,
|
||||
Ongoing: false,
|
||||
Paused: false,
|
||||
Mode: "",
|
||||
Session: 0,
|
||||
TotalSession: 0,
|
||||
TimeLeft: 0,
|
||||
Ongoing: false,
|
||||
Paused: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -9,6 +9,8 @@ import (
|
|||
"log"
|
||||
)
|
||||
|
||||
var pomodoroConfig = shared.DefaultPomodoroConfig
|
||||
|
||||
// handleClientCommands listens for commands from WebSocket clients
|
||||
func handleClientCommands(ws *websocket.Conn) {
|
||||
for {
|
||||
|
@ -33,7 +35,10 @@ func handleClientCommands(ws *websocket.Conn) {
|
|||
switch clientCommand.Command {
|
||||
case "start":
|
||||
if !pomodoro.IsPomodoroOngoing() {
|
||||
go pomodoro.RunPomodoro() // Start the timer with the list of clients
|
||||
if clientCommand.Config != shared.UnsetPomodoroConfig {
|
||||
pomodoroConfig = clientCommand.Config
|
||||
}
|
||||
go pomodoro.RunPomodoro(pomodoroConfig) // Start the timer with the list of clients
|
||||
}
|
||||
case "stop":
|
||||
if pomodoro.IsPomodoroOngoing() {
|
||||
|
@ -47,14 +52,6 @@ func handleClientCommands(ws *websocket.Conn) {
|
|||
if pomodoro.IsPomodoroOngoing() && pomodoro.IsPomodoroPaused() {
|
||||
pomodoro.ResumePomodoro() // Resume the timer
|
||||
}
|
||||
case "updateSettings":
|
||||
if !pomodoro.IsPomodoroOngoing() {
|
||||
if clientCommand.PomodoroSettings != shared.UnsetPomodoroConfig {
|
||||
shared.Message.PomodoroSettings = clientCommand.PomodoroSettings
|
||||
shared.Message.TimeLeft = clientCommand.PomodoroSettings.Work
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -8,9 +8,9 @@ import (
|
|||
|
||||
// ClientCommand represents a command from the client (start/stop).
|
||||
type ClientCommand struct {
|
||||
Command string `json:"command"` // comman send to the server
|
||||
Password string `json:"password"` // pomodoro control password
|
||||
PomodoroSettings GoTomatoPomodoroConfig `json:"settings"` // pomodoro config
|
||||
Command string `json:"command"` // comman send to the server
|
||||
Password string `json:"password"` // pomodoro control password
|
||||
Config GoTomatoPomodoroConfig `json:"config"` // pomodoro config
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
|
|
|
@ -2,10 +2,10 @@ package models
|
|||
|
||||
// ServerMessage represents the data sent to the client via WebSocket.
|
||||
type ServerMessage struct {
|
||||
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
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue