DefectiveArea_IN.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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 : 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()
  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. lock (LoggerT)
  147. {
  148. LoggerT.BeginTracking();
  149. string outputMessage = e.ToString();
  150. Logger.Trace(outputMessage, logKeyT, logger_t);
  151. FormLogShow?.ShowLog(outputMessage);
  152. if (string.IsNullOrWhiteSpace(e.Content))
  153. {
  154. outputMessage = "接收数据为空";
  155. Logger.Warn(outputMessage, logKeyT, logger_t);
  156. Logger.Warn(outputMessage, logKeyE, logger_e);
  157. FormLogShow?.ShowLog(outputMessage);
  158. return;
  159. }
  160. outputMessage = e.Content;
  161. Logger.Trace(outputMessage, logKeyT, logger_t);
  162. FormLogShow?.ShowLog(outputMessage);
  163. string[] c = e.Content.Split('#');
  164. string flag = null;
  165. if (c.Length > 0)
  166. {
  167. flag = c[0];
  168. }
  169. string barcode = null;
  170. if (c.Length > 1)
  171. {
  172. barcode = c[1];
  173. }
  174. if (string.IsNullOrWhiteSpace(flag))
  175. {
  176. //outputMessage = $"[{e.Content}]接收设备代码为空";
  177. //Logger.Warn(outputMessage, logKeyT, logger_t);
  178. //Logger.Warn(outputMessage, logKeyE, logger_e);
  179. //FormLogShow?.ShowLog(outputMessage);
  180. //return;
  181. flag = "00";
  182. }
  183. plc_s = GetPLC_S_DA_IN(flag, e, logKeyE);
  184. if (plc_s == null)
  185. {
  186. outputMessage = $"[{e.Content}]设备代码[{flag}]未找到ini配置或有错误";
  187. Logger.Error(null, outputMessage, logKeyT, logger_t);
  188. Logger.Error(null, outputMessage, logKeyE, logger_e);
  189. FormLogShow?.ShowLog(outputMessage);
  190. return;
  191. }
  192. outputMessage = $"[{e.Content}]PLC_Connect={JsonHelper.FromObject(plc_s)}";
  193. Logger.Trace(outputMessage, logKeyT, logger_t);
  194. FormLogShow?.ShowLog(outputMessage);
  195. plc = new SocketClient<SiemensS7_1200Model>();
  196. plc.Connect(plc_s.IP, plc_s.Port);
  197. //outputMessage = $"[{e.Content}]PLC_Connect=[{plc_s.IP}:{plc_s.Port}]";
  198. //Logger.Trace(outputMessage, logKeyT, logger_t);
  199. //FormLogShow?.ShowLog(outputMessage);
  200. if (string.IsNullOrWhiteSpace(barcode) ||
  201. barcode == "0" ||
  202. barcode.Length != 11 ||
  203. barcode.StartsWith("0"))
  204. {
  205. outputMessage = $"[{e.Content}]接收条码格式错误";
  206. Logger.Warn(outputMessage, logKeyT, logger_t);
  207. Logger.Warn(outputMessage, logKeyE, logger_e);
  208. FormLogShow?.ShowLog(outputMessage);
  209. //SendError(plc_s, e, logKeyT, logKeyE, plc);
  210. //return;
  211. barcode = "00000000000";
  212. }
  213. // 5. 写入PLC:barcode
  214. outputMessage = $"[{e.Content}]SET_BarCode=[{barcode}]{plc_s.Add_Code + plc_s.Add_CodeNum}";
  215. Logger.Trace(outputMessage, logKeyT, logger_t);
  216. FormLogShow?.ShowLog(outputMessage);
  217. PLCResult bar_result = plc.Write<string>(plc_s.Add_Code, plc_s.Add_CodeNum, barcode);
  218. outputMessage = $"[{e.Content}]END_BarCode=[{bar_result}]{plc_s.Add_Code + plc_s.Add_CodeNum}";
  219. Logger.Trace(outputMessage, logKeyT, logger_t);
  220. FormLogShow?.ShowLog(outputMessage);
  221. //DataRow codeRow = GetGoodsCode(barcode, e, logKeyE);
  222. //int groutingdailydetailid = 0;
  223. ////if (codeRow == null)
  224. ////{
  225. //// outputMessage = $"[{e.Content}]此条码[{barcode}]不存在";
  226. //// Logger.Warn(outputMessage, logKeyT, logger_t);
  227. //// Logger.Warn(outputMessage, logKeyE, logger_e);
  228. //// FormLogShow?.ShowLog(outputMessage);
  229. //// SendError(plc_s, e, logKeyT, logKeyE, plc);
  230. //// return;
  231. ////}
  232. //if (codeRow != null)
  233. //{
  234. // groutingdailydetailid = codeRow["groutingdailydetailid"].ToInt32();
  235. //}
  236. //outputMessage = $"[{e.Content}]GET_LenNo=[ ]{plc_s.Add_Len + plc_s.Add_LenNum}";
  237. //Logger.Trace(outputMessage, logKeyT, logger_t);
  238. //FormLogShow?.ShowLog(outputMessage);
  239. //PLCResult<short> len_result = plc.Read<short>(plc_s.Add_Len, plc_s.Add_LenNum);
  240. //outputMessage = $"[{e.Content}]END_LenNo=[{len_result.Data}]{plc_s.Add_Len + plc_s.Add_LenNum}";
  241. //Logger.Trace(outputMessage, logKeyT, logger_t);
  242. //FormLogShow?.ShowLog(outputMessage);
  243. //int lenNo = len_result.Data;
  244. //if (lenNo < 1 || lenNo > 6)
  245. //{
  246. // lenNo = 1;
  247. //}
  248. //// 4. 写入DB
  249. //int r = SetDefectiveArea(barcode, groutingdailydetailid, lenNo, e, logKeyE);
  250. //if (r < 1)
  251. //{
  252. // SendError(plc_s, e, logKeyT, logKeyE, plc);
  253. //}
  254. if (barcode.StartsWith("0"))
  255. {
  256. // 7. 写入PLC ERROR标识 = 0
  257. outputMessage = $"[{e.Content}]SET_E_Code=[1]{plc_s.Add_Error + plc_s.Add_ErrorNum}";
  258. Logger.Trace(outputMessage, logKeyT, logger_t);
  259. FormLogShow?.ShowLog(outputMessage);
  260. PLCResult e_result1 = plc.Write<short>(plc_s.Add_Error, plc_s.Add_ErrorNum, 1);
  261. outputMessage = $"[{e.Content}]END_E_Code=[{e_result1}]{plc_s.Add_Error + plc_s.Add_ErrorNum}";
  262. Logger.Trace(outputMessage, logKeyT, logger_t);
  263. FormLogShow?.ShowLog(outputMessage);
  264. return;
  265. }
  266. //xuwei add 2022-07-22 识别二部三部产品
  267. string depCode = GetDepCode(barcode, e, logKeyT);
  268. outputMessage = $"[{e.Content}]SET_Dep_Code=[{depCode}]{plc_s.Add_Dep_Code + plc_s.Add_Dep_CodeNum}";
  269. Logger.Trace(outputMessage, logKeyT, logger_t);
  270. FormLogShow?.ShowLog(outputMessage);
  271. PLCResult dep_result = plc.Write<short>(plc_s.Add_Dep_Code, plc_s.Add_Dep_CodeNum, Convert.ToInt16(depCode));
  272. outputMessage = $"[{e.Content}]END_Dep_Code=[{dep_result}]{plc_s.Add_Dep_Code + plc_s.Add_Dep_CodeNum}";
  273. Logger.Trace(outputMessage, logKeyT, logger_t);
  274. FormLogShow?.ShowLog(outputMessage);
  275. //xuwei end
  276. // 5. 写入PLC:FLAG=1 完成
  277. outputMessage = $"[{e.Content}]SET_F_Code=[1]{plc_s.Add_F_Code + plc_s.Add_F_CodeNum}";
  278. Logger.Trace(outputMessage, logKeyT, logger_t);
  279. FormLogShow?.ShowLog(outputMessage);
  280. PLCResult f_result = plc.Write<short>(plc_s.Add_F_Code, plc_s.Add_F_CodeNum, 1);
  281. outputMessage = $"[{e.Content}]END_F_Code=[{f_result}]{plc_s.Add_F_Code + plc_s.Add_F_CodeNum}";
  282. Logger.Trace(outputMessage, logKeyT, logger_t);
  283. FormLogShow?.ShowLog(outputMessage);
  284. // 7. 写入PLC ERROR标识 = 0
  285. outputMessage = $"[{e.Content}]SET_E_Code=[0]{plc_s.Add_Error + plc_s.Add_ErrorNum}";
  286. Logger.Trace(outputMessage, logKeyT, logger_t);
  287. FormLogShow?.ShowLog(outputMessage);
  288. PLCResult e_result = plc.Write<short>(plc_s.Add_Error, plc_s.Add_ErrorNum, 0);
  289. outputMessage = $"[{e.Content}]END_E_Code=[{e_result}]{plc_s.Add_Error + plc_s.Add_ErrorNum}";
  290. Logger.Trace(outputMessage, logKeyT, logger_t);
  291. FormLogShow?.ShowLog(outputMessage);
  292. }
  293. }
  294. catch (Exception ex)
  295. {
  296. FormLogShow?.ShowLog($"[{e.Content}]ERROR={ex.Message}");
  297. if (plc_s != null)
  298. {
  299. Logger.Error(ex, $"[{e.Content}]ERROR", logKeyT, logger_t);
  300. Logger.Error(ex, $"[{e.Content}]ERROR", logKeyE, logger_e);
  301. SendError(plc_s, e, logKeyT, logKeyE, plc);
  302. }
  303. else
  304. {
  305. Logger.Error(ex, $"[{e.Content}]ERROR-NOFLAG", logKeyT, logger_t);
  306. Logger.Error(ex, $"[{e.Content}]ERROR-NOFLAG", logKeyE, logger_e);
  307. }
  308. }
  309. finally
  310. {
  311. LoggerT?.EndTracking();
  312. plc?.Disconnect();
  313. plc?.Close();
  314. //e.ReturnMessage("0000");
  315. }
  316. }
  317. private void SendError(PLC_S_DA_IN plc_s, ReceiveSession e, string loggerNameT,
  318. string loggerNameE, SocketClient<SiemensS7_1200Model> plc)
  319. {
  320. try
  321. {
  322. //using (SocketClient<SiemensS7_1200Model> plc = new SocketClient<SiemensS7_1200Model>())
  323. {
  324. string outputMessage = $"[{e.Content}]SET_ErrorNum=[1]{plc_s.Add_Error + plc_s.Add_ErrorNum}";
  325. Logger.Trace(outputMessage, loggerNameT, logger_t);
  326. FormLogShow?.ShowLog(outputMessage);
  327. PLCResult p_r = plc?.Write<short>(plc_s.Add_Error, plc_s.Add_ErrorNum, (short)1);
  328. outputMessage = $"[{e.Content}]END_ErrorNum=[{p_r}]{plc_s.Add_Error + plc_s.Add_ErrorNum}";
  329. Logger.Trace(outputMessage, loggerNameT, logger_t);
  330. FormLogShow?.ShowLog(outputMessage);
  331. }
  332. }
  333. catch (Exception ex)
  334. {
  335. Logger.Error(ex, $"[{e.Content}]ERROR-SendError", loggerNameT, logger_t);
  336. Logger.Error(ex, $"[{e.Content}]ERROR-SendError", loggerNameE, logger_e);
  337. FormLogShow?.ShowLog($"[{e.Content}]ERROR-SendError={ex.Message}");
  338. }
  339. }
  340. private string GetDepCode(string barcode, ReceiveSession e, string loggerName)
  341. {
  342. IDataAccess dataAccess = null;
  343. try
  344. {
  345. dataAccess = PLC_S_DataAccess.GetDataAccess(e);
  346. string sqlString = @"
  347. SELECT
  348. SUBSTR(GROUTINGLINECODE,1,1)
  349. FROM
  350. TP_PM_INPRODUCTION PD
  351. WHERE
  352. PD.BARCODE = :BARCODE
  353. ";
  354. object depCode = dataAccess.ExecuteScalar(sqlString, new CDAParameter(":BARCODE", barcode));
  355. if(depCode is object)
  356. {
  357. if (depCode.ToString().ToUpper() == "C") return "3"; else return "2";
  358. }
  359. else
  360. {
  361. return "0";
  362. }
  363. }
  364. catch (Exception ex)
  365. {
  366. Logger.Error(ex, $"[{e.Content}]ERROR-GetDepCode", loggerName, logger_e);
  367. FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetDepCode={ex.Message}");
  368. return "-1";
  369. }
  370. finally
  371. {
  372. dataAccess?.Close();
  373. }
  374. }
  375. private DataRow GetGoodsCode(string barcode, ReceiveSession e, string loggerName)
  376. {
  377. IDataAccess dataAccess = null;
  378. try
  379. {
  380. dataAccess = PLC_S_DataAccess.GetDataAccess(e);
  381. //string sqlString = "select g.goodscode, g.seatcovercode, gdd.groutingdailydetailid\n" +
  382. string sqlString = "select gdd.groutingdailydetailid\n" +
  383. " from tp_pm_groutingdailydetail gdd\n" +
  384. //" inner join tp_mst_goods g\n" +
  385. //" on g.goodsid = gdd.goodsid\n" +
  386. " where gdd.barcode = :barcode";
  387. DataTable dataTable = dataAccess.ExecuteDatatable(sqlString, new CDAParameter(":barcode", barcode));
  388. if (dataTable.HasData())
  389. {
  390. return dataTable.Rows[0];
  391. }
  392. return null;
  393. }
  394. catch (Exception ex)
  395. {
  396. Logger.Error(ex, $"[{e.Content}]ERROR-GetGoodsCode", loggerName, logger_e);
  397. FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetGoodsCode={ex.Message}");
  398. return null;
  399. }
  400. finally
  401. {
  402. dataAccess?.Close();
  403. }
  404. }
  405. private int SetDefectiveArea(string barcode, int groutingdailydetailid, int lenNo,
  406. ReceiveSession e, string loggerName)
  407. {
  408. IDataAccess dataAccess = null;
  409. try
  410. {
  411. dataAccess = PLC_S_DataAccess.GetDataAccess(e);
  412. string sqlString = "INSERT INTO tp_pm_defectivearea\n" +
  413. " (BARCODE, GROUTINGDAILYDETAILID, LINENO, SEQNO, REMARKS, ACCOUNTID, VALUEFLAG)\n" +
  414. "VALUES\n" +
  415. " (@BARCODE@, @GROUTINGDAILYDETAILID@, @LINENO@, (select nvl(max(SEQNO), 0)+1 from tp_pm_defectivearea t where t.LINENO=@LINENO@), @REMARKS@, 1, '1')";
  416. CDAParameter[] parameters = new CDAParameter[]
  417. {
  418. new CDAParameter("BARCODE", barcode),
  419. new CDAParameter("GROUTINGDAILYDETAILID", groutingdailydetailid),
  420. new CDAParameter("LINENO", lenNo),
  421. new CDAParameter("REMARKS", null),
  422. };
  423. int r = dataAccess.ExecuteNonQuery(sqlString, parameters);
  424. return r;
  425. }
  426. catch (Exception ex)
  427. {
  428. Logger.Error(ex, $"[{e.Content}]ERROR-SetDefectiveArea", loggerName, logger_e);
  429. FormLogShow?.ShowLog($"[{e.Content}]ERROR-SetDefectiveArea={ex.Message}");
  430. return -1;
  431. }
  432. finally
  433. {
  434. dataAccess?.Close();
  435. }
  436. }
  437. private PLC_S_DA_IN GetPLC_S_DA_IN(string flag, ReceiveSession e, string loggerName)
  438. {
  439. try
  440. {
  441. flag = M_NAME + flag;
  442. if (PLC_FLAGS.ContainsKey(flag))
  443. {
  444. return PLC_FLAGS[flag];
  445. }
  446. else
  447. {
  448. PLC_S_DA_IN plc_s = new PLC_S_DA_IN();
  449. INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_{M_NAME}.ini");
  450. plc_s.IP = ini.Read(flag, "IP");
  451. plc_s.Port = ini.Read(flag, "Port").ToInt32();
  452. plc_s.Add_Code = ini.Read(flag, "Add_Code");
  453. plc_s.Add_CodeNum = ini.Read(flag, "Add_CodeNum");
  454. //plc_s.Add_Len = ini.Read(flag, "Add_Len");
  455. //plc_s.Add_LenNum = ini.Read(flag, "Add_LenNum");
  456. plc_s.Add_F_Code = ini.Read(flag, "Add_F_Code");
  457. plc_s.Add_F_CodeNum = ini.Read(flag, "Add_F_CodeNum");
  458. plc_s.Add_Error = ini.Read(flag, "Add_Error");
  459. plc_s.Add_ErrorNum = ini.Read(flag, "Add_ErrorNum");
  460. plc_s.Add_Dep_Code = ini.Read(flag, "Add_Dep_Code");
  461. plc_s.Add_Dep_CodeNum = ini.Read(flag, "Add_Dep_CodeNum");
  462. PLC_FLAGS.Add(flag, plc_s);
  463. return plc_s;
  464. }
  465. }
  466. catch (Exception ex)
  467. {
  468. Logger.Error(ex, $"[{e.Content}]ERROR-GetPLC_S_DA_IN", loggerName, logger_e);
  469. FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetPLC_S_DA_IN={ex.Message}");
  470. return null;
  471. }
  472. }
  473. }
  474. }