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.Net.Mail; using PLMTEST; using DataLinkMesWork; using System.Collections; using System.Drawing.Imaging; using Newtonsoft.Json.Linq; using System.Web.Http; using NPOI.SS.Formula.Functions; using System.IO; using System.Net.Sockets; using System.Security.Cryptography; using System.Windows.Forms; using NPOI.SS.Formula; using static System.Windows.Forms.VisualStyles.VisualStyleElement; using System.Net.NetworkInformation; using System.Resources; using System.Runtime.Versioning; using NPOI.SS.UserModel; using System.Diagnostics; using System.Runtime.Remoting.Metadata.W3cXsd2001; using System.Xml.Linq; using Adapters; using System.Security.Policy; namespace MesWork { public class Analysis_MOM_Msg { /// /// UUID生成 /// /// public static string UuidUtil() { string result = Guid.NewGuid().ToString(); return result; } /// /// 获取时间戳 /// /// public static Int64 GetTimeStamp() { TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0); return Convert.ToInt64(ts.TotalSeconds); } /// /// 接收检测数据 /// /// /// public static string OnlineCheckData(string url, [FromBody] JObject jobj) { var result = new msgResHeader(); result.MsgId = ""; result.Message = ""; result.Code = "200"; result.Data = ""; string errorMessage = ""; string parentID = ""; int AID = -1; int isError = 0; string str = jobj.ToString(); try { //存储日志 var CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); //Event_MOM_Log_Insert,@创建时间,@内容 var param = new SqlParameter[] { new SqlParameter("@接口地址",url), new SqlParameter("@接口类型",2), // 1. 主动调用接口 2. 被调用接口 new SqlParameter("@请求内容",str), new SqlParameter("@请求时间",CreateTime) }; DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("接口_IOT接口交互日志_请求记录", MesWorkForm.ConnectionString, ref param, out DataTable dt, out errorMessage); if (jobj == null) { result.Code = "500"; result.Message = "参数格式不正确!"; string retString1 = JsonConvert.SerializeObject(result); MW_Log.LoggerHelper.Info("接收检测数据_res", retString1); return retString1; } // 解析订单内容,存储数据库,根据实际业务来写 OnlineCheckData onlineCheckData = JsonConvert.DeserializeObject(str); if (onlineCheckData.MsgId == "") { throw new Exception("参数MsgId为空!"); } result.MsgId = onlineCheckData.MsgId; if (onlineCheckData.OpCode == "") { throw new Exception("参数OpCode为空!"); } if (onlineCheckData.ProductionCode == "") { throw new Exception("参数ProductionCode为空!"); } int Id_tagCF = 0; SqlParameter[] param1 = new SqlParameter[6]; param1[0] = new SqlParameter("@EngineID", onlineCheckData.ProductionCode); param1[1] = new SqlParameter("@工位号", onlineCheckData.OpCode); param1[2] = new SqlParameter("@操作者工号", onlineCheckData.CheckPerson); param1[3] = new SqlParameter("@扫码时间", onlineCheckData.ScanTime); param1[4] = new SqlParameter("@合格标志", onlineCheckData.QualityMark); param1[5] = new SqlParameter("@Id_tagCF", Id_tagCF); param1[5].Direction = ParameterDirection.Output; DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("CATL_测量数据合格索引_增加_测试设备", MesWorkForm.ConnectionString, ref param1, out errorMessage); if (errorMessage != "") { throw new Exception(errorMessage); } Id_tagCF = Convert.ToInt32(param1[5].Value.ToString()); List onlineCheckData_CheckDataList = onlineCheckData.CheckData; for (int i = 0;i < onlineCheckData_CheckDataList.Count;i++) { OnlineCheckData_CheckData onlineCheckData_CheckData = onlineCheckData_CheckDataList[i]; var param2 = new SqlParameter[] { new SqlParameter("@Id_tagCF", Id_tagCF), new SqlParameter("@EngineID", onlineCheckData.ProductionCode), new SqlParameter("@TagID",onlineCheckData_CheckData.CheckCode), new SqlParameter("@TagValue",onlineCheckData_CheckData.CheckValue), new SqlParameter("@IsGoodBad",onlineCheckData_CheckData.QualityMark), new SqlParameter("@工位号",onlineCheckData.OpCode), new SqlParameter("@理论值",onlineCheckData_CheckData.StandardValue), new SqlParameter("@上限值",onlineCheckData_CheckData.MaxValue), new SqlParameter("@下限值",onlineCheckData_CheckData.MinValue), new SqlParameter("@测量项目",onlineCheckData_CheckData.CheckName), new SqlParameter("@测量单位",onlineCheckData_CheckData.Unit), }; DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("CATL_测量数据合格_增加_测试设备", MesWorkForm.ConnectionString, ref param2, out errorMessage); if (errorMessage != "") { throw new Exception(errorMessage); } } } catch (Exception err) { result.Code = "500"; result.Message = err.Message; } string retString = JsonConvert.SerializeObject(result); MW_Log.LoggerHelper.Info("接收检测数据_res", retString); return retString; } /// /// 接收查询检测次数 /// /// /// public static string OnlineCheckDataCount(string url, [FromBody] JObject jobj) { var result = new msgResHeader2(); result.MsgId = ""; result.Message = ""; result.Code = "200"; result.Data = new checkCountData(); result.Data.CheckCountOk = 0; result.Data.CheckCountNg = 0; string errorMessage = ""; string parentID = ""; int AID = -1; if (jobj == null) { result.Code = "500"; result.Message = "参数格式不正确!"; string retString1 = JsonConvert.SerializeObject(result); MW_Log.LoggerHelper.Info("接收查询检测次数_res", retString1); return retString1; } int isError = 0; string str = jobj.ToString(); try { //存储日志 var CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); //Event_MOM_Log_Insert,@创建时间,@内容 var param = new SqlParameter[] { new SqlParameter("@接口地址",url), new SqlParameter("@接口类型",2), // 1. 主动调用接口 2. 被调用接口 new SqlParameter("@请求内容",str), new SqlParameter("@请求时间",CreateTime) }; DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("接口_IOT接口交互日志_请求记录", MesWorkForm.ConnectionString, ref param, out DataTable dt, out errorMessage); // 解析订单内容,存储数据库,根据实际业务来写 OnlineCheckData onlineCheckData = JsonConvert.DeserializeObject(str); if (onlineCheckData.MsgId == "") { throw new Exception("参数MsgId为空!"); } result.MsgId = onlineCheckData.MsgId; if (onlineCheckData.OpCode == "") { throw new Exception("参数OpCode为空!"); } if (onlineCheckData.ProductionCode == "") { throw new Exception("参数ProductionCode为空!"); } SqlParameter[] param1 = new SqlParameter[2]; param1[0] = new SqlParameter("@EngineID", onlineCheckData.ProductionCode); param1[1] = new SqlParameter("@工位号", onlineCheckData.OpCode); DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("CATL_测量数据合格_查询测试次数_测试设备", MesWorkForm.ConnectionString, ref param1, out DataTable dt1, out errorMessage); if (errorMessage != "") { throw new Exception(errorMessage); } if (dt1 != null && dt1.Rows.Count > 0) { result.Data.CheckCountOk = Convert.ToInt32(dt1.Rows[0]["OK"]); result.Data.CheckCountNg = Convert.ToInt32(dt1.Rows[0]["NG"]); } } catch (Exception err) { result.Code = "500"; result.Message = err.Message; } string retString = JsonConvert.SerializeObject(result); MW_Log.LoggerHelper.Info("接收查询检测次数_res", retString); return retString; } /// /// 接收查询检测次数 /// /// /// public static string OnlineCheckDataCountBool(string url, [FromBody] JObject jobj) { var result = new msgResHeader3(); result.MsgId = ""; result.Message = ""; result.Code = "200"; result.Data = new checkCountBoolData(); result.Data.isCheck = false; string errorMessage = ""; string parentID = ""; int AID = -1; if (jobj == null) { result.Code = "500"; result.Message = "参数格式不正确!"; string retString1 = JsonConvert.SerializeObject(result); MW_Log.LoggerHelper.Info("接收查询检测次数Bool_res", retString1); return retString1; } int isError = 0; string str = jobj.ToString(); try { //存储日志 var CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); //Event_MOM_Log_Insert,@创建时间,@内容 var param = new SqlParameter[] { new SqlParameter("@接口地址",url), new SqlParameter("@接口类型",2), // 1. 主动调用接口 2. 被调用接口 new SqlParameter("@请求内容",str), new SqlParameter("@请求时间",CreateTime) }; DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("接口_IOT接口交互日志_请求记录", MesWorkForm.ConnectionString, ref param, out DataTable dt, out errorMessage); // 解析订单内容,存储数据库,根据实际业务来写 OnlineCheckData onlineCheckData = JsonConvert.DeserializeObject(str); if (onlineCheckData.MsgId == "") { throw new Exception("参数MsgId为空!"); } result.MsgId = onlineCheckData.MsgId; if (onlineCheckData.OpCode == "") { throw new Exception("参数OpCode为空!"); } if (onlineCheckData.ProductionCode == "") { throw new Exception("参数ProductionCode为空!"); } SqlParameter[] param1 = new SqlParameter[2]; param1[0] = new SqlParameter("@EngineID", onlineCheckData.ProductionCode); param1[1] = new SqlParameter("@工位号", onlineCheckData.OpCode); DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("CATL_测量数据合格_查询测试次数_测试设备", MesWorkForm.ConnectionString, ref param1, out DataTable dt1, out errorMessage); if (errorMessage != "") { throw new Exception(errorMessage); } if (dt1 != null && dt1.Rows.Count > 0) { if(Convert.ToInt32(dt1.Rows[0]["OK"]) > 0 || Convert.ToInt32(dt1.Rows[0]["NG"]) > 0) { result.Data.isCheck = true; } } } catch (Exception err) { result.Code = "500"; result.Message = err.Message; } string retString = JsonConvert.SerializeObject(result); MW_Log.LoggerHelper.Info("接收查询检测次数_res", retString); return retString; } /// /// 接收打印任务 /// /// /// public static string ReceivePrintTask(string url, [FromBody] JObject jobj) { var result = new msgResHeader(); result.MsgId = ""; result.Message = ""; result.Code = "200"; result.Data = ""; string errorMessage = ""; string parentID = ""; int AID = -1; if (jobj == null) { result.Code = "500"; result.Message = "参数格式不正确!"; string retString1 = JsonConvert.SerializeObject(result); MW_Log.LoggerHelper.Info("接收打印任务_res", retString1); return retString1; } string str = jobj.ToString(); try { //存储日志 var CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); //Event_MOM_Log_Insert,@创建时间,@内容 var param = new SqlParameter[] { new SqlParameter("@接口地址",url), new SqlParameter("@接口类型",2), // 1. 主动调用接口 2. 被调用接口 new SqlParameter("@请求内容",str), new SqlParameter("@请求时间",CreateTime) }; DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("接口_IOT接口交互日志_请求记录", MesWorkForm.ConnectionString, ref param, out DataTable dt, out errorMessage); // 解析订单内容,存储数据库,根据实际业务来写 PrintTaskData printTaskData = JsonConvert.DeserializeObject(str); if (printTaskData.printTemplete == "") { throw new Exception("参数printTemplete为空!"); } if (printTaskData.productCode == "") { throw new Exception("参数productCode为空!"); } if (printTaskData.printDate == "") { throw new Exception("参数printDate为空!"); } string msg = $"MARK|{printTaskData.printTemplete}|{printTaskData.productCode},{printTaskData.printDate}"; // 连接打印机 Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { socket.Connect(MesWorkForm.MarkingMachine_Ip, int.Parse(MesWorkForm.MarkingMachine_Port)); } catch(Exception err) { throw new Exception("连接打印机失败:" + err.Message); } // 发送数据 try { if (socket.Connected) { byte[] decBytes = System.Text.Encoding.UTF8.GetBytes(msg); //socket.Send(decBytes); socket.Send(decBytes, 0, decBytes.Length, SocketFlags.None); } else { throw new Exception("连接打印机失败"); } } catch (Exception err) { throw new Exception("发送打印数据失败:" + err.Message); } // 关闭 try { if (socket.Connected) { socket.Shutdown(SocketShutdown.Both); socket.Close(100); } } catch (Exception ex) { } } catch (Exception err) { result.Code = "500"; result.Message = err.Message; } string retString = JsonConvert.SerializeObject(result); MW_Log.LoggerHelper.Info("接收打印任务_res", retString); return retString; } public static string GetCharData(string url, [FromBody] JObject jobj) { var result = new CharData(); result.reqId = ""; result.msg = ""; result.errorDetail = ""; result.code = "0"; result.data = new List(); string errorMessage = ""; string parentID = ""; int AID = -1; if (jobj == null) { result.code = "500"; result.msg = "参数格式不正确!"; string retString1 = JsonConvert.SerializeObject(result); MW_Log.LoggerHelper.Info("接收充电数据查询_res", retString1); return retString1; } string str = jobj.ToString(); try { //存储日志 var CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); //Event_MOM_Log_Insert,@创建时间,@内容 var param = new SqlParameter[] { new SqlParameter("@接口地址",url), new SqlParameter("@接口类型",2), // 1. 主动调用接口 2. 被调用接口 new SqlParameter("@请求内容",str), new SqlParameter("@请求时间",CreateTime) }; if (MesWorkForm.charDataToken == "") { throw new Exception("未获取到Token!"); } DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("接口_IOT接口交互日志_请求记录", MesWorkForm.ConnectionString, ref param, out DataTable dt, out errorMessage); CharDataSearchPrams charDataSearchPrams = JsonConvert.DeserializeObject(str); List testResultList = MesWorkForm.GetCharData_Details(charDataSearchPrams, out string code, out string errorDetail); //string syncTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); //for (int i = 0; i < testResultList.Count; i++) //{ // string vin = testResultList[i].deviceId; // string testCode = testResultList[i].testCode; // string testStatus = testResultList[i].testStatus; // string testBeginTime = testResultList[i].testBeginTime; // string testEndTime = testResultList[i].testEndTime; // string faultInfo = testResultList[i].faultInfo; // B_DB_Opera.BaseData_tagTable_CharData_Insert(vin, testCode, testStatus, testBeginTime, testEndTime, faultInfo, syncTime, out string errMsg); //} result.data = testResultList; result.code = code; result.msg = errorDetail; result.errorDetail = errorDetail; } catch (Exception err) { result.code = "500"; result.msg = err.Message; result.errorDetail = err.Message; } string retString = JsonConvert.SerializeObject(result); MW_Log.LoggerHelper.Info("接收充电数据查询_res", retString); return retString; } } }