/** * Coroutine sending log files and hardware values every 10 minutes **/ IEnumerator SendRequest() { Logger logger = Logger.Instance; while (true) { yield return(new WaitForSeconds(300.0f)); string[] filenames = GetLogsFilesNames(); foreach (string f in filenames) { Debug.Log("Monitoring : treating file :" + f); // Creates a form containing the data to send WWWForm form = new WWWForm(); form.AddBinaryData("logUploader", Encoding.UTF8.GetBytes(GetFileContent(f)), f, "text/plain"); UnityWebRequest www = UnityWebRequest.Post(GetLogURL(), form); www.SetRequestHeader("x-access-token", mSharingServer.GetToken()); // Sends the request and yields until the send completes yield return(www.SendWebRequest()); if (www.isNetworkError || www.isHttpError) { Debug.Log("Monitoring server error :" + www.downloadHandler.text); } else { Debug.Log("Monitoring has send: " + f); File.Delete(Application.persistentDataPath + "/" + f); } } StringBuilder sb = new StringBuilder(); JsonWriter json = new JsonWriter(sb); json.WriteObjectStart(); json.WritePropertyName("timestamp"); json.Write(DateTime.Now.ToString()); json.WritePropertyName("cameraBattery"); json.Write(mCamData.GetCameraBattery()); json.WritePropertyName("tabletBattery"); json.Write(mTabletBattery.GetCurrentBatteryLevel()); json.WriteObjectEnd(); WWWForm form2 = new WWWForm(); form2.AddField("data", sb.ToString()); UnityWebRequest www2 = UnityWebRequest.Post(GetHardwareURL(), form2); www2.SetRequestHeader("x-access-token", mSharingServer.GetToken()); yield return(www2.SendWebRequest()); if (www2.isNetworkError || www2.isHttpError) { Debug.Log(www2.error); } } }
/** * Checks if auth file exists, if not creates it * If auth file is empty ask for our token to the server * Else go to welcome screen **/ private void ManageRegistrationScreen() { mTimeout.Reset(); if (!System.IO.File.Exists(Application.persistentDataPath + "/auth_file.txt")) { Debug.Log("creating auth.txt"); System.IO.File.Create(Application.persistentDataPath + "/auth_file.txt"); } else { if (new System.IO.FileInfo(Application.persistentDataPath + "/auth_file.txt").Length == 0 && timeStart.AddSeconds(10) < DateTime.Now) { string androidID = mAndroidSID.GetSID(); Debug.Log("SID is " + androidID); timeStart = DateTime.Now; if (authentification_state == 0) { try { bool auth = mSharingServer.GetAuth(); if (auth == true) { authentification_state = 1; } else { mSharingServer.SendToServerAuthentication(androidID); } } catch (Exception e) { Debug.Log(e.Message); } } else { string token = mSharingServer.GetToken(); if (token != null) { using (System.IO.StreamWriter outputFile = new System.IO.StreamWriter(Application.persistentDataPath + "/auth_file.txt")) { outputFile.WriteLine(token); } authentification_state = 2; } else { mSharingServer.SendToServerAskToken(androidID); } } } else if (new System.IO.FileInfo(Application.persistentDataPath + "/auth_file.txt").Length != 0) { System.IO.StreamReader file = new System.IO.StreamReader(Application.persistentDataPath + "/auth_file.txt"); string line = file.ReadLine(); Debug.Log("Token :" + line); mSharingServer.SetToken(line); authentification_state = 3; mCurrentState = ScreensStates.WELCOME; UpdateScreen(); } } }