Skip to content

Commit 20bb6eb

Browse files
committed
adding qrcode functions and persistence
1 parent dfcf5ff commit 20bb6eb

1 file changed

Lines changed: 97 additions & 6 deletions

File tree

Allow2/Allow2.cs

Lines changed: 97 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ namespace Allow2
2121
/// </summary>
2222
public static class Allow2
2323
{
24-
25-
public static string deviceToken = "Not Set"; // ie: "iug893-kjg-fiug23" - not persisted: always set this on start
24+
static string uuid;
25+
static string _deviceToken = "Not Set"; // ie: "iug893-kjg-fiug23" - not persisted: always set this on start
2626
public static EnvType env = EnvType.Production;
2727

2828
//
2929
// relevant persistence items
3030
//
3131
static int userId; // ie: 27634
3232
static string pairToken; // ie: "98hbieg87-ilulieugil-dilufkucy"
33-
static string timezone; // ie: "Australia/Brisbane"
33+
static string _timezone; // ie: "Australia/Brisbane"
3434

3535
//
3636
// cannot instantiate this class
@@ -70,12 +70,84 @@ public static string serviceUrl
7070
}
7171
}
7272

73+
public static string timezone
74+
{
75+
get
76+
{
77+
return _timezone;
78+
}
79+
set
80+
{
81+
_timezone = value;
82+
PlayerPrefs.SetString("timezone", _timezone);
83+
}
84+
}
85+
86+
public static string deviceToken
87+
{
88+
get
89+
{
90+
return _deviceToken;
91+
}
92+
set
93+
{
94+
_deviceToken = value;
95+
PlayerPrefs.SetString("deviceToken", _deviceToken);
96+
if ((userId < 1) || (pairToken == null)) {
97+
// todo: start a regular timer to keep trying to call home on startup until we confirm we are NOT paired.
98+
CheckForBrokenPairing();
99+
}
100+
}
101+
}
102+
73103
private static void persist() {
74104
// todo: write to protected namespace storage?
105+
PlayerPrefs.SetInt("userId", userId);
75106
}
76107

77108
public delegate void resultClosure(string err, Allow2CheckResult result);
78109

110+
static Allow2()
111+
{
112+
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
115+
uuid = PlayerPrefs.GetString("uuid");
116+
if (uuid == null) {
117+
118+
PlayerPrefs.SetString("uuid", uuid);
119+
}
120+
}
121+
userId = PlayerPrefs.GetInt("userId");
122+
pairToken = PlayerPrefs.GetString("pairToken");
123+
timezone = PlayerPrefs.GetString("timezone");
124+
}
125+
126+
static IEnumerator CheckForBrokenPairing()
127+
{
128+
// here we just ask the question, was this unique ID paired and somehow it lost it's pairing?
129+
WWWForm form = new WWWForm();
130+
form.AddField("uuid", uuid);
131+
form.AddField("deviceToken", _deviceToken);
132+
133+
using (UnityWebRequest www = UnityWebRequest.Post(apiUrl + "/api/isDevicePaired", form))
134+
{
135+
yield return www.SendWebRequest();
136+
137+
// we actually only need to check for a 200 response to know the server is informed and it's been checked.
138+
if (www.isNetworkError || www.isHttpError)
139+
{
140+
Debug.Log(www.error);
141+
}
142+
else
143+
{
144+
Debug.Log(www.downloadHandler.text);
145+
var response = Allow2_SimpleJSON.JSON.Parse(www.downloadHandler.text);
146+
// extract
147+
}
148+
}
149+
}
150+
79151
//void Awake()
80152
//{
81153
// DontDestroyOnLoad(this);
@@ -90,7 +162,7 @@ resultClosure callback
90162
WWWForm form = new WWWForm();
91163
form.AddField("user", user);
92164
form.AddField("pass", pass);
93-
form.AddField("deviceToken", deviceToken);
165+
form.AddField("deviceToken", _deviceToken);
94166
form.AddField("name", deviceName);
95167

96168
Debug.Log(apiUrl + "/api/pairDevice");
@@ -110,21 +182,40 @@ resultClosure callback
110182
var response = Allow2_SimpleJSON.JSON.Parse(www.downloadHandler.text);
111183
// extract
112184

113-
// persist
185+
persist();
114186

115187
// return
116188
callback(null, null);
117189
}
118190
}
119191
}
120192

193+
public static string getQR()
194+
{
195+
return "https://api.allow2.com/genqr/" +
196+
WWW.EscapeURL(_deviceToken) + "/" +
197+
WWW.EscapeURL(uuid) + "/" +
198+
WWW.EscapeURL(name);
199+
}
200+
121201
public static IEnumerator Check(int childId,
122202
int[] activities,
123203
resultClosure callback,
124204
bool log = false
125205
)
126206
{
127-
UnityWebRequest www = UnityWebRequest.Get("http://www.my-server.com");
207+
if ((userId < 1) || (pairToken == null)) {
208+
callback(Allow2Error.NotPaired, null);
209+
yield break;
210+
}
211+
212+
WWWForm form = new WWWForm();
213+
form.AddField("user", user);
214+
form.AddField("pass", pass);
215+
form.AddField("deviceToken", _deviceToken);
216+
form.AddField("name", deviceName);
217+
218+
UnityWebRequest www = UnityWebRequest.Get(apiUrl + "/api/pairDevice");
128219
yield return www.SendWebRequest();
129220

130221
if (www.isNetworkError || www.isHttpError)

0 commit comments

Comments
 (0)