129 lines
3.9 KiB
C#
129 lines
3.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Text.RegularExpressions;
|
|
using System.Text;
|
|
using Unity.VisualScripting.Antlr3.Runtime;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using Newtonsoft.Json.Linq;
|
|
using Newtonsoft.Json.Serialization;
|
|
using Newtonsoft.Json;
|
|
|
|
public class ElectricityData
|
|
{
|
|
public int code { get; set; }
|
|
public string msg { get; set; }
|
|
public List<ElData> data { get; set; }
|
|
}
|
|
public class ElData
|
|
{
|
|
public float EH { get; set; }
|
|
public string State { get; set; }
|
|
public float P { get; set; }
|
|
}
|
|
public class CoalData
|
|
{
|
|
public int code { get; set; }
|
|
public string msg { get; set; }
|
|
public List<CoData> data { get; set; }
|
|
}
|
|
public class CoData
|
|
{
|
|
public float Coal { get; set; }
|
|
public string State { get; set; }
|
|
public float Carbon { get; set; }
|
|
}
|
|
public class WaterData
|
|
{
|
|
public int code { get; set; }
|
|
public string msg { get; set; }
|
|
public List<WaData> data { get; set; }
|
|
}
|
|
public class WaData
|
|
{
|
|
public float Water { get; set; }
|
|
public float OutletPressure { get; set; }
|
|
public float Speed { get; set; }
|
|
public float State { get; set; }
|
|
}
|
|
|
|
public enum GetType
|
|
{
|
|
Electricity,
|
|
Coal,
|
|
Water
|
|
}
|
|
|
|
public class ServerPort : MonoBehaviour
|
|
{
|
|
public static ServerPort Instance;
|
|
private const string UrlPath = "http://111.229.30.246:32761/api/";
|
|
private const string electricity = "GetFacilityInformation?name=";
|
|
private const string coal = "GetCoalEquipment?name=";
|
|
private const string water = "GetWaterEquipment?name=";
|
|
private void Awake()
|
|
{
|
|
Instance = this;
|
|
}
|
|
public void GetElectricity(string EquipmentName, Action<ElectricityData> actionResult)
|
|
{
|
|
ElectricityData temp = new ElectricityData();
|
|
StartCoroutine(Get(UrlPath + electricity + EquipmentName, g =>
|
|
{
|
|
temp = JsonConvert.DeserializeObject<ElectricityData>(g);
|
|
actionResult?.Invoke(temp);
|
|
|
|
}));
|
|
}
|
|
public void GetCoal(string EquipmentName, Action<CoalData> actionResult)
|
|
{
|
|
CoalData temp = new CoalData();
|
|
StartCoroutine(Get(UrlPath + coal + EquipmentName, g =>
|
|
{
|
|
temp = JsonConvert.DeserializeObject<CoalData>(g);
|
|
actionResult?.Invoke(temp);
|
|
}));
|
|
}
|
|
public void GetWater(string EquipmentName, Action<WaterData> actionResult)
|
|
{
|
|
WaterData temp = new WaterData();
|
|
StartCoroutine(Get(UrlPath + water + EquipmentName, g =>
|
|
{
|
|
temp = JsonConvert.DeserializeObject<WaterData>(g);
|
|
actionResult?.Invoke(temp);
|
|
}));
|
|
}
|
|
|
|
|
|
public IEnumerator Get(string path, Action<string> actionResult)
|
|
{
|
|
Debug.Log(string.Format("<b><color=#EDFF0B>[HttpGet]{0}</color></b>", "GetPath:" + path));
|
|
using (UnityWebRequest _request = new UnityWebRequest(path, UnityWebRequest.kHttpVerbGET))
|
|
{
|
|
_request.downloadHandler = new DownloadHandlerBuffer();
|
|
_request.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
|
|
_request.timeout = 60;
|
|
UnityWebRequestAsyncOperation op = _request.SendWebRequest();
|
|
yield return op;
|
|
while (!op.isDone)
|
|
{
|
|
yield return null;
|
|
}
|
|
string result = "";
|
|
if (op.webRequest.isHttpError || op.webRequest.isNetworkError)
|
|
{
|
|
Debug.Log("Get web resource failed: " + path);
|
|
Debug.Log("ErrorCode: " + op.webRequest.responseCode);
|
|
Debug.Log("Path:" + path + op.webRequest.error);
|
|
}
|
|
else
|
|
{
|
|
result = op.webRequest.downloadHandler.text;
|
|
}
|
|
result = Regex.Unescape(result);
|
|
actionResult?.Invoke(result);
|
|
Debug.Log(string.Format("<b><color=#EDFF0B>[HttpGet]{0}</color></b>", result));
|
|
}
|
|
}
|
|
} |