DefectiveArea_IN_OLD.cs 17 KB

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