@@ -22,11 +22,18 @@ namespace Allow2
2222 public static class Allow2
2323 {
2424
25- public static string deviceToken = "Not Set" ; // ie: "346-34269hcubi-187gigi8g-14i3ugkug",
25+ public static string deviceToken = "Not Set" ; // ie: "iug893-kjg-fiug23" - not persisted: always set this on start
2626 public static EnvType env = EnvType . Production ;
27-
28- //
29- // cannot instantiate this class
27+
28+ //
29+ // relevant persistence items
30+ //
31+ static int userId ; // ie: 27634
32+ static string pairToken ; // ie: "98hbieg87-ilulieugil-dilufkucy"
33+ static string timezone ; // ie: "Australia/Brisbane"
34+
35+ //
36+ // cannot instantiate this class
3037 //
3138 //protected Allow2() {
3239 //}
@@ -62,58 +69,111 @@ public static string serviceUrl
6269 }
6370 }
6471 }
65-
66- public static IEnumerator Pair ( string user , // ie: "fred@gmail.com",
67- string pass , // ie: "my super secret password",
68- string deviceName // ie: "Fred's iPhone"
69- ) {
70- WWWForm form = new WWWForm ( ) ;
71- form . AddField ( "user" , user ) ;
72- form . AddField ( "pass" , pass ) ;
73- form . AddField ( "deviceToken" , deviceToken ) ;
74- form . AddField ( "name" , deviceName ) ;
72+
73+ private static void persist ( ) {
74+ // todo: write to protected namespace storage?
75+ }
76+
77+ public delegate void resultClosure ( string err , Allow2CheckResult result ) ;
78+
79+ //void Awake()
80+ //{
81+ // DontDestroyOnLoad(this);
82+ //}
83+
84+ public static IEnumerator Pair ( string user , // ie: "fred@gmail.com",
85+ string pass , // ie: "my super secret password",
86+ string deviceName , // ie: "Fred's iPhone"
87+ resultClosure callback
88+ )
89+ {
90+ WWWForm form = new WWWForm ( ) ;
91+ form . AddField ( "user" , user ) ;
92+ form . AddField ( "pass" , pass ) ;
93+ form . AddField ( "deviceToken" , deviceToken ) ;
94+ form . AddField ( "name" , deviceName ) ;
7595
7696 Debug . Log ( apiUrl + "/api/pairDevice" ) ;
77- Debug . Log ( form ) ;
78-
79- using ( UnityWebRequest www = UnityWebRequest . Post ( apiUrl + "/api/pairDevice" , form ) )
80- {
81- yield return www . SendWebRequest ( ) ;
82-
83- if ( www . isNetworkError || www . isHttpError )
84- {
85- Debug . Log ( www . error ) ;
86- }
87- else
88- {
89- Debug . Log ( www . downloadHandler . text ) ;
90- }
91- }
92- }
93-
94- public static IEnumerator Check ( int userId ,
95- string pairToken , // ie: "98hbieg87-ilulieugil-dilufkucy"
96- string deviceToken , // ie: "iug893-kjg-fiug23"
97- string timezone , // ie: "Australia/Brisbane"
98- int childId ,
97+
98+ using ( UnityWebRequest www = UnityWebRequest . Post ( apiUrl + "/api/pairDevice" , form ) )
99+ {
100+ yield return www . SendWebRequest ( ) ;
101+
102+ if ( www . isNetworkError || www . isHttpError )
103+ {
104+ Debug . Log ( www . error ) ;
105+ callback ( www . error , null ) ;
106+ }
107+ else
108+ {
109+ Debug . Log ( www . downloadHandler . text ) ;
110+ var response = Allow2_SimpleJSON . JSON . Parse ( www . downloadHandler . text ) ;
111+ // extract
112+
113+ // persist
114+
115+ // return
116+ callback ( null , null ) ;
117+ }
118+ }
119+ }
120+
121+ public static IEnumerator Check ( int childId ,
99122 int [ ] activities ,
100- bool log = false ,
101- bool staging = false // INTERNAL USE ONLY!
102- ) {
123+ resultClosure callback ,
124+ bool log = false
125+ )
126+ {
103127 UnityWebRequest www = UnityWebRequest . Get ( "http://www.my-server.com" ) ;
104128 yield return www . SendWebRequest ( ) ;
105129
106130 if ( www . isNetworkError || www . isHttpError )
107131 {
108132 Debug . Log ( www . error ) ;
133+ callback ( www . error , null ) ;
109134 }
110135 else
111136 {
112- // Show results as text
113137 Debug . Log ( www . downloadHandler . text ) ;
138+ var json = Allow2_SimpleJSON . JSON . Parse ( www . downloadHandler . text ) ;
139+
140+ if ( ( json [ "error" ] == "invalid pairId" ) ||
141+ ( json [ "error" ] == "invalid pairToken" ) )
142+ {
143+ // todo: || (response?.statusCode == 401) {
144+ // special case, no longer controlled
145+ userId = 0 ;
146+ pairToken = null ;
147+ persist ( ) ;
148+ //childId = 0;
149+ //_children = []
150+ //_dayTypes = []
151+ var failOpen = new Allow2CheckResult ( ) ;
152+ failOpen . Add ( "subscription" , new Allow2_SimpleJSON . JSONArray ( ) ) ;
153+ failOpen . Add ( "allowed" , true ) ;
154+ failOpen . Add ( "activities" , new Allow2_SimpleJSON . JSONArray ( ) ) ;
155+ failOpen . Add ( "dayTypes" , new Allow2_SimpleJSON . JSONArray ( ) ) ;
156+ failOpen . Add ( "allDayTypes" , new Allow2_SimpleJSON . JSONArray ( ) ) ;
157+ failOpen . Add ( "children" , new Allow2_SimpleJSON . JSONArray ( ) ) ;
158+ callback ( null , failOpen ) ;
159+ yield break ;
160+ }
161+
162+ if ( json [ "allowed" ] == null ) {
163+ callback ( Allow2Error . InvalidResponse , null ) ;
164+ yield break ;
165+ }
114166
115- // Or retrieve results as binary data
116- byte [ ] results = www . downloadHandler . data ;
167+ var response = new Allow2CheckResult ( ) ;
168+ response . Add ( "activities" , json [ "activities" ] ) ;
169+ response . Add ( "subscription" , json [ "subscription" ] ) ;
170+ response . Add ( "dayTypes" , json [ "dayTypes" ] ) ;
171+ response . Add ( "children" , json [ "children" ] ) ;
172+ var _dayTypes = json [ "allDayTypes" ] ;
173+ response . Add ( "allDayTypes" , _dayTypes ) ;
174+ var _children = json [ "allDayTypes" ] ;
175+ response . Add ( "children" , _children ) ;
176+ callback ( null , response ) ;
117177 }
118178 }
119179 }
0 commit comments