using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public static class InterfaceManager
{
private static string _IP = "10.155.161.49";
public static string IP
{
get { return _IP; }
set
{
if (_IP != value)
{
_IP = value;
}
}
}
private static string _Port = "8083";
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(); }
}
///
/// 获取小车接口
///
public static string Carinterface { get => IpAddress + "/api/kepserver/car_Info/"; }
///
/// 获取提升机弹窗
///
public static string Hoisinterface { get => IpAddress + "/api/kepserver/eLV_Info/"; }
///
/// 码垛机弹窗
///
public static string Palletizerface { get => IpAddress + "/api/kepserver/dPM_Info/"; }
///
/// 换电池系统
///
public static string Batteryface { get => IpAddress + "/api/kepserver/qCBD_Info/"; }
///
/// 获取箱子接口
///
public static string Boxface { get => IpAddress + "/api/wmsLocation/getLocation"; }
///
/// 箱子信息接口
///
public static string Boxinformationface { get => IpAddress + "/api/wmsLocation/palletDetail/"; }
///
/// 落地式提升机
///
public static string Floorface { get => IpAddress + "/api/kepserver/fE_Info/"; }
///
/// 加去盖机
///
public static string Decapface { get => IpAddress + "/api/kepserver/cM_Info/"; }
///
/// 出库机
///
public static string Outoftheface { get => IpAddress + "/api/kepserver/convoyors_OUT_Info/"; }
///
/// 入库机
///
public static string Intotheface { get => IpAddress + "/api/kepserver/convoyors_IN_Info/"; }
///
/// MQTT初始化接口
///
public static string Initializes { get => IpAddress + "/api/wmsLocation/palletDetailByPalletNum/"; }
public static IEnumerator Getbytes(string url, Action 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("没有获取到数据");
}
}
}
}
///
/// Get网络请求
///
/// 路径
/// 方法回调
///
public static IEnumerator Getstring(string url, Action callback)
{
using (UnityWebRequest www = UnityWebRequest.Get(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("没有获取到数据");
}
}
}
}
///
/// Get每隔一段时间请求一下
///
/// 请求路径
/// 每隔多长时间请求一下
/// 回调方法
///
public static IEnumerator Getstring(string url, int tiem, Action 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("没有请求到数据");
}
}
}
}
}
///
/// post的请求接口
///
/// 路径
/// 字典方式传入值
/// 回调方法
///
public static IEnumerator Poststring(string url, Dictionary keyvaluepir, Action 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("没有请求到数据");
}
}
}
///
/// Post的请求
///
/// 路径
/// 返回的数据
///
public static IEnumerator Post1(string url, System.Action 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);
}
}
}
}
}