using DBModel; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using XS_BLL; using XS_DAL; namespace XS_HttpServer_LR { public partial class XS_HttpServerToLR : Form { #region 全局变量 /// /// 展示数据 /// public string jsonData; /// /// 服务已启动 /// bool Connect { get; set; } List HttpPostRequest = new List(); /// /// 车辆实时上报 /// public int ti = 0; #endregion 全局变量 public XS_HttpServerToLR() { InitializeComponent(); } private void XS_HttpServerToLR_Load(object sender, EventArgs e) { XS_BLL.DisplaySet.CheckForIllegalCrossThreadCalls = false; XS_BLL.DisplaySet ds = new XS_BLL.DisplaySet(); ds.Dock = DockStyle.Fill; this.pDisplay.Controls.Add(ds); } #region 点击事件 /// /// 启动或停止接口 /// /// /// private void Btn_StartOrStop_Click(object sender, EventArgs e) { #region 数据连接验证 if (this.btn_StartOrStop.Text == "启动接口") { if (UtilityBLL.IsDbConnected(Global.connstring)) { new DisplaySet().AddListBoxItem("", "接数据库成功。。。"); } else { MessageBox.Show("数据库连接失败!!!"); new DisplaySet().AddListBoxItem("", "数据库连接失败。。。"); new DisplaySet().ErrorAddListBoxItem("", "数据库连接失败。。。"); return; } } #endregion string retTemp = string.Empty; #region 监听1 if (!Connect) { if (Global.deviceCell.Count <= 0) { DataTable dt = DBOperator.GetTableSql("select FCELLCODE as deviceCell,FLaneWay as tunnel from ST_CELL union (select CONVERT(varchar(20), F_DeviceIndex) as deviceCell,F_tunnel as tunnel from T_Base_Device) order by deviceCell"); for (int i = 0; i < dt.Rows.Count; i++) { Global.deviceCell.Add(dt.Rows[i]["deviceCell"].ToString(), dt.Rows[i]["tunnel"].ToString()); } } #region 发起请求线程 new Thread(() => upper_computer_interface()) { IsBackground = true }.Start(); #endregion Connect = true; this.btn_StartOrStop.Text = "停止接口"; this.button2.Text = "停止上传"; try { HttpPostRequest = new List(); HttpPostRequest.Add(new MyHttp() { HttpPostListener = new HttpListener(), HttpPostUrl = Global.Receive_HTTPURL }); foreach (var hpr in HttpPostRequest) { try { string url = hpr.HttpPostUrl; hpr.HttpPostListener.Prefixes.Add(url); hpr.HttpPostListener.Start(); Thread ThrednHttpPostRequest = new Thread(new ParameterizedThreadStart(HttpPostRequestHandle)) { IsBackground = true }; ThrednHttpPostRequest.Start(hpr); } catch (Exception ex) { new DisplaySet().AddListBoxItem("", ex.Message); } new DisplaySet().AddListBoxItem("", "启动监听成功:" + hpr.HttpPostUrl); } } catch (Exception ex) { new DisplaySet().AddListBoxItem("", "启动监听失败:" + ex.Message); } } else { Connect = false; this.btn_StartOrStop.Text = "启动接口"; this.button2.Text = "启动上传"; Thread.Sleep(2000); try { foreach (var hpr in HttpPostRequest) { hpr.HttpPostListener.Stop(); hpr.HttpPostListener.Prefixes.Clear(); hpr.HttpPostListener.Close(); hpr.HttpPostListener = null; new DisplaySet().AddListBoxItem("", "关闭监听成功:" + hpr.HttpPostUrl); } } catch (Exception ex) { new DisplaySet().AddListBoxItem("", "关闭监听失败:" + ex.Message); } } #endregion } /// /// 清除数据 /// /// /// private void BtnReset_Click(object sender, EventArgs e) { new DisplaySet().eliminate(); } /// /// 数据查询 /// /// /// private void Btn_Query_Click(object sender, EventArgs e) { new DisplaySet().FileExhibition(); } /// /// 连接配置 /// /// /// private void Btn_UpCon_Click(object sender, EventArgs e) { if (!Connect) { FrmSetConfig frm = new FrmSetConfig(); this.Visible = false; frm.ShowDialog(); //this.Dispose(); this.Visible = true; } else { MessageBox.Show("请先停止接口!!!"); } } #endregion 点击事件 #region 接口方法 private static object obj = new object(); /// /// 调取上位机WMS接口方法 /// private void upper_computer_interface() { Boolean done = false; try { while (!done) { lock (obj) { try { if (Connect) { //new DisplaySet().AddListBoxItem("", "接口运行中:"); //入库请求接口 WCS_REQUEST_IN_BLL.Wcs_request_in(); //任务状态上报接口 WCS_STATUS_FEEDBACK_BLL.Wcs_status_feedback(); //WCS取消任务接口 WCS_TASK_CANCEL_BLL.Wcs_task_cancel(); //WCS上报车辆状态 if (ti % 5 == 0) { WCS_STACK_STATUS_BLL.Wcs_stack_status(); } if (ti > 3600) { UtilityBLL.DelInterface(" io_interface ", " where INTERFACE_DATETIME < (DATEADD(DAY, -7, GETDATE()))"); ti = 0; } ti++; } } catch (Exception) { } Thread.Sleep(Global.ThreadTime * 1000); } } } catch (Exception) { } } /// /// 获取监听数据 /// /// private void HttpPostRequestHandle(object myHttp) { while (true) { try { if (myHttp is MyHttp) { HttpListenerContext rContext = (myHttp as MyHttp).HttpPostListener.GetContext(); Thread thread = new Thread(new ParameterizedThreadStart((context) => { HttpListenerContext MyContext = (HttpListenerContext)context; string RecieveBuffer = string.Empty; string en1 = MyContext.Request.ContentEncoding.EncodingName; string ContentType = MyContext.Request.ContentType; using (Stream myRequestStream = MyContext.Request.InputStream) { using (StreamReader myStreamReader = new StreamReader(myRequestStream, Encoding.UTF8)) { RecieveBuffer = myStreamReader.ReadToEnd(); myStreamReader.Close(); myRequestStream.Close(); } } MyContext.Response.StatusCode = 200; MyContext.Response.Headers.Add("Access-Control-Allow-Origin", "*"); MyContext.Response.ContentType = "application/json"; MyContext.Response.ContentEncoding = Encoding.UTF8; string url = MyContext.Request.Url.ToString(); string repose = string.Empty;//方法返回异常 string message = string.Empty;//方法名 string retjson = string.Empty;//返回JSON串 string direction = string.Empty;//接口方向 string methodName = string.Empty; string glUrl = Global.Receive_HTTPURL.TrimEnd('/'); if (url.Substring(url.Length - 1, 1) != "/") { methodName = url.Substring(url.LastIndexOf('/') + 1, url.Length - url.LastIndexOf('/') - 1); } else { methodName = UtilityBLL.GetReceiveValues(RecieveBuffer, "method"); } switch (methodName) { case "test": message = "测试接口"; direction = "WMS ==>> WCS"; retjson = "{\"code\":\"0\",\"message\":\"sucess\",\"data\":[{\"id\":\"485484115282370560\",\"orderId\":\"485823953844051968\",\"materialNo\":\"F03C.K38.784\",\"storageLocation\":\"M002\",\"storageBin\":\"A280330\",\"quantity\":\"2\",\"materialName\":\"10606无油衬套\",\"description\":\"C-MUBZ10-16\",\"timeStamp\":\"2022-09-02 16:21:00\"}]}"; break; case "wms_request_out": message = "01 出库任务接口"; direction = "WMS ==>> WCS"; retjson = WMS_REQUEST_OUT_BLL.Wms_request_out(RecieveBuffer); break; case "wms_request_move": message = "02 移库任务接口"; retjson = WMS_REQUEST_MOVE_BLL.Wms_request_move(RecieveBuffer); break; case "wms_request_cancel": message = "03 WMS取消任务接口"; direction = "WMS ==>> WCS"; retjson = WMS_REQUEST_CANCEL_BLL.Wms_request_cancel(RecieveBuffer); break; /* case "wcs_request_in": message = "04 入库请求接口"; direction = "WCS ==>> WMS"; retjson = WCS_REQUEST_IN_BLL.Wcs_request_in(RecieveBuffer); break; case "wcs_status_feedback": message = "05 任务状态上报接口"; direction = "WCS ==>> WMS"; retjson = WCS_STATUS_FEEDBACK_BLL.Wcs_status_feedback(RecieveBuffer); break; case "wcs_task_cancel": message = "06 WCS取消任务接口"; direction = "WCS ==>> WMS"; retjson = WCS_TASK_CANCEL_BLL.Wcs_task_cancel(RecieveBuffer); break; case "wcs_stack_status": message = "07 WCS上报车辆状态"; direction = "WCS ==>> WMS"; retjson = WCS_STACK_STATUS_BLL.Wcs_stack_status(RecieveBuffer); break; */ default: message = "无效方法:"; direction = "无效方法"; //XS_Model.T_RESULT rModdel = new XS_Model.T_RESULT(); //rModdel.code = "200"; //rModdel.msg = "无效方法"; //rModdel.taskId = ""; //retjson = JsonConvert.SerializeObject(rModdel); direction = "WMS ==>> WCS"; JObject objMis = new JObject(); objMis.Add(new JProperty("method", methodName)); objMis.Add(new JProperty("timestamp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); JObject objDate = new JObject(); objDate.Add(new JProperty("code", "200"));//库号 objDate.Add(new JProperty("msg", "无效方法"));//入库输送站台编号 objDate.Add(new JProperty("taskId", ""));//容器编号 //objDate.Add(new JProperty("containerCode", "T00000001")); //objDate.Add(new JProperty("sourceAddress", "12003")); //objDate.Add(new JProperty("targetAddress", "01-03-03")); //objDate.Add(new JProperty("tasklevel", "0")); objMis.Add(new JProperty("data", objDate)); retjson = JsonConvert.DeserializeObject(objMis.ToString()).ToString(); break; } jsonData = retjson; byte[] SendBuffer = System.Text.Encoding.UTF8.GetBytes(retjson); MyContext.Response.ContentLength64 = SendBuffer.Length; using (var myOutputStream = MyContext.Response.OutputStream) { myOutputStream.Write(SendBuffer, 0, SendBuffer.Length); myOutputStream.Close(); } })) { IsBackground = true }; thread.Start(rContext); } } catch (Exception) { } Thread.Sleep(50); } } #endregion 接口方法 public class MyHttp { /// /// 侦听器 /// public HttpListener HttpPostListener { get; set; } /// /// URL地址 /// public string HttpPostUrl { get; set; } } private void XS_HttpServerToLR_FormClosing(object sender, FormClosingEventArgs e) { if (Connect) { MessageBox.Show("请先停止接口!!!"); e.Cancel = true; } } private void Button2_Click(object sender, EventArgs e) { if (!Connect) { Connect = true; this.button2.Text = "停止上传"; new DisplaySet().AddListBoxItem("", "启动上传接口:"); } else { Connect = false; this.button2.Text = "启动上传"; new DisplaySet().AddListBoxItem("", "停止上传接口:"); } } private void CheckBox1_CheckedChanged(object sender, EventArgs e) { if (checkBox1.Checked == true) { Global.checkBox1 = 1; } else { Global.checkBox1 = 0; } } } }