@@ -36,6 +36,12 @@ public static class Allow2
3636 static string pairToken ; // ie: "98hbieg87-ilulieugil-dilufkucy"
3737 static string _timezone ; // ie: "Australia/Brisbane"
3838 static Dictionary < int , string > _children = new Dictionary < int , string > ( ) ;
39+ static int _childId = 0 ;
40+
41+ //HashSet<string> checkers = new HashSet<string>(); // contains uuids for running autocheckers (abort if your uuid is missing)
42+ static string checkerUuid = null ; // uuid for the current checker
43+ static IEnumerator checker = null ; // the current autochecker
44+ static string pollUuid = null ;
3945
4046 public static int childId ; // ie: 34
4147
@@ -179,10 +185,19 @@ static IEnumerator CheckForBrokenPairing()
179185 }
180186
181187 /*
182- *
188+ * Pairing
183189 *
184190 */
185- public static IEnumerator Pair ( string user , // ie: "fred@gmail.com",
191+ public static void Pair ( MonoBehaviour behavior ,
192+ string user , // ie: "fred@gmail.com",
193+ string pass , // ie: "my super secret password",
194+ string deviceName , // ie: "Fred's iPhone"
195+ resultClosure callback )
196+ {
197+ behavior . StartCoroutine ( _Pair ( user , pass , deviceName , callback ) ) ;
198+ }
199+
200+ static IEnumerator _Pair ( string user , // ie: "fred@gmail.com",
186201 string pass , // ie: "my super secret password",
187202 string deviceName , // ie: "Fred's iPhone"
188203 resultClosure callback
@@ -251,8 +266,11 @@ resultClosure callback
251266 }
252267 }
253268
254- public static IEnumerator GetQR ( string name ,
255- imageClosure callback )
269+ public static void GetQR ( MonoBehaviour behavior , string name , imageClosure callback ) {
270+ behavior . StartCoroutine ( _GetQR ( name , callback ) ) ;
271+ }
272+
273+ static IEnumerator _GetQR ( string name , imageClosure callback )
256274 {
257275 string qrURL = ApiUrl + "/genqr/" +
258276 WWW . EscapeURL ( _deviceToken ) + "/" +
@@ -292,11 +310,21 @@ private static Dictionary<int, string> ParseChildren(JSONNode json)
292310 return children ;
293311 }
294312
295- public static IEnumerator Check ( int childId , // childId == 0 ? Get Updated Child List and confirm Pairing
296- int [ ] activities ,
297- resultClosure callback ,
298- bool log = false
299- )
313+ public static void Check ( MonoBehaviour behaviour ,
314+ int childId , // childId == 0 ? Get Updated Child List and confirm Pairing
315+ int [ ] activities ,
316+ resultClosure callback ,
317+ bool log = false
318+ )
319+ {
320+ behaviour . StartCoroutine ( _Check ( childId , activities , callback , log , null ) ) ;
321+ }
322+
323+ static IEnumerator _Check ( string myUuid ,
324+ int childId , // childId == 0 ? Get Updated Child List and confirm Pairing
325+ int [ ] activities ,
326+ resultClosure callback ,
327+ bool log ) // if set, then this is an autochecker and we drop it if we replace it. If null, it's adhoc, always return a value
300328 {
301329 if ( ! IsPaired )
302330 {
@@ -355,6 +383,11 @@ public static IEnumerator Check(int childId, // childId == 0 ? Get Updated Ch
355383 www . chunkedTransfer = false ;
356384 yield return www . SendWebRequest ( ) ;
357385
386+ if ( ( myUuid != null ) && ( checkerUuid != myUuid ) ) {
387+ Debug . Log ( "drop response for check: " + myUuid ) ;
388+ yield break ; // this check is aborted, just drop the response and don't return;
389+ }
390+
358391 Debug . Log ( www . downloadHandler . text ) ;
359392 var json = Allow2_SimpleJSON . JSON . Parse ( www . downloadHandler . text ) ;
360393
@@ -403,8 +436,8 @@ public static IEnumerator Check(int childId, // childId == 0 ? Get Updated Ch
403436 var _dayTypes = json [ "allDayTypes" ] ;
404437 response . Add ( "allDayTypes" , _dayTypes ) ;
405438 var oldChildIds = _children . Keys ;
406- var children = json [ "allDayTypes " ] ;
407- _children = Children ;
439+ var children = json [ "children " ] ;
440+ _children = ParseChildren ( children ) ;
408441 response . Add ( "children" , children ) ;
409442
410443 if ( oldChildIds != _children . Keys )
@@ -418,18 +451,43 @@ public static IEnumerator Check(int childId, // childId == 0 ? Get Updated Ch
418451 }
419452 }
420453
421- public static IEnumerator StartChecking ( int childId ,
422- int [ ] activities ,
423- resultClosure callback ,
424- bool log = false
425- )
454+ private static IEnumerator CheckLoop ( string myUuid ,
455+ int childId ,
456+ int [ ] activities ,
457+ resultClosure callback ,
458+ bool log = false )
426459 {
427- return Check ( childId , activities , callback , log ) ;
460+ while ( checkerUuid == myUuid ) {
461+ Debug . Log ( "check uuid: " + myUuid ) ;
462+ yield return _Check ( myUuid , childId , activities , delegate ( string err , Allow2CheckResult result ) {
463+ if ( ! IsPaired && ( checkerUuid != myUuid ) ) {
464+ checkerUuid = null ; // stop the checker, we have been unpaired
465+ }
466+ callback ( err , result ) ;
467+ } , log ) ;
468+ yield return new WaitForSeconds ( 3 ) ;
469+ }
428470 }
429471
430- public static IEnumerator StopChecking ( )
472+ public static void StartChecking ( MonoBehaviour behavior ,
473+ int childId ,
474+ int [ ] activities ,
475+ resultClosure callback ,
476+ bool log )
431477 {
432- return null ;
478+ // change the parameters
479+ bool changed = ( _childId != childId ) ;
480+ _childId = childId ;
481+ if ( changed || ( checkerUuid == null ) ) {
482+ //switch checker
483+ checkerUuid = System . Guid . NewGuid ( ) . ToString ( ) ; // this will abort the current checker and kill it.
484+ /*checker = */ behavior . StartCoroutine ( CheckLoop ( checkerUuid , childId , activities , callback , log ) ) ;
485+ }
486+ }
487+
488+ public static void StopChecking ( )
489+ {
490+ checkerUuid = null ; // this will kill the running checker
433491 }
434492
435493 public static IEnumerator Request (
@@ -460,7 +518,81 @@ public static IEnumerator Request(
460518 // body["changeDayType"] = true
461519 //}
462520
521+ }
522+
523+ public static void StartPairing ( MonoBehaviour behavior ,
524+ string deviceName , // ie: "Fred's iPhone"
525+ resultClosure callback )
526+ {
527+ //switch checker
528+ pollUuid = System . Guid . NewGuid ( ) . ToString ( ) ; // this will abort the current poll and kill it.
529+ behavior . StartCoroutine ( PollLoop ( pollUuid , deviceName , callback ) ) ;
530+ }
531+
532+ public static void StopPolling ( )
533+ {
534+ pollUuid = null ; // this will kill the running checker
535+ }
536+
537+ private static IEnumerator PollLoop ( string myUuid , string deviceName , resultClosure callback )
538+ {
539+ while ( pollUuid == myUuid )
540+ {
541+ Debug . Log ( "poll uuid: " + myUuid ) ;
542+ yield return _Poll ( myUuid , deviceName , delegate ( string err , Allow2CheckResult result ) {
543+ if ( IsPaired )
544+ {
545+ pollUuid = null ; // stop the checker, we have been paired
546+ }
547+ callback ( err , result ) ;
548+ } ) ;
549+ yield return new WaitForSeconds ( 3 ) ;
550+ }
551+ }
552+
553+ static IEnumerator _Poll ( string myUuid , string deviceName , resultClosure callback )
554+ {
555+ JSONNode body = new JSONObject ( ) ;
556+ body . Add ( "uuid" , uuid ) ;
557+ body . Add ( "deviceToken" , _deviceToken ) ;
558+ body . Add ( "deviceName" , deviceName ) ;
559+ string bodyStr = body . ToString ( ) ;
560+
561+ byte [ ] bytes = Encoding . UTF8 . GetBytes ( bodyStr ) ;
562+ using ( UnityWebRequest www = new UnityWebRequest ( ApiUrl + "/api/checkPairing" ) )
563+ {
564+ www . method = UnityWebRequest . kHttpVerbPOST ;
565+ www . uploadHandler = new UploadHandlerRaw ( bytes ) ;
566+ www . downloadHandler = new DownloadHandlerBuffer ( ) ;
567+ www . uploadHandler . contentType = "application/json" ;
568+ www . chunkedTransfer = false ;
569+ yield return www . SendWebRequest ( ) ;
463570
571+ // anything other than a 200 response is a "try again" as far as we are concerned
572+ if ( www . responseCode != 200 )
573+ {
574+ callback ( Allow2Error . NoConnection , null ) ; // let the caller know we are having problems
575+ yield break ;
576+ }
577+
578+ Debug . Log ( www . downloadHandler . text ) ;
579+ var json = Allow2_SimpleJSON . JSON . Parse ( www . downloadHandler . text ) ;
580+
581+ string status = json [ "status" ] ;
582+
583+ if ( status != "success" )
584+ {
585+ callback ( json [ "message" ] ?? "Unknown Error" , null ) ;
586+ yield break ;
587+ }
588+
589+ pairToken = json [ "pairToken" ] ;
590+ userId = json [ "userId" ] ;
591+ childId = json [ "childId" ] ;
592+ _children = childId > 0 ? new Dictionary < int , string > ( ) : ParseChildren ( json [ "children" ] ) ;
593+
594+ callback ( null , null ) ;
595+ }
464596 }
465597 }
466598}
0 commit comments