U3D_TobaccoWarehouseISMDTSy.../Assets/Scripts/YL/InterfaceManager.cs

284 lines
9.2 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public static class InterfaceManager
{
private static string _IP;
public static string IP
{
get { return _IP; }
set
{
if (_IP != value)
{
_IP = value;
}
}
}
private static string _Port;
public static string Port
{
get { return _Port; }
set
{
if (_Port != value)
{
_Port = value;
}
}
}
public static string IpAddress { get => string.Format("http://{0}:{1}", IP, Port); }
public static string GetLocalTxt(string path)
{
using (System.IO.StreamReader reader = new System.IO.StreamReader(path)) { return reader.ReadToEnd(); }
}
/// <summary>
/// 获取小车接口
/// </summary>
public static string Carinterface { get => IpAddress + "/api/kepserver/car_Info?car_id="; }
/// <summary>
/// 获取提升机弹窗
/// </summary>
public static string Hoisinterface { get => IpAddress + "/api/kepserver/eLV_Info/"; }
/// <summary>
/// 码垛机弹窗
/// </summary>
public static string Palletizerface { get => IpAddress + "/api/kepserver/dPM_Info/"; }
/// <summary>
/// 换电池系统
/// </summary>
public static string Batteryface { get => IpAddress + "/api/kepserver/qCBD_Info/"; }
/// <summary>
/// 获取箱子接口
/// </summary>
public static string Boxface { get => IpAddress + "/api/wmsLocation/getLocation"; }
/// <summary>
/// 箱子信息接口
/// </summary>
public static string Boxinformationface { get => IpAddress + "/api/wmsLocation/palletDetail/"; }
/// <summary>
/// 落地式提升机
/// </summary>
public static string Floorface { get => IpAddress + "/api/kepserver/fE_Info/"; }
/// <summary>
/// 加去盖机
/// </summary>
public static string Decapface { get => IpAddress + "/api/kepserver/cM_Info/"; }
/// <summary>
/// 出库机
/// </summary>
public static string Outoftheface { get => IpAddress + "/api/kepserver/convoyors_OUT_Info/"; }
/// <summary>
/// 入库机
/// </summary>
public static string Intotheface { get => IpAddress + "/api/kepserver/convoyors_IN_Info/"; }
/// <summary>
/// MQTT初始化接口
/// </summary>
public static string Initializes { get => IpAddress + "/api/wmsLocation/palletDetailByPalletNum/"; }
/// <summary>
/// 通过任务号获取信息
/// </summary>
public static string Tasknumber { get => IpAddress + "/api/location/PalletCycleDetailByPalletNum?palletNum="; }
/// <summary>
/// 隐藏部分箱子
/// </summary>
public static string Batchnumber { get => IpAddress + "/api/wmsLocation/GetComponentByBatch?batch="; }
public static string Highlights { get => IpAddress + "/api/location/GetPalletCycleWithLocation?rfidCode="; }
public static IEnumerator Getbytes(string url, Action<string> callback)
{
using (UnityWebRequest www = new UnityWebRequest(url))
{
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.ProtocolError)
{
callback(null);
Debug.LogError("网络请求失败了");
}
else
{
if (!string.IsNullOrEmpty(www.downloadHandler.text))
{
callback(www.downloadHandler.text);
}
else
{
callback(null);
Debug.Log("没有获取到数据");
}
}
}
}
/// <summary>
/// Get网络请求
/// </summary>
/// <param name="url">路径</param>
/// <param name="callback">方法回调</param>
/// <returns></returns>
public static IEnumerator Getstring(string url, Action<string, string> callback)
{
using (UnityWebRequest www = UnityWebRequest.Get(url))
{
www.SetRequestHeader("Accept-Charset", "UTF-8");
www.SetRequestHeader("Cache-Control", "no-cache");
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.ProtocolError)
{
callback(null, www.error);
Debug.LogError("网络请求失败了");
}
else
{
if (!string.IsNullOrEmpty(www.downloadHandler.text))
{
callback(www.downloadHandler.text, null);
}
else
{
callback(null, null);
Debug.Log("没有获取到数据");
}
}
}
}
/// <summary>
/// Get网络请求
/// </summary>
/// <param name="url">路径</param>
/// <param name="callback">方法回调</param>
/// <returns></returns>
public static IEnumerator Getstring(string url, Action<string> callback)
{
using (UnityWebRequest www = UnityWebRequest.Get(url))
{
www.SetRequestHeader("Accept-Charset", "UTF-8");
www.SetRequestHeader("Cache-Control", "no-cache");
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.ProtocolError)
{
callback(null);
Debug.LogError("网络请求失败了");
}
else
{
if (!string.IsNullOrEmpty(www.downloadHandler.text))
{
callback(www.downloadHandler.text);
}
else
{
callback(null);
Debug.Log("没有获取到数据");
}
}
}
}
/// <summary>
/// Get每隔一段时间请求一下
/// </summary>
/// <param name="url">请求路径</param>
/// <param name="tiem">每隔多长时间请求一下</param>
/// <param name="callback">回调方法</param>
/// <returns></returns>
public static IEnumerator Getstring(string url, int tiem, Action<string> callback)
{
while (true)
{
using (UnityWebRequest www = UnityWebRequest.Get(url))
{
yield return new WaitForSeconds(tiem);
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.ProtocolError)
{
callback(null);
Debug.LogError("网络请求失败了");
}
else
{
if (!string.IsNullOrEmpty(www.downloadHandler.text))
{
callback(www.downloadHandler.text);
}
else
{
callback(null);
Debug.Log("没有请求到数据");
}
}
}
}
}
/// <summary>
/// post的请求接口
/// </summary>
/// <param name="url">路径</param>
/// <param name="keyvaluepir">字典方式传入值</param>
/// <param name="callback">回调方法</param>
/// <returns></returns>
public static IEnumerator Poststring(string url, Dictionary<string, string> keyvaluepir, Action<bool, string> callback)
{
WWWForm wWWForm = new WWWForm();
foreach (var item in keyvaluepir)
{
wWWForm.AddField(item.Key, item.Value);
}
UnityWebRequest www = UnityWebRequest.Post(url, wWWForm);
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.ProtocolError)
{
callback(false, null);
Debug.LogError("网络请求失败了");
}
else
{
if (!string.IsNullOrEmpty(www.downloadHandler.text))
{
callback(true, www.downloadHandler.text);
}
else
{
callback(false, null);
Debug.Log("没有请求到数据");
}
}
}
/// <summary>
/// Post的请求
/// </summary>
/// <param name="url">路径</param>
/// <param name="action">返回的数据</param>
/// <returns></returns>
public static IEnumerator Post1(string url, System.Action<string> action)
{
WWWForm form = new WWWForm();//网络通信
using (UnityWebRequest request = UnityWebRequest.Post(url, form))
{
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
{
action(null);
Debug.Log("网络请求失败了");
}
else
{
if (!string.IsNullOrEmpty(request.downloadHandler.text))
{
action(request.downloadHandler.text);
}
else
{
action(null);
}
}
}
}
}