1515using UnityEngine ;
1616using UnityEngine . Networking ;
1717using Allow2_SimpleJSON ;
18+ using System . Text ;
1819
1920namespace Allow2
2021{
@@ -112,9 +113,9 @@ public static string DeviceToken
112113 _deviceToken = value ;
113114 PlayerPrefs . SetString ( "deviceToken" , _deviceToken ) ;
114115 //if (!IsPaired)
115- //{
116- // todo: start a regular timer to keep trying to call home on startup until we confirm we are NOT paired.
117- CheckForBrokenPairing ( ) ;
116+ //{
117+ // todo: start a regular timer to keep trying to call home on startup until we confirm we are NOT paired.
118+ CheckForBrokenPairing ( ) ;
118119 //}
119120 }
120121 }
@@ -123,6 +124,7 @@ private static void Persist()
123124 {
124125 // todo: write to protected namespace storage?
125126 PlayerPrefs . SetInt ( "userId" , userId ) ;
127+ PlayerPrefs . SetString ( "pairToken" , pairToken ) ;
126128 }
127129
128130 // no persistence here
@@ -184,7 +186,8 @@ public static IEnumerator Pair(string user, // ie: "fred@gmail.com",
184186 resultClosure callback
185187 )
186188 {
187- if ( IsPaired ) {
189+ if ( IsPaired )
190+ {
188191 callback ( Allow2Error . AlreadyPaired , null ) ;
189192 yield break ;
190193 }
@@ -277,9 +280,11 @@ public static IEnumerator GetQR(string name,
277280 // )
278281 //}
279282
280- private static Dictionary < int , string > ParseChildren ( JSONNode json ) {
283+ private static Dictionary < int , string > ParseChildren ( JSONNode json )
284+ {
281285 Dictionary < int , string > children = new Dictionary < int , string > ( ) ;
282- foreach ( JSONNode child in json ) {
286+ foreach ( JSONNode child in json )
287+ {
283288 children [ child [ "id" ] ] = child [ "name" ] ;
284289 }
285290 return children ;
@@ -301,97 +306,114 @@ public static IEnumerator Check(int childId, // childId == 0 ? Get Updated Ch
301306 Debug . Log ( pairToken ) ;
302307 Debug . Log ( _deviceToken ) ;
303308
304- WWWForm form = new WWWForm ( ) ;
305- form . AddField ( "userId" , userId ) ;
306- form . AddField ( "pairToken" , pairToken ) ;
307- form . AddField ( "deviceToken" , _deviceToken ) ;
308- form . AddField ( "tz" , _timezone ) ;
309- //form.AddField("activities", activities);
310- form . AddField ( "log" , log ? 1 : 0 ) ;
309+ JSONNode body = new JSONObject ( ) ;
310+ body . Add ( "userId" , userId ) ;
311+ body . Add ( "pairToken" , pairToken ) ;
312+ body . Add ( "deviceToken" , _deviceToken ) ;
313+ body . Add ( "tz" , _timezone ) ;
314+ JSONArray activityJson = new JSONArray ( ) ;
315+ foreach ( int activity in activities )
316+ {
317+ JSONNode activityParams = new JSONObject ( ) ;
318+ activityParams . Add ( "id" , activity ) ;
319+ activityParams . Add ( "log" , log ) ;
320+ activityJson . Add ( activityParams ) ;
321+ }
322+ body . Add ( "activities" , activityJson ) ;
323+ body . Add ( "log" , log ) ;
311324 if ( childId > 0 )
312325 {
313- form . AddField ( "childId" , childId ) ;
326+ body . Add ( "childId" , childId ) ;
314327 }
328+ string bodyStr = body . ToString ( ) ;
315329
316330 // check the cache first
317- string key = form . ToString ( ) ;
318- if ( resultCache . ContainsKey ( key ) ) {
319- Allow2CheckResult checkResult = resultCache [ key ] ;
331+ if ( resultCache . ContainsKey ( bodyStr ) )
332+ {
333+ Allow2CheckResult checkResult = resultCache [ bodyStr ] ;
320334
321- if ( checkResult . Expires . CompareTo ( new DateTime ( ) ) < 0 ) {
335+ if ( checkResult . Expires . CompareTo ( new DateTime ( ) ) < 0 )
336+ {
322337 // not expired yet, use cached value
323338 callback ( null , checkResult ) ;
324339 yield break ;
325340 }
326341
327342 // clear cached value and ask the server again
328- resultCache . Remove ( key ) ;
343+ resultCache . Remove ( bodyStr ) ;
329344 }
330345
331- UnityWebRequest www = UnityWebRequest . Get ( ServiceUrl + "/serviceapi/check" ) ;
332- yield return www . SendWebRequest ( ) ;
346+ byte [ ] bytes = Encoding . UTF8 . GetBytes ( bodyStr ) ;
347+ using ( UnityWebRequest www = new UnityWebRequest ( ServiceUrl + "/serviceapi/check" ) )
348+ {
349+ www . method = UnityWebRequest . kHttpVerbPOST ;
350+ www . uploadHandler = new UploadHandlerRaw ( bytes ) ;
351+ www . downloadHandler = new DownloadHandlerBuffer ( ) ;
352+ www . uploadHandler . contentType = "application/json" ;
353+ www . chunkedTransfer = false ;
354+ yield return www . SendWebRequest ( ) ;
333355
334- Debug . Log ( www . downloadHandler . text ) ;
335- var json = Allow2_SimpleJSON . JSON . Parse ( www . downloadHandler . text ) ;
356+ Debug . Log ( www . downloadHandler . text ) ;
357+ var json = Allow2_SimpleJSON . JSON . Parse ( www . downloadHandler . text ) ;
336358
337- if ( ( www . responseCode == 401 ) ||
338- ( www . responseCode == 404 ) ||
339- ( ( json [ "status" ] == "error" ) &&
340- ( ( json [ "message" ] == "Invalid user." ) ||
341- ( json [ "message" ] == "invalid pairToken" ) ) ) )
342- {
343- // special case, no longer controlled
344- Debug . Log ( "No Longer Paired" ) ;
345- userId = 0 ;
346- pairToken = null ;
347- Persist ( ) ;
348- //childId = 0;
349- //_children = []
350- //_dayTypes = []
351- var failOpen = new Allow2CheckResult ( ) ;
352- failOpen . Add ( "subscription" , new Allow2_SimpleJSON . JSONArray ( ) ) ;
353- failOpen . Add ( "allowed" , true ) ;
354- failOpen . Add ( "activities" , new Allow2_SimpleJSON . JSONArray ( ) ) ;
355- failOpen . Add ( "dayTypes" , new Allow2_SimpleJSON . JSONArray ( ) ) ;
356- failOpen . Add ( "allDayTypes" , new Allow2_SimpleJSON . JSONArray ( ) ) ;
357- failOpen . Add ( "children" , new Allow2_SimpleJSON . JSONArray ( ) ) ;
358- callback ( null , failOpen ) ;
359- yield break ;
360- }
359+ if ( ( www . responseCode == 401 ) ||
360+ ( ( json [ "status" ] == "error" ) &&
361+ ( ( json [ "message" ] == "Invalid user." ) ||
362+ ( json [ "message" ] == "invalid pairToken" ) ) ) )
363+ {
364+ // special case, no longer controlled
365+ Debug . Log ( "No Longer Paired" ) ;
366+ userId = 0 ;
367+ pairToken = null ;
368+ Persist ( ) ;
369+ //childId = 0;
370+ //_children = []
371+ //_dayTypes = []
372+ var failOpen = new Allow2CheckResult ( ) ;
373+ failOpen . Add ( "subscription" , new Allow2_SimpleJSON . JSONArray ( ) ) ;
374+ failOpen . Add ( "allowed" , true ) ;
375+ failOpen . Add ( "activities" , new Allow2_SimpleJSON . JSONArray ( ) ) ;
376+ failOpen . Add ( "dayTypes" , new Allow2_SimpleJSON . JSONArray ( ) ) ;
377+ failOpen . Add ( "allDayTypes" , new Allow2_SimpleJSON . JSONArray ( ) ) ;
378+ failOpen . Add ( "children" , new Allow2_SimpleJSON . JSONArray ( ) ) ;
379+ callback ( null , failOpen ) ;
380+ yield break ;
381+ }
361382
362- if ( www . isNetworkError || www . isHttpError )
363- {
364- Debug . Log ( www . error ) ;
365- callback ( www . error , null ) ;
366- yield break ;
367- }
383+ if ( www . isNetworkError || www . isHttpError )
384+ {
385+ Debug . Log ( www . error ) ;
386+ callback ( www . error , null ) ;
387+ yield break ;
388+ }
368389
369- if ( json [ "allowed" ] == null )
370- {
371- callback ( Allow2Error . InvalidResponse , null ) ;
372- yield break ;
373- }
390+ if ( json [ "allowed" ] == null )
391+ {
392+ callback ( Allow2Error . InvalidResponse , null ) ;
393+ yield break ;
394+ }
374395
375- var response = new Allow2CheckResult ( ) ;
376- response . Add ( "activities" , json [ "activities" ] ) ;
377- response . Add ( "subscription" , json [ "subscription" ] ) ;
378- response . Add ( "dayTypes" , json [ "dayTypes" ] ) ;
379- response . Add ( "children" , json [ "children" ] ) ;
380- var _dayTypes = json [ "allDayTypes" ] ;
381- response . Add ( "allDayTypes" , _dayTypes ) ;
382- var oldChildIds = _children . Keys ;
383- var children = json [ "allDayTypes" ] ;
384- _children = Children ;
385- response . Add ( "children" , children ) ;
386-
387- if ( oldChildIds != _children . Keys )
388- {
389- Persist ( ) ; // only persist if the children change, this won't happen often.
390- }
396+ var response = new Allow2CheckResult ( ) ;
397+ response . Add ( "activities" , json [ "activities" ] ) ;
398+ response . Add ( "subscription" , json [ "subscription" ] ) ;
399+ response . Add ( "dayTypes" , json [ "dayTypes" ] ) ;
400+ response . Add ( "children" , json [ "children" ] ) ;
401+ var _dayTypes = json [ "allDayTypes" ] ;
402+ response . Add ( "allDayTypes" , _dayTypes ) ;
403+ var oldChildIds = _children . Keys ;
404+ var children = json [ "allDayTypes" ] ;
405+ _children = Children ;
406+ response . Add ( "children" , children ) ;
407+
408+ if ( oldChildIds != _children . Keys )
409+ {
410+ Persist ( ) ; // only persist if the children change, this won't happen often.
411+ }
391412
392- // cache the response
393- resultCache [ key ] = response ;
394- callback ( null , response ) ;
413+ // cache the response
414+ resultCache [ bodyStr ] = response ;
415+ callback ( null , response ) ;
416+ }
395417 }
396418
397419 public static IEnumerator StartChecking ( int childId ,
0 commit comments