forked from tbnobody/OpenDTU
-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathPowerMeterSml.cpp
73 lines (64 loc) · 1.96 KB
/
PowerMeterSml.cpp
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
// SPDX-License-Identifier: GPL-2.0-or-later
#include "PowerMeterSml.h"
#include "MessageOutput.h"
float PowerMeterSml::getPowerTotal() const
{
std::lock_guard<std::mutex> l(_mutex);
if (_values.activePowerTotal.has_value()) { return *_values.activePowerTotal; }
return 0;
}
void PowerMeterSml::doMqttPublish() const
{
#define PUB(t, m) \
if (_values.m.has_value()) { mqttPublish(t, *_values.m); }
std::lock_guard<std::mutex> l(_mutex);
PUB("power1", activePowerL1);
PUB("power2", activePowerL2);
PUB("power3", activePowerL3);
PUB("voltage1", voltageL1);
PUB("voltage2", voltageL2);
PUB("voltage3", voltageL3);
PUB("current1", currentL1);
PUB("current2", currentL2);
PUB("current3", currentL3);
PUB("import", energyImport);
PUB("export", energyExport);
#undef PUB
}
void PowerMeterSml::reset()
{
smlReset();
_cache = { std::nullopt };
}
void PowerMeterSml::processSmlByte(uint8_t byte)
{
switch (smlState(byte)) {
case SML_LISTEND:
for (auto& handler: smlHandlerList) {
if (!smlOBISCheck(handler.OBIS)) { continue; }
float helper = 0.0;
handler.decoder(helper);
if (_verboseLogging) {
MessageOutput.printf("[%s] decoded %s to %.2f\r\n",
_user.c_str(), handler.name, helper);
}
std::lock_guard<std::mutex> l(_mutex);
*handler.target = helper;
}
break;
case SML_FINAL:
gotUpdate();
_values = _cache;
reset();
MessageOutput.printf("[%s] TotalPower: %5.2f\r\n",
_user.c_str(), getPowerTotal());
break;
case SML_CHECKSUM_ERROR:
reset();
MessageOutput.printf("[%s] checksum verification failed\r\n",
_user.c_str());
break;
default:
break;
}
}