11//
22// Allow2Unity
3+ // Allow2.cs
34//
4- // Created by Andrew Longhorn in early 2019.
5+ // Created by Andrew Longhorn in Jan 2019.
56// Copyright © 2019 Allow2 Pty Ltd. All rights reserved.
67//
78// LICENSE:
89// See LICENSE file in root directory
910//
1011
11- using Application ;
12+ using System ;
1213using System . Collections ;
14+ using System . Collections . Generic ;
1315using UnityEngine ;
1416using UnityEngine . Networking ;
1517
@@ -21,24 +23,19 @@ namespace Allow2
2123 /// </summary>
2224 public static class Allow2
2325 {
24- static string uuid ;
25- static string _deviceToken = "Not Set" ; // ie: "iug893-kjg-fiug23" - not persisted: always set this on start
26+ private static readonly string uuid ;
27+ private static string _deviceToken = "Not Set" ; // ie: "iug893-kjg-fiug23" - not persisted: always set this on start
2628 public static EnvType env = EnvType . Production ;
2729
2830 //
2931 // relevant persistence items
3032 //
3133 static int userId ; // ie: 27634
34+ static int _childId ; // ie: 34
3235 static string pairToken ; // ie: "98hbieg87-ilulieugil-dilufkucy"
33- static string _timezone ; // ie: "Australia/Brisbane"
36+ static string _timezone ; // ie: "Australia/Brisbane"
3437
35- //
36- // cannot instantiate this class
37- //
38- //protected Allow2() {
39- //}
40-
41- public static string apiUrl
38+ public static string ApiUrl
4239 {
4340 get
4441 {
@@ -54,7 +51,7 @@ public static string apiUrl
5451 }
5552 }
5653
57- public static string serviceUrl
54+ public static string ServiceUrl
5855 {
5956 get
6057 {
@@ -70,7 +67,17 @@ public static string serviceUrl
7067 }
7168 }
7269
73- public static string timezone
70+ public static resultClosure checkResultHandler ;
71+
72+ public static bool IsPaired
73+ {
74+ get
75+ {
76+ return ( userId > 0 ) && ( pairToken != null ) ;
77+ }
78+ }
79+
80+ public static string Timezone
7481 {
7582 get
7683 {
@@ -83,7 +90,7 @@ public static string timezone
8390 }
8491 }
8592
86- public static string deviceToken
93+ public static string DeviceToken
8794 {
8895 get
8996 {
@@ -93,44 +100,51 @@ public static string deviceToken
93100 {
94101 _deviceToken = value ;
95102 PlayerPrefs . SetString ( "deviceToken" , _deviceToken ) ;
96- if ( ( userId < 1 ) || ( pairToken == null ) ) {
103+ if ( ! IsPaired )
104+ {
97105 // todo: start a regular timer to keep trying to call home on startup until we confirm we are NOT paired.
98106 CheckForBrokenPairing ( ) ;
99107 }
100108 }
101109 }
102110
103- private static void persist ( ) {
111+ private static void Persist ( )
112+ {
104113 // todo: write to protected namespace storage?
105114 PlayerPrefs . SetInt ( "userId" , userId ) ;
106115 }
107116
117+ // no persistence here
118+ private static Dictionary < string , Allow2CheckResult > resultCache = new Dictionary < string , Allow2CheckResult > ( ) ;
119+
108120 public delegate void resultClosure ( string err , Allow2CheckResult result ) ;
109121
110122 static Allow2 ( )
111123 {
112124 uuid = SystemInfo . deviceUniqueIdentifier ;
113- if ( uuid == SystemInfo . unsupportedIdentifier ) {
114- // cannot use on this platform, kludge is to generate and store one, use auditing on the server side to detect disconnection
125+ if ( uuid == SystemInfo . unsupportedIdentifier )
126+ {
127+ // cannot use on this platform, kludge is to generate and store one, use auditing on the server side to detect disconnection in any case
115128 uuid = PlayerPrefs . GetString ( "uuid" ) ;
116- if ( uuid == null ) {
117-
129+ if ( uuid == null )
130+ {
131+ uuid = System . Guid . NewGuid ( ) . ToString ( ) ;
118132 PlayerPrefs . SetString ( "uuid" , uuid ) ;
119133 }
120134 }
121135 userId = PlayerPrefs . GetInt ( "userId" ) ;
122136 pairToken = PlayerPrefs . GetString ( "pairToken" ) ;
123- timezone = PlayerPrefs . GetString ( "timezone" ) ;
137+ _timezone = PlayerPrefs . GetString ( "timezone" ) ;
124138 }
125139
126140 static IEnumerator CheckForBrokenPairing ( )
127141 {
128- // here we just ask the question, was this unique ID paired and somehow it lost it's pairing?
142+ // here we just ask the question, was our unique ID paired and somehow it lost it's pairing?
129143 WWWForm form = new WWWForm ( ) ;
130144 form . AddField ( "uuid" , uuid ) ;
131145 form . AddField ( "deviceToken" , _deviceToken ) ;
132146
133- using ( UnityWebRequest www = UnityWebRequest . Post ( apiUrl + "/api/isDevicePaired" , form ) )
147+ using ( UnityWebRequest www = UnityWebRequest . Post ( ApiUrl + "/api/isDevicePaired" , form ) )
134148 {
135149 yield return www . SendWebRequest ( ) ;
136150
@@ -148,11 +162,10 @@ static IEnumerator CheckForBrokenPairing()
148162 }
149163 }
150164
151- //void Awake()
152- //{
153- // DontDestroyOnLoad(this);
154- //}
155-
165+ /*
166+ *
167+ *
168+ */
156169 public static IEnumerator Pair ( string user , // ie: "fred@gmail.com",
157170 string pass , // ie: "my super secret password",
158171 string deviceName , // ie: "Fred's iPhone"
@@ -164,16 +177,17 @@ resultClosure callback
164177 form . AddField ( "pass" , pass ) ;
165178 form . AddField ( "deviceToken" , _deviceToken ) ;
166179 form . AddField ( "name" , deviceName ) ;
180+ form . AddField ( "uuid" , uuid ) ;
167181
168- Debug . Log ( apiUrl + "/api/pairDevice" ) ;
182+ Debug . Log ( ApiUrl + "/api/pairDevice" ) ;
169183
170- using ( UnityWebRequest www = UnityWebRequest . Post ( apiUrl + "/api/pairDevice" , form ) )
184+ using ( UnityWebRequest www = UnityWebRequest . Post ( ApiUrl + "/api/pairDevice" , form ) )
171185 {
172186 yield return www . SendWebRequest ( ) ;
173187
174188 if ( www . isNetworkError || www . isHttpError )
175189 {
176- Debug . Log ( www . error ) ;
190+ Debug . Log ( www . error . ToString ( ) ) ;
177191 callback ( www . error , null ) ;
178192 }
179193 else
@@ -182,40 +196,62 @@ resultClosure callback
182196 var response = Allow2_SimpleJSON . JSON . Parse ( www . downloadHandler . text ) ;
183197 // extract
184198
185- persist ( ) ;
199+ Persist ( ) ;
186200
187201 // return
188202 callback ( null , null ) ;
189203 }
190204 }
191205 }
192206
193- public static string getQR ( )
207+ public static string GetQR ( string name )
194208 {
195- return "https://api.allow2.com /genqr/" +
209+ return ApiUrl + " /genqr/" +
196210 WWW . EscapeURL ( _deviceToken ) + "/" +
197211 WWW . EscapeURL ( uuid ) + "/" +
198212 WWW . EscapeURL ( name ) ;
199213 }
200214
201- public static IEnumerator Check ( int childId ,
215+ public static IEnumerator Check ( int childId , // childId == 0 ? Get Updated Child List and confirm Pairing
202216 int [ ] activities ,
203217 resultClosure callback ,
204218 bool log = false
205219 )
206220 {
207- if ( ( userId < 1 ) || ( pairToken == null ) ) {
221+ if ( ! IsPaired )
222+ {
208223 callback ( Allow2Error . NotPaired , null ) ;
209224 yield break ;
210225 }
211226
212227 WWWForm form = new WWWForm ( ) ;
213- form . AddField ( "user " , user ) ;
214- form . AddField ( "pass " , pass ) ;
228+ form . AddField ( "userId " , userId ) ;
229+ form . AddField ( "pairToken " , pairToken ) ;
215230 form . AddField ( "deviceToken" , _deviceToken ) ;
216- form . AddField ( "name" , deviceName ) ;
231+ form . AddField ( "tz" , _timezone ) ;
232+ //form.AddField("activities", activities);
233+ form . AddField ( "log" , log ? 1 : 0 ) ;
234+ if ( childId > 0 )
235+ {
236+ form . AddField ( "childId" , childId ) ;
237+ }
238+
239+ // check the cache first
240+ string key = form . ToString ( ) ;
241+ if ( resultCache . ContainsKey ( key ) ) {
242+ Allow2CheckResult checkResult = resultCache [ key ] ;
243+
244+ if ( checkResult . Expires . CompareTo ( new DateTime ( ) ) < 0 ) {
245+ // not expired yet, use cached value
246+ callback ( null , checkResult ) ;
247+ yield break ;
248+ }
249+
250+ // clear cached value and ask the server again
251+ resultCache . Remove ( key ) ;
252+ }
217253
218- UnityWebRequest www = UnityWebRequest . Get ( apiUrl + "/api/pairDevice" ) ;
254+ UnityWebRequest www = UnityWebRequest . Get ( ApiUrl + "/api/pairDevice" ) ;
219255 yield return www . SendWebRequest ( ) ;
220256
221257 if ( www . isNetworkError || www . isHttpError )
@@ -235,7 +271,7 @@ public static IEnumerator Check(int childId,
235271 // special case, no longer controlled
236272 userId = 0 ;
237273 pairToken = null ;
238- persist ( ) ;
274+ Persist ( ) ;
239275 //childId = 0;
240276 //_children = []
241277 //_dayTypes = []
@@ -250,7 +286,8 @@ public static IEnumerator Check(int childId,
250286 yield break ;
251287 }
252288
253- if ( json [ "allowed" ] == null ) {
289+ if ( json [ "allowed" ] == null )
290+ {
254291 callback ( Allow2Error . InvalidResponse , null ) ;
255292 yield break ;
256293 }
@@ -264,8 +301,42 @@ public static IEnumerator Check(int childId,
264301 response . Add ( "allDayTypes" , _dayTypes ) ;
265302 var _children = json [ "allDayTypes" ] ;
266303 response . Add ( "children" , _children ) ;
304+
305+ // cache the response
306+ resultCache [ key ] = response ;
267307 callback ( null , response ) ;
268308 }
269309 }
310+
311+ public static IEnumerator Request (
312+ int dayTypeId ,
313+ int [ ] lift ,
314+ string message ,
315+ resultClosure callback )
316+ {
317+ if ( ! IsPaired )
318+ {
319+ callback ( Allow2Error . NotPaired , null ) ;
320+ yield break ;
321+ }
322+ if ( _childId < 1 )
323+ {
324+ callback ( Allow2Error . MissingChildId , null ) ;
325+ yield break ;
326+ }
327+
328+ WWWForm form = new WWWForm ( ) ;
329+ form . AddField ( "userId" , userId ) ;
330+ form . AddField ( "pairToken" , pairToken ) ;
331+ form . AddField ( "deviceToken" , _deviceToken ) ;
332+ form . AddField ( "childId" , _childId ) ;
333+ //form.AddField("lift", lift.asJson);
334+ //if (dayTypeId != nil) {
335+ // body["dayType"] = JSON(dayTypeId!)
336+ // body["changeDayType"] = true
337+ //}
338+
339+
340+ }
270341 }
271- }
342+ }
0 commit comments