forked from hidaba/PylontechMonitoring
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpy_systemmanager.cpp
More file actions
156 lines (126 loc) · 4.79 KB
/
Copy pathpy_systemmanager.cpp
File metadata and controls
156 lines (126 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <Arduino.h>
#include <Preferences.h>
#include <WiFi.h>
#include "nvs_flash.h"
#include "py_systemmanager.h"
#include "py_log.h" // Log()
#include "py_wifimanager.h" // WiFi reset
#include "config.h" // zentrale Konfig
// ----------------------------------------------------
// Konfiguration
// ----------------------------------------------------
static const uint8_t BUTTON_PIN = 0; // BOOT-Button
static const unsigned long LONG_PRESS_TIME = 15000; // 15s
static const unsigned long SHORT_PRESS_MAX = 1000; // <1s
static const unsigned long MULTI_PRESS_TIMEOUT = 1000; // 1s Pause
// ----------------------------------------------------
// Interne Variablen
// ----------------------------------------------------
static unsigned long buttonPressStart = 0;
static unsigned long lastButtonRelease = 0;
static int shortPressCount = 0;
static bool buttonWasPressed = false;
// Boot-Counter
static Preferences prefs;
static int bootCounter = 0;
// ----------------------------------------------------
// Initialisierung
// ----------------------------------------------------
void SystemManager::begin() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
prefs.begin("system", false);
bootCounter = prefs.getInt("bootcount", 0);
bootCounter++;
prefs.putInt("bootcount", bootCounter);
prefs.end();
Log(LOG_INFO, "SystemManager: BootCounter = " + String(bootCounter));
}
// ----------------------------------------------------
// Button-Handling
// ----------------------------------------------------
void SystemManager::loop() {
handleButton();
}
// ----------------------------------------------------
// Button-Auswertung
// ----------------------------------------------------
void SystemManager::handleButton() {
bool pressed = (digitalRead(BUTTON_PIN) == LOW);
unsigned long now = millis();
// --- Button gedrückt ---
if (pressed && !buttonWasPressed) {
buttonWasPressed = true;
buttonPressStart = now;
}
// --- Button losgelassen ---
if (!pressed && buttonWasPressed) {
buttonWasPressed = false;
unsigned long pressDuration = now - buttonPressStart;
// Langer Druck → Factory Reset
if (pressDuration > LONG_PRESS_TIME) {
Log(LOG_WARN, "SystemManager: LONG PRESS → FACTORY RESET");
triggerFactoryReset();
return;
}
// Kurzer Druck → zählen
if (pressDuration < SHORT_PRESS_MAX) {
shortPressCount++;
lastButtonRelease = now;
}
}
// --- Auswertung der kurzen Drücke ---
if (shortPressCount > 0 && (now - lastButtonRelease > MULTI_PRESS_TIMEOUT)) {
if (shortPressCount == 1) {
Log(LOG_INFO, "SystemManager: 1x short press → AP TEMPORARY");
triggerAPTemporary();
}
if (shortPressCount >= 5) {
Log(LOG_WARN, "SystemManager: 5x short press → WIFI RESET");
triggerWiFiReset();
}
shortPressCount = 0;
}
}
// ----------------------------------------------------
// Event-Funktionen
// ----------------------------------------------------
void SystemManager::triggerAPTemporary() {
Log(LOG_INFO, "SystemManager: AP TEMPORARY triggered");
WiFiManagerModule::startTemporaryAP(600000); // 10 Minuten
}
void SystemManager::triggerWiFiReset() {
Log(LOG_WARN, "SystemManager: WIFI RESET triggered");
WiFiManagerModule::resetWiFi();
}
void SystemManager::triggerFactoryReset() {
Log(LOG_ERROR, "SystemManager: FACTORY RESET initiated");
delay(200);
// ----------------------------------------------------
// ALLE gespeicherten Daten löschen
// ----------------------------------------------------
Preferences p;
Log(LOG_WARN, "SystemManager: FULL NVS ERASE");
nvs_flash_erase();
nvs_flash_init();
//ESP.restart();
// ----------------------------------------------------
// Optional: WiFi trennen
// ----------------------------------------------------
//WiFi.disconnect(true);
//WiFi.mode(WIFI_AP_STA);
Log(LOG_WARN, "SystemManager: all data cleared → restarting...");
delay(500);
ESP.restart();
}
// ----------------------------------------------------
// BootCounter Getter
// ----------------------------------------------------
int SystemManager::getBootCounter() {
return bootCounter;
}
// ----------------------------------------------------
// Callback-Variablen
// ----------------------------------------------------
std::function<void()> SystemManager::onAPTemporary = nullptr;
std::function<void()> SystemManager::onWiFiReset = nullptr;
std::function<void()> SystemManager::onFactoryReset = nullptr;