204 lines
7.0 KiB
C#
204 lines
7.0 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
using Nancy;
|
||
using Nancy.Hosting.Self;
|
||
using Newtonsoft.Json;
|
||
|
||
namespace HxBusiness
|
||
{
|
||
public class BusinessManage
|
||
{
|
||
#region 属性
|
||
private ConnectedManager _webClient;
|
||
private ConnectedManager webClient
|
||
{
|
||
get
|
||
{
|
||
if (_webClient != null) return _webClient;
|
||
else
|
||
{
|
||
_webClient = new ConnectedManager();
|
||
return _webClient;
|
||
}
|
||
}
|
||
}
|
||
|
||
private ApiModel _apiModel;
|
||
public ApiModel apiModel
|
||
{
|
||
get
|
||
{
|
||
if (_apiModel != null) return _apiModel;
|
||
else
|
||
{
|
||
_apiModel = new ApiModel()
|
||
{
|
||
requestUrl = "http://192.168.128.1:8080",
|
||
leaveUrl = "http://192.168.128.1:8080",
|
||
sendUrl = "http://192.168.128.1:8080"
|
||
};
|
||
return _apiModel;
|
||
}
|
||
}
|
||
set
|
||
{
|
||
_apiModel = value;
|
||
}
|
||
}
|
||
#endregion
|
||
public BusinessManage()
|
||
{
|
||
try
|
||
{
|
||
//启动服务
|
||
//数据处理
|
||
string result = new ConfigBiz().ReadDb<ApiModel>();
|
||
if (string.IsNullOrEmpty(result)) throw new Exception("请配置url");
|
||
apiModel = JsonConvert.DeserializeObject<ApiModel>(result);
|
||
//启动api服务
|
||
//HostConfiguration hostConfigs = new HostConfiguration();
|
||
//hostConfigs.UrlReservations.CreateAutomatically = true;
|
||
//Uri uri = new Uri(apiModel.sendUrl);
|
||
//var host = new NancyHost(hostConfigs, uri);
|
||
//host.Start();
|
||
WriteTxt.SaveLog1("启动api!");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
WriteTxt.SaveLog1("初始化失败!" + ex.ToString(), "异常日志");
|
||
}
|
||
}
|
||
public void Request(string agvid, string rfid, AgvState state)
|
||
{
|
||
if (state == null || !state.agvid.ToString().Equals(agvid)) return;
|
||
if (state.state == E_State.开始)
|
||
{
|
||
int requestCount = 0;
|
||
bool result = false;
|
||
ThreadPool.QueueUserWorkItem((o) =>
|
||
{
|
||
while (!result)
|
||
{
|
||
if (requestCount % 2 == 0)
|
||
{
|
||
result = RequestLeaved(agvid, rfid);
|
||
requestCount++;
|
||
}
|
||
Thread.Sleep(1000);
|
||
}
|
||
});
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 请求离开
|
||
/// </summary>
|
||
/// <param name="agvid">设备编号</param>
|
||
/// <param name="rfid">请求时地标</param>
|
||
/// <returns></returns>
|
||
public bool RequestLeaved(string agvid, string rfid)
|
||
{
|
||
try
|
||
{
|
||
if (apiModel == null)
|
||
{
|
||
WriteTxt.SaveLog1("路口请求时配置为空!", "异常日志");
|
||
return false;
|
||
}
|
||
if (string.IsNullOrEmpty(agvid) || string.IsNullOrEmpty(rfid))
|
||
{
|
||
WriteTxt.SaveLog1("路口请求时请求参数为空!", "异常日志");
|
||
return false;
|
||
}
|
||
|
||
|
||
RequestModel model = new RequestModel()
|
||
{
|
||
equipmentCode = agvid,
|
||
requestCode = "AGV" + agvid + "-" + rfid,
|
||
arriveTime = DateTime.Now.ToString("yyyyMMddHHmmssfff"),
|
||
taskPriority = 100,
|
||
taskDeadLine = DateTime.Now.ToString("yyyyMMddHHmmssfff"),
|
||
allowedQueueUp = false,
|
||
interchangePointCode = rfid
|
||
};
|
||
string postStr = JsonConvert.SerializeObject(model);
|
||
string result = webClient.PostForUrl(apiModel.requestUrl, postStr);
|
||
|
||
WriteTxt.SaveLog1(string.Format("路口请求url:{0},请求数据:{1},获取回复:{2}!", apiModel.requestUrl, postStr, result));
|
||
ResponseModel response = JsonConvert.DeserializeObject<ResponseModel>(result);
|
||
if (response.code == 0)
|
||
return true;
|
||
return false;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
WriteTxt.SaveLog1("路口请求离开异常!" + ex.ToString(), "异常日志");
|
||
return false;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 离开释放
|
||
/// </summary>
|
||
/// <param name="agvid">设备编号</param>
|
||
/// <param name="rfid">离开时地标</param>
|
||
/// <returns></returns>
|
||
public bool ByLeaved(string agvid, string rfid, string Cross_Location_Go)
|
||
{
|
||
try
|
||
{
|
||
if (apiModel == null)
|
||
{
|
||
WriteTxt.SaveLog1("离开时请求时配置为空!", "异常日志");
|
||
return false;
|
||
}
|
||
if (string.IsNullOrEmpty(agvid))
|
||
{
|
||
WriteTxt.SaveLog1("离开时请求时请求参数为空!", "异常日志");
|
||
return false;
|
||
}
|
||
|
||
RequestLeavedModel model = new RequestLeavedModel()
|
||
{
|
||
adapterCode = "HUAXIAO",
|
||
equipmentCode = agvid,
|
||
requestCode = "AGV" + agvid + "-" + Cross_Location_Go,
|
||
leaveTime = DateTime.Now.ToString("yyyyMMddHHmmssfff")
|
||
};
|
||
string postStr = JsonConvert.SerializeObject(model);
|
||
string result = webClient.PostForUrl(apiModel.leaveUrl, postStr);
|
||
|
||
WriteTxt.SaveLog1(string.Format("离开请求url:{0},请求数据:{1},获取回复:{2}!", apiModel.requestUrl, postStr, result));
|
||
ResponseModel response = JsonConvert.DeserializeObject<ResponseModel>(result);
|
||
if (response.code == 0)
|
||
return true;
|
||
return false;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
WriteTxt.SaveLog1("离开时请求离开异常!" + ex.ToString(), "异常日志");
|
||
return false;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 被通知启动
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public bool GetStart()
|
||
{
|
||
return true;
|
||
}
|
||
/// <summary>
|
||
/// 被通知停止
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public bool GetStop()
|
||
{
|
||
return true;
|
||
}
|
||
}
|
||
}
|