queue.ino
3.42 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "queue.h"
void enqueue(int command, double start, double data) {
/* Serial.println("enqueue"); */
/* // no dupes, only create new if it is scheduled */
/* while(ptr != NULL) { */
/* if ( command == T_PUMPLEVEL */
/* || command == T_PUMPDURATION */
/* || command == T_PUMPABORT */
/* && */
/* ptr->command == T_PUMPLEVEL */
/* || ptr->command == T_PUMPDURATION */
/* ) { */
/* break; */
/* } */
/* } */
struct action *ptr = (struct action*) malloc(sizeof(struct action));
ptr->command = command;
ptr->start = start;
ptr->data = data;
ptr->next = NULL;
ptr->running = 0;
if (head == NULL) {
head = ptr;
}
if (tail != NULL) {
tail->next = ptr;
}
tail = ptr;
/* struct action *ptr = head; */
/* if ( ptr == NULL ) { */
// in an empty list, this is head
// previous tail is not last anymore
// this is now tail
/*
/* } */
/* if ( command == T_PUMPABORT ) { */
/* ptr->end = 1; */
/* ptr->running = 1; */
/* } else { */
/* } */
}
int entry_start(struct action *ptr) {
switch( ptr->command ) {
case C_FILL_DURATION:
return fill_start(ptr);
case C_ROTATE_DURATION:
return rotate_start(ptr);
case C_FERTILIZE_DURATION:
return fertilize_start(ptr);
}
return 0;
}
void entry_running(struct action *ptr) {
unsigned int ut = time(NULL);
String remainingStr(ptr->end - ut);
delay(10); // Don't remove! mqttClient crashes otherwise
switch( ptr->command ) {
case C_FILL_DURATION:
mqttClient.publish("home/compost/action/3/duration", remainingStr );
break;
case C_ROTATE_DURATION:
mqttClient.publish("home/compost/action/1/duration", remainingStr );
break;
case C_FERTILIZE_DURATION:
mqttClient.publish("home/compost/action/2/duration", remainingStr );
break;
}
}
/* void device_running(struct action *ptr) { */
/* /\* int device = sequence_head->device; *\/ */
/* /\* String pre = "home/compost/device/"; *\/ */
/* /\* int device = 1; *\/ */
/* /\* String state(pre + device + "state"); *\/ */
/* /\* String duration(pre + device + "/duration"); *\/ */
/* } */
void entry_end(struct action *ptr) {
String remainingStr(0);
mqttClient.publish(PREAMBLE T_PUMPSTATE, remainingStr );
}
void entry_end(struct button *ptr) {
}
void entry_end(int source) {
}
void loop_queue() {
struct action *ptr = head;
struct action *next = NULL;
struct action *prev = NULL;
unsigned int ut = time(NULL);
while(ptr != NULL) {
next=ptr->next;
if ( ptr->running == 0
&& ut >= ptr->start
) {
if ( entry_start(ptr) ) { // pump is blocked ?
ptr->end = ptr->data + ut;
ptr->running = 1;
} else {
ptr->start++; // delay action;
}
}
///////////////////////// action
if ( ptr->running == 1 ) {
entry_running(ptr);
}
///////////////////////// end action
if ( ptr->running == 1
&& ut >= ptr->end
) {
/* Serial.println("end"); */
entry_end(ptr);
/* Serial.println("end1"); */
/* action is due to removal */
if (prev != NULL) {
prev->next = ptr->next;
}
if (ptr == head) {
head = ptr->next;
}
/* Serial.println("end5"); */
/* if (ptr == tail) { */
/* tail = prev; */
/* } */
free(ptr);
} else {
prev = ptr;
}
ptr = next;
}
}
void setup_queue() {
}