GoodsCode_XW.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using Curtain.DataAccess;
  7. using Curtain.Extension.ExObjectConvert;
  8. using Curtain.Extension.ExSystemData;
  9. using Curtain.Framework.Json;
  10. using Curtain.Helpers;
  11. using Curtain.Log;
  12. using Curtain.Net.Sockets.PLC;
  13. using Curtain.Net.Sockets.PLC.Model;
  14. using Curtain.Net.Sockets.PLC.Model.Melsec;
  15. using Curtain.Net.Sockets.PLC.Model.Siemens;
  16. namespace PLC_S_1.ServerModel
  17. {
  18. /// <summary>
  19. /// 小件-成检-物料架
  20. /// </summary>
  21. public class GoodsCode_XW : SocketServer<SimpleSocketServerModel>, IShowFormLog, IWindowsServer
  22. {
  23. public Dictionary<string, PLC_S_GC_XW> PLC_FLAGS = new Dictionary<string, PLC_S_GC_XW>();
  24. public const string M_NAME = "GC_XW";
  25. private readonly Logger logger = Logger.CreateLogger(M_NAME);
  26. private readonly LogInfo logger_t = new LogInfo();
  27. private readonly LogInfo logger_e = new LogInfo();
  28. //private bool
  29. public int Interval
  30. {
  31. get;
  32. set;
  33. }
  34. public string EPTS_CODE
  35. {
  36. get;
  37. set;
  38. }
  39. public GoodsCode_XW()
  40. {
  41. logger.FileExistDays = 30;
  42. logger.FilePrefix = "";
  43. logger.FileSuffix = M_NAME;
  44. logger.LevelOneFile = true;
  45. logger.FileNameWithoutDate = true;
  46. logger_t.SubFolderFormat = "<name>";
  47. logger_t.FileExistDays = 30;
  48. logger_t.LevelOneFile = true;
  49. logger_e.FileExistDays = 10;
  50. this.Model.FormatType = CommandFormatType.StartStopChar;
  51. this.ServerStarting += ServerSocket_ServerStarting;
  52. this.ServerStarted += ServerSocket_ServerStarted;
  53. this.ServerStoping += ServerSocket_ServerStoping;
  54. this.ServerStoped += ServerSocket_ServerStoped;
  55. this.ServerMessage += ServerSocket_ServerMessage;
  56. this.Received += ServerSocket_Received;
  57. }
  58. #region Server
  59. public IFormLogShow FormLogShow
  60. {
  61. get;
  62. set;
  63. }
  64. private void ServerSocket_ServerStarting(object sender, CancelEventArgs e)
  65. {
  66. try
  67. {
  68. logger.OutputTrace($"{M_NAME}_ServerStarting");
  69. }
  70. catch { }
  71. }
  72. private void ServerSocket_ServerStarted(object sender, EventArgs e)
  73. {
  74. try
  75. {
  76. logger.OutputTrace($"{M_NAME}_ServerStarted");
  77. }
  78. catch { }
  79. }
  80. private void ServerSocket_ServerStoping(object sender, CancelEventArgs e)
  81. {
  82. try
  83. {
  84. logger.OutputTrace($"{M_NAME}_ServerStoping");
  85. }
  86. catch { }
  87. }
  88. private void ServerSocket_ServerStoped(object sender, EventArgs e)
  89. {
  90. try
  91. {
  92. logger.OutputTrace($"{M_NAME}_ServerStoped");
  93. }
  94. catch { }
  95. }
  96. private void ServerSocket_ServerMessage(object sender, ServerMessageEventArgs e)
  97. {
  98. try
  99. {
  100. string message = e.ToString();
  101. string cip = e?.Client?.IP;
  102. if (e.Type == ServerMessageType.Debug)
  103. {
  104. Logger.Debug(message, $"{M_NAME}-T[{cip}]", logger_t);
  105. }
  106. if (e.Type == ServerMessageType.Error)
  107. {
  108. Logger.Error(e.Exception, message, $"{M_NAME}-E[{cip}]", logger_e);
  109. }
  110. if (e.Type == ServerMessageType.Trace)
  111. {
  112. Logger.Trace(message, $"{M_NAME}-T[{cip}]", logger_t);
  113. }
  114. if (e.Type == ServerMessageType.Warning)
  115. {
  116. Logger.Warn(message, $"{M_NAME}-E[{cip}]", logger_e);
  117. }
  118. FormLogShow?.ShowLog("ServerMessage=" + message);
  119. }
  120. catch { }
  121. }
  122. #endregion
  123. /// <summary>
  124. /// 返回产品型号
  125. /// </summary>
  126. /// <param name="sender"></param>
  127. /// <param name="e"></param>
  128. private void ServerSocket_Received(object sender, ReceiveSession e)
  129. {
  130. PLC_S_GC_XW plc_s = null;
  131. string cIP = e?.Client?.IP;
  132. string logKeyT = $"{M_NAME}-T[{cIP}]";
  133. string logKeyE = $"{M_NAME}-E[{cIP}]";
  134. SocketClient<MelsecA1EAsciiModel> plc = null;
  135. Logger LoggerT = null;
  136. try
  137. {
  138. LoggerT = Logger.CreateLogger(logKeyT, logger_t);
  139. lock (LoggerT)
  140. {
  141. LoggerT.BeginTracking();
  142. string outputMessage = e.ToString();
  143. Logger.Trace(outputMessage, logKeyT, logger_t);
  144. FormLogShow?.ShowLog(outputMessage);
  145. if (string.IsNullOrWhiteSpace(e.Content))
  146. {
  147. outputMessage = "接收数据为空";
  148. Logger.Warn(outputMessage, logKeyT, logger_t);
  149. Logger.Warn(outputMessage, logKeyE, logger_e);
  150. FormLogShow?.ShowLog(outputMessage);
  151. return;
  152. }
  153. outputMessage = e.Content;
  154. Logger.Trace(outputMessage, logKeyT, logger_t);
  155. FormLogShow?.ShowLog(outputMessage);
  156. string[] c = e.Content.Split('#');
  157. string flag = null;
  158. if (c.Length > 0)
  159. {
  160. flag = c[0];
  161. }
  162. string barcode = null;
  163. if (c.Length > 1)
  164. {
  165. barcode = c[1];
  166. }
  167. if (string.IsNullOrWhiteSpace(flag))
  168. {
  169. outputMessage = $"[{e.Content}]接收设备代码为空";
  170. Logger.Warn(outputMessage, logKeyT, logger_t);
  171. Logger.Warn(outputMessage, logKeyE, logger_e);
  172. FormLogShow?.ShowLog(outputMessage);
  173. return;
  174. }
  175. plc_s = GetPLC_S_GC(flag, e, logKeyE);
  176. if (plc_s == null)
  177. {
  178. outputMessage = $"[{e.Content}]设备代码[{flag}]未找到ini配置或有错误";
  179. Logger.Error(null, outputMessage, logKeyT, logger_t);
  180. Logger.Error(null, outputMessage, logKeyE, logger_e);
  181. FormLogShow?.ShowLog(outputMessage);
  182. return;
  183. }
  184. outputMessage = $"[{e.Content}]PLC_Connect={JsonHelper.FromObject(plc_s)}";
  185. Logger.Trace(outputMessage, logKeyT, logger_t);
  186. FormLogShow?.ShowLog(outputMessage);
  187. plc = new SocketClient<MelsecA1EAsciiModel>();
  188. plc.Connect(plc_s.IP, plc_s.Port);
  189. //outputMessage = $"[{e.Content}]PLC_Connect=[{plc_s.IP}:{plc_s.Port}]";
  190. //Logger.Trace(outputMessage, logKeyT, logger_t);
  191. //FormLogShow?.ShowLog(outputMessage);
  192. if (string.IsNullOrWhiteSpace(barcode) ||
  193. barcode == "0" ||
  194. barcode.Length != 11 ||
  195. barcode.StartsWith("0"))
  196. {
  197. outputMessage = $"[{e.Content}]接收条码格式错误";
  198. Logger.Warn(outputMessage, logKeyT, logger_t);
  199. Logger.Warn(outputMessage, logKeyE, logger_e);
  200. FormLogShow?.ShowLog(outputMessage);
  201. SendError(plc_s, e, logKeyT, logKeyE, plc);
  202. return;
  203. }
  204. DataRow codeRow = GetGoodsCode(barcode, e, logKeyE);
  205. if (codeRow == null)
  206. {
  207. outputMessage = $"[{e.Content}]此条码[{barcode}]不存在";
  208. Logger.Warn(outputMessage, logKeyT, logger_t);
  209. Logger.Warn(outputMessage, logKeyE, logger_e);
  210. FormLogShow?.ShowLog(outputMessage);
  211. SendError(plc_s, e, logKeyT, logKeyE, plc);
  212. return;
  213. }
  214. string code = codeRow["goodscode"].ToString();
  215. string intcode = codeRow["seatcovercode"].ToString();
  216. outputMessage = $"[{e.Content}]产品型号={code}[{intcode}]";
  217. Logger.Trace(outputMessage, logKeyT, logger_t);
  218. FormLogShow?.ShowLog(outputMessage);
  219. if (string.IsNullOrWhiteSpace(intcode))
  220. {
  221. outputMessage = $"[{e.Content}]此条码[{barcode}]未设置型号代码";
  222. Logger.Error(null, outputMessage, logKeyT, logger_t);
  223. Logger.Error(null, outputMessage, logKeyE, logger_e);
  224. FormLogShow?.ShowLog(outputMessage);
  225. SendError(plc_s, e, logKeyT, logKeyE, plc);
  226. return;
  227. }
  228. PLCResult plc_r = null;
  229. outputMessage = $"[{e.Content}]SET_GoodsCode=[{intcode}]{plc_s.Add_Code + plc_s.Add_CodeNum}";
  230. Logger.Trace(outputMessage, logKeyT, logger_t);
  231. FormLogShow?.ShowLog(outputMessage);
  232. plc_r = plc.Write<int>(plc_s.Add_Code, plc_s.Add_CodeNum, (intcode.ToInt32()));
  233. outputMessage = $"[{e.Content}]END_GoodsCode=[{plc_r}]{plc_s.Add_Code + plc_s.Add_CodeNum}";
  234. Logger.Trace(outputMessage, logKeyT, logger_t);
  235. FormLogShow?.ShowLog(outputMessage);
  236. //2.状态(int)【型号写入完成(写入完成置1)】
  237. outputMessage = $"[{e.Content}]SET_F_Code=[1]{plc_s.Add_F_Code + plc_s.Add_F_CodeNum}";
  238. Logger.Trace(outputMessage, logKeyT, logger_t);
  239. FormLogShow?.ShowLog(outputMessage);
  240. plc_r = plc?.Write<int>(plc_s.Add_F_Code, plc_s.Add_F_CodeNum, 1);
  241. outputMessage = $"[{e.Content}]END_F_Code=[{plc_r}]{plc_s.Add_F_Code + plc_s.Add_F_CodeNum}";
  242. Logger.Trace(outputMessage, logKeyT, logger_t);
  243. FormLogShow?.ShowLog(outputMessage);
  244. }
  245. }
  246. catch (Exception ex)
  247. {
  248. FormLogShow?.ShowLog($"[{e.Content}]ERROR={ex.Message}");
  249. if (plc_s != null)
  250. {
  251. Logger.Error(ex, $"[{e.Content}]ERROR", logKeyT, logger_t);
  252. Logger.Error(ex, $"[{e.Content}]ERROR", logKeyE, logger_e);
  253. SendError(plc_s, e, logKeyT, logKeyE, plc);
  254. }
  255. else
  256. {
  257. Logger.Error(ex, $"[{e.Content}]ERROR-NOFLAG", logKeyT, logger_t);
  258. Logger.Error(ex, $"[{e.Content}]ERROR-NOFLAG", logKeyE, logger_e);
  259. }
  260. }
  261. finally
  262. {
  263. LoggerT?.EndTracking();
  264. plc?.Disconnect();
  265. plc?.Close();
  266. //e.ReturnMessage("0000");
  267. }
  268. }
  269. private void SendError(PLC_S_GC_XW plc_s, ReceiveSession e, string loggerNameT,
  270. string loggerNameE, SocketClient<MelsecA1EAsciiModel> plc)
  271. {
  272. try
  273. {
  274. //using (SocketClient<SiemensS7_1200Model> plc = new SocketClient<SiemensS7_1200Model>())
  275. {
  276. string outputMessage = $"[{e.Content}]SET_ErrorNum=[1]{plc_s.Add_F_Code + plc_s.Add_F_CodeNum}";
  277. Logger.Trace(outputMessage, loggerNameT, logger_t);
  278. FormLogShow?.ShowLog(outputMessage);
  279. PLCResult plc_r = plc?.Write<int>(plc_s.Add_F_Code, plc_s.Add_F_CodeNum, 0);
  280. outputMessage = $"[{e.Content}]END_ErrorNum=[{plc_r}]{plc_s.Add_F_Code + plc_s.Add_F_CodeNum}";
  281. Logger.Trace(outputMessage, loggerNameT, logger_t);
  282. FormLogShow?.ShowLog(outputMessage);
  283. }
  284. }
  285. catch (Exception ex)
  286. {
  287. Logger.Error(ex, $"[{e.Content}]ERROR-SendError", loggerNameT, logger_t);
  288. Logger.Error(ex, $"[{e.Content}]ERROR-SendError", loggerNameE, logger_e);
  289. FormLogShow?.ShowLog($"[{e.Content}]ERROR-SendError={ex.Message}");
  290. }
  291. }
  292. private DataRow GetGoodsCode(string barcode, ReceiveSession e, string loggerName)
  293. {
  294. IDataAccess dataAccess = null;
  295. try
  296. {
  297. dataAccess = PLC_S_DataAccess.GetDataAccess(e);
  298. string sqlString = "select g.goodscode, g.seatcovercode\n" +
  299. " from tp_pm_groutingdailydetail gdd\n" +
  300. " inner join tp_mst_goods g\n" +
  301. " on g.goodsid = gdd.goodsid\n" +
  302. " where gdd.barcode = :barcode";
  303. DataTable dataTable = dataAccess.ExecuteDatatable(sqlString, new CDAParameter(":barcode", barcode));
  304. if (dataTable.HasData())
  305. {
  306. return dataTable.Rows[0];
  307. }
  308. return null;
  309. }
  310. catch (Exception ex)
  311. {
  312. Logger.Error(ex, $"[{e.Content}]ERROR-GetGoodsCode", loggerName, logger_e);
  313. FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetGoodsCode={ex.Message}");
  314. return null;
  315. }
  316. finally
  317. {
  318. dataAccess?.Close();
  319. }
  320. }
  321. private PLC_S_GC_XW GetPLC_S_GC(string flag, ReceiveSession e, string loggerName)
  322. {
  323. try
  324. {
  325. flag = M_NAME + flag;
  326. if (PLC_FLAGS.ContainsKey(flag))
  327. {
  328. return PLC_FLAGS[flag];
  329. }
  330. else
  331. {
  332. PLC_S_GC_XW plc_s = new PLC_S_GC_XW();
  333. INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_{M_NAME}.ini");
  334. plc_s.IP = ini.Read(flag, "IP");
  335. plc_s.Port = ini.Read(flag, "Port").ToInt32();
  336. plc_s.Add_Code = ini.Read(flag, "Add_Code");
  337. plc_s.Add_CodeNum = ini.Read(flag, "Add_CodeNum");
  338. plc_s.Add_F_Code = ini.Read(flag, "Add_F_Code");
  339. plc_s.Add_F_CodeNum = ini.Read(flag, "Add_F_CodeNum");
  340. //plc_s.Add_Error = ini.Read(flag, "Add_Error");
  341. //plc_s.Add_ErrorNum = ini.Read(flag, "Add_ErrorNum");
  342. PLC_FLAGS.Add(flag, plc_s);
  343. return plc_s;
  344. }
  345. }
  346. catch (Exception ex)
  347. {
  348. Logger.Error(ex, $"[{e.Content}]ERROR-GetPLC_S_GC", loggerName, logger_e);
  349. FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetPLC_S_GC={ex.Message}");
  350. return null;
  351. }
  352. }
  353. }
  354. }