public static TransData GetInstance(string[] parameters) { TransData td = new SocketService.TransData(); if (parameters.Length == 6) { td.FuncCode = parameters[0]; td.DeviceCode = parameters[1]; td.OpeIndex = parameters[2]; td.SN = parameters[3]; td.ProcessData = parameters[4]; td.Status = parameters[5]; } else { td.FuncCode = ""; td.DeviceCode = ""; td.OpeIndex = ""; td.SN = ""; td.ProcessData = ""; td.Status = ""; } return(td); }
public TransData ToTransData(string key, string body, string cmdFullText) { try { LogInfo log = null; if (body.Length == 0) { log = new SocketService.LogInfo(this.Session as MesSession, LogLevel.Info, GlobalData.Pre_Receive + body); log = new SocketService.LogInfo(this.Session as MesSession, LogLevel.Error, "协议解析出错,原因:发送协议内容为空."); SendError("The protocol is empty."); return(null); } string[] arrStr = body.Split(';'); TransData transData = new SocketService.TransData(); transData.FuncCode = arrStr[0]; switch (key) { case "HEA": #region 心跳 if (arrStr.Length != 4) { log = new SocketService.LogInfo(this.Session as MesSession, LogLevel.Info, GlobalData.Pre_Receive + body); log = new SocketService.LogInfo(this.Session as MesSession, LogLevel.Error, $"协议解析出错,原因:协议长度不对,(按分号分割,正确为4段),当前请求内容:{body}。"); SendError($"Parsing fail, should be 4 items, current is: [{body}]"); return(null); } transData.DeviceCode = arrStr[1]; transData.OpeIndex = arrStr[2]; transData.Status = arrStr[3]; #endregion log = new SocketService.LogInfo(this.Session as MesSession, LogLevel.Debug, GlobalData.Pre_Receive + body); break; default: #region 业务数据 if (arrStr.Length != 6) { log = new SocketService.LogInfo(this.Session as MesSession, LogLevel.Info, GlobalData.Pre_Receive + body); log = new SocketService.LogInfo(this.Session as MesSession, LogLevel.Error, $"协议解析出错,原因:协议长度不对,(按分号分割,正确为6段),当前请求内容:{body}。"); SendError($"Parsing fail, should be 6 items, current is: [{body}]"); return(null); } transData.DeviceCode = arrStr[1]; transData.OpeIndex = arrStr[2]; transData.SN = arrStr[3]; transData.ProcessData = arrStr[4]; transData.Status = arrStr[5]; #endregion log = new SocketService.LogInfo(this.Session as MesSession, LogLevel.Info, GlobalData.Pre_Receive + body); break; } return(transData); } catch (Exception e) { LogInfo log = new SocketService.LogInfo(this.Session as MesSession, LogLevel.Info, GlobalData.Pre_Receive + body); log = new SocketService.LogInfo(this.Session as MesSession, LogLevel.Error, $"协议解析出错,当前请求内容:{body};异常信息:{e.Message}。"); SendError($"Parsing fail, try catch, current is: [{body}]"); return(null); } }