Skip to content

Commit c91b296

Browse files
committed
add examples
add qrcode debounce upgrade project
1 parent c96d637 commit c91b296

6 files changed

Lines changed: 247 additions & 25 deletions

File tree

Allow2/Allow2.cs

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

Allow2/EnumeratedTypes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public enum EnvType {
2323
/// <summary>
2424
/// Activities: These are the current activities for Allow2.
2525
/// </summary>
26-
public enum Activity {
26+
public enum Activity: int {
2727
Internet = 1,
2828
Computer = 2,
2929
Gaming = 3,

Allow2/Examples/CheckButton.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// Copyright (C) 2019 Allow2
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
// the License.
6+
//
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
12+
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
//
14+
// See the License for the specific language governing permissions and limitations under the License.
15+
//
16+
namespace Allow2.Allow2Examples
17+
{
18+
19+
using UnityEngine;
20+
21+
public class CheckButton : MonoBehaviour
22+
{
23+
24+
public int childId = 68;
25+
public int[] activities = {
26+
(int)Activity.Internet,
27+
(int)Activity.Computer
28+
};
29+
30+
public void Check()
31+
{
32+
Debug.Log("Check");
33+
Allow2.Check(
34+
this,
35+
childId,
36+
activities,
37+
delegate (string err, Allow2CheckResult result)
38+
{
39+
Debug.Log("Check Error" + err);
40+
Debug.Log("Paired: " + Allow2.IsPaired);
41+
if (result != null)
42+
{
43+
Debug.Log("Allowed: " + result.IsAllowed);
44+
if (!result.IsAllowed)
45+
{
46+
Debug.Log(result.Explanation);
47+
}
48+
}
49+
},
50+
true);
51+
}
52+
53+
}
54+
}

Allow2/Examples/DeviceNameInput.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// Copyright (C) 2019 Allow2
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
// the License.
6+
//
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
12+
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
//
14+
// See the License for the specific language governing permissions and limitations under the License.
15+
//
16+
namespace Allow2.Allow2Examples
17+
{
18+
19+
using UnityEngine;
20+
using UnityEngine.UI;
21+
22+
public class DeviceNameInput : MonoBehaviour
23+
{
24+
25+
public InputField inputField;
26+
27+
void Awake()
28+
{
29+
inputField.text = SystemInfo.deviceName;
30+
}
31+
32+
public void InputValueChanged(string input)
33+
{
34+
Allow2.GetQR(this, input, delegate (string err, Texture qrCode)
35+
{
36+
Debug.Log("qrcode error: " + (err ?? "No Error") + " : " + (qrCode ? "yes" : "no"));
37+
GameObject qrImage = GameObject.Find("qrcode");
38+
Debug.Log("qrImage error: " + (qrImage ? "yes" : "no"));
39+
qrImage.GetComponent<RawImage>().texture = qrCode;
40+
});
41+
}
42+
}
43+
}

Allow2/Examples/PairButton.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// Copyright (C) 2019 Allow2
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
// the License.
6+
//
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
12+
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
//
14+
// See the License for the specific language governing permissions and limitations under the License.
15+
//
16+
namespace Allow2.Allow2Examples
17+
{
18+
19+
using UnityEngine;
20+
using UnityEngine.UI;
21+
22+
public class PairButton : MonoBehaviour
23+
{
24+
public void Pair()
25+
{
26+
Debug.Log("Start Pairing");
27+
Allow2.Pair(
28+
this,
29+
"andrew@imagineit.com.au",
30+
"2}LJA84$3y88H]o",
31+
"Unity Test",
32+
delegate (string err, Allow2CheckResult result)
33+
{
34+
Debug.Log("Stop Pairing");
35+
Debug.Log("Pairing Error" + err);
36+
if (result)
37+
{
38+
Debug.Log("Pairing Result" + result.ToString());
39+
}
40+
}
41+
);
42+
}
43+
}
44+
}

Allow2/Examples/SceneClass.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// Copyright (C) 2019 Allow2
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
// the License.
6+
//
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
12+
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
//
14+
// See the License for the specific language governing permissions and limitations under the License.
15+
//
16+
namespace Allow2.Allow2Examples
17+
{
18+
19+
using UnityEngine;
20+
using UnityEngine.UI;
21+
22+
public class SceneClass : MonoBehaviour
23+
{
24+
void Awake()
25+
{
26+
// staging is only really for Allow2 internal development, omit this line in your code
27+
Allow2.env = EnvType.Staging;
28+
29+
// set the deviceToken
30+
// this needs top be done before ANY calls to the Allow2 platform
31+
// create your own deviceToken at https://developer.allow2.com for free
32+
// use it to manage your app/game/device and track metrics
33+
// it's also important to use your own deviceToken and app definition in order to make use of
34+
// the additional marketing channel opportunities that Allow2 provides for free
35+
Allow2.DeviceToken = "B0hNax6VCFi9vphu";
36+
37+
// We can also now check if the device/app/game is already paired and what children are in the account
38+
Debug.Log("isPaired: " + Allow2.IsPaired);
39+
Debug.Log("Children: " + Allow2.Children);
40+
41+
// and in the pairing interface, we need a QR code to make the process simple for our users
42+
Allow2.GetQR(this, SystemInfo.deviceName, delegate (string err, Texture qrCode)
43+
{
44+
Debug.Log("qrcode error: " + (err ?? "No Error") + " : " + (qrCode ? "yes" : "no"));
45+
GameObject qrImage = GameObject.Find("qrcode");
46+
Debug.Log("qrImage error: " + (qrImage ? "yes" : "no"));
47+
qrImage.GetComponent<RawImage>().texture = qrCode;
48+
});
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)