276 lines
9.7 KiB
C#
276 lines
9.7 KiB
C#
using System;
|
|
using System.Text;
|
|
using System.Collections;
|
|
using Best.HTTP;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using Newtonsoft.Json;
|
|
|
|
/// <summary>
|
|
/// Option.ini
|
|
///
|
|
/// --httpSetting--
|
|
/// apiKey=""
|
|
/// httpServer=""
|
|
/// kpiAPI=""
|
|
/// predictionTimeAPI=""
|
|
///
|
|
/// --mqttClientSetting--
|
|
/// brokerAddress=""
|
|
/// brokerPort=""
|
|
/// topics="a","b","c"...
|
|
/// --mqttBrokerSetting--
|
|
/// address=""
|
|
/// port=""
|
|
/// topics="a","b","c"...
|
|
///
|
|
/// --defaultInfo--
|
|
/// machineInfoPath=""
|
|
///
|
|
/// --controllerOption--
|
|
/// forward=""
|
|
/// backward=""
|
|
/// left=""
|
|
/// right=""
|
|
/// zoomSpeed=""
|
|
/// moveSpeed=""
|
|
/// sprintSpeed=""
|
|
/// rotateSpeed=""
|
|
///
|
|
/// --graphicOption--
|
|
/// shadow=true
|
|
/// uiEffect=true
|
|
/// uiAnimation=true
|
|
/// uiResolution=1.0
|
|
/// machineEffect=true
|
|
/// machineAnimation=true
|
|
/// graphicQuality=1
|
|
/// </summary>
|
|
namespace EnglewoodLAB.UI
|
|
{
|
|
public class WorkTimeRequesttBody
|
|
{
|
|
public string startDate;
|
|
public string endDate;
|
|
public string MCHCD;
|
|
public string WO;
|
|
}
|
|
public class WorkConditionsRequestBody
|
|
{
|
|
public string startDate;
|
|
public string endDate;
|
|
public string MCHCD;
|
|
public string WO;
|
|
}
|
|
public class HTTPRequester : Protocol, ISingle, IOptionable
|
|
{
|
|
public Action<MachineKPIData> onMachineKPIData;
|
|
|
|
public Action<WorkItemCodeData> onWorkConditionsItemCodeData;
|
|
public Action<WorkItemCodeData> onWorkTimeItemCodeData;
|
|
|
|
public Action<WorkConditionsData> onSendWorkConditionsData;
|
|
public Action<WorkTimeData> onSendWorkTimeData;
|
|
|
|
[OptionSection]
|
|
string httpSetting;
|
|
[OptionKey]
|
|
string apiKey = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwiZW1haWwiOiJjaHVuaWxlbmdAdXZjLmNvLmtyIiwiZ3JvdXBJZCI6MiwiZ3JvdXBDb2RlIjoiY2h1bmlsZW5nIiwicm9sZSI6IlRlbmFudEFkbWluIiwibmFtZSI6IuyynOydvOyXlOyngOuLiOyWtOungSIsImxpY2Vuc2UiOiJidXNpbmVzcyIsInBob25lIjoiMDEwMDAwMDAwMDAiLCJpYXQiOjE3MTgxNjI2MjksImV4cCI6MjAzMzUyMjYyOX0.zdlNswh_88M44oxs1HuaoA-rFdUoS50tHQ1-xNqWzIc";
|
|
[OptionKey]
|
|
string httpServer = "http://106.247.236.204:8863";
|
|
[OptionKey]
|
|
string kpiAPI = "/api/usp_ppmr020/list/facilityKpi";
|
|
|
|
public string testAPIKey = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MywiZW1haWwiOiJ1dmMiLCJncm91cElkIjoyLCJncm91cENvZGUiOiJjaHVuaWxlbmciLCJyb2xlIjoiVGVuYW50QWRtaW4iLCJuYW1lIjoi7Jyg67mE7JSoIiwibGljZW5zZSI6ImJ1c2luZXNzIiwicGhvbmUiOiIiLCJpYXQiOjE3NDA1MzY0NzUsImV4cCI6NDg5NDEzNjQ3NX0.hr4D0bOA4K09Vhp12itiJgd-nVDQ3VZO8D7MVP5Ltw0";
|
|
public string facilityAPI = "/api/workConditionAnalysis/facilityCode";
|
|
public string workItemCodeAPI = "/api/workConditionAnalysis/workOrderAndItemCode";
|
|
public string workingConditionsAPI = "/api/workConditionAnalysis/analyze";
|
|
public string workingTimeAPI = "/api/workTimeAnalysis/analyze";
|
|
|
|
public WorkConditionFacilityData workConditionFacilityData;
|
|
public WorkItemCodeData workItemCodeData;
|
|
public WorkConditionsData workConditionsData;
|
|
public WorkTimeData workTimeData;
|
|
|
|
private string startDate;
|
|
private string endDate;
|
|
public void Start()
|
|
{
|
|
HTTPConnect();
|
|
}
|
|
public void HTTPConnect()
|
|
{
|
|
var path = httpServer + kpiAPI;
|
|
|
|
var yesterday = DateTime.Now.AddDays(-1);
|
|
var yesterdayTime = yesterday.ToString("yyyyMMdd");
|
|
var todayTime = DateTime.Now.ToString("yyyyMMdd");
|
|
|
|
var query = $"?&p_frdt={yesterdayTime}&p_todt={todayTime}&limit={100}";
|
|
|
|
var request = HTTPRequest.CreateGet(path + query, RequestFinishedCallback);
|
|
request.AddHeader("access-token", apiKey);
|
|
|
|
request.Send();
|
|
}
|
|
private void RequestFinishedCallback(HTTPRequest req, HTTPResponse resp)
|
|
{
|
|
switch (req.State)
|
|
{
|
|
case HTTPRequestStates.Finished:
|
|
if (resp.IsSuccess)
|
|
{
|
|
isSuccess = true;
|
|
errorMessage = "Connected";
|
|
|
|
var payload = Encoding.UTF8.GetString(resp.Data);
|
|
var response = JsonConvert.DeserializeObject<MachineKPIData>(payload);
|
|
|
|
onMachineKPIData?.Invoke(response);
|
|
}
|
|
else
|
|
{
|
|
isSuccess = false;
|
|
errorMessage = "UnConnected";
|
|
}
|
|
break;
|
|
|
|
default:
|
|
isSuccess = false;
|
|
errorMessage = "UnConnected";
|
|
break;
|
|
}
|
|
}
|
|
public void GetConditionWorkOrderCodeData(string startDate, string endDate, string facilityCode)
|
|
{
|
|
StartCoroutine(GetConditionWorkOrderCode(startDate, endDate, facilityCode));
|
|
}
|
|
|
|
private void StartCoroutine(IEnumerator enumerator)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
IEnumerator GetConditionWorkOrderCode(string startDate, string endDate, string facilityCode)
|
|
{
|
|
this.startDate = startDate;
|
|
this.endDate = endDate;
|
|
|
|
var path = httpServer + workItemCodeAPI;
|
|
var query = $"?startDate={this.startDate}&endDate={this.endDate}&workcd={facilityCode}";
|
|
|
|
string url = path + query;
|
|
|
|
UnityWebRequest www = UnityWebRequest.Get(url);
|
|
www.SetRequestHeader("access-token", testAPIKey);
|
|
|
|
yield return www.SendWebRequest();
|
|
|
|
if (www.error == null)
|
|
{
|
|
var payload = Encoding.UTF8.GetString(www.downloadHandler.data);
|
|
var response = JsonConvert.DeserializeObject<WorkItemCodeData>(payload);
|
|
workItemCodeData = response;
|
|
onWorkConditionsItemCodeData?.Invoke(workItemCodeData);
|
|
}
|
|
}
|
|
public void GetTimeWorkOrderCodeData(string startDate, string endDate, string facilityCode)
|
|
{
|
|
StartCoroutine(GetTimeWorkOrderCode(startDate, endDate, facilityCode));
|
|
}
|
|
IEnumerator GetTimeWorkOrderCode(string startDate, string endDate, string facilityCode)
|
|
{
|
|
this.startDate = startDate;
|
|
this.endDate = endDate;
|
|
|
|
var path = httpServer + workItemCodeAPI;
|
|
var query = $"?startDate={this.startDate}&endDate={this.endDate}&workcd={facilityCode}";
|
|
|
|
string url = path + query;
|
|
|
|
UnityWebRequest www = UnityWebRequest.Get(url);
|
|
www.SetRequestHeader("access-token", testAPIKey);
|
|
|
|
yield return www.SendWebRequest();
|
|
|
|
if (www.error == null)
|
|
{
|
|
var payload = Encoding.UTF8.GetString(www.downloadHandler.data);
|
|
var response = JsonConvert.DeserializeObject<WorkItemCodeData>(payload);
|
|
workItemCodeData = response;
|
|
onWorkTimeItemCodeData?.Invoke(workItemCodeData);
|
|
}
|
|
}
|
|
|
|
public void SearchWorkConditions(string startDate, string endDate, string MCHCD, string WO)
|
|
{
|
|
var WorkConditionRequest = new WorkConditionsRequestBody
|
|
{
|
|
startDate = startDate,
|
|
endDate = endDate,
|
|
MCHCD = MCHCD,
|
|
WO = WO
|
|
};
|
|
|
|
var path = httpServer + workingConditionsAPI;
|
|
|
|
var json = JsonUtility.ToJson(WorkConditionRequest);
|
|
StartCoroutine(WorkConditionsPost(path, json));
|
|
}
|
|
IEnumerator WorkConditionsPost(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)
|
|
{
|
|
var payload = Encoding.UTF8.GetString(request.downloadHandler.data);
|
|
var response = JsonConvert.DeserializeObject<WorkConditionsData>(payload);
|
|
|
|
workConditionsData = response;
|
|
onSendWorkConditionsData?.Invoke(workConditionsData);
|
|
}
|
|
}
|
|
public void SearchWorkTime(string startDate, string endDate, string MCHCD, string WO)
|
|
{
|
|
var WorkConditionRequest = new WorkConditionsRequestBody
|
|
{
|
|
startDate = startDate,
|
|
endDate = endDate,
|
|
MCHCD = MCHCD,
|
|
WO = WO
|
|
};
|
|
|
|
var path = httpServer + workingTimeAPI;
|
|
|
|
var json = JsonUtility.ToJson(WorkConditionRequest);
|
|
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)
|
|
{
|
|
var payload = Encoding.UTF8.GetString(request.downloadHandler.data);
|
|
var response = JsonConvert.DeserializeObject<WorkTimeData>(payload);
|
|
|
|
workTimeData = response;
|
|
onSendWorkTimeData?.Invoke(workTimeData);
|
|
}
|
|
}
|
|
}
|
|
} |