Skip to content

Commit 5261d78

Browse files
authored
Merge pull request #4001 from tonhuisman/bugfix/p037-event-generation-overloads-esp
[P037] Fix event generation bug, add event-limit options
2 parents 985a6a5 + b727f25 commit 5261d78

19 files changed

+573
-369
lines changed

docs/source/Plugin/P037.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ The options that can be included or excluded are:
4141

4242
* Apply mappings
4343

44+
* To replace by comma in event
45+
4446
Initial setup after adding the plugin:
4547

4648
.. image:: P037_DeviceSetup_Initial.png
@@ -179,6 +181,20 @@ Option: Generate events for accepted topics
179181

180182
When this option is disabled it has the backward-compatible behavior of discarding that message as invalid.
181183

184+
Option: Limit events being generated
185+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
186+
187+
* **Deduplicate events**: Receiving many MQTT messages, especially in JSON format, can cause a lot of events to be generated. To somewhat limit the system-load, caused by the amount of events, new events can be de-duplicated while still in the event queue, meaning that an event that is already queued, will *not* be inserted in the queue, but discarded.
188+
189+
* **Max. # events in event queue**: As a protection against event-overflow this configures a check for the queue-length, so if more than the selected number of events is still in the queue, new events will be discarded until some events are processed, and the remaining is less than this count. When set to 0 this check is disabled.
190+
191+
Option: Modify separater character in events
192+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
193+
194+
* **To replace by comma in event**: Select a character that is to be replaced by a comma, before it is put into the event-queue. There is a limited set of characters that can be replaced, to avoid ending up with malformed events.
195+
196+
This can be used to 'transform' the content of a JSON message so the used separator is a comma, for easier use in rules.
197+
182198
Topic Subscriptions
183199
-------------------
184200

14.1 KB
Loading
14.9 KB
Loading
14.8 KB
Loading
14.9 KB
Loading

src/_P037_MQTTImport.ino

Lines changed: 175 additions & 53 deletions
Large diffs are not rendered by default.

src/_P050_TCS34725.ino

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,7 @@ boolean Plugin_050(uint8_t function, struct EventStruct *event, String& string)
254254

255255
case PLUGIN_EXIT:
256256
{
257-
P050_data_struct *P050_data = static_cast<P050_data_struct *>(getPluginTaskData(event->TaskIndex));
258-
259-
if (nullptr != P050_data) {
260-
delete P050_data; // call destructor
261-
success = true;
262-
}
257+
success = true;
263258
break;
264259
}
265260

src/_P099_XPT2046Touch.ino

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -406,15 +406,7 @@ boolean Plugin_099(uint8_t function, struct EventStruct *event, String& string)
406406

407407
case PLUGIN_EXIT:
408408
{
409-
P099_data_struct *P099_data = static_cast<P099_data_struct *>(getPluginTaskData(event->TaskIndex));
410-
411-
if (nullptr == P099_data) {
412-
return success;
413-
}
414-
clearPluginTaskData(event->TaskIndex);
415-
P099_data = nullptr;
416409
success = true;
417-
418410
break;
419411
}
420412

src/_P108_DDS238.ino

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,6 @@ boolean Plugin_108(uint8_t function, struct EventStruct *event, String& string)
302302
}
303303

304304
case PLUGIN_EXIT: {
305-
// clearPluginTaskData(event->TaskIndex); // DF - not present in P085
306305
success = true;
307306
break;
308307
}

src/src/DataStructs/EventQueue.cpp

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,47 @@
44

55
EventQueueStruct::EventQueueStruct() {}
66

7-
void EventQueueStruct::add(const String& event)
7+
void EventQueueStruct::add(const String& event, bool deduplicate)
88
{
99
#ifdef USE_SECOND_HEAP
1010
HeapSelectIram ephemeral;
11-
#endif
11+
#endif // ifdef USE_SECOND_HEAP
1212

13-
_eventQueue.push_back(event);
13+
if (!deduplicate || !isDuplicate(event)) {
14+
_eventQueue.push_back(event);
15+
}
1416
}
1517

16-
void EventQueueStruct::add(const __FlashStringHelper * event)
18+
void EventQueueStruct::add(const __FlashStringHelper *event, bool deduplicate)
1719
{
1820
#ifdef USE_SECOND_HEAP
1921
HeapSelectIram ephemeral;
20-
#endif
22+
#endif // ifdef USE_SECOND_HEAP
23+
2124
// Wrap in String() constructor to make sure it is using the 2nd heap allocator if present.
22-
_eventQueue.push_back(String(event));
25+
if (!deduplicate || !isDuplicate(event)) {
26+
_eventQueue.push_back(String(event));
27+
}
2328
}
2429

25-
void EventQueueStruct::addMove(String&& event)
30+
void EventQueueStruct::addMove(String&& event, bool deduplicate)
2631
{
27-
if (!event.length()) return;
32+
if (!event.length()) { return; }
2833
#ifdef USE_SECOND_HEAP
2934
HeapSelectIram ephemeral;
35+
3036
if (!mmu_is_iram(&(event[0]))) {
3137
// Wrap in String constructor to make sure it is stored in the 2nd heap.
32-
_eventQueue.push_back(String(event));
38+
if (!deduplicate || !isDuplicate(event)) {
39+
_eventQueue.push_back(String(event));
40+
}
3341
return;
3442
}
35-
#endif
36-
_eventQueue.emplace_back(std::move(event));
43+
#endif // ifdef USE_SECOND_HEAP
44+
45+
if (!deduplicate || !isDuplicate(event)) {
46+
_eventQueue.emplace_back(std::move(event));
47+
}
3748
}
3849

3950
bool EventQueueStruct::getNext(String& event)
@@ -48,9 +59,9 @@ bool EventQueueStruct::getNext(String& event)
4859
HeapSelectDram ephemeral;
4960
event = std::move(String(_eventQueue.front()));
5061
}
51-
#else
62+
#else // ifdef USE_SECOND_HEAP
5263
event = std::move(_eventQueue.front());
53-
#endif
64+
#endif // ifdef USE_SECOND_HEAP
5465
_eventQueue.pop_front();
5566
return true;
5667
}
@@ -63,4 +74,8 @@ void EventQueueStruct::clear()
6374
bool EventQueueStruct::isEmpty() const
6475
{
6576
return _eventQueue.empty();
66-
}
77+
}
78+
79+
bool EventQueueStruct::isDuplicate(const String& event) {
80+
return std::find(_eventQueue.begin(), _eventQueue.end(), event) != _eventQueue.end();
81+
}

0 commit comments

Comments
 (0)