@@ -127,7 +127,7 @@ void SpacebrewYun::addSubscribe(const String& name, const String& type) {
127
127
}
128
128
129
129
void SpacebrewYun::connect (String _server, int _port) {
130
- Serial.print (F (" NEW LIB " ));
130
+ Serial.println (F (" v2.3 " ));
131
131
_started = true ;
132
132
server = _server;
133
133
port = _port;
@@ -214,9 +214,9 @@ void SpacebrewYun::monitor() {
214
214
215
215
if (c == char (CONNECTION_START) && _started && !_connected) {
216
216
if (_verbose) {
217
- Serial.print (F (" Connected to spacebrew server at : " ));
217
+ Serial.print (F (" Connected to: " ));
218
218
Serial.println (server);
219
- Serial.print (F (" Application name set to : " ));
219
+ Serial.print (F (" Application name: " ));
220
220
Serial.println (name);
221
221
}
222
222
if (_onOpen != NULL ){
@@ -228,7 +228,7 @@ void SpacebrewYun::monitor() {
228
228
else if (c == char (CONNECTION_END) && _connected) {
229
229
_connected = false ;
230
230
if (_verbose) {
231
- Serial.print (F (" Disconnected from spacebrew server at : " ));
231
+ Serial.print (F (" Disconnected from: " ));
232
232
Serial.println (server);
233
233
}
234
234
if (_onClose != NULL ){
@@ -239,7 +239,7 @@ void SpacebrewYun::monitor() {
239
239
if (_verbose) {
240
240
if (c == char (CONNECTION_ERROR)) {
241
241
_error_msg = true ;
242
- Serial.println (F (" ERROR :: with Spacebrew.py Connection ::" ));
242
+ Serial.println (F (" ERROR :: Spacebrew.py ::" ));
243
243
}
244
244
else if (_error_msg && c == char (MSG_END)) {
245
245
_error_msg = false ;
@@ -251,24 +251,39 @@ void SpacebrewYun::monitor() {
251
251
}
252
252
253
253
if (_connected) {
254
+ // set flag to read data message name
254
255
if (c == char (MSG_START)) {
255
256
read_name = true ;
257
+
258
+ // set flag to read data message payload
256
259
} else if (c == char (MSG_DIV) || sub_name.length () > sub_name_max) {
257
260
read_name = false ;
258
261
read_msg = true ;
262
+
263
+ // set flag to read confirm message
264
+ } else if (c == char (MSG_CONFIRM)) {
265
+ read_confirm = true ;
266
+
267
+ // process data or confirm message, or reset message
259
268
} else if (c == char (MSG_END) || sub_msg.length () > sub_msg_str_max) {
260
269
if (read_msg == true ) {
261
- read_msg = false ;
262
270
onMessage ();
263
- // delay(2);
264
271
}
265
272
if (read_confirm == true ) {
266
- read_confirm = false ;
267
273
onConfirm ();
268
274
delay (2 );
269
275
}
270
- } else if (c == char (MSG_CONFIRM)) {
271
- read_confirm = true ;
276
+
277
+ read_confirm = false ;
278
+ read_msg = false ;
279
+ sub_name = " " ;
280
+ sub_msg = " " ;
281
+ sub_type = " " ;
282
+
283
+ // send a message received confirmation
284
+ Console.print (char (7 ));
285
+
286
+ // read message body
272
287
} else {
273
288
if (read_name == true ) {
274
289
sub_name += c;
@@ -300,6 +315,8 @@ void SpacebrewYun::monitor() {
300
315
}
301
316
}
302
317
318
+ // Serial.println(F(" - END monitor"));
319
+
303
320
}
304
321
305
322
void SpacebrewYun::onConfirm () {
@@ -313,10 +330,6 @@ void SpacebrewYun::onConfirm() {
313
330
curr = curr->next ;
314
331
}
315
332
}
316
-
317
- sub_name = " " ;
318
- sub_msg = " " ;
319
- sub_type = " " ;
320
333
}
321
334
322
335
boolean SpacebrewYun::connected () {
@@ -328,45 +341,50 @@ void SpacebrewYun::verbose(boolean verbose = true) {
328
341
}
329
342
330
343
void SpacebrewYun::onMessage () {
331
- if (subscribers != NULL ) {
344
+ Serial.print (F (" onMessage: name " ));
345
+ Serial.print (sub_name);
346
+ Serial.print (F (" , value " ));
347
+ Serial.print (sub_msg);
348
+
349
+ if (subscribers != NULL && sub_name.equals (" " ) == false ) {
332
350
struct Subscriber *curr = subscribers;
333
- while ((curr != NULL ) && (sub_type == " " )){
351
+ while ((curr != NULL ) && (sub_type. equals ( " " ) == true )){
334
352
if (sub_name.equals (curr->name ) == true ) {
335
353
sub_type = curr->type ;
336
354
}
337
355
curr = curr->next ;
338
356
}
339
357
}
340
358
359
+ Serial.print (F (" , type " ));
360
+ Serial.println (sub_type);
361
+
341
362
if ( sub_type.equals (" range" ) ) {
342
363
if (_onRangeMessage != NULL ) {
343
364
_onRangeMessage ( sub_name, int (sub_msg.toInt ()) );
344
365
} else {
345
- Serial.println (F (" ERROR :: Range message received , no callback method is registered " ));
366
+ Serial.println (F (" ERROR :: Range message, no callback" ));
346
367
}
347
368
} else if ( sub_type.equals (" boolean" ) ) {
348
369
if (_onBooleanMessage != NULL ) {
349
370
_onBooleanMessage ( sub_name, ( sub_msg.equals (" false" ) ? false : true ) );
350
371
} else {
351
- Serial.println (F (" ERROR :: Boolean message received , no callback method is registered " ));
372
+ Serial.println (F (" ERROR :: Boolean message, no callback" ));
352
373
}
353
374
} else if ( sub_type.equals (" string" ) ) {
354
375
if (_onStringMessage != NULL ) {
355
376
_onStringMessage ( sub_name, sub_msg );
356
377
} else {
357
- Serial.println (F (" ERROR :: String message received , no callback method is registered " ));
378
+ Serial.println (F (" ERROR :: String message, no callback" ));
358
379
}
359
- } else {
380
+ } else if ( sub_type. equals ( " custom " ) ) {
360
381
if (_onCustomMessage != NULL ) {
361
382
_onCustomMessage ( sub_name, sub_msg, sub_type );
362
383
} else {
363
- Serial.println (F (" ERROR :: Custom message received , no callback method is registered " ));
384
+ Serial.println (F (" ERROR :: Custom message, no callback" ));
364
385
}
365
386
}
366
387
367
- sub_name = " " ;
368
- sub_msg = " " ;
369
- sub_type = " " ;
370
388
}
371
389
372
390
@@ -414,7 +432,7 @@ void SpacebrewYun::getPids() {
414
432
pids.run ();
415
433
416
434
if (_verbose) {
417
- Serial.println (F (" Checking if spacebrew process already running" ));
435
+ Serial.println (F (" Checking if spacebrew running" ));
418
436
}
419
437
420
438
int sbPidsIndex = 0 ;
@@ -455,7 +473,7 @@ void SpacebrewYun::killPids() {
455
473
char * newPID = itoa (sbPids[i], pid, 10 );
456
474
457
475
if (_verbose) {
458
- Serial.print (F (" Stopping existing spacebrew processes with pids : " ));
476
+ Serial.print (F (" Stopping existing processes: " ));
459
477
Serial.println (newPID);
460
478
}
461
479
0 commit comments