Skip to content

Commit dfcf5ff

Browse files
committed
imp[lementing return values
1 parent 581fa83 commit dfcf5ff

4 files changed

Lines changed: 1490 additions & 42 deletions

File tree

Allow2/Allow2.cs

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

Allow2/Allow2CheckResult.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
namespace Allow2
3+
{
4+
public class Allow2CheckResult : Allow2_SimpleJSON.JSONObject // shortcut implementation for now
5+
{
6+
//public Allow2CheckResult()
7+
//{
8+
//}
9+
}
10+
}

Allow2/EnumeratedTypes.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,15 @@ public enum Activity {
2121
Social = 9,
2222
PhoneTime = 10
2323
}
24+
25+
public static class Allow2Error
26+
{
27+
public const string NotPaired = "NotPaired";
28+
public const string AlreadyPaired = "AlreadyPaired";
29+
public const string MissingChildId = "MissingChildId";
30+
public const string NotAuthorised = "NotAuthorised";
31+
public const string InvalidResponse = "InvalidResponse";
32+
}
33+
}
34+
2435
}

0 commit comments

Comments
 (0)