@@ -127,7 +127,7 @@ void SpacebrewYun::addSubscribe(const String& name, const String& type) {
127127}
128128
129129void SpacebrewYun::connect (String _server, int _port) {
130- Serial.print (F (" NEW LIB " ));
130+ Serial.println (F (" v2.3 " ));
131131 _started = true ;
132132 server = _server;
133133 port = _port;
@@ -214,9 +214,9 @@ void SpacebrewYun::monitor() {
214214
215215 if (c == char (CONNECTION_START) && _started && !_connected) {
216216 if (_verbose) {
217- Serial.print (F (" Connected to spacebrew server at : " ));
217+ Serial.print (F (" Connected to: " ));
218218 Serial.println (server);
219- Serial.print (F (" Application name set to : " ));
219+ Serial.print (F (" Application name: " ));
220220 Serial.println (name);
221221 }
222222 if (_onOpen != NULL ){
@@ -228,7 +228,7 @@ void SpacebrewYun::monitor() {
228228 else if (c == char (CONNECTION_END) && _connected) {
229229 _connected = false ;
230230 if (_verbose) {
231- Serial.print (F (" Disconnected from spacebrew server at : " ));
231+ Serial.print (F (" Disconnected from: " ));
232232 Serial.println (server);
233233 }
234234 if (_onClose != NULL ){
@@ -239,7 +239,7 @@ void SpacebrewYun::monitor() {
239239 if (_verbose) {
240240 if (c == char (CONNECTION_ERROR)) {
241241 _error_msg = true ;
242- Serial.println (F (" ERROR :: with Spacebrew.py Connection ::" ));
242+ Serial.println (F (" ERROR :: Spacebrew.py ::" ));
243243 }
244244 else if (_error_msg && c == char (MSG_END)) {
245245 _error_msg = false ;
@@ -251,24 +251,39 @@ void SpacebrewYun::monitor() {
251251 }
252252
253253 if (_connected) {
254+ // set flag to read data message name
254255 if (c == char (MSG_START)) {
255256 read_name = true ;
257+
258+ // set flag to read data message payload
256259 } else if (c == char (MSG_DIV) || sub_name.length () > sub_name_max) {
257260 read_name = false ;
258261 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
259268 } else if (c == char (MSG_END) || sub_msg.length () > sub_msg_str_max) {
260269 if (read_msg == true ) {
261- read_msg = false ;
262270 onMessage ();
263- // delay(2);
264271 }
265272 if (read_confirm == true ) {
266- read_confirm = false ;
267273 onConfirm ();
268274 delay (2 );
269275 }
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
272287 } else {
273288 if (read_name == true ) {
274289 sub_name += c;
@@ -300,6 +315,8 @@ void SpacebrewYun::monitor() {
300315 }
301316 }
302317
318+ // Serial.println(F(" - END monitor"));
319+
303320}
304321
305322void SpacebrewYun::onConfirm () {
@@ -313,10 +330,6 @@ void SpacebrewYun::onConfirm() {
313330 curr = curr->next ;
314331 }
315332 }
316-
317- sub_name = " " ;
318- sub_msg = " " ;
319- sub_type = " " ;
320333}
321334
322335boolean SpacebrewYun::connected () {
@@ -328,45 +341,50 @@ void SpacebrewYun::verbose(boolean verbose = true) {
328341}
329342
330343void 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 ) {
332350 struct Subscriber *curr = subscribers;
333- while ((curr != NULL ) && (sub_type == " " )){
351+ while ((curr != NULL ) && (sub_type. equals ( " " ) == true )){
334352 if (sub_name.equals (curr->name ) == true ) {
335353 sub_type = curr->type ;
336354 }
337355 curr = curr->next ;
338356 }
339357 }
340358
359+ Serial.print (F (" , type " ));
360+ Serial.println (sub_type);
361+
341362 if ( sub_type.equals (" range" ) ) {
342363 if (_onRangeMessage != NULL ) {
343364 _onRangeMessage ( sub_name, int (sub_msg.toInt ()) );
344365 } else {
345- Serial.println (F (" ERROR :: Range message received , no callback method is registered " ));
366+ Serial.println (F (" ERROR :: Range message, no callback" ));
346367 }
347368 } else if ( sub_type.equals (" boolean" ) ) {
348369 if (_onBooleanMessage != NULL ) {
349370 _onBooleanMessage ( sub_name, ( sub_msg.equals (" false" ) ? false : true ) );
350371 } else {
351- Serial.println (F (" ERROR :: Boolean message received , no callback method is registered " ));
372+ Serial.println (F (" ERROR :: Boolean message, no callback" ));
352373 }
353374 } else if ( sub_type.equals (" string" ) ) {
354375 if (_onStringMessage != NULL ) {
355376 _onStringMessage ( sub_name, sub_msg );
356377 } else {
357- Serial.println (F (" ERROR :: String message received , no callback method is registered " ));
378+ Serial.println (F (" ERROR :: String message, no callback" ));
358379 }
359- } else {
380+ } else if ( sub_type. equals ( " custom " ) ) {
360381 if (_onCustomMessage != NULL ) {
361382 _onCustomMessage ( sub_name, sub_msg, sub_type );
362383 } else {
363- Serial.println (F (" ERROR :: Custom message received , no callback method is registered " ));
384+ Serial.println (F (" ERROR :: Custom message, no callback" ));
364385 }
365386 }
366387
367- sub_name = " " ;
368- sub_msg = " " ;
369- sub_type = " " ;
370388}
371389
372390
@@ -414,7 +432,7 @@ void SpacebrewYun::getPids() {
414432 pids.run ();
415433
416434 if (_verbose) {
417- Serial.println (F (" Checking if spacebrew process already running" ));
435+ Serial.println (F (" Checking if spacebrew running" ));
418436 }
419437
420438 int sbPidsIndex = 0 ;
@@ -455,7 +473,7 @@ void SpacebrewYun::killPids() {
455473 char * newPID = itoa (sbPids[i], pid, 10 );
456474
457475 if (_verbose) {
458- Serial.print (F (" Stopping existing spacebrew processes with pids : " ));
476+ Serial.print (F (" Stopping existing processes: " ));
459477 Serial.println (newPID);
460478 }
461479
0 commit comments