using Newtonsoft.Json.Linq; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web.Http; using NPOI.POIFS.Properties; using Robots; using System.IO; using System.Net; using Opc.Ua; using NPOI.SS.Formula.Functions; namespace MesWork { public class AnalysisMsg { /// /// UUID生成 /// /// public static string UuidUtil() { string result = Guid.NewGuid().ToString(); return result; } /// /// IPC-接收装配信息 /// /// /// public static string product(string url, [FromBody] JObject jobj) { string MsgId = ""; var result = new msgResHeader(); result.Code = "200"; result.Message = ""; int AID = -1; string errorMessage = ""; if (jobj == null) { throw new Exception("参数格式不正确!"); } int isError = 0; string str = jobj.ToString(); publicCore.LogInsert("product", "Info", str); try { //存储日志 var CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); //Event_MOM_Log_Insert,@创建时间,@内容 var param1 = new SqlParameter[] { new SqlParameter("@接口地址",url), new SqlParameter("@接口类型",2), // 1. 主动调用接口 2. 被调用接口 new SqlParameter("@请求内容",str), new SqlParameter("@请求时间",CreateTime) }; DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("接口_IOT接口交互日志_请求记录", MesWorkForm.ConnectionString, ref param1, out DataTable dt, out errorMessage); if (errorMessage != "") { throw new Exception("数据库存储过程【接口_IOT接口交互日志_请求记录】调用失败!"); } if (dt.Rows.Count > 0) { AID = Convert.ToInt32(dt.Rows[0]["AID"]); } // 解析订单内容,存储数据库,根据实际业务来写 IPC_product productDataList = JsonConvert.DeserializeObject(str); List product_AssemblyInformation = productDataList.AssemblyInformation; string Location = productDataList.Location; MsgId = productDataList.MsgId; if (MsgId == "") { throw new Exception("参数MsgId为空!"); } if (Location == "") { throw new Exception("参数Location为空!"); } var parsingParam1 = new SqlParameter[] { new SqlParameter("@Location",Location), }; DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("接口_工位装配信息_删除", MesWorkForm.ConnectionString, ref parsingParam1, out errorMessage); if (errorMessage != "") { throw new Exception("数据库存储过程【接口_工位装配信息_删除】调用失败!"); } foreach (var assemblyInformation in product_AssemblyInformation) { if (assemblyInformation.productPosition <= 0) { throw new Exception("参数productPosition不正确!"); } if (assemblyInformation.productNo == "") { throw new Exception("参数productNo不正确!"); } var parsingParam = new SqlParameter[] { new SqlParameter("@Location",Location), new SqlParameter("@productNo",assemblyInformation.productNo), new SqlParameter("@MsgId",MsgId), new SqlParameter("@productName",assemblyInformation.productName), new SqlParameter("@productPosition",assemblyInformation.productPosition) }; DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("接口_工位装配信息", MesWorkForm.ConnectionString, ref parsingParam, out errorMessage); if (errorMessage != "") { throw new Exception("数据库存储过程【接口_工位装配信息】调用失败!"); } } publicCore.SolveAgvTaskNum(Location, "10"); publicCore.JudgeMaterialFull(Location, product_AssemblyInformation); publicCore.FixedAllow(Location, true); //响应结果 CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); var resParam = new SqlParameter[] { new SqlParameter("@AID",AID), new SqlParameter("@响应时间",CreateTime), new SqlParameter("@响应内容",JsonConvert.SerializeObject(result)) }; DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("接口_IOT接口交互日志_响应记录", MesWorkForm.ConnectionString, ref resParam, out errorMessage); if (dt.Rows.Count > 0) { AID = Convert.ToInt32(dt.Rows[0]["AID"]); } } catch (Exception err) { result.Code = "500"; result.Message = err.Message; result.MsgId = MsgId; } string retString = JsonConvert.SerializeObject(result); publicCore.LogInsert("product", "Info", "IPC-接收装配信息:" + retString); return retString; } /// /// IPC-查询设备可进入或可离开状态 /// /// /// public static string RequestEnterOrLeave(string url, [FromBody] JObject jobj) { string MsgId = ""; var result = new msgResHeader(); result.Code = "200"; result.Message = ""; int AID = -1; string errorMessage = ""; if (jobj == null) { throw new Exception("参数格式不正确!"); } int isError = 0; string str = jobj.ToString(); publicCore.LogInsert("RequestEnterOrLeave", "Info", str); try { //存储日志 var CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); //Event_MOM_Log_Insert,@创建时间,@内容 var param1 = new SqlParameter[] { new SqlParameter("@接口地址",url), new SqlParameter("@接口类型",2), // 1. 主动调用接口 2. 被调用接口 new SqlParameter("@请求内容",str), new SqlParameter("@请求时间",CreateTime) }; DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("接口_IOT接口交互日志_请求记录", MesWorkForm.ConnectionString, ref param1, out DataTable dt, out errorMessage); if (errorMessage != "") { throw new Exception("数据库存储过程【接口_IOT接口交互日志_请求记录】调用失败!"); } if (dt.Rows.Count > 0) { AID = Convert.ToInt32(dt.Rows[0]["AID"]); } // 解析订单内容,存储数据库,根据实际业务来写 IPC_RequestEnterOrLeave requestEnterOrLeave = JsonConvert.DeserializeObject(str); string Location = requestEnterOrLeave.Location; int RequestType = requestEnterOrLeave.RequestType; MsgId = requestEnterOrLeave.MsgId; if (MsgId == "") { throw new Exception("参数MsgId为空!"); } if (Location == "") { throw new Exception("参数Location为空!"); } if (RequestType != 1 && RequestType != 2) { throw new Exception("参数RequestType错误!"); } // 1:查询是否可以进入 if (RequestType == 1) { // 这块有问题,需要区分下是送 还是取 的请求进入 // 如果是上料的请求进入 执行11 // 1.AGV已到达上料区 // 如果是下料的请求进入 执行31 // 1.AGV已到达下料区 publicCore.FSSRequestMsgId = MsgId; publicCore.SolveAgvTaskNum(Location, "11"); } // 2:查询是否可以离开 if (RequestType == 2) { // 这块有问题,需要区分下是送 还是取 的请求离开 // 如果是上料的请求离开 执行21 // 1:AGV 上料完成 // 如果是下料的请求离开 执行41 // AGV正在离开 // 里面判断 publicCore.FSSRequestMsgId = MsgId; publicCore.SolveAgvTaskNum(Location, "21"); } //响应结果 CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); var resParam = new SqlParameter[] { new SqlParameter("@AID",AID), new SqlParameter("@响应时间",CreateTime), new SqlParameter("@响应内容",JsonConvert.SerializeObject(result)) }; DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("接口_IOT接口交互日志_响应记录", MesWorkForm.ConnectionString, ref resParam, out errorMessage); if (dt.Rows.Count > 0) { AID = Convert.ToInt32(dt.Rows[0]["AID"]); } } catch (Exception err) { result.Code = "500"; result.Message = err.Message; result.MsgId = MsgId; } string retString = JsonConvert.SerializeObject(result); publicCore.LogInsert("RequestEnterOrLeave", "Info", "IPC-查询设备可进入或可离开状态:" + retString); return retString; } /// /// IPC-上下料完成AGV离开 /// /// /// public static string WorkFinsih(string url, [FromBody] JObject jobj) { string MsgId = ""; var result = new msgResHeader(); result.Code = "200"; result.Message = ""; int AID = -1; string errorMessage = ""; if (jobj == null) { throw new Exception("参数格式不正确!"); } int isError = 0; string str = jobj.ToString(); publicCore.LogInsert("WorkFinsih", "Info", str); try { //存储日志 var CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); //Event_MOM_Log_Insert,@创建时间,@内容 var param1 = new SqlParameter[] { new SqlParameter("@接口地址",url), new SqlParameter("@接口类型",2), // 1. 主动调用接口 2. 被调用接口 new SqlParameter("@请求内容",str), new SqlParameter("@请求时间",CreateTime) }; DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("接口_IOT接口交互日志_请求记录", MesWorkForm.ConnectionString, ref param1, out DataTable dt, out errorMessage); if (errorMessage != "") { throw new Exception("数据库存储过程【接口_IOT接口交互日志_请求记录】调用失败!"); } if (dt.Rows.Count > 0) { AID = Convert.ToInt32(dt.Rows[0]["AID"]); } // 解析订单内容,存储数据库,根据实际业务来写 IPC_WorkFinsih WorkFinsih = JsonConvert.DeserializeObject(str); string Location = WorkFinsih.Location; int finishType = WorkFinsih.FinishType; MsgId = WorkFinsih.MsgId; if (MsgId == "") { throw new Exception("参数MsgId为空!"); } if (Location == "") { throw new Exception("参数Location为空!"); } if (finishType != 1 && finishType != 2) { throw new Exception("参数FinishType错误!"); } if (finishType == 1) { // 1.AGV上料完成 已离开 publicCore.SolveAgvTaskNum(Location, "51"); } if (finishType == 2) { // 1.AGV下料完成 已离开 publicCore.SolveAgvTaskNum(Location, "51"); publicCore.FixedAllow(Location, false); } //响应结果 CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); var resParam = new SqlParameter[] { new SqlParameter("@AID",AID), new SqlParameter("@响应时间",CreateTime), new SqlParameter("@响应内容",JsonConvert.SerializeObject(result)) }; DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("接口_IOT接口交互日志_响应记录", MesWorkForm.ConnectionString, ref resParam, out errorMessage); if (dt.Rows.Count > 0) { AID = Convert.ToInt32(dt.Rows[0]["AID"]); } } catch (Exception err) { result.Code = "500"; result.Message = err.Message; result.MsgId = MsgId; } string retString = JsonConvert.SerializeObject(result); publicCore.LogInsert("WorkFinsih", "Info", "IPC-上下料完成AGV离开res:" + retString); return retString; } public static string IPCDeviceStatus(string url, [FromBody] JObject jobj) { string MsgId = ""; var result = new msgResHeader(); result.Code = "200"; result.Message = ""; int AID = -1; string errorMessage = ""; if (jobj == null) { throw new Exception("参数格式不正确!"); } int isError = 0; string str = jobj.ToString(); publicCore.LogInsert("WorkFinsih", "Info", str); try { //存储日志 var CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); //Event_MOM_Log_Insert,@创建时间,@内容 var param1 = new SqlParameter[] { new SqlParameter("@接口地址",url), new SqlParameter("@接口类型",2), // 1. 主动调用接口 2. 被调用接口 new SqlParameter("@请求内容",str), new SqlParameter("@请求时间",CreateTime) }; DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("接口_IOT接口交互日志_请求记录", MesWorkForm.ConnectionString, ref param1, out DataTable dt, out errorMessage); if (errorMessage != "") { throw new Exception("数据库存储过程【接口_IOT接口交互日志_请求记录】调用失败!"); } if (dt.Rows.Count > 0) { AID = Convert.ToInt32(dt.Rows[0]["AID"]); } // 解析订单内容,存储数据库,根据实际业务来写 IPC_DeviceStatus WorkFinsih = JsonConvert.DeserializeObject(str); string Location = WorkFinsih.Location; MsgId = WorkFinsih.MsgId; if (MsgId == "") { throw new Exception("参数MsgId为空!"); } if (Location == "") { throw new Exception("参数Location为空!"); } // 调用接口 发送当前工位 状态 MesWork.publicCore.SolveDeviceStatus_WebApi_Use(Location); //响应结果 CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); var resParam = new SqlParameter[] { new SqlParameter("@AID",AID), new SqlParameter("@响应时间",CreateTime), new SqlParameter("@响应内容",JsonConvert.SerializeObject(result)) }; DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("接口_IOT接口交互日志_响应记录", MesWorkForm.ConnectionString, ref resParam, out errorMessage); if (dt.Rows.Count > 0) { AID = Convert.ToInt32(dt.Rows[0]["AID"]); } } catch (Exception err) { result.Code = "500"; result.Message = err.Message; result.MsgId = MsgId; } string retString = JsonConvert.SerializeObject(result); publicCore.LogInsert("WorkFinsih", "Info", "IPC-上下料完成AGV离开res:" + retString); return retString; } /// /// FSS-接收允许进入或离开 /// /// /// public static void ResponseEnterOrLeave(string Location, int ResponseType) { try { // 创建父集合 FSS_ResponseEnterOrLeave responseEnterOrLeave = new FSS_ResponseEnterOrLeave(); // 将管控传递来的MsgId 返回回去 responseEnterOrLeave.MsgId = publicCore.FSSRequestMsgId; //UuidUtil(); if (publicCore.FSSRequestMsgId == "") { publicCore.LogInsert("ResponseEnterOrLeave", "Info", "接口【FSS-接收允许进入或离开】未获取到请求时的MsgId 已使用随机UUID 请确认请求是否有误:" ); responseEnterOrLeave.MsgId = UuidUtil(); } responseEnterOrLeave.ResponseType = ResponseType; responseEnterOrLeave.Location = Location; responseEnterOrLeave.MsgSource = 1; responseEnterOrLeave.SendTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); //准备上传的数据 反序列化 string bodydata = JsonConvert.SerializeObject(responseEnterOrLeave); publicCore.LogInsert("ResponseEnterOrLeave", "Info", bodydata); string urlstr = MesWorkForm.ResponseEnterOrLeaveUrl; string retString; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlstr); request.Method = "post"; byte[] bytes = Encoding.UTF8.GetBytes(bodydata); request.Accept = "*/*"; request.ContentType = "application/json;charset=utf-8"; // 反序列化后先把JSON做接口记录 //存储日志 int AID = -1; var CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); //Event_MOM_Log_Insert,@创建时间,@内容 var reqParam = new SqlParameter[] { new SqlParameter("@接口地址",urlstr), new SqlParameter("@接口类型",1), // 1. 主动调用接口 2. 被调用接口 new SqlParameter("@请求内容",bodydata), new SqlParameter("@请求时间",CreateTime) }; DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("接口_IOT接口交互日志_请求记录", MesWorkForm.ConnectionString, ref reqParam, out DataTable reqDt, out string errorMessage); if (reqDt.Rows.Count > 0) { AID = Convert.ToInt32(reqDt.Rows[0]["AID"]); } request.ContentLength = bytes.Length; // 准备请求体 Stream myResponseStream = request.GetRequestStream(); myResponseStream.Write(bytes, 0, bytes.Length); //发送webapi请求 等待相应 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); retString = myStreamReader.ReadToEnd(); //响应结果 CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); var resParam = new SqlParameter[] { new SqlParameter("@AID",AID), new SqlParameter("@响应时间",CreateTime), new SqlParameter("@响应内容",retString) }; DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("接口_IOT接口交互日志_响应记录", MesWorkForm.ConnectionString, ref resParam, out errorMessage); // 序列化响应内容 msgResHeader mrh = JsonConvert.DeserializeObject(retString); int statusCode = (int)response.StatusCode; // 对响应内容进行处理 更新数据库标志 if (statusCode == 200) { // 清空FSS 请求MsgId publicCore.FSSRequestMsgId = ""; myStreamReader.Close(); myResponseStream.Close(); if (response != null) { // 失败 if (mrh.Code != "200") { publicCore.LogInsert("ResponseEnterOrLeave", "Error", "接口【FSS-接收允许进入或离开】失败:" + mrh.Message); } response.Close(); } if (request != null) { request.Abort(); } publicCore.LogInsert("ResponseEnterOrLeave", "Info", retString); } else { publicCore.LogInsert("ResponseEnterOrLeave", "Info", "接口【FSS-接收允许进入或离开】失败:"+ statusCode); } } catch (Exception err) { publicCore.LogInsert("ResponseEnterOrLeave", "Info", "接口【FSS-接收允许进入或离开】失败:" + err.Message); } } /// /// FSS-接收IPC下料请求 /// /// /// public static void FinishCall(string Location) { try { // 创建父集合 FSS_FinishCall finishCall = new FSS_FinishCall(); finishCall.MsgId = UuidUtil(); finishCall.Location = Location; finishCall.MsgSource = 1; finishCall.SendTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); //准备上传的数据 反序列化 string bodydata = JsonConvert.SerializeObject(finishCall); publicCore.LogInsert("FinishCall", "Info", bodydata); string urlstr = MesWorkForm.FinishCallUrl; string retString; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlstr); request.Method = "post"; byte[] bytes = Encoding.UTF8.GetBytes(bodydata); request.Accept = "*/*"; request.ContentType = "application/json;charset=utf-8"; // 反序列化后先把JSON做接口记录 //存储日志 int AID = -1; var CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); //Event_MOM_Log_Insert,@创建时间,@内容 var reqParam = new SqlParameter[] { new SqlParameter("@接口地址",urlstr), new SqlParameter("@接口类型",1), // 1. 主动调用接口 2. 被调用接口 new SqlParameter("@请求内容",bodydata), new SqlParameter("@请求时间",CreateTime) }; DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("接口_IOT接口交互日志_请求记录", MesWorkForm.ConnectionString, ref reqParam, out DataTable reqDt, out string errorMessage); if (reqDt.Rows.Count > 0) { AID = Convert.ToInt32(reqDt.Rows[0]["AID"]); } request.ContentLength = bytes.Length; // 准备请求体 Stream myResponseStream = request.GetRequestStream(); myResponseStream.Write(bytes, 0, bytes.Length); //发送webapi请求 等待相应 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); retString = myStreamReader.ReadToEnd(); //响应结果 CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); var resParam = new SqlParameter[] { new SqlParameter("@AID",AID), new SqlParameter("@响应时间",CreateTime), new SqlParameter("@响应内容",retString) }; DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("接口_IOT接口交互日志_响应记录", MesWorkForm.ConnectionString, ref resParam, out errorMessage); // 序列化响应内容 msgResHeader mrh = JsonConvert.DeserializeObject(retString); int statusCode = (int)response.StatusCode; // 对响应内容进行处理 更新数据库标志 if (statusCode == 200) { myStreamReader.Close(); myResponseStream.Close(); if (response != null) { // 失败 if (mrh.Code != "200") { publicCore.LogInsert("FinishCall", "Error", "接口【FSS-接收IPC下料请求】失败:" + mrh.Message); } response.Close(); } if (request != null) { request.Abort(); } publicCore.LogInsert("FinishCall", "Info", retString); } else { publicCore.LogInsert("FinishCall", "Error", "接口【FSS-接收IPC下料请求】失败:" + statusCode); } } catch (Exception err) { publicCore.LogInsert("FinishCall", "Error", "接口【FSS-接收IPC下料请求】失败:" + err.Message); } } /// /// FSS-接收IPC状态变化 /// /// /// public static void DeviceStatus(string Location, int DeviceStatus) { try { // 创建父集合 FSS_DeviceStatus deviceStatus = new FSS_DeviceStatus(); deviceStatus.MsgId = UuidUtil(); deviceStatus.Location = Location; deviceStatus.DeviceStatus = DeviceStatus; deviceStatus.MsgSource = 1; deviceStatus.SendTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); //准备上传的数据 反序列化 string bodydata = JsonConvert.SerializeObject(deviceStatus); publicCore.LogInsert("DeviceStatus", "Info", bodydata); string urlstr = MesWorkForm.DeviceStatusUrl; string retString; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlstr); request.Method = "post"; byte[] bytes = Encoding.UTF8.GetBytes(bodydata); request.Accept = "*/*"; request.ContentType = "application/json;charset=utf-8"; // 反序列化后先把JSON做接口记录 //存储日志 int AID = -1; var CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); //Event_MOM_Log_Insert,@创建时间,@内容 var reqParam = new SqlParameter[] { new SqlParameter("@接口地址",urlstr), new SqlParameter("@接口类型",1), // 1. 主动调用接口 2. 被调用接口 new SqlParameter("@请求内容",bodydata), new SqlParameter("@请求时间",CreateTime) }; DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("接口_IOT接口交互日志_请求记录", MesWorkForm.ConnectionString, ref reqParam, out DataTable reqDt, out string errorMessage); if (reqDt.Rows.Count > 0) { AID = Convert.ToInt32(reqDt.Rows[0]["AID"]); } request.ContentLength = bytes.Length; // 准备请求体 Stream myResponseStream = request.GetRequestStream(); myResponseStream.Write(bytes, 0, bytes.Length); //发送webapi请求 等待相应 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); retString = myStreamReader.ReadToEnd(); //响应结果 CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); var resParam = new SqlParameter[] { new SqlParameter("@AID",AID), new SqlParameter("@响应时间",CreateTime), new SqlParameter("@响应内容",retString) }; DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("接口_IOT接口交互日志_响应记录", MesWorkForm.ConnectionString, ref resParam, out errorMessage); // 序列化响应内容 msgResHeader mrh = JsonConvert.DeserializeObject(retString); int statusCode = (int)response.StatusCode; // 对响应内容进行处理 更新数据库标志 if (statusCode == 200) { myStreamReader.Close(); myResponseStream.Close(); if (response != null) { // 失败 if (mrh.Code != "200") { publicCore.LogInsert("DeviceStatus", "Error", "接口【FSS-接收IPC状态变化】失败" + mrh.Message); } response.Close(); } if (request != null) { request.Abort(); } publicCore.LogInsert("DeviceStatus", "Info", retString); } else { publicCore.LogInsert("DeviceStatus", "Error", "接口【FSS-接收IPC状态变化】失败" + statusCode); } } catch (Exception err) { publicCore.LogInsert("DeviceStatus", "Error", "接口【FSS-接收IPC状态变化】失败" + err.Message); } } } }