private void SaveToFile(OfflineStorageSessions sessionToStore) { AllStorageSessions loadedSessions; string getSessions = PlayerPrefs.GetString("offlineSessions", ""); //if null store make new and store if (getSessions.Equals("")) { Debug.Log("get sessions is null (good for first one)"); loadedSessions = new AllStorageSessions(); loadedSessions.allSessions.Add(sessionToStore); //now one element in list string jsonSessions = JsonUtility.ToJson(loadedSessions); //this MAY need to be redone for AllStorageSessions? PlayerPrefs.SetString("offlineSessions", jsonSessions); } else { //if not null, load into List, add to list, then store Debug.Log("get sessions is not null - good for second"); loadedSessions = JsonUtility.FromJson <AllStorageSessions>(getSessions); loadedSessions.allSessions.Add(sessionToStore); string jsonSessions = JsonUtility.ToJson(loadedSessions); PlayerPrefs.SetString("offlineSessions", jsonSessions); Debug.Log("the json: " + jsonSessions); } }
private FizzyoRequestReturnType UploadSession(OfflineStorageSessions session) { ///https://api.fizzyo-ucl.co.uk/api/v1/games/<id>/sessions string postAnalytics = FizzyoFramework.Instance.apiPath + "api/v1/games/" + FizzyoFramework.Instance.gameID + "/sessions"; WWWForm form = new WWWForm(); form.AddField("secret", FizzyoFramework.Instance.gameSecret); form.AddField("userId", FizzyoFramework.Instance.User.UserID); form.AddField("setCount", session.setCount); form.AddField("breathCount", session.breathCount); form.AddField("goodBreathCount", session.goodBreathCount); form.AddField("badBreathCount", session.badBreathCount); form.AddField("score", session.score); form.AddField("startTime", session.startTime.ToString()); form.AddField("endTime", session.endTime.ToString()); Dictionary <string, string> headers = form.headers; headers["Authorization"] = "Bearer " + FizzyoFramework.Instance.User.AccessToken; #if UNITY_UWP headers.Add("User-Agent", " FizzyoClient " + FizzyoFramework.Instance.ClientVersion); #endif byte[] rawData = form.data; WWW sendPostAnalytics = new WWW(postAnalytics, rawData, headers); while (!sendPostAnalytics.isDone) { } ; if (sendPostAnalytics.error != null) { Debug.Log("[FizzyoAnalytics] Posting offline analytics failed. "); StoreOffline(); return(FizzyoRequestReturnType.FAILED_TO_CONNECT); } Debug.Log("[FizzyoAnalytics] Posting offline analytics successful."); return(FizzyoRequestReturnType.SUCCESS); }
private void StoreOffline() { Debug.Log("in storeoffline"); OfflineStorageSessions sessionToStore = new OfflineStorageSessions(); //make a OfflineStorageSession and pass it into SaveToFile try { sessionToStore.breathCount = FizzyoFramework.Instance.Recogniser.BreathCount; sessionToStore.badBreathCount = FizzyoFramework.Instance.Recogniser.BadBreaths; sessionToStore.goodBreathCount = FizzyoFramework.Instance.Recogniser.GoodBreaths; TimeSpan tspan = DateTime.UtcNow - new DateTime(1970, 1, 1); sessionToStore.endTime = ((double)tspan.TotalSeconds) * 1000; sessionToStore.startTime = this.startTime; sessionToStore.score = this._score; sessionToStore.setCount = this._setCount; } catch { //error getting variables, dont save return; } SaveToFile(sessionToStore); }
public void UploadCache() { //load into list --> run through list trying to upload Debug.Log("in UploadCache"); AllStorageSessions loadedSessions = new AllStorageSessions(); string jsonSession = PlayerPrefs.GetString("offlineSessions", ""); if (jsonSession.Equals("")) { return; //nothing to upload } else { loadedSessions = JsonUtility.FromJson <AllStorageSessions>(jsonSession); //now run through and upload Debug.Log("trying to upload"); for (int i = loadedSessions.allSessions.Count - 1; i >= 0; i--) { OfflineStorageSessions uploadSession = loadedSessions.allSessions[i]; FizzyoRequestReturnType uploadStatus = UploadSession(uploadSession); if (uploadStatus == FizzyoRequestReturnType.SUCCESS) { //remove from queue -- maybe unnecessary loadedSessions.allSessions.RemoveAt(i); } else { //send the whole array and store it again string jsonSessions = JsonUtility.ToJson(loadedSessions); //this MAY need to be redone for AllStorageSessions? PlayerPrefs.SetString("offlineSessions", jsonSessions); return; } } //if we haven't returned, then everthing has been uploaded by now PlayerPrefs.SetString("offlineSessions", ""); //so reset queue } Debug.Log("player prefs should now be null " + PlayerPrefs.GetString("offlineSessions", "") + "?"); }