229 lines
7.3 KiB
C#
229 lines
7.3 KiB
C#
|
|
using System.Collections;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using UnityEngine;
|
||
|
|
using Best.HTTP;
|
||
|
|
using System;
|
||
|
|
using Newtonsoft.Json;
|
||
|
|
using System.Text;
|
||
|
|
using WI;
|
||
|
|
using System.Linq;
|
||
|
|
|
||
|
|
/// <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 CHN
|
||
|
|
{
|
||
|
|
public class HTTPRequester : Protocol, ISingle, IOptionable
|
||
|
|
{
|
||
|
|
public Action<MachineKPIData> onMachineKPIData;
|
||
|
|
|
||
|
|
[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";
|
||
|
|
|
||
|
|
|
||
|
|
//string mpwod01tworker = "/api/mpwod01t/list/worker";
|
||
|
|
|
||
|
|
string mpwod01t = "/api/mpwod01t/list";
|
||
|
|
string mkord01t = "/api/mkord01t/list";
|
||
|
|
string insti02t = "/api/insti02t/list";
|
||
|
|
string mpeqi02t = "/api/mpeqi02t/list";
|
||
|
|
string bimch01t = "/api/bimch01t/list";
|
||
|
|
string biitm01t = "/api/biitm01t/list";
|
||
|
|
string bicod01t = "/api/bicod01t/list";
|
||
|
|
string bilos01t = "/api/bilos01t/list";
|
||
|
|
|
||
|
|
//string usp_ppmr020_workOrder = "/api/usp_ppmr020/list/workOrder";
|
||
|
|
//string usp_ppmr020_facilityDate = "/api/usp_ppmr020/list/facilityDate";
|
||
|
|
//string usp_ppmr020_facilityItem = "/api/usp_ppmr020/list/facilityItem";
|
||
|
|
//string usp_ppmr020_item = "/api/usp_ppmr020/list/item";
|
||
|
|
//string usp_ppmr020_itemDate= "/api/usp_ppmr020/list/itemDate";
|
||
|
|
//string usp_ppmr030_workOrder = "/api/usp_ppmr030/list/workOrder";
|
||
|
|
//string usp_ppmr030_facilityDate = "/api/usp_ppmr030/list/facilityDate";
|
||
|
|
//string usp_ppmr030_facilityItem = "/api/usp_ppmr030/list/facilityItem";
|
||
|
|
//string usp_ppmr030_item = "/api/usp_ppmr030/list/item";
|
||
|
|
//string usp_ppmr030_itemDate = "/api/usp_ppmr030/list/itemDate";
|
||
|
|
//string usp_ppmr040_workOrder = "/api/usp_ppmr040/list/workOrder";
|
||
|
|
//string usp_ppmr040_facilityDate = "/api/usp_ppmr040/list/facilityDate";
|
||
|
|
//string usp_ppmr040_facilityItem = "/api/usp_ppmr040/list/facilityItem";
|
||
|
|
//string usp_ppmr040_item = "/api/usp_ppmr040/list/item";
|
||
|
|
//string usp_ppmr040_itemDate = "/api/usp_ppmr040/list/itemDate";
|
||
|
|
|
||
|
|
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();
|
||
|
|
|
||
|
|
//Get();
|
||
|
|
}
|
||
|
|
|
||
|
|
void Get()
|
||
|
|
{
|
||
|
|
List<string> apiList = new();
|
||
|
|
apiList.Add(mpwod01t);
|
||
|
|
//apiList.Add(mpwod01tworker);
|
||
|
|
apiList.Add(mkord01t);
|
||
|
|
apiList.Add(biitm01t);
|
||
|
|
|
||
|
|
apiList.Add(bimch01t);
|
||
|
|
apiList.Add(mpeqi02t);
|
||
|
|
apiList.Add(insti02t);
|
||
|
|
apiList.Add(bicod01t);
|
||
|
|
|
||
|
|
apiList.Add(bilos01t);
|
||
|
|
//apiList.Add(usp_ppmr020_workOrder);
|
||
|
|
//apiList.Add(usp_ppmr020_facilityDate);
|
||
|
|
//apiList.Add(usp_ppmr020_facilityItem);
|
||
|
|
|
||
|
|
//apiList.Add(usp_ppmr020_item);
|
||
|
|
//apiList.Add(usp_ppmr020_itemDate);
|
||
|
|
//apiList.Add(usp_ppmr030_workOrder);
|
||
|
|
//apiList.Add(usp_ppmr030_facilityDate);
|
||
|
|
|
||
|
|
//apiList.Add(usp_ppmr030_facilityItem);
|
||
|
|
//apiList.Add(usp_ppmr030_item);
|
||
|
|
//apiList.Add(usp_ppmr030_itemDate);
|
||
|
|
//apiList.Add(usp_ppmr040_workOrder);
|
||
|
|
|
||
|
|
//apiList.Add(usp_ppmr040_facilityDate);
|
||
|
|
//apiList.Add(usp_ppmr040_facilityItem);
|
||
|
|
//apiList.Add(usp_ppmr040_item);
|
||
|
|
//apiList.Add(usp_ppmr040_itemDate);
|
||
|
|
|
||
|
|
foreach (var api in apiList)
|
||
|
|
{
|
||
|
|
var path = httpServer + api;
|
||
|
|
var query = $"?&limit=1&page=1";
|
||
|
|
var request = HTTPRequest.CreateGet(path+query, GetDataFormats);
|
||
|
|
request.AddHeader("access-token", apiKey);
|
||
|
|
|
||
|
|
request.Send();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public class rawData
|
||
|
|
{
|
||
|
|
public string status;
|
||
|
|
public string code;
|
||
|
|
public string message;
|
||
|
|
public raw data;
|
||
|
|
public string remark;
|
||
|
|
}
|
||
|
|
|
||
|
|
public class raw
|
||
|
|
{
|
||
|
|
public string count;
|
||
|
|
public string rows;
|
||
|
|
|
||
|
|
}
|
||
|
|
void GetDataFormats(HTTPRequest req, HTTPResponse resp)
|
||
|
|
{
|
||
|
|
switch (req.State)
|
||
|
|
{
|
||
|
|
case HTTPRequestStates.Finished:
|
||
|
|
if (resp.IsSuccess)
|
||
|
|
{
|
||
|
|
var payload = Encoding.UTF8.GetString(resp.Data);
|
||
|
|
Debug.Log($"{req.Uri.ToString()} = {payload.Split(',')[4]}");
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
Debug.Log($"Server sent an error: {resp.StatusCode}-{resp.Message}");
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
default:
|
||
|
|
Debug.LogError($"Request finished with error! Request state: {req.State}");
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
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 = $"Server sent an error: {resp.StatusCode}-{resp.Message}";
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
default:
|
||
|
|
isSuccess = false;
|
||
|
|
errorMessage = $"Request state: {req.State}";
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|