forked from Homegear/homegear-nodes-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyNode.cpp
257 lines (236 loc) · 7.57 KB
/
MyNode.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* Copyright 2013-2019 Homegear GmbH
*
* Homegear is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Homegear is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Homegear. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
#include "MyNode.h"
namespace MyNode
{
MyNode::MyNode(std::string path, std::string nodeNamespace, std::string type, const std::atomic_bool* frontendConnected) : Flows::INode(path, nodeNamespace, type, frontendConnected)
{
_stopThread = true;
_stopped = true;
_threadRunning = false;
}
MyNode::~MyNode()
{
_stopThread = true;
waitForStop();
}
bool MyNode::init(Flows::PNodeInfo info)
{
try
{
auto settingsIterator = info->info->structValue->find("on-delay");
if(settingsIterator != info->info->structValue->end()) _delay = Flows::Math::getUnsignedNumber(settingsIterator->second->stringValue);
return true;
}
catch(const std::exception& ex)
{
_out->printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what());
}
catch(...)
{
_out->printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__);
}
return false;
}
bool MyNode::start()
{
try
{
_stopped = false;
int64_t delayTo = getNodeData("delayTo")->integerValue64;
if (delayTo > 0)
{
std::lock_guard<std::mutex> timerGuard(_timerThreadMutex);
_stopThread = true;
if (_timerThread.joinable())_timerThread.join();
_stopThread = false;
_timerThread = std::thread(&MyNode::timer, this, delayTo);
}
_lastInputState = getNodeData("lastOutputState")->booleanValue;
return true;
}
catch(const std::exception& ex)
{
_out->printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what());
}
catch(...)
{
_out->printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__);
}
return false;
}
void MyNode::stop()
{
try
{
_stopped = true;
std::lock_guard<std::mutex> timerGuard(_timerThreadMutex);
_stopThread = true;
}
catch(const std::exception& ex)
{
_out->printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what());
}
catch(...)
{
_out->printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__);
}
}
void MyNode::waitForStop()
{
try
{
std::lock_guard<std::mutex> timerGuard(_timerThreadMutex);
_stopThread = true;
if (_timerThread.joinable()) _timerThread.join();
}
catch(const std::exception& ex)
{
_out->printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what());
}
catch(...)
{
_out->printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__);
}
}
void MyNode::timer(int64_t delayTo)
{
try
{
int64_t restTime = delayTo - Flows::HelperFunctions::getTime();
int64_t sleepingTime = 10;
if(_delay >= 1000) sleepingTime = 100;
else if(_delay >= 30000) sleepingTime = 1000;
while (restTime > 0)
{
if ((restTime / sleepingTime) % (1000 / sleepingTime) == 0)
{
Flows::PVariable outputMessage2 = std::make_shared<Flows::Variable>(Flows::VariableType::tStruct);
outputMessage2->structValue->emplace("payload", std::make_shared<Flows::Variable>((restTime / sleepingTime) * sleepingTime));
output(1, outputMessage2); //rest time
}
std::this_thread::sleep_for(std::chrono::milliseconds(sleepingTime));
if(_stopThread)
{
Flows::PVariable outputMessage3 = std::make_shared<Flows::Variable>(Flows::VariableType::tStruct);
outputMessage3->structValue->emplace("payload", std::make_shared<Flows::Variable>(-1));
output(1, outputMessage3); //rest time
setNodeData("delayTo", std::make_shared<Flows::Variable>(0));
_threadRunning = false;
return;
}
restTime = delayTo - Flows::HelperFunctions::getTime();
}
_threadRunning = false;
Flows::PVariable outputMessage2 = std::make_shared<Flows::Variable>(Flows::VariableType::tStruct);
outputMessage2->structValue->emplace("payload", std::make_shared<Flows::Variable>(0));
output(1, outputMessage2); //rest time
Flows::PVariable outputMessage = std::make_shared<Flows::Variable>(Flows::VariableType::tStruct);
outputMessage->structValue->emplace("payload", std::make_shared<Flows::Variable>(true));
output(0, outputMessage); //true
setNodeData("delayTo", std::make_shared<Flows::Variable>(0));
}
catch(const std::exception& ex)
{
_threadRunning = false;
_out->printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what());
}
catch(...)
{
_threadRunning = false;
_out->printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__);
}
}
void MyNode::input(const Flows::PNodeInfo info, uint32_t index, const Flows::PVariable message)
{
try
{
Flows::PVariable& input = message->structValue->at("payload");
if(index == 0)
{
if (*input)
{
if (!_threadRunning && (!_lastInputState || _firstInput))
{
_lastInputState = true;
setNodeData("lastOutputState", std::make_shared<Flows::Variable>(true));
int64_t delayTo = _delay + Flows::HelperFunctions::getTime();
setNodeData("delayTo", std::make_shared<Flows::Variable>(delayTo));
std::lock_guard<std::mutex> timerGuard(_timerThreadMutex);
_stopThread = true;
if (_timerThread.joinable())_timerThread.join();
if (_stopped) return;
_stopThread = false;
_threadRunning = true;
_timerThread = std::thread(&MyNode::timer, this, delayTo);
}
}
else
{
{
std::lock_guard<std::mutex> timerGuard(_timerThreadMutex);
_stopThread = true;
}
if (!_threadRunning && (_lastInputState || _firstInput)) //Only fire "false" once
{
Flows::PVariable outputMessage = std::make_shared<Flows::Variable>(Flows::VariableType::tStruct);
outputMessage->structValue->emplace("payload", std::make_shared<Flows::Variable>(false));
output(0, outputMessage); //false
}
_lastInputState = false;
setNodeData("lastOutputState", std::make_shared<Flows::Variable>(false));
}
if (_firstInput) _firstInput = false;
}
else
{
if(*input && _lastInputState)
{
int64_t delayTo = _delay + Flows::HelperFunctions::getTime();
setNodeData("delayTo", std::make_shared<Flows::Variable>(delayTo));
std::lock_guard<std::mutex> timerGuard(_timerThreadMutex);
_stopThread = true;
if (_timerThread.joinable())_timerThread.join();
if (_stopped) return;
_stopThread = false;
_threadRunning = true;
_timerThread = std::thread(&MyNode::timer, this, delayTo);
}
}
}
catch(const std::exception& ex)
{
_out->printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what());
}
catch(...)
{
_out->printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__);
}
}
}