Repository navigation
Expand file tree
/
Copy pathMwaveSensorModule.cpp
More file actions
153 lines (135 loc) · 5.86 KB
/
Copy pathMwaveSensorModule.cpp
File metadata and controls
153 lines (135 loc) · 5.86 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
#include "MwaveSensorModule.hpp"
#include "MultiLogger.hpp"
// Statische Member hier definieren und initialisieren
char MwaveSensorModule::sensorDataBuffer[256];
int MwaveSensorModule::sensorDataIndex = 0;
MwaveSensorModule::MwaveSensorModule(DeviceConfig& deviceConf, HardwareConfig& hardwareConf, HardwareSerial& serial)
: config(deviceConf), hwConfig(hardwareConf), sensorSerial(serial) {}
void MwaveSensorModule::begin() {
if (config.mwaveSensorEnabled) {
sensorSerial.begin(115200, SERIAL_8N1, hwConfig.mwaveRxPin, hwConfig.mwaveTxPin);
Log.println("[MWave] Sensor-Schnittstelle initialisiert.");
sendHexData("FDFCFBFA0800120000006400000004030201");
if (hwConfig.displayRelayPin != 255) {
pinMode(hwConfig.displayRelayPin, OUTPUT);
digitalWrite(hwConfig.displayRelayPin, HIGH); // Standard AN
Log.printf("[MWave] Relais-Pin %d initialisiert.\n", hwConfig.displayRelayPin);
}
} else {
Log.println("[MWave] Sensor ist deaktiviert.");
}
}
void MwaveSensorModule::update(time_t now_utc) {
if (!config.mwaveSensorEnabled) {
if (!isDisplayOnState) {
isDisplayOnState = true; // Falls Sensor deaktiviert wird, Display sicherheitshalber einschalten
if (hwConfig.displayRelayPin != 255) digitalWrite(hwConfig.displayRelayPin, HIGH);
Log.println("[MWave] Sensor deaktiviert, schalte Display dauerhaft AN.");
logStateChange(true, now_utc);
}
return;
}
if (!initialStateSet) {
lastStateChangeToOnTime = now_utc;
logStateChange(true, now_utc);
initialStateSet = true;
Log.println("[MWave] Initialzustand: AN gesetzt.");
}
handleSensorSerial(now_utc);
slidingEventHistory.erase(
std::remove_if(slidingEventHistory.begin(), slidingEventHistory.end(),
[now_utc, this](const SensorEvent& event) {
return (now_utc - event.timestamp) > this->config.mwaveOnCheckDuration;
}
),
slidingEventHistory.end()
);
if (slidingEventHistory.empty()) {
currentOnPercentage = 0.0f;
} else {
int on_count = 0;
for (const auto& event : slidingEventHistory) {
if (event.type == EVENT_ON) on_count++;
}
currentOnPercentage = (float)on_count / slidingEventHistory.size() * 100.0f;
}
if (isDisplayOnState) {
if (offCheckWindowStartTime == 0) offCheckWindowStartTime = now_utc;
if (now_utc - offCheckWindowStartTime >= config.mwaveOffCheckDuration) {
float on_ratio = (onCountInOffWindow + offCountInOffWindow > 0) ? ((float)onCountInOffWindow / (float)(onCountInOffWindow + offCountInOffWindow) * 100.0f) : 0.0f;
if (offCountInOffWindow > 0 && on_ratio <= config.mwaveOffCheckOnPercent) {
isDisplayOnState = false;
lastStateChangeToOffTime = firstOffTimeInOffWindow;
logStateChange(false, lastStateChangeToOffTime);
if (hwConfig.displayRelayPin != 255) digitalWrite(hwConfig.displayRelayPin, LOW);
Log.printf("[MWave] >> Zustand: AUS (um %llu)\n", (long long unsigned)lastStateChangeToOffTime);
}
resetOffCheckWindowCounters();
}
} else {
if (currentOnPercentage > config.mwaveOnCheckPercentage) {
isDisplayOnState = true;
lastStateChangeToOnTime = now_utc;
logStateChange(true, lastStateChangeToOnTime);
if (hwConfig.displayRelayPin != 255) digitalWrite(hwConfig.displayRelayPin, HIGH);
Log.printf("[MWave] >> Zustand: AN (Anteil: %.1f%%)\n", currentOnPercentage);
resetOffCheckWindowCounters();
}
}
}
bool MwaveSensorModule::isDisplayOn() const { return isDisplayOnState; }
time_t MwaveSensorModule::getLastOnTime() const { return lastStateChangeToOnTime; }
time_t MwaveSensorModule::getLastOffTime() const { return lastStateChangeToOffTime; }
float MwaveSensorModule::getOnPercentage() const { return currentOnPercentage; }
const PsramVector<DisplayStateLogEntry>& MwaveSensorModule::getDisplayStateLog() const { return displayStateLog; }
void MwaveSensorModule::sendHexData(const char* hexString) {
int len = strlen(hexString);
byte buf[len / 2];
for (int i = 0; i < len; i += 2) {
char byte_str[3] = {hexString[i], hexString[i+1], '\0'};
buf[i / 2] = (byte)strtoul(byte_str, NULL, 16);
}
sensorSerial.write(buf, sizeof(buf));
}
void MwaveSensorModule::processSensorData(const char* data, time_t now) {
SensorEventType type;
if (strcmp(data, "ON") == 0) {
type = EVENT_ON;
onCountInOffWindow++;
} else if (strcmp(data, "OFF") == 0) {
type = EVENT_OFF;
offCountInOffWindow++;
if (firstOffTimeInOffWindow == 0) {
firstOffTimeInOffWindow = now;
}
} else {
return;
}
slidingEventHistory.push_back({now, type});
}
void MwaveSensorModule::handleSensorSerial(time_t now) {
while (sensorSerial.available() > 0) {
char c = (char)sensorSerial.read();
if (c == '\n') {
sensorDataBuffer[sensorDataIndex] = '\0';
if (sensorDataIndex > 0) processSensorData(sensorDataBuffer, now);
sensorDataIndex = 0;
} else if (sensorDataIndex < (sizeof(sensorDataBuffer) - 1)) {
if (c != '\r') sensorDataBuffer[sensorDataIndex++] = c;
} else {
sensorDataIndex = 0;
}
}
}
void MwaveSensorModule::resetOffCheckWindowCounters() {
offCheckWindowStartTime = 0;
onCountInOffWindow = 0;
offCountInOffWindow = 0;
firstOffTimeInOffWindow = 0;
}
void MwaveSensorModule::logStateChange(bool state, time_t timestamp) {
if (displayStateLog.size() >= 10) {
displayStateLog.erase(displayStateLog.begin());
}
displayStateLog.push_back({timestamp, state});
}