Repository navigation
Expand file tree
/
Copy pathpy_scheduler.cpp
More file actions
147 lines (114 loc) · 4.04 KB
/
Copy pathpy_scheduler.cpp
File metadata and controls
147 lines (114 loc) · 4.04 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
#include "py_scheduler.h"
#include "py_log.h"
#include "config.h"
#include "py_parser.h"
PyScheduler py_scheduler;
void PyScheduler::begin(PyUart* u) {
uart = u;
queue.clear();
bootTime = millis();
lastPwr = millis();
lastBat = millis();
lastStat = millis();
initialPwrDone = false;
initialBatDone = false;
initialStatDone = false;
initialDiscoveryDone = false;
Log(LOG_INFO, "Scheduler: started");
}
void PyScheduler::enqueue(const String& cmd) {
Log(LOG_DEBUG, "Scheduler: enqueue → " + cmd);
queue.push_back(cmd);
}
String PyScheduler::popNextCommand() {
if (queue.empty()) return "";
String cmd = queue.front();
queue.erase(queue.begin());
Log(LOG_DEBUG, "Scheduler: pop → " + cmd);
return cmd;
}
void PyScheduler::loop() {
unsigned long now = millis();
// UART busy? Then wait
if (!uart || uart->isBusy()) return;
// Initial sequence (only once)
unsigned long sinceBoot = now - bootTime;
if (!initialPwrDone && sinceBoot >= 20000) {
enqueue("pwr");
Log(LOG_INFO, "Scheduler: INITIAL PWR");
initialPwrDone = true;
return;
}
if (initialPwrDone && !initialBatDone && sinceBoot >= 25000) {
enqueue("bat 1");
Log(LOG_INFO, "Scheduler: INITIAL BAT");
initialBatDone = true;
return;
}
if (initialBatDone && !initialStatDone && sinceBoot >= 45000) {
enqueue("stat 1");
Log(LOG_INFO, "Scheduler: INITIAL STAT");
initialStatDone = true;
return;
}
if (initialStatDone && !initialDiscoveryDone && sinceBoot >= 50000) {
Log(LOG_INFO, "Scheduler: INITIAL DISCOVERY triggered");
discoveryPwrNeeded = true;
discoveryBatNeeded = true;
discoveryStatNeeded = true;
initialDiscoveryDone = true;
return;
}
// Cyclic scheduling
if (now - lastPwr >= config.battery.intervalPwr) {
enqueue("pwr");
lastPwr = now;
Log(LOG_INFO, "Scheduler: PWR scheduled");
}
if (now - lastBat >= config.battery.intervalBat) {
if (config.battery.enableBat) {
for (int i = 1; i <= config.detectedModules; i++) {
enqueue("bat " + String(i));
}
Log(LOG_INFO, "Scheduler: BAT scheduled (" + String(config.detectedModules) + " modules)");
}
lastBat = now;
}
if (now - lastStat >= config.battery.intervalStat) {
if (config.battery.enableStat) {
for (int i = 1; i <= config.detectedModules; i++) {
enqueue("stat " + String(i));
}
Log(LOG_INFO, "Scheduler: STAT scheduled (" + String(config.detectedModules) + " modules)");
}
lastStat = now;
}
static unsigned long lastSend = 0;
if (!queue.empty()) {
// 1 second pause between commands
if (millis() - lastSend < 1000)
return;
String cmd = popNextCommand();
int id = nextCommandId++; // unique ID for this command
if (cmd.startsWith("console:")) {
String realCmd = cmd.substring(8); // strip "console:"
Log(LOG_INFO, "Scheduler: DIRECT console command → " + realCmd);
if (uart->sendCommand(realCmd.c_str(), id)) {
lastSend = millis();
lastCommandFinished = lastSend;
} else {
Log(LOG_WARN, "Scheduler: sendCommand failed for console '" + realCmd + "'");
}
return;
}
if (cmd.length() > 0) {
Log(LOG_INFO, "Scheduler: send → " + cmd + " (id=" + String(id) + ")");
if (uart->sendCommand(cmd.c_str(), id)) {
lastSend = millis();
lastCommandFinished = lastSend;
} else {
Log(LOG_WARN, "Scheduler: sendCommand failed for '" + cmd + "'");
}
}
}
}