-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueuer.h
71 lines (52 loc) · 1.75 KB
/
Queuer.h
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
/*
* Anyone who implements Queuer can be part of the queue handled by the QueueMaster
* The Queuer has one or more tasks that can be carried out when it is
* their turn in the queue. This is defined in the loop(); and is called by the
* QueueMaster when it's the Queuers turn.
*
* Extending classes must implement it's own tasks in loop(); These should not
* be executed if finishedWithTasks is true, which is set by the QueueMaster through
* reset() since there potentially can be two queuers on the same channel, and we don't
* want the first one to finish to restart its loop() before the second one has finished.
*/
#ifndef QUEUER_H
#define QUEUER_H
#define MAX_NBR_OF_SUBSCRIBERS 6
#include <HomeSpan.h>
#include "Subscriber.h"
struct QueueMaster;
struct Queuer {
Subscriber* subscribers[MAX_NBR_OF_SUBSCRIBERS];
int nbrOfSubscribers = 0;
int id;
int lastPublishedValue = -1;
bool finishedWithTasks = false;
Queuer(int id = 0) {
this->id = id;
}
virtual void loop() = 0;
virtual bool shouldPublishToSubscribers(int newValue) = 0;
virtual int preprocessValueBeforePublish(int newValue) {
return newValue;
}
void onTasksDone(int newValue) {
if (this->shouldPublishToSubscribers(newValue)) {
for (int i = 0; i < nbrOfSubscribers; i++) {
subscribers[i]->onNewSubscribedValue(preprocessValueBeforePublish(newValue), id);
};
this->lastPublishedValue = newValue;
}
finishedWithTasks = true;
}
void reset() {
finishedWithTasks = false;
}
void subscribe(Subscriber* s) {
if (nbrOfSubscribers < MAX_NBR_OF_SUBSCRIBERS) {
subscribers[nbrOfSubscribers++] = s;
} else {
LOG1("WARNING: Tried to subscribe to full subscriber list"); LOG1("\n");
}
}
};
#endif