// Use this for initialization void Start() { rC = GetComponent <RobotConnection>(); gameDataMap = new JsonMapObjects(); gameDataMap.mapName = new List <string>(); #if !UNITY_EDITOR gameDataPos = new JsonRobotObjects(); gameDataPos.position = new List <string>(); gameDataPos.rotationY = new List <float>(); #endif posAsJson = ""; mapAsJson = ""; }
/// <summary> /// This method sends a POST request to get the desired data to download and writes them in the created txt file /// Not usable in Web build /// </summary> /// <returns></returns> private IEnumerator Download() { data = new JsonRobotObjects(); data.position = new List <string>(); data.rotationY = new List <float>(); //data.time = new List<float>(); //data.mapName = new List<string>(); id = 1; while (keepGoing) //until there are other results to download { var uwr = UnityWebRequest.Post(url, "POST"); idObject = new IdPost(id.ToString()); byte[] idToSend = new System.Text.UTF8Encoding().GetBytes(JsonUtility.ToJson(idObject)); uwr.uploadHandler = (UploadHandler) new UploadHandlerRaw(idToSend); uwr.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer(); //uwr.SetRequestHeader("Content-Type", "application/json"); //Send the request then wait here until it returns yield return(uwr.SendWebRequest()); if (uwr.downloadHandler.text != "Id not existent") { Debug.Log(uwr.downloadHandler.text); gameDataPos = JsonUtility.FromJson <TrajectoryReceived>(uwr.downloadHandler.text); gameDataPos.position = Regex.Replace(gameDataPos.position, @"[()]", ""); gameDataPos.position = Regex.Replace(gameDataPos.position, @"[ ]", ""); string[] positions = gameDataPos.position.Split(','); pos = new List <string>(); for (int i = 0; i < positions.Length; i = i + 2) { pos.Add(positions[i] + "," + positions[i + 1]); } gameDataPos.rotation = Regex.Replace(gameDataPos.rotation, @"[()]", ""); gameDataPos.rotation = Regex.Replace(gameDataPos.rotation, @"[ ]", ""); string[] rotString = gameDataPos.rotation.Split(','); rot = new List <float>(); for (int i = 0; i < rotString.Length; i++) { rot.Add(float.Parse(rotString[i])); } gameDataPos.timerobot = Regex.Replace(gameDataPos.timerobot, @"[()]", ""); gameDataPos.timerobot = Regex.Replace(gameDataPos.timerobot, @"[ ]", ""); //string[] timeRob = gameDataPos.timerobot.Split(','); //time = new List<float>(); //for (int i = 0; i < timeRob.Length; i++) //{ // time.Add(float.Parse(timeRob[i])); //} gameDataPos.mapname = Regex.Replace(gameDataPos.mapname, @"[()]", ""); gameDataPos.mapname = Regex.Replace(gameDataPos.mapname, @"[ ]", ""); //string[] name = gameDataPos.mapname.Split(','); //mName = new List<string>(); //for (int i = 0; i < name.Length; i++) //{ // mName.Add(name[i]); //} data.position = pos; data.rotationY = rot; data.time = float.Parse(gameDataPos.timerobot); data.mapName = gameDataPos.mapname; data.ip = gameDataPos.ip; data.os = gameDataPos.os; File.WriteAllText(downloadedContentPath + "/Result" + id.ToString() + "t.txt", JsonUtility.ToJson(data)); WriteTupleLog(pos, rot, gameDataPos.mapname, id); id++; } else { Debug.Log("Finished download"); keepGoing = false; } } }