-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCurrentTransformerIoT.cpp
66 lines (54 loc) · 2.39 KB
/
CurrentTransformerIoT.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
#include <CurrentTransformerIoT.hpp>
CurrentTransformerIoT::CurrentTransformerIoT() {
}
CurrentTransformerIoT::~CurrentTransformerIoT() {
}
bool CurrentTransformerIoT::begin(uint8_t card_id, ExpansionCard *card, PubSubClient *mqtt, char *base_topic) {
ESP_LOGD("CurrentTransformerIoT", "Beginning CurrentTransformerIoT");
this->card_id = card_id;
this->currentTransformerCard = (CurrentTransformerCard*) card;
this->mqtt = mqtt;
this->base_topic = base_topic;
auto bindedCTCallback = std::bind(&CurrentTransformerIoT::handleCTCallback, this, std::placeholders::_1, std::placeholders::_2);
this->currentTransformerCard->registerCallback(bindedCTCallback);
return true;
}
void CurrentTransformerIoT::handleMqttMessage(char *topic, char *payload) {
uint8_t payload_length = strlen(payload);
if(this->processSetEnergyMessage(topic, payload, payload_length)) return;
if (!strcmp(topic, CT_RESET_ENERGY_TOPIC)) {
this->currentTransformerCard->resetEnergy();
return;
} else if (!strcmp(topic, CT_REQUESTSTATE_TOPIC)) {
this->publishReport();
return;
}
}
void CurrentTransformerIoT::subscribe() {
this->subscribeRelative(CT_SET_ENERGY_TOPIC);
this->subscribeRelative(CT_RESET_ENERGY_TOPIC);
this->subscribeRelative(CT_REQUESTSTATE_TOPIC);
}
void CurrentTransformerIoT::loop() {
// Not used, still need this to meet polymorphism requirements
}
void CurrentTransformerIoT::publishReport() {
char outputBuffer[256];
snprintf(outputBuffer, sizeof(outputBuffer), "%.2f", this->currentTransformerCard->getPower());
this->publishRelative(CT_POWER_TOPIC, outputBuffer);
snprintf(outputBuffer, sizeof(outputBuffer), "%.2f", this->currentTransformerCard->getEnergy());
this->publishRelative(CT_ENERGY_TOPIC, outputBuffer);
snprintf(outputBuffer, sizeof(outputBuffer), "%.2f", this->currentTransformerCard->getCurrent());
this->publishRelative(CT_CURRENT_TOPIC, outputBuffer);
}
uint8_t CurrentTransformerIoT::getType() {
return this->currentTransformerCard->getType();
}
bool CurrentTransformerIoT::processSetEnergyMessage(char* topic, char* payload, uint8_t topic_length) {
if(strcmp(topic, CT_SET_ENERGY_TOPIC)) return false;
this->currentTransformerCard->setEnergy(atof(payload));
return true;
}
void CurrentTransformerIoT::handleCTCallback(float current, double energy) {
this->publishReport();
}