GoodsLevel_QR.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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.ExCompareOperator;
  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. using PLC_S.Proxy;
  17. namespace PLC_S.ServerModel
  18. {
  19. /// <summary>
  20. /// 返回产品等级(1:不合格,2:智能,3:连体,4:扫码异常,5:MES异常)等信息(毛坯入库对接乾润)
  21. /// </summary>
  22. public class GoodsLevel_QR : SocketServer<SimpleSocketServerModel>, IShowFormLog, IWindowsServer
  23. {
  24. public Dictionary<string, PLC_S_GL_QR> PLC_FLAGS = new Dictionary<string, PLC_S_GL_QR>();
  25. public const string M_NAME = "GL_QR";
  26. private MES_S _mes_s = null;
  27. private readonly Logger logger = Logger.CreateLogger(M_NAME);
  28. private readonly LogInfo logger_t = new LogInfo();
  29. private readonly LogInfo logger_e = new LogInfo();
  30. private bool _S_STOP = false;
  31. public int Interval
  32. {
  33. get;
  34. set;
  35. }
  36. public string EPTS_CODE
  37. {
  38. get;
  39. set;
  40. }
  41. public GoodsLevel_QR()
  42. {
  43. logger.FileExistDays = 30;
  44. logger.FilePrefix = "";
  45. logger.FileSuffix = M_NAME;
  46. logger.LevelOneFile = true;
  47. logger.FileNameWithoutDate = true;
  48. logger_t.SubFolderFormat = "<name>";
  49. logger_t.FileExistDays = 30;
  50. logger_t.LevelOneFile = true;
  51. logger_e.FileExistDays = 10;
  52. this.Model.FormatType = CommandFormatType.StartStopChar;
  53. this.ServerStarting += ServerSocket_ServerStarting;
  54. this.ServerStarted += ServerSocket_ServerStarted;
  55. this.ServerStoping += ServerSocket_ServerStoping;
  56. this.ServerStoped += ServerSocket_ServerStoped;
  57. this.ServerMessage += ServerSocket_ServerMessage;
  58. this.Received += ServerSocket_Received;
  59. _mes_s = MES_S.Get();
  60. }
  61. #region Server
  62. public IFormLogShow FormLogShow
  63. {
  64. get;
  65. set;
  66. }
  67. private void ServerSocket_ServerStarting(object sender, CancelEventArgs e)
  68. {
  69. try
  70. {
  71. _S_STOP = false;
  72. logger.OutputTrace($"{M_NAME}_ServerStarting");
  73. }
  74. catch { }
  75. }
  76. private void ServerSocket_ServerStarted(object sender, EventArgs e)
  77. {
  78. try
  79. {
  80. _S_STOP = false;
  81. logger.OutputTrace($"{M_NAME}_ServerStarted");
  82. }
  83. catch { }
  84. }
  85. private void ServerSocket_ServerStoping(object sender, CancelEventArgs e)
  86. {
  87. try
  88. {
  89. _S_STOP = false;
  90. logger.OutputTrace($"{M_NAME}_ServerStoping");
  91. }
  92. catch { }
  93. }
  94. private void ServerSocket_ServerStoped(object sender, EventArgs e)
  95. {
  96. try
  97. {
  98. _S_STOP = false;
  99. logger.OutputTrace($"{M_NAME}_ServerStoped");
  100. }
  101. catch { }
  102. }
  103. private void ServerSocket_ServerMessage(object sender, ServerMessageEventArgs e)
  104. {
  105. try
  106. {
  107. _S_STOP = false;
  108. string message = e.ToString();
  109. string cip = e?.Client?.IP;
  110. if (e.Type == ServerMessageType.Debug)
  111. {
  112. Logger.Debug(message, $"{M_NAME}-T[{cip}]", logger_t);
  113. }
  114. if (e.Type == ServerMessageType.Error)
  115. {
  116. Logger.Error(e.Exception, message, $"{M_NAME}-E[{cip}]", logger_e);
  117. }
  118. if (e.Type == ServerMessageType.Trace)
  119. {
  120. Logger.Trace(message, $"{M_NAME}-T[{cip}]", logger_t);
  121. }
  122. if (e.Type == ServerMessageType.Warning)
  123. {
  124. Logger.Warn(message, $"{M_NAME}-E[{cip}]", logger_e);
  125. }
  126. FormLogShow?.ShowLog("ServerMessage=" + message);
  127. }
  128. catch { }
  129. }
  130. #endregion
  131. int int_code = 0;
  132. /// <summary>
  133. /// 返回产品型号
  134. /// </summary>
  135. /// <param name="sender"></param>
  136. /// <param name="e"></param>
  137. private void ServerSocket_Received(object sender, ReceiveSession e)
  138. {
  139. PLC_S_GL_QR plc_s = null;
  140. string cIP = e?.Client?.IP;
  141. string logKeyT = $"{M_NAME}-T[{cIP}]";
  142. string logKeyE = $"{M_NAME}-E[{cIP}]";
  143. _S_STOP = false;
  144. SocketClient<SiemensS7_1200Model> plc = null;
  145. Logger LoggerT = null;
  146. try
  147. {
  148. LoggerT = Logger.CreateLogger(logKeyT, logger_t);
  149. lock (LoggerT)
  150. {
  151. LoggerT.BeginTracking();
  152. string outputMessage = e.ToString();
  153. Logger.Trace(outputMessage, logKeyT, logger_t);
  154. FormLogShow?.ShowLog(outputMessage);
  155. if (string.IsNullOrWhiteSpace(e.Content))
  156. {
  157. outputMessage = "接收数据为空";
  158. Logger.Warn(outputMessage, logKeyT, logger_t);
  159. Logger.Warn(outputMessage, logKeyE, logger_e);
  160. FormLogShow?.ShowLog(outputMessage);
  161. return;
  162. }
  163. outputMessage = e.Content;
  164. Logger.Trace(outputMessage, logKeyT, logger_t);
  165. FormLogShow?.ShowLog(outputMessage);
  166. string[] c = e.Content.Split('#');
  167. string flag = null;
  168. if (c.Length > 0)
  169. {
  170. flag = c[0];
  171. }
  172. string barcode = null;
  173. if (c.Length > 1)
  174. {
  175. barcode = c[1];
  176. }
  177. //if (flag == "1")
  178. //{
  179. // flag = "6311";
  180. //}
  181. //else if (flag == "2")
  182. //{
  183. // flag = "6312";
  184. //}
  185. //else
  186. //{
  187. // flag = null;
  188. //}
  189. if (string.IsNullOrWhiteSpace(flag))
  190. {
  191. outputMessage = $"[{e.Content}]接收设备代码为空";
  192. Logger.Warn(outputMessage, logKeyT, logger_t);
  193. Logger.Warn(outputMessage, logKeyE, logger_e);
  194. FormLogShow?.ShowLog(outputMessage);
  195. return;
  196. }
  197. plc_s = GetPLC_S_GC(flag, e, logKeyE);
  198. if (plc_s == null)
  199. {
  200. outputMessage = $"[{e.Content}]设备代码[{flag}]未找到ini配置或有错误";
  201. Logger.Error(null, outputMessage, logKeyT, logger_t);
  202. Logger.Error(null, outputMessage, logKeyE, logger_e);
  203. FormLogShow?.ShowLog(outputMessage);
  204. return;
  205. }
  206. outputMessage = $"[{e.Content}]PLC_Connect={JsonHelper.FromObject(plc_s)}";
  207. Logger.Trace(outputMessage, logKeyT, logger_t);
  208. FormLogShow?.ShowLog(outputMessage);
  209. plc = new SocketClient<SiemensS7_1200Model>();
  210. plc.Connect(plc_s.IP, plc_s.Port);
  211. //outputMessage = $"[{e.Content}]PLC_Connect=[{plc_s.IP}:{plc_s.Port}]";
  212. //Logger.Trace(outputMessage, logKeyT, logger_t);
  213. //FormLogShow?.ShowLog(outputMessage);
  214. if (string.IsNullOrWhiteSpace(barcode) ||
  215. barcode == "0" ||
  216. barcode.Length != 11 ||
  217. barcode.StartsWith("0"))
  218. {
  219. outputMessage = $"[{e.Content}]接收条码格式错误";
  220. Logger.Warn(outputMessage, logKeyT, logger_t);
  221. Logger.Warn(outputMessage, logKeyE, logger_e);
  222. FormLogShow?.ShowLog(outputMessage);
  223. SendCode(plc_s, e, logKeyT, logKeyE, plc, 4);
  224. return;
  225. }
  226. //int_code++;
  227. //if (int_code > 3)
  228. //{
  229. // int_code = 1;
  230. //}
  231. //if (int_code == 1)
  232. //{
  233. // SendCode(plc_s, e, logKeyT, logKeyE, plc, 1);
  234. //}
  235. //else if (int_code == 2)
  236. //{
  237. // SendCode(plc_s, e, logKeyT, logKeyE, plc, 2);
  238. //}
  239. //else if (int_code == 3)
  240. //{
  241. // SendCode(plc_s, e, logKeyT, logKeyE, plc, 3);
  242. //}
  243. //else
  244. //{
  245. // SendCode(plc_s, e, logKeyT, logKeyE, plc, 3);
  246. //}
  247. //return;
  248. DataRow codeRow = GetGoodsLevel(barcode, e, logKeyE);
  249. if (codeRow == null)
  250. {
  251. outputMessage = $"[{e.Content}]此条码[{barcode}]不存在";
  252. Logger.Warn(outputMessage, logKeyT, logger_t);
  253. Logger.Warn(outputMessage, logKeyE, logger_e);
  254. FormLogShow?.ShowLog(outputMessage);
  255. SendCode(plc_s, e, logKeyT, logKeyE, plc, 4);
  256. return;
  257. }
  258. string level = codeRow["GOODSLEVELTYPEID"].ToString();
  259. string gtid = codeRow["goodstypeid"].ToString();
  260. string p_id = codeRow["flowprocedureid"].ToString();
  261. int goodslinetype = Convert.ToInt32(codeRow["goodslinetype"]);
  262. // 1不合格,2智能,3连体
  263. short code = 1;
  264. string codeName = "不合格";
  265. if (string.IsNullOrEmpty(level) || level.EqualAny("1", "4"))
  266. {
  267. if (gtid.EqualAny("18", "19"))
  268. {
  269. code = 2;
  270. codeName = "良品[智能]";
  271. }
  272. else
  273. {
  274. code = 3;
  275. codeName = "良品[连体]";
  276. }
  277. }
  278. outputMessage = $"[{e.Content}]产品等级={codeName}[{code}][{level}]{gtid}";
  279. Logger.Trace(outputMessage, logKeyT, logger_t);
  280. FormLogShow?.ShowLog(outputMessage);
  281. if (code > 1 && p_id != plc_s.P_ID.ToString())
  282. {
  283. //xuwei add 2022-02-18 调整为工位打卡模式=================
  284. string userCode = "";
  285. //默认读取userCode
  286. if ( plc_s.WS_ID == 0)
  287. {
  288. userCode = plc_s.USER_CODE;
  289. }
  290. //优先从工位取userCode
  291. if (plc_s.WS_ID != 0)
  292. {
  293. userCode = GetUserCodeFromWorkStation(plc_s.WS_ID.ToString(), e, logKeyE);
  294. outputMessage = $"[{e.Content}]DB_UserCode=[{userCode}]";
  295. Logger.Trace(outputMessage, logKeyT, logger_t);
  296. FormLogShow?.ShowLog(outputMessage);
  297. }
  298. //xuwei end===============================================
  299. // 毛坯入库计件
  300. string result = SmartDeviceProxy.Instance.Invoke<string>(
  301. p => p.AddWorkPieceByStatus3(
  302. _mes_s.AccountCode,
  303. userCode,
  304. null,
  305. plc_s.P_ID,
  306. barcode,
  307. null));
  308. outputMessage = $"[{e.Content}]MES_Result=[{result}]";
  309. Logger.Trace(outputMessage, logKeyT, logger_t);
  310. FormLogShow?.ShowLog(outputMessage);
  311. // 返回状态(成功、失败)
  312. if (result != "OK")
  313. {
  314. if (result == "EE")
  315. {
  316. outputMessage = $"MES服务端异常(详情参见MES端日志)";
  317. }
  318. else if (result == "EU-01")
  319. {
  320. outputMessage = $"无效的帐套或工号(详情参见MES端日志)";
  321. }
  322. else
  323. {
  324. outputMessage = $"其他错误(详情参见MES端日志)[{result}]";
  325. }
  326. Logger.Error(null, outputMessage, logKeyT, logger_t);
  327. Logger.Error(null, outputMessage, logKeyE, logger_e);
  328. FormLogShow?.ShowLog(outputMessage);
  329. SendCode(plc_s, e, logKeyT, logKeyE, plc, 5);
  330. return;
  331. }
  332. }
  333. //1. 产品分级(int)【1不合格,2智能,3连体】
  334. SendCode(plc_s, e, logKeyT, logKeyE, plc, code);
  335. //2.高压标识(int)【1:高压,0:其他】
  336. SET_GoodsLineType(e, plc_s, logKeyT, plc, goodslinetype);
  337. }
  338. }
  339. catch (Exception ex)
  340. {
  341. FormLogShow?.ShowLog($"[{e.Content}]ERROR={ex.Message}");
  342. if (plc_s != null)
  343. {
  344. Logger.Error(ex, $"[{e.Content}]ERROR", logKeyT, logger_t);
  345. Logger.Error(ex, $"[{e.Content}]ERROR", logKeyE, logger_e);
  346. if (plc?.Socket?.Connected ?? false)
  347. {
  348. SendCode(plc_s, e, logKeyT, logKeyE, plc, 5);
  349. }
  350. }
  351. else
  352. {
  353. Logger.Error(ex, $"[{e.Content}]ERROR-NOFLAG", logKeyT, logger_t);
  354. Logger.Error(ex, $"[{e.Content}]ERROR-NOFLAG", logKeyE, logger_e);
  355. }
  356. }
  357. finally
  358. {
  359. LoggerT?.EndTracking();
  360. plc?.Disconnect();
  361. plc?.Close();
  362. //e.ReturnMessage("0000");
  363. }
  364. }
  365. private void SendCode(PLC_S_GL_QR plc_s, ReceiveSession e, string loggerNameT,
  366. string loggerNameE, SocketClient<SiemensS7_1200Model> plc, short code)
  367. {
  368. try
  369. {
  370. //using (SocketClient<SiemensS7_1200Model> plc = new SocketClient<SiemensS7_1200Model>())
  371. {
  372. // 扫码结果:【1:不合格,2:智能,3:连体,4:扫码异常,5:MES异常】
  373. string outputMessage = $"[{e.Content}]SET_CODE=[{code}]{plc_s.Add_Code + plc_s.Add_CodeNum}";
  374. Logger.Trace(outputMessage, loggerNameT, logger_t);
  375. FormLogShow?.ShowLog(outputMessage);
  376. PLCResult p_r = plc?.Write<short>(plc_s.Add_Code, plc_s.Add_CodeNum, code);
  377. outputMessage = $"[{e.Content}]END_CODE=[{p_r}]{plc_s.Add_Code + plc_s.Add_CodeNum}";
  378. Logger.Trace(outputMessage, loggerNameT, logger_t);
  379. FormLogShow?.ShowLog(outputMessage);
  380. }
  381. }
  382. catch (Exception ex)
  383. {
  384. Logger.Error(ex, $"[{e.Content}]ERROR-SendCode", loggerNameT, logger_t);
  385. Logger.Error(ex, $"[{e.Content}]ERROR-SendCode", loggerNameE, logger_e);
  386. FormLogShow?.ShowLog($"[{e.Content}]ERROR-SendCode={ex.Message}");
  387. }
  388. }
  389. //qq add 2025-05-27 增加高压标识
  390. private void SET_GoodsLineType(ReceiveSession e, PLC_S_GL_QR plc_s, string logKeyT, SocketClient<SiemensS7_1200Model> plc, int code)
  391. {
  392. if (!string.IsNullOrEmpty(plc_s.Add_GoodsLineType) && !string.IsNullOrEmpty(plc_s.Add_GoodsLineTypeNum))
  393. {
  394. string outputMessage = $"[{e.Content}]SET_GoodsLineType=[{code}]{plc_s.Add_GoodsLineType + plc_s.Add_GoodsLineTypeNum}";
  395. Logger.Trace(outputMessage, logKeyT, logger_t);
  396. FormLogShow?.ShowLog(outputMessage);
  397. PLCResult p_r = plc.Write<short>(plc_s.Add_GoodsLineType, plc_s.Add_GoodsLineTypeNum, (short)(code));
  398. outputMessage = $"[{e.Content}]END_GoodsLineType=[{p_r}]{plc_s.Add_GoodsLineType + plc_s.Add_GoodsLineTypeNum}";
  399. Logger.Trace(outputMessage, logKeyT, logger_t);
  400. FormLogShow?.ShowLog(outputMessage);
  401. }
  402. }
  403. private DataRow GetGoodsLevel(string barcode, ReceiveSession e, string loggerName)
  404. {
  405. IDataAccess dataAccess = null;
  406. try
  407. {
  408. dataAccess = PLC_S_DataAccess.GetDataAccess(e);
  409. string sqlString = "select gdd.GOODSLEVELTYPEID,g.goodstypeid, inp.flowprocedureid," +
  410. " case when g.GOODS_LINE_TYPE=1 then 1 else 2 end as goodslinetype \n" +
  411. " from tp_pm_groutingdailydetail gdd\n" +
  412. " LEFT JOIN tp_pm_inproduction inp on inp.groutingdailydetailid = gdd.groutingdailydetailid\n" +
  413. " inner join tp_mst_goods g on g.goodsid = gdd.goodsid\n" +
  414. " where gdd.barcode = :barcode";
  415. DataTable dataTable = dataAccess.ExecuteDatatable(sqlString, new CDAParameter(":barcode", barcode));
  416. if (dataTable.HasData())
  417. {
  418. return dataTable.Rows[0];
  419. }
  420. return null;
  421. }
  422. catch (Exception ex)
  423. {
  424. Logger.Error(ex, $"[{e.Content}]ERROR-GetGoodsLevel", loggerName, logger_e);
  425. FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetGoodsLevel={ex.Message}");
  426. return null;
  427. }
  428. finally
  429. {
  430. dataAccess?.Close();
  431. }
  432. }
  433. private string GetUserCodeFromWorkStation(string workStationId, ReceiveSession e, string loggerName)
  434. {
  435. try
  436. {
  437. using (IDataAccess conn = PLC_S_DataAccess.GetDataAccess())
  438. {
  439. string sqlStr = @"
  440. SELECT
  441. T.USERCODE
  442. FROM
  443. TP_MST_WORKSTATION T
  444. INNER JOIN TP_MST_USER U ON U.USERCODE = T.USERCODE
  445. WHERE
  446. T.WORKSTATIONID = :WORKSTATIONID
  447. ";
  448. object result = conn.ExecuteScalar(sqlStr, new CDAParameter(":WORKSTATIONID", workStationId));
  449. return result == null ? "0" : result.ToString();
  450. }
  451. }
  452. catch (Exception ex)
  453. {
  454. Logger.Error(ex, $"[{e.Content}]ERROR-GetUserCodeFromWorkStation", loggerName, logger_e);
  455. FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetUserCodeFromWorkStation={ex.Message}");
  456. return "0";
  457. }
  458. }
  459. private PLC_S_GL_QR GetPLC_S_GC(string flag, ReceiveSession e, string loggerName)
  460. {
  461. try
  462. {
  463. flag = M_NAME + flag;
  464. if (PLC_FLAGS.ContainsKey(flag))
  465. {
  466. return PLC_FLAGS[flag];
  467. }
  468. else
  469. {
  470. PLC_S_GL_QR plc_s = new PLC_S_GL_QR();
  471. INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_{M_NAME}.ini");
  472. plc_s.IP = ini.Read(flag, "IP");
  473. plc_s.Port = ini.Read(flag, "Port").ToInt32();
  474. plc_s.P_ID = ini.Read(flag, "P_ID").ToInt32();
  475. plc_s.USER_CODE = ini.Read(flag, "USER_CODE");
  476. plc_s.Add_Code = ini.Read(flag, "Add_Code");
  477. plc_s.Add_CodeNum = ini.Read(flag, "Add_CodeNum");
  478. plc_s.WS_ID = Convert.ToInt32(ini.Read(flag, "WS_ID"));
  479. plc_s.WS_NAME = ini.Read(flag, "WS_NAME");
  480. //plc_s.Add_F_Code = ini.Read(flag, "Add_F_Code");
  481. //plc_s.Add_F_CodeNum = ini.Read(flag, "Add_F_CodeNum");
  482. //plc_s.Add_Error = ini.Read(flag, "Add_Error");
  483. //plc_s.Add_ErrorNum = ini.Read(flag, "Add_ErrorNum");
  484. //qq add 2025-05-27 增加高压标识
  485. plc_s.Add_GoodsLineType = ini.Read(flag, "Add_GoodsLineType");
  486. plc_s.Add_GoodsLineTypeNum = ini.Read(flag, "Add_GoodsLineTypeNum");
  487. PLC_FLAGS.Add(flag, plc_s);
  488. return plc_s;
  489. }
  490. }
  491. catch (Exception ex)
  492. {
  493. Logger.Error(ex, $"[{e.Content}]ERROR-GetPLC_S_GC", loggerName, logger_e);
  494. FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetPLC_S_GC={ex.Message}");
  495. return null;
  496. }
  497. }
  498. }
  499. }