Skip to content

Commit c96d637

Browse files
committed
add qrcode pair polling
1 parent 247345b commit c96d637

5 files changed

Lines changed: 203 additions & 22 deletions

File tree

Allow2/Allow2.cs

Lines changed: 151 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

Allow2/Allow2Ban.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class Allow2Ban
2222
public int Duration { get; private set; }
2323
public bool Selected { get; private set; }
2424

25-
Allow2Ban(string name, JSONNode val)
25+
public Allow2Ban(string name, JSONNode val)
2626
{
2727
Id = val["id"].AsInt;
2828
Title = name;

Allow2/Allow2CheckResult.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public Allow2Ban[] CurrentBans {
171171
JSONArray items = activity["bans"]["bans"].AsArray;
172172
foreach (JSONNode item in items)
173173
{
174-
//bans.Add(new Allow2Ban(name, item));
174+
bans.Add(new Allow2Ban(name, item));
175175
}
176176
}
177177
else
@@ -183,6 +183,37 @@ public Allow2Ban[] CurrentBans {
183183
return bans.ToArray();
184184
}
185185
}
186+
187+
public Allow2Day Today
188+
{
189+
get
190+
{
191+
if (this["dayTypes"] == null) { return null; }
192+
return Allow2Day.DayOrNull(this["dayTypes"]["today"]);
193+
}
194+
}
195+
196+
public Allow2Day Tomorrow
197+
{
198+
get
199+
{
200+
if (this["dayTypes"] == null) { return null; }
201+
return Allow2Day.DayOrNull(this["dayTypes"]["tomorrow"]);
202+
}
203+
}
204+
205+
public Allow2Day[] AllDayTypes
206+
{
207+
get
208+
{
209+
List<Allow2Day> dayTypes = new List<Allow2Day>();
210+
foreach (JSONNode dayType in this["allDayTypes"])
211+
{
212+
dayTypes.Add(new Allow2Day(dayType));
213+
}
214+
return dayTypes.ToArray();
215+
}
216+
}
186217
}
187218

188219
}

Allow2/Allow2Day.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,34 @@
99
// See LICENSE file in root directory
1010
//
1111

12+
using Allow2_SimpleJSON;
13+
1214
namespace Allow2
1315
{
1416
public class Allow2Day // shortcut implementation for now
1517
{
1618
public int Id { get; private set; }
1719
public string Name { get; private set; }
1820

19-
Allow2Day(int id, string name)
21+
public Allow2Day(int id, string name)
2022
{
2123
Id = id;
2224
Name = name;
2325
}
26+
27+
public Allow2Day(JSONNode json)
28+
{
29+
Id = json["id"].AsInt;
30+
Name = json["name"];
31+
}
32+
33+
public static Allow2Day DayOrNull(JSONNode json)
34+
{
35+
if (json == null) {
36+
return null;
37+
}
38+
39+
return new Allow2Day(json["id"].AsInt, json["name"]);
40+
}
2441
}
2542
}

Allow2/EnumeratedTypes.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public static class Allow2Error
4343
public const string MissingChildId = "MissingChildId";
4444
public const string NotAuthorised = "NotAuthorised";
4545
public const string InvalidResponse = "InvalidResponse";
46+
public const string NoConnection = "NoConnection";
4647
}
4748

4849
}

0 commit comments

Comments
 (0)