@@ -40,22 +40,17 @@ void Store::insertCommand(const char *payload) {
40
40
command.ha = doc[" ha" ].as <bool >();
41
41
command.ha_class = doc[" ha_class" ].as <std::string>();
42
42
43
- std::vector<Command> *usedCommands = nullptr ;
44
- if (command.active )
45
- usedCommands = &activeCommands;
46
- else
47
- usedCommands = &passiveCommands;
48
-
49
43
const std::vector<Command>::const_iterator it =
50
- std::find_if (usedCommands-> begin (), usedCommands-> end (),
44
+ std::find_if (allCommands. begin (), allCommands. end (),
51
45
[&key](const Command &cmd) { return cmd.key == key; });
52
46
53
- if (it != usedCommands-> end ()) usedCommands-> erase (it);
47
+ if (it != allCommands. end ()) allCommands. erase (it);
54
48
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 );
57
53
58
- init = true ;
59
54
lastInsert = millis ();
60
55
}
61
56
}
@@ -71,72 +66,41 @@ void Store::removeCommand(const char *payload) {
71
66
} else {
72
67
std::string key = doc[" key" ].as <std::string>();
73
68
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 (),
76
71
[&key](const Command &cmd) { return cmd.key == key; });
77
72
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 ();
82
78
} 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 ());
95
81
}
96
82
}
97
83
}
98
84
99
85
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);
102
87
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 )
107
89
mqttClient.publish (" ebus/commands" , 0 , false , " " );
108
90
}
109
91
110
92
const std::string Store::getCommands () const {
111
93
std::string payload;
112
94
JsonDocument doc;
113
95
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) {
134
98
JsonObject obj = doc.add <JsonObject>();
135
99
136
100
obj[" key" ] = command.key ;
137
101
obj[" command" ] = ebus::Sequence::to_string (command.command );
138
102
obj[" unit" ] = command.unit ;
139
- obj[" active" ] = false ;
103
+ obj[" active" ] = command. active ;
140
104
obj[" interval" ] = command.interval ;
141
105
obj[" master" ] = command.master ;
142
106
obj[" position" ] = command.position ;
@@ -164,42 +128,40 @@ void Store::doLoop() {
164
128
}
165
129
}
166
130
167
- const bool Store::active () const { return activeCommands. size () > 0 ; }
131
+ const bool Store::active () const { return activeCommands > 0 ; }
168
132
169
133
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
+ }
176
144
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
+ }
183
151
}
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 ;
192
152
}
193
153
194
- return command;
154
+ if (!init && millis () < next->last + next->interval * 1000 ) next = nullptr ;
155
+
156
+ return next;
195
157
}
196
158
197
159
std::vector<Command *> Store::findPassiveCommands (
198
160
const std::vector<uint8_t > &master) {
199
161
std::vector<Command *> commands;
200
162
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 ))
203
165
commands.push_back (&(command));
204
166
}
205
167
@@ -265,6 +227,13 @@ void Store::wipeCommands() {
265
227
commands.end ();
266
228
}
267
229
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
+
268
237
void Store::checkNewCommands () {
269
238
if (newCommands.size () > 0 ) {
270
239
if (millis () > lastInsert + distanceInsert) {
@@ -278,10 +247,8 @@ void Store::checkNewCommands() {
278
247
void Store::checkPubCommands () {
279
248
if (pubCommands.size () > 0 ) {
280
249
if (millis () > lastPublish + distancePublish) {
281
- std::string payload = pubCommands.front ();
250
+ publishCommand ( pubCommands.front (), false );
282
251
pubCommands.pop_front ();
283
- publishCommand (&activeCommands, payload, false );
284
- publishCommand (&passiveCommands, payload, false );
285
252
}
286
253
}
287
254
}
@@ -290,32 +257,14 @@ const std::string Store::serializeCommands() const {
290
257
std::string payload;
291
258
JsonDocument doc;
292
259
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) {
313
262
JsonArray arr = doc.add <JsonArray>();
314
263
315
264
arr.add (command.key );
316
265
arr.add (ebus::Sequence::to_string (command.command ));
317
266
arr.add (command.unit );
318
- arr.add (false );
267
+ arr.add (command. active );
319
268
arr.add (command.interval );
320
269
arr.add (command.master );
321
270
arr.add (command.position );
@@ -369,43 +318,34 @@ void Store::deserializeCommands(const char *payload) {
369
318
}
370
319
}
371
320
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 ;
400
323
401
- mqttClient. publish (topic. c_str (), 0 , false , payload. c_str ()) ;
324
+ std::string payload;
402
325
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 ());
407
345
408
- if (it->ha ) publishHomeAssistant (&(*it), remove );
346
+ if (remove ) {
347
+ topic = " ebus/values/" + command->topic ;
348
+ mqttClient.publish (topic.c_str (), 0 , false , " " );
409
349
}
410
350
}
411
351
0 commit comments