@@ -41,7 +41,9 @@ public static class Allow2
4141 //HashSet<string> checkers = new HashSet<string>(); // contains uuids for running autocheckers (abort if your uuid is missing)
4242 static string checkerUuid = null ; // uuid for the current checker
4343 static IEnumerator checker = null ; // the current autochecker
44- static string pollUuid = null ;
44+ static Coroutine qrCall = null ;
45+ static DateTime qrDebounce = new DateTime ( 1970 , 1 , 1 , 0 , 0 , 0 , 0 , System . DateTimeKind . Utc ) ;
46+ static string pairingUuid = null ;
4547
4648 public static int childId ; // ie: 34
4749
@@ -266,22 +268,34 @@ resultClosure callback
266268 }
267269 }
268270
271+ const int QRDebounceDelay = 500 ;
272+
269273 public static void GetQR ( MonoBehaviour behavior , string name , imageClosure callback ) {
270- behavior . StartCoroutine ( _GetQR ( name , callback ) ) ;
274+ DateTime now = new DateTime ( ) ;
275+ if ( ( qrCall != null ) && ( qrDebounce . CompareTo ( now ) > 0 ) ) {
276+ Debug . Log ( "debounce" ) ;
277+ Coroutine oldCall = qrCall ;
278+ qrCall = null ;
279+ behavior . StopCoroutine ( oldCall ) ;
280+ }
281+ qrDebounce = now . AddMilliseconds ( QRDebounceDelay ) ;
282+ qrCall = behavior . StartCoroutine ( _GetQR ( name , callback ) ) ;
271283 }
272284
273285 static IEnumerator _GetQR ( string name , imageClosure callback )
274286 {
287+ yield return new WaitForSeconds ( QRDebounceDelay / 1000 ) ;
275288 string qrURL = ApiUrl + "/genqr/" +
276- WWW . EscapeURL ( _deviceToken ) + "/" +
277- WWW . EscapeURL ( uuid ) + "/" +
278- WWW . EscapeURL ( name ) ;
289+ UnityWebRequest . EscapeURL ( _deviceToken ) + "/" +
290+ UnityWebRequest . EscapeURL ( uuid ) + "/" +
291+ UnityWebRequest . EscapeURL ( name ) ;
279292 UnityWebRequest www = UnityWebRequestTexture . GetTexture ( qrURL ) ;
280293 yield return www . SendWebRequest ( ) ;
281294
282295 if ( www . isNetworkError || www . isHttpError )
283296 {
284297 Debug . Log ( www . error ) ;
298+ Texture errorImage = Resources . Load ( "Allow2/QRError" ) as Texture2D ;
285299 callback ( www . error , null ) ;
286300 yield break ;
287301 }
@@ -317,7 +331,7 @@ public static void Check(MonoBehaviour behaviour,
317331 bool log = false
318332 )
319333 {
320- behaviour . StartCoroutine ( _Check ( childId , activities , callback , log , null ) ) ;
334+ behaviour . StartCoroutine ( _Check ( null , childId , activities , callback , log ) ) ;
321335 }
322336
323337 static IEnumerator _Check ( string myUuid ,
@@ -507,55 +521,71 @@ public static IEnumerator Request(
507521 yield break ;
508522 }
509523
510- WWWForm form = new WWWForm ( ) ;
511- form . AddField ( "userId" , userId ) ;
512- form . AddField ( "pairToken" , pairToken ) ;
513- form . AddField ( "deviceToken" , _deviceToken ) ;
514- form . AddField ( "childId" , childId ) ;
524+ WWWForm body = new WWWForm ( ) ;
525+ body . AddField ( "userId" , userId ) ;
526+ body . AddField ( "pairToken" , pairToken ) ;
527+ body . AddField ( "deviceToken" , _deviceToken ) ;
528+ body . AddField ( "childId" , childId ) ;
515529 //form.AddField("lift", lift.asJson);
516530 //if (dayTypeId != nil) {
517531 // body["dayType"] = JSON(dayTypeId!)
518532 // body["changeDayType"] = true
519533 //}
534+ string bodyStr = body . ToString ( ) ;
520535
536+ byte [ ] bytes = Encoding . UTF8 . GetBytes ( bodyStr ) ;
537+ using ( UnityWebRequest www = new UnityWebRequest ( ApiUrl + "/api/checkPairing" ) )
538+ {
539+ www . method = UnityWebRequest . kHttpVerbPOST ;
540+ www . uploadHandler = new UploadHandlerRaw ( bytes ) ;
541+ www . downloadHandler = new DownloadHandlerBuffer ( ) ;
542+ www . uploadHandler . contentType = "application/json" ;
543+ www . chunkedTransfer = false ;
544+ yield return www . SendWebRequest ( ) ;
545+
546+ // anything other than a 200 response is a "try again" as far as we are concerned
547+ if ( www . responseCode != 200 )
548+ {
549+ callback ( Allow2Error . NoConnection , null ) ; // let the caller know we are having problems
550+ yield break ;
551+ }
552+
553+ }
521554 }
522555
523- public static void StartPairing ( MonoBehaviour behavior ,
524- string deviceName , // ie: "Fred's iPhone"
525- resultClosure callback )
556+ public static void StartPairing ( MonoBehaviour behavior , resultClosure callback )
526557 {
527558 //switch checker
528- pollUuid = System . Guid . NewGuid ( ) . ToString ( ) ; // this will abort the current poll and kill it.
529- behavior . StartCoroutine ( PollLoop ( pollUuid , deviceName , callback ) ) ;
559+ pairingUuid = System . Guid . NewGuid ( ) . ToString ( ) ; // this will abort the current poll and kill it.
560+ behavior . StartCoroutine ( PairingLoop ( pairingUuid , callback ) ) ;
530561 }
531562
532- public static void StopPolling ( )
563+ public static void StopPairing ( )
533564 {
534- pollUuid = null ; // this will kill the running checker
565+ pairingUuid = null ; // this will kill the running checker
535566 }
536567
537- private static IEnumerator PollLoop ( string myUuid , string deviceName , resultClosure callback )
568+ private static IEnumerator PairingLoop ( string myUuid , resultClosure callback )
538569 {
539- while ( pollUuid == myUuid )
570+ while ( pairingUuid == myUuid )
540571 {
541572 Debug . Log ( "poll uuid: " + myUuid ) ;
542- yield return _Poll ( myUuid , deviceName , delegate ( string err , Allow2CheckResult result ) {
573+ yield return _PollPairing ( myUuid , delegate ( string err , Allow2CheckResult result ) {
543574 if ( IsPaired )
544575 {
545- pollUuid = null ; // stop the checker, we have been paired
576+ pairingUuid = null ; // stop the checker, we have been paired
546577 }
547578 callback ( err , result ) ;
548579 } ) ;
549580 yield return new WaitForSeconds ( 3 ) ;
550581 }
551582 }
552583
553- static IEnumerator _Poll ( string myUuid , string deviceName , resultClosure callback )
584+ static IEnumerator _PollPairing ( string myUuid , resultClosure callback )
554585 {
555586 JSONNode body = new JSONObject ( ) ;
556587 body . Add ( "uuid" , uuid ) ;
557588 body . Add ( "deviceToken" , _deviceToken ) ;
558- body . Add ( "deviceName" , deviceName ) ;
559589 string bodyStr = body . ToString ( ) ;
560590
561591 byte [ ] bytes = Encoding . UTF8 . GetBytes ( bodyStr ) ;
0 commit comments