using CHN; using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using UnityEngine; using UnityEngine.Networking; public class HTTPTest : MonoBehaviour { public string apiKey = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MywiZW1haWwiOiJ1dmMiLCJncm91cElkIjoyLCJncm91cENvZGUiOiJjaHVuaWxlbmciLCJyb2xlIjoiVGVuYW50QWRtaW4iLCJuYW1lIjoi7Jyg67mE7JSoIiwibGljZW5zZSI6ImJ1c2luZXNzIiwicGhvbmUiOiIiLCJpYXQiOjE3NDA1MzY0NzUsImV4cCI6NDg5NDEzNjQ3NX0.hr4D0bOA4K09Vhp12itiJgd-nVDQ3VZO8D7MVP5Ltw0"; public string httpServer = "http://220.90.135.112:3100"; public string facilityAPI = "/api/workConditionAnalysis/facilityCode"; public string workingConditionsAPI = "/api/workConditionAnalysis/analyze"; public string workingTimeAPI = "/api/workTimeAnalysis/analyze"; public string startDate; public string endDate; public WorkConditionsRequestBody testWorkConditionRequest; public WorkTimeRequesttBody testWorkTimeRequest; public WorkConditionFacilityData workConditionFacilityData; public WorkConditionsData workConditionsData; public WorkTimeData workTimeData; void Start() { StartCoroutine(HTTPWebRequest()); } private void Update() { if (Input.GetKeyDown(KeyCode.K)) { WorkConditionInQuiry(); } else if (Input.GetKeyDown(KeyCode.J)) { WorkTimeInQuiry(); } if(Input.GetKeyDown(KeyCode.O)) { //FindObjectOfType().panel_workconditionanalysis.Open(); } } IEnumerator HTTPWebRequest() { var path = httpServer + facilityAPI; var query = $"?startDate={startDate}&endDate={endDate}"; string url = path + query; UnityWebRequest www = UnityWebRequest.Get(url); www.SetRequestHeader("access-token", apiKey); yield return www.SendWebRequest(); if (www.error == null) { var payload = Encoding.UTF8.GetString(www.downloadHandler.data); var response = JsonConvert.DeserializeObject(payload); workConditionFacilityData = response; } else { Debug.Log(www.error); } } private void WorkConditionInQuiry() { var path = httpServer + workingConditionsAPI; testWorkConditionRequest = TestWorkConditionSetting(); var json = JsonUtility.ToJson(testWorkConditionRequest); StopAllCoroutines(); StartCoroutine(WorkConditionsPost(path, json)); } IEnumerator WorkConditionsPost(string url, string jsonData) { var request = new UnityWebRequest(url, "POST"); byte[] bodyRaw = new System.Text.UTF8Encoding().GetBytes(jsonData); request.uploadHandler = new UploadHandlerRaw(bodyRaw); request.downloadHandler = new DownloadHandlerBuffer(); request.SetRequestHeader("access-token", apiKey); request.SetRequestHeader("Content-Type", "application/json"); yield return request.SendWebRequest(); if (request.result != UnityWebRequest.Result.Success) { Debug.Log("Error: " + request.error); } else { var payload = Encoding.UTF8.GetString(request.downloadHandler.data); var response = JsonConvert.DeserializeObject(payload); workConditionsData = response; Debug.Log("Received: " + request.downloadHandler.text); } } private void WorkTimeInQuiry() { var path = httpServer + workingTimeAPI; testWorkTimeRequest = TestWorkTimeSetting(); var json = JsonUtility.ToJson(testWorkTimeRequest); StopAllCoroutines(); StartCoroutine(WorkTimePost(path, json)); } public void WorkTimeInQuiry(string startDate, string endDate, string MCHCD, string WO) { testWorkTimeRequest = new WorkTimeRequesttBody { startDate = startDate, endDate = endDate, MCHCD = MCHCD, WO = WO }; var path = httpServer + workingTimeAPI; var json = JsonUtility.ToJson(testWorkTimeRequest); StopAllCoroutines(); StartCoroutine(WorkTimePost(path, json)); } IEnumerator WorkTimePost(string url, string jsonData) { var request = new UnityWebRequest(url, "POST"); byte[] bodyRaw = new UTF8Encoding().GetBytes(jsonData); request.uploadHandler = new UploadHandlerRaw(bodyRaw); request.downloadHandler = new DownloadHandlerBuffer(); request.SetRequestHeader("access-token", apiKey); request.SetRequestHeader("Content-Type", "application/json"); yield return request.SendWebRequest(); if (request.result != UnityWebRequest.Result.Success) { Debug.Log("Error: " + request.error); } else { var payload = Encoding.UTF8.GetString(request.downloadHandler.data); var response = JsonConvert.DeserializeObject(payload); workTimeData = response; Debug.Log("Received: " + request.downloadHandler.text); } } #region TestJson WorkTimeRequesttBody TestWorkTimeSetting() { WorkTimeRequesttBody request = new WorkTimeRequesttBody { startDate = "2025-01-10T00:00:00Z", endDate = "2025-02-20T00:00:00Z", MCHCD = "IJ01", WO = "WO202502170014" }; return request; } WorkConditionsRequestBody TestWorkConditionSetting() { WorkConditionsRequestBody request = new WorkConditionsRequestBody { startDate = "2025-01-10T00:00:00Z", endDate = "2025-02-20T00:00:00Z", MCHCD = "IJ01", WO = "WO202502170014" }; return request; } #endregion }