Refactor for Gamemodes
This commit is contained in:
127
src/master.cpp
127
src/master.cpp
@@ -21,135 +21,10 @@
|
||||
#include <timesync.h>
|
||||
#include <webserverrouter.h>
|
||||
#include <wificlass.h>
|
||||
#include <gamemodes.h>
|
||||
|
||||
const char *firmwareversion = "1.0.0"; // Version der Firmware
|
||||
|
||||
void handleStart1(uint64_t timestamp = 0) {
|
||||
if (!timerData.isRunning1 && timerData.isReady1) {
|
||||
timerData.isReady1 = false;
|
||||
timerData.startTime1 = (timestamp > 0) ? timestamp : millis();
|
||||
timerData.localStartTime1 = millis(); // Set local start time
|
||||
timerData.isRunning1 = true;
|
||||
timerData.endTime1 = 0;
|
||||
publishLaneStatus(1, "running");
|
||||
Serial.println("Bahn 1 gestartet");
|
||||
}
|
||||
}
|
||||
|
||||
void handleStop1(uint64_t timestamp = 0) {
|
||||
if (timerData.isRunning1) {
|
||||
timerData.endTime1 = (timestamp > 0) ? timestamp : millis();
|
||||
timerData.finishedSince1 = millis(); // Set finished time
|
||||
timerData.isRunning1 = false;
|
||||
unsigned long currentTime = timerData.endTime1 - timerData.startTime1;
|
||||
|
||||
if (timerData.bestTime1 == 0 || currentTime < timerData.bestTime1) {
|
||||
timerData.bestTime1 = currentTime;
|
||||
saveBestTimes();
|
||||
}
|
||||
publishLaneStatus(1, "stopped");
|
||||
Serial.println("Bahn 1 gestoppt - Zeit: " + String(currentTime / 1000.0) +
|
||||
"s");
|
||||
}
|
||||
}
|
||||
|
||||
void handleStart2(uint64_t timestamp = 0) {
|
||||
if (!timerData.isRunning2 && timerData.isReady2) {
|
||||
timerData.isReady2 = false;
|
||||
timerData.startTime2 = (timestamp > 0) ? timestamp : millis();
|
||||
timerData.localStartTime2 = millis(); // Set local start time
|
||||
timerData.isRunning2 = true;
|
||||
timerData.endTime2 = 0;
|
||||
publishLaneStatus(2, "running");
|
||||
Serial.println("Bahn 2 gestartet");
|
||||
}
|
||||
}
|
||||
|
||||
void handleStop2(uint64_t timestamp = 0) {
|
||||
if (timerData.isRunning2) {
|
||||
timerData.endTime2 = (timestamp > 0) ? timestamp : millis();
|
||||
timerData.finishedSince2 = millis(); // Set finished time
|
||||
timerData.isRunning2 = false;
|
||||
unsigned long currentTime = timerData.endTime2 - timerData.startTime2;
|
||||
|
||||
if (timerData.bestTime2 == 0 || currentTime < timerData.bestTime2) {
|
||||
timerData.bestTime2 = currentTime;
|
||||
saveBestTimes();
|
||||
}
|
||||
publishLaneStatus(2, "stopped");
|
||||
Serial.println("Bahn 2 gestoppt - Zeit: " + String(currentTime / 1000.0) +
|
||||
"s");
|
||||
}
|
||||
}
|
||||
|
||||
void checkAutoReset() {
|
||||
unsigned long currentTime = millis();
|
||||
|
||||
if (timerData.isRunning1 &&
|
||||
(currentTime - timerData.localStartTime1 > maxTimeBeforeReset)) {
|
||||
timerData.isRunning1 = false;
|
||||
timerData.startTime1 = 0;
|
||||
publishLaneStatus(1, "ready");
|
||||
Serial.println("Bahn 1 automatisch zurückgesetzt");
|
||||
}
|
||||
|
||||
if (timerData.isRunning2 &&
|
||||
(currentTime - timerData.localStartTime2 > maxTimeBeforeReset)) {
|
||||
timerData.isRunning2 = false;
|
||||
timerData.startTime2 = 0;
|
||||
publishLaneStatus(2, "ready");
|
||||
Serial.println("Bahn 2 automatisch zurückgesetzt");
|
||||
}
|
||||
|
||||
// Automatischer Reset nach 10 Sekunden "Beendet"
|
||||
if (!timerData.isRunning1 && timerData.endTime1 > 0 &&
|
||||
timerData.finishedSince1 > 0) {
|
||||
if (millis() - timerData.finishedSince1 > maxTimeDisplay) {
|
||||
timerData.startTime1 = 0;
|
||||
timerData.endTime1 = 0;
|
||||
timerData.finishedSince1 = 0;
|
||||
timerData.isReady1 = true; // Zurücksetzen auf "Bereit"
|
||||
|
||||
JsonDocument messageDoc;
|
||||
messageDoc["firstname"] = "";
|
||||
messageDoc["lastname"] = "";
|
||||
messageDoc["lane"] = "start1"; // Add lane information
|
||||
|
||||
String message;
|
||||
serializeJson(messageDoc, message);
|
||||
|
||||
// Push the message to the frontend
|
||||
pushUpdateToFrontend(message);
|
||||
publishLaneStatus(1, "ready");
|
||||
|
||||
Serial.println("Bahn 1 automatisch auf 'Bereit' zurückgesetzt");
|
||||
}
|
||||
}
|
||||
|
||||
if (!timerData.isRunning2 && timerData.endTime2 > 0 &&
|
||||
timerData.finishedSince2 > 0) {
|
||||
if (currentTime - timerData.finishedSince2 > maxTimeDisplay) {
|
||||
timerData.startTime2 = 0;
|
||||
timerData.endTime2 = 0;
|
||||
timerData.finishedSince2 = 0;
|
||||
timerData.isReady2 = true; // Zurücksetzen auf "Bereit"
|
||||
|
||||
JsonDocument messageDoc;
|
||||
messageDoc["firstname"] = "";
|
||||
messageDoc["lastname"] = "";
|
||||
messageDoc["lane"] = "start2"; // Add lane information
|
||||
|
||||
String message;
|
||||
serializeJson(messageDoc, message);
|
||||
|
||||
// Push the message to the frontend
|
||||
pushUpdateToFrontend(message);
|
||||
publishLaneStatus(2, "ready");
|
||||
|
||||
Serial.println("Bahn 2 automatisch auf 'Bereit' zurückgesetzt");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void saveButtonConfig() {
|
||||
preferences.begin("buttons", false);
|
||||
|
||||
Reference in New Issue
Block a user