forked from letscontrolit/ESPEasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_C008.cpp
162 lines (134 loc) · 5.09 KB
/
_C008.cpp
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
154
155
156
157
158
159
160
161
162
#include "src/Helpers/_CPlugin_Helper.h"
#ifdef USES_C008
// #######################################################################################################
// ########################### Controller Plugin 008: Generic HTTP #######################################
// #######################################################################################################
# define CPLUGIN_008
# define CPLUGIN_ID_008 8
# define CPLUGIN_NAME_008 "Generic HTTP"
bool CPlugin_008(CPlugin::Function function, struct EventStruct *event, String& string)
{
bool success = false;
switch (function)
{
case CPlugin::Function::CPLUGIN_PROTOCOL_ADD:
{
Protocol[++protocolCount].Number = CPLUGIN_ID_008;
Protocol[protocolCount].usesMQTT = false;
Protocol[protocolCount].usesTemplate = true;
Protocol[protocolCount].usesAccount = true;
Protocol[protocolCount].usesPassword = true;
Protocol[protocolCount].usesExtCreds = true;
Protocol[protocolCount].defaultPort = 80;
Protocol[protocolCount].usesID = true;
break;
}
case CPlugin::Function::CPLUGIN_GET_DEVICENAME:
{
string = F(CPLUGIN_NAME_008);
break;
}
case CPlugin::Function::CPLUGIN_INIT:
{
success = init_c008_delay_queue(event->ControllerIndex);
break;
}
case CPlugin::Function::CPLUGIN_EXIT:
{
exit_c008_delay_queue();
break;
}
case CPlugin::Function::CPLUGIN_PROTOCOL_TEMPLATE:
{
event->String1 = String();
event->String2 = F("demo.php?name=%sysname%&task=%tskname%&valuename=%valname%&value=%value%");
break;
}
case CPlugin::Function::CPLUGIN_PROTOCOL_SEND:
{
if (C008_DelayHandler == nullptr) {
break;
}
if (C008_DelayHandler->queueFull(event->ControllerIndex)) {
break;
}
String pubname;
{
// Place the ControllerSettings in a scope to free the memory as soon as we got all relevant information.
MakeControllerSettings(ControllerSettings); //-V522
if (!AllocatedControllerSettings()) {
addLog(LOG_LEVEL_ERROR, F("C008 : Generic HTTP - Cannot send, out of RAM"));
break;
}
LoadControllerSettings(event->ControllerIndex, ControllerSettings);
pubname = ControllerSettings.Publish;
}
uint8_t valueCount = getValueCountForTask(event->TaskIndex);
std::unique_ptr<C008_queue_element> element(new C008_queue_element(event, valueCount));
success = C008_DelayHandler->addToQueue(std::move(element));
if (success) {
// Element was added.
// Now we try to append to the existing element
// and thus preventing the need to create a long string only to copy it to a queue element.
C008_queue_element& element = static_cast<C008_queue_element&>(*(C008_DelayHandler->sendQueue.back()));
// Collect the values at the same run, to make sure all are from the same sample
//LoadTaskSettings(event->TaskIndex); // FIXME TD-er: This can probably be removed
parseControllerVariables(pubname, event, true);
for (uint8_t x = 0; x < valueCount; x++)
{
bool isvalid;
const String formattedValue = formatUserVar(event, x, isvalid);
if (isvalid) {
element.txt[x] = '/';
element.txt[x] += pubname;
parseSingleControllerVariable(element.txt[x], event, x, true);
element.txt[x].replace(F("%value%"), formattedValue);
# ifndef BUILD_NO_DEBUG
if (loglevelActiveFor(LOG_LEVEL_DEBUG_MORE))
addLog(LOG_LEVEL_DEBUG_MORE, element.txt[x]);
# endif // ifndef BUILD_NO_DEBUG
}
}
}
Scheduler.scheduleNextDelayQueue(ESPEasy_Scheduler::IntervalTimer_e::TIMER_C008_DELAY_QUEUE, C008_DelayHandler->getNextScheduleTime());
break;
}
case CPlugin::Function::CPLUGIN_FLUSH:
{
process_c008_delay_queue();
delay(0);
break;
}
default:
break;
}
return success;
}
// ********************************************************************************
// Generic HTTP get request
// ********************************************************************************
// Uncrustify may change this into multi line, which will result in failed builds
// *INDENT-OFF*
bool do_process_c008_delay_queue(int controller_number, const Queue_element_base& element_base, ControllerSettingsStruct& ControllerSettings) {
const C008_queue_element& element = static_cast<const C008_queue_element&>(element_base);
// *INDENT-ON*
while (element.txt[element.valuesSent].isEmpty()) {
// A non valid value, which we are not going to send.
// Increase sent counter until a valid value is found.
if (element.checkDone(true)) {
return true;
}
}
int httpCode = -1;
send_via_http(
controller_number,
ControllerSettings,
element._controller_idx,
element.txt[element.valuesSent],
F("GET"),
EMPTY_STRING,
EMPTY_STRING,
httpCode);
return element.checkDone((httpCode >= 100) && (httpCode < 300));
}
#endif // ifdef USES_C008