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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
#include "SpacebrewYun.h"
SpacebrewYun::SpacebrewYun(const String& _name, const String& _description) {
name = _name;
description = _description;
server = "sandbox.spacebrew.cc";
port = 9000;
_connected = false;
_verbose = false;
_error_msg = false;
sub_name = "";
sub_msg = "";
sub_type = "";
read_name = false;
read_msg = false;
sub_name_max = 25;
sub_msg_max = 50;
for ( int i = 0; i < pidLength; i++ ) {
pid [i] = '\0';
}
for ( int i = 0; i < sbPidsLen; i++ ) {
sbPids [i] = '\0';
}
Console.buffer(64);
}
// boolean SpacebrewYun::_connected = false;
SpacebrewYun::OnBooleanMessage SpacebrewYun::_onBooleanMessage = NULL;
SpacebrewYun::OnRangeMessage SpacebrewYun::_onRangeMessage = NULL;
SpacebrewYun::OnStringMessage SpacebrewYun::_onStringMessage = NULL;
SpacebrewYun::OnCustomMessage SpacebrewYun::_onCustomMessage = NULL;
SpacebrewYun::OnSBOpen SpacebrewYun::_onOpen = NULL;
SpacebrewYun::OnSBClose SpacebrewYun::_onClose = NULL;
SpacebrewYun::OnSBError SpacebrewYun::_onError = NULL;
void SpacebrewYun::onOpen(OnSBOpen function){
_onOpen = function;
}
void SpacebrewYun::onClose(OnSBClose function){
_onClose = function;
}
void SpacebrewYun::onRangeMessage(OnRangeMessage function){
_onRangeMessage = function;
}
void SpacebrewYun::onStringMessage(OnStringMessage function){
_onStringMessage = function;
}
void SpacebrewYun::onBooleanMessage(OnBooleanMessage function){
_onBooleanMessage = function;
}
void SpacebrewYun::onCustomMessage(OnCustomMessage function){
_onCustomMessage = function;
}
void SpacebrewYun::onError(OnSBError function){
_onError = function;
}
void SpacebrewYun::addPublish(const String& name, const String& type) {
Publisher *p = new Publisher();
p->name = createString(name.length() + 1);
p->type = createString(type.length() + 1);
name.toCharArray(p->name, name.length() + 1);
type.toCharArray(p->type, type.length() + 1);
if (publishers == NULL){
publishers = p;
} else {
Publisher *curr = publishers;
while(curr->next != NULL){
curr = curr->next;
}
curr->next = p;
}
}
void SpacebrewYun::addSubscribe(const String& name, const String& type) {
Subscriber *p = new Subscriber();
p->name = createString(name.length() + 1);
p->type = createString(type.length() + 1);
name.toCharArray(p->name, name.length() + 1);
type.toCharArray(p->type, type.length() + 1);
if (subscribers == NULL){
subscribers = p;
}
else {
Subscriber *curr = subscribers;
while(curr->next != NULL){
curr = curr->next;
}
curr->next = p;
}
}
void SpacebrewYun::connect(String _server, int _port) {
server = _server;
port = _port;
killPids();
brew.begin("python"); // Process should launch the "curl" command
brew.addParameter("/usr/lib/python2.7/spacebrew/spacebrew.py"); // Process should launch the "curl" command
brew.addParameter("--server");
brew.addParameter(server);
brew.addParameter("--port");
brew.addParameter(String(port));
brew.addParameter("-n");
brew.addParameter(name);
brew.addParameter("-d");
brew.addParameter(description);
if (subscribers != NULL) {
Subscriber *curr = subscribers;
while(curr != NULL){
if (_verbose) {
Serial.print(F("Creating subcribers: "));
Serial.println(curr->name);
}
brew.addParameter("-s"); // Add the URL parameter to "curl"
brew.addParameter(curr->name); // Add the URL parameter to "curl"
brew.addParameter(","); // Add the URL parameter to "curl"
brew.addParameter(curr->type); // Add the URL parameter to "curl"
if (curr->next == NULL) curr = NULL;
else curr = curr->next;
}
}
if (publishers != NULL) {
Publisher *curr = publishers;
while(curr != NULL){
if (_verbose) {
Serial.print(F("Creating publishers: "));
Serial.println(curr->name);
}
brew.addParameter("-p"); // Add the URL parameter to "curl"
brew.addParameter(curr->name); // Add the URL parameter to "curl"
brew.addParameter(","); // Add the URL parameter to "curl"
brew.addParameter(curr->type); // Add the URL parameter to "curl"
if (curr->next == NULL) curr = NULL;
else curr = curr->next;
}
}
Console.begin();
brew.runAsynchronously();
while (!Console) { ; }
}
void SpacebrewYun::monitor() {
while (Console.available() > 0) {
char c = Console.read();
if (c == char(CONNECTION_START) && !_connected) {
_connected = true;
if (_verbose) {
Serial.print(F("Connected to spacebrew server at: "));
Serial.println(server);
Serial.print(F("Application name set to: "));
Serial.println(name);
}
if (_onOpen != NULL){
_onOpen();
}
}
else if (c == char(CONNECTION_END) && _connected) {
_connected = false;
if (_verbose) {
Serial.print(F("Disconnected from spacebrew server at: "));
Serial.println(server);
}
if (_onClose != NULL){
_onClose();
}
}
if (_verbose) {
if (c == char(CONNECTION_ERROR)) {
_error_msg = true;
Serial.println(F("ERROR :: with Spacebrew.py Connection ::"));
}
else if (_error_msg && c == char(MSG_END)) {
_error_msg = false;
Serial.println();
}
if (_error_msg) {
Serial.print(c);
}
}
if (_connected) {
if (c == char(MSG_START)) {
read_name = true;
} else if (c == char(MSG_DIV) || sub_name.length() > sub_name_max) {
read_name = false;
read_msg = true;
} else if (c == char(MSG_END) || sub_msg.length() > sub_msg_max) {
read_msg = false;
onMessage();
} else {
if (read_name == true) {
sub_name += c;
} else if (read_msg == true) {
sub_msg += c;
}
else if (_verbose) {
Serial.print(c);
}
}
}
}
}
boolean SpacebrewYun::connected() {
return SpacebrewYun::_connected;
}
void SpacebrewYun::verbose(boolean verbose = true) {
_verbose = verbose;
}
void SpacebrewYun::onMessage() {
if (subscribers != NULL) {
Subscriber *curr = subscribers;
while((curr != NULL) && (sub_type == "")){
if (sub_name.equals(curr->name) == true) {
sub_type = curr->type;
}
if (curr->next == NULL) curr = NULL;
else curr = curr->next;
}
}
if ( sub_type.equals("range") ) {
if (_onRangeMessage != NULL) {
_onRangeMessage( sub_name, int(sub_msg.toInt()) );
} else {
Serial.println(F("ERROR :: Range message received, no callback method is registered"));
}
} else if ( sub_type.equals("boolean") ) {
if (_onBooleanMessage != NULL) {
_onBooleanMessage( sub_name, ( sub_msg.equals("false") ? false : true ) );
} else {
Serial.println(F("ERROR :: Boolean message received, no callback method is registered"));
}
} else if ( sub_type.equals("string") ) {
if (_onStringMessage != NULL) {
_onStringMessage( sub_name, sub_msg );
} else {
Serial.println(F("ERROR :: String message received, no callback method is registered"));
}
} else {
if (_onCustomMessage != NULL) {
_onCustomMessage( sub_name, sub_msg, sub_type );
} else {
Serial.println(F("ERROR :: Custom message received, no callback method is registered"));
}
}
sub_name = "";
sub_msg = "";
sub_type = "";
}
void SpacebrewYun::send(const String& name, const String& value){
Console.print(char(29));
Console.print(name);
Console.print(char(30));
Console.print(value);
Console.print(char(31));
Console.flush();
true;
}
/**
* method that gets the pid of all spacebrew.py instances running on the linino.
*/
void SpacebrewYun::getPids() {
// request the pid of all python processes
pids.begin("python");
pids.addParameter("/usr/lib/python2.7/spacebrew/getProcPid.py"); // Process should launch the "curl" command
pids.run();
if (_verbose) {
Serial.println(F("Checking if spacebrew process already running"));
}
int sbPidsIndex = 0;
int pidCharIndex = 0;
char c = '\0';
while ( pids.available() > 0 ) {
c = pids.read();
if ( c >= '0' && c <= '9' ) {
pid[pidCharIndex] = c;
pidCharIndex = (pidCharIndex + 1) % pidLength;
}
else if ( (c == ' ' || c == '\n') && pidCharIndex > 0) {
sbPids[sbPidsIndex] = atoi(pid);
if ( sbPidsIndex < (sbPidsLen - 1) ) sbPidsIndex = (sbPidsIndex + 1);
for( int i = 0; i < pidLength; i++ ){
pid[i] = '\0';
pidCharIndex = 0;
}
}
}
}
/**
* method that kills all of the spacebrew.py instances that are running
* on the linino.
*/
void SpacebrewYun::killPids() {
getPids();
delay(400);
for (int i = 0; i < sbPidsLen; i ++) {
if (sbPids[i] > 0) {
char * newPID = itoa(sbPids[i], pid, 10);
if (_verbose) {
Serial.print(F("Stopping existing spacebrew processes with pids: "));
Serial.println(newPID);
}
Process p;
p.begin("kill");
p.addParameter("-9");
p.addParameter(newPID); // Process should launch the "curl" command
p.run(); // Run the process and wait for its termination
delay(400);
}
}
}
|