Skip to content

Commit ead08a2

Browse files
authored
activeCommands and passiveCommands combined into allCommands (#113)
1 parent e58ba77 commit ead08a2

File tree

2 files changed

+85
-144
lines changed

2 files changed

+85
-144
lines changed

Diff for: include/store.hpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,28 @@ class Store {
4949
static void wipeCommands();
5050

5151
private:
52-
std::vector<Command> activeCommands;
53-
std::vector<Command> passiveCommands;
52+
std::vector<Command> allCommands;
53+
54+
size_t activeCommands = 0;
55+
size_t passiveCommands = 0;
5456

5557
std::deque<std::string> newCommands;
5658
uint32_t distanceInsert = 300;
5759
uint32_t lastInsert = 0;
5860

59-
std::deque<std::string> pubCommands;
61+
std::deque<const Command *> pubCommands;
6062
uint32_t distancePublish = 100;
6163
uint32_t lastPublish = 0;
6264

63-
bool init = true;
65+
void countCommands();
6466

6567
void checkNewCommands();
6668
void checkPubCommands();
6769

6870
const std::string serializeCommands() const;
6971
void deserializeCommands(const char *payload);
7072

71-
static void publishCommand(const std::vector<Command> *commands,
72-
const std::string &key, const bool remove);
73+
static void publishCommand(const Command *command, const bool remove);
7374

7475
static void publishHomeAssistant(const Command *command, const bool remove);
7576
};

Diff for: src/store.cpp

+78-138
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,17 @@ void Store::insertCommand(const char *payload) {
4040
command.ha = doc["ha"].as<bool>();
4141
command.ha_class = doc["ha_class"].as<std::string>();
4242

43-
std::vector<Command> *usedCommands = nullptr;
44-
if (command.active)
45-
usedCommands = &activeCommands;
46-
else
47-
usedCommands = &passiveCommands;
48-
4943
const std::vector<Command>::const_iterator it =
50-
std::find_if(usedCommands->begin(), usedCommands->end(),
44+
std::find_if(allCommands.begin(), allCommands.end(),
5145
[&key](const Command &cmd) { return cmd.key == key; });
5246

53-
if (it != usedCommands->end()) usedCommands->erase(it);
47+
if (it != allCommands.end()) allCommands.erase(it);
5448

55-
usedCommands->push_back(command);
56-
publishCommand(usedCommands, key, false);
49+
allCommands.push_back(command);
50+
countCommands();
51+
publishCommand(&allCommands.back(), false);
52+
if (command.ha) publishHomeAssistant(&allCommands.back(), false);
5753

58-
init = true;
5954
lastInsert = millis();
6055
}
6156
}
@@ -71,72 +66,41 @@ void Store::removeCommand(const char *payload) {
7166
} else {
7267
std::string key = doc["key"].as<std::string>();
7368

74-
const std::vector<Command>::const_iterator actIt =
75-
std::find_if(activeCommands.begin(), activeCommands.end(),
69+
const std::vector<Command>::const_iterator it =
70+
std::find_if(allCommands.begin(), allCommands.end(),
7671
[&key](const Command &cmd) { return cmd.key == key; });
7772

78-
if (actIt != activeCommands.end()) {
79-
publishCommand(&activeCommands, key, true);
80-
81-
activeCommands.erase(actIt);
73+
if (it != allCommands.end()) {
74+
publishCommand(&(*it), true);
75+
if (it->ha) publishHomeAssistant(&(*it), true);
76+
allCommands.erase(it);
77+
countCommands();
8278
} else {
83-
const std::vector<Command>::const_iterator pasIt =
84-
std::find_if(passiveCommands.begin(), passiveCommands.end(),
85-
[&key](const Command &cmd) { return cmd.key == key; });
86-
87-
if (pasIt != passiveCommands.end()) {
88-
publishCommand(&passiveCommands, key, true);
89-
90-
passiveCommands.erase(pasIt);
91-
} else {
92-
std::string err = key + " not found";
93-
mqttClient.publish("ebus/config/error", 0, false, err.c_str());
94-
}
79+
std::string err = key + " not found";
80+
mqttClient.publish("ebus/config/error", 0, false, err.c_str());
9581
}
9682
}
9783
}
9884

9985
void Store::publishCommands() {
100-
for (const Command &command : activeCommands)
101-
pubCommands.push_back(command.key);
86+
for (const Command &command : allCommands) pubCommands.push_back(&command);
10287

103-
for (const Command &command : passiveCommands)
104-
pubCommands.push_back(command.key);
105-
106-
if (activeCommands.size() + passiveCommands.size() == 0)
88+
if (allCommands.size() == 0)
10789
mqttClient.publish("ebus/commands", 0, false, "");
10890
}
10991

11092
const std::string Store::getCommands() const {
11193
std::string payload;
11294
JsonDocument doc;
11395

114-
if (activeCommands.size() > 0) {
115-
for (const Command &command : activeCommands) {
116-
JsonObject obj = doc.add<JsonObject>();
117-
118-
obj["key"] = command.key;
119-
obj["command"] = ebus::Sequence::to_string(command.command);
120-
obj["unit"] = command.unit;
121-
obj["active"] = true;
122-
obj["interval"] = command.interval;
123-
obj["master"] = command.master;
124-
obj["position"] = command.position;
125-
obj["datatype"] = ebus::datatype2string(command.datatype);
126-
obj["topic"] = command.topic;
127-
obj["ha"] = command.ha;
128-
obj["ha_class"] = command.ha_class;
129-
}
130-
}
131-
132-
if (passiveCommands.size() > 0) {
133-
for (const Command &command : passiveCommands) {
96+
if (allCommands.size() > 0) {
97+
for (const Command &command : allCommands) {
13498
JsonObject obj = doc.add<JsonObject>();
13599

136100
obj["key"] = command.key;
137101
obj["command"] = ebus::Sequence::to_string(command.command);
138102
obj["unit"] = command.unit;
139-
obj["active"] = false;
103+
obj["active"] = command.active;
140104
obj["interval"] = command.interval;
141105
obj["master"] = command.master;
142106
obj["position"] = command.position;
@@ -164,42 +128,40 @@ void Store::doLoop() {
164128
}
165129
}
166130

167-
const bool Store::active() const { return activeCommands.size() > 0; }
131+
const bool Store::active() const { return activeCommands > 0; }
168132

169133
Command *Store::nextActiveCommand() {
170-
Command *command = nullptr;
171-
172-
if (init) {
173-
size_t count =
174-
std::count_if(activeCommands.begin(), activeCommands.end(),
175-
[](const Command &cmd) { return cmd.last == 0; });
134+
Command *next = nullptr;
135+
bool init = false;
136+
137+
for (Command &cmd : allCommands) {
138+
if (cmd.active) {
139+
if (cmd.last == 0) {
140+
next = &cmd;
141+
init = true;
142+
break;
143+
}
176144

177-
if (count == 0) {
178-
init = false;
179-
} else {
180-
command =
181-
&(*std::find_if(activeCommands.begin(), activeCommands.end(),
182-
[](const Command &cmd) { return cmd.last == 0; }));
145+
if (next == nullptr) {
146+
next = &cmd;
147+
} else {
148+
if (cmd.last + cmd.interval * 1000 < next->last + next->interval * 1000)
149+
next = &cmd;
150+
}
183151
}
184-
} else {
185-
command = &(*std::min_element(activeCommands.begin(), activeCommands.end(),
186-
[](const Command &lhs, const Command &rhs) {
187-
return (lhs.last + lhs.interval * 1000) <
188-
(rhs.last + rhs.interval * 1000);
189-
}));
190-
191-
if (millis() < command->last + command->interval * 1000) command = nullptr;
192152
}
193153

194-
return command;
154+
if (!init && millis() < next->last + next->interval * 1000) next = nullptr;
155+
156+
return next;
195157
}
196158

197159
std::vector<Command *> Store::findPassiveCommands(
198160
const std::vector<uint8_t> &master) {
199161
std::vector<Command *> commands;
200162

201-
for (Command &command : passiveCommands) {
202-
if (ebus::Sequence::contains(master, command.command))
163+
for (Command &command : allCommands) {
164+
if (!command.active && ebus::Sequence::contains(master, command.command))
203165
commands.push_back(&(command));
204166
}
205167

@@ -265,6 +227,13 @@ void Store::wipeCommands() {
265227
commands.end();
266228
}
267229

230+
void Store::countCommands() {
231+
activeCommands = std::count_if(allCommands.begin(), allCommands.end(),
232+
[](const Command &cmd) { return cmd.active; });
233+
234+
passiveCommands = allCommands.size() - activeCommands;
235+
}
236+
268237
void Store::checkNewCommands() {
269238
if (newCommands.size() > 0) {
270239
if (millis() > lastInsert + distanceInsert) {
@@ -278,10 +247,8 @@ void Store::checkNewCommands() {
278247
void Store::checkPubCommands() {
279248
if (pubCommands.size() > 0) {
280249
if (millis() > lastPublish + distancePublish) {
281-
std::string payload = pubCommands.front();
250+
publishCommand(pubCommands.front(), false);
282251
pubCommands.pop_front();
283-
publishCommand(&activeCommands, payload, false);
284-
publishCommand(&passiveCommands, payload, false);
285252
}
286253
}
287254
}
@@ -290,32 +257,14 @@ const std::string Store::serializeCommands() const {
290257
std::string payload;
291258
JsonDocument doc;
292259

293-
if (activeCommands.size() > 0) {
294-
for (const Command &command : activeCommands) {
295-
JsonArray arr = doc.add<JsonArray>();
296-
297-
arr.add(command.key);
298-
arr.add(ebus::Sequence::to_string(command.command));
299-
arr.add(command.unit);
300-
arr.add(true);
301-
arr.add(command.interval);
302-
arr.add(command.master);
303-
arr.add(command.position);
304-
arr.add(ebus::datatype2string(command.datatype));
305-
arr.add(command.topic);
306-
arr.add(command.ha);
307-
arr.add(command.ha_class);
308-
}
309-
}
310-
311-
if (passiveCommands.size() > 0) {
312-
for (const Command &command : passiveCommands) {
260+
if (allCommands.size() > 0) {
261+
for (const Command &command : allCommands) {
313262
JsonArray arr = doc.add<JsonArray>();
314263

315264
arr.add(command.key);
316265
arr.add(ebus::Sequence::to_string(command.command));
317266
arr.add(command.unit);
318-
arr.add(false);
267+
arr.add(command.active);
319268
arr.add(command.interval);
320269
arr.add(command.master);
321270
arr.add(command.position);
@@ -369,43 +318,34 @@ void Store::deserializeCommands(const char *payload) {
369318
}
370319
}
371320

372-
void Store::publishCommand(const std::vector<Command> *commands,
373-
const std::string &key, const bool remove) {
374-
const std::vector<Command>::const_iterator it =
375-
std::find_if(commands->begin(), commands->end(),
376-
[&key](const Command &cmd) { return cmd.key == key; });
377-
378-
if (it != commands->end()) {
379-
std::string topic = "ebus/commands/" + it->key;
380-
381-
std::string payload;
382-
383-
if (!remove) {
384-
JsonDocument doc;
385-
386-
doc["key"] = it->key;
387-
doc["command"] = ebus::Sequence::to_string(it->command);
388-
doc["unit"] = it->unit;
389-
doc["active"] = it->active;
390-
doc["interval"] = it->interval;
391-
doc["master"] = it->master;
392-
doc["position"] = it->position;
393-
doc["datatype"] = ebus::datatype2string(it->datatype);
394-
doc["topic"] = it->topic;
395-
doc["ha"] = it->ha;
396-
doc["ha_class"] = it->ha_class;
397-
398-
serializeJson(doc, payload);
399-
}
321+
void Store::publishCommand(const Command *command, const bool remove) {
322+
std::string topic = "ebus/commands/" + command->key;
400323

401-
mqttClient.publish(topic.c_str(), 0, false, payload.c_str());
324+
std::string payload;
402325

403-
if (remove) {
404-
topic = "ebus/values/" + it->topic;
405-
mqttClient.publish(topic.c_str(), 0, false, "");
406-
}
326+
if (!remove) {
327+
JsonDocument doc;
328+
329+
doc["key"] = command->key;
330+
doc["command"] = ebus::Sequence::to_string(command->command);
331+
doc["unit"] = command->unit;
332+
doc["active"] = command->active;
333+
doc["interval"] = command->interval;
334+
doc["master"] = command->master;
335+
doc["position"] = command->position;
336+
doc["datatype"] = ebus::datatype2string(command->datatype);
337+
doc["topic"] = command->topic;
338+
doc["ha"] = command->ha;
339+
doc["ha_class"] = command->ha_class;
340+
341+
serializeJson(doc, payload);
342+
}
343+
344+
mqttClient.publish(topic.c_str(), 0, false, payload.c_str());
407345

408-
if (it->ha) publishHomeAssistant(&(*it), remove);
346+
if (remove) {
347+
topic = "ebus/values/" + command->topic;
348+
mqttClient.publish(topic.c_str(), 0, false, "");
409349
}
410350
}
411351

0 commit comments

Comments
 (0)