GoodsLevel_QR.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. // 1不合格,2智能,3连体
  262. short code = 1;
  263. string codeName = "不合格";
  264. if (string.IsNullOrEmpty(level) || level.EqualAny("1", "4"))
  265. {
  266. if (gtid.EqualAny("18", "19"))
  267. {
  268. code = 2;
  269. codeName = "良品[智能]";
  270. }
  271. else
  272. {
  273. code = 3;
  274. codeName = "良品[连体]";
  275. }
  276. }
  277. outputMessage = $"[{e.Content}]产品等级={codeName}[{code}][{level}]{gtid}";
  278. Logger.Trace(outputMessage, logKeyT, logger_t);
  279. FormLogShow?.ShowLog(outputMessage);
  280. if (code > 1 && p_id != plc_s.P_ID.ToString())
  281. {
  282. //xuwei add 2022-02-18 调整为工位打卡模式=================
  283. string userCode = "";
  284. //默认读取userCode
  285. if ( plc_s.WS_ID == 0)
  286. {
  287. userCode = plc_s.USER_CODE;
  288. }
  289. //优先从工位取userCode
  290. if (plc_s.WS_ID != 0)
  291. {
  292. userCode = GetUserCodeFromWorkStation(plc_s.WS_ID.ToString(), e, logKeyE);
  293. outputMessage = $"[{e.Content}]DB_UserCode=[{userCode}]";
  294. Logger.Trace(outputMessage, logKeyT, logger_t);
  295. FormLogShow?.ShowLog(outputMessage);
  296. }
  297. //xuwei end===============================================
  298. // 毛坯入库计件
  299. string result = SmartDeviceProxy.Instance.Invoke<string>(
  300. p => p.AddWorkPieceByStatus3(
  301. _mes_s.AccountCode,
  302. userCode,
  303. null,
  304. plc_s.P_ID,
  305. barcode,
  306. null));
  307. outputMessage = $"[{e.Content}]MES_Result=[{result}]";
  308. Logger.Trace(outputMessage, logKeyT, logger_t);
  309. FormLogShow?.ShowLog(outputMessage);
  310. // 返回状态(成功、失败)
  311. if (result != "OK")
  312. {
  313. if (result == "EE")
  314. {
  315. outputMessage = $"MES服务端异常(详情参见MES端日志)";
  316. }
  317. else if (result == "EU-01")
  318. {
  319. outputMessage = $"无效的帐套或工号(详情参见MES端日志)";
  320. }
  321. else
  322. {
  323. outputMessage = $"其他错误(详情参见MES端日志)[{result}]";
  324. }
  325. Logger.Error(null, outputMessage, logKeyT, logger_t);
  326. Logger.Error(null, outputMessage, logKeyE, logger_e);
  327. FormLogShow?.ShowLog(outputMessage);
  328. SendCode(plc_s, e, logKeyT, logKeyE, plc, 5);
  329. return;
  330. }
  331. }
  332. //1. 产品分级(int)【1不合格,2智能,3连体】
  333. SendCode(plc_s, e, logKeyT, logKeyE, plc, code);
  334. }
  335. }
  336. catch (Exception ex)
  337. {
  338. FormLogShow?.ShowLog($"[{e.Content}]ERROR={ex.Message}");
  339. if (plc_s != null)
  340. {
  341. Logger.Error(ex, $"[{e.Content}]ERROR", logKeyT, logger_t);
  342. Logger.Error(ex, $"[{e.Content}]ERROR", logKeyE, logger_e);
  343. if (plc?.Socket?.Connected ?? false)
  344. {
  345. SendCode(plc_s, e, logKeyT, logKeyE, plc, 5);
  346. }
  347. }
  348. else
  349. {
  350. Logger.Error(ex, $"[{e.Content}]ERROR-NOFLAG", logKeyT, logger_t);
  351. Logger.Error(ex, $"[{e.Content}]ERROR-NOFLAG", logKeyE, logger_e);
  352. }
  353. }
  354. finally
  355. {
  356. LoggerT?.EndTracking();
  357. plc?.Disconnect();
  358. plc?.Close();
  359. //e.ReturnMessage("0000");
  360. }
  361. }
  362. private void SendCode(PLC_S_GL_QR plc_s, ReceiveSession e, string loggerNameT,
  363. string loggerNameE, SocketClient<SiemensS7_1200Model> plc, short code)
  364. {
  365. try
  366. {
  367. //using (SocketClient<SiemensS7_1200Model> plc = new SocketClient<SiemensS7_1200Model>())
  368. {
  369. // 扫码结果:【1:不合格,2:智能,3:连体,4:扫码异常,5:MES异常】
  370. string outputMessage = $"[{e.Content}]SET_CODE=[{code}]{plc_s.Add_Code + plc_s.Add_CodeNum}";
  371. Logger.Trace(outputMessage, loggerNameT, logger_t);
  372. FormLogShow?.ShowLog(outputMessage);
  373. PLCResult p_r = plc?.Write<short>(plc_s.Add_Code, plc_s.Add_CodeNum, code);
  374. outputMessage = $"[{e.Content}]END_CODE=[{p_r}]{plc_s.Add_Code + plc_s.Add_CodeNum}";
  375. Logger.Trace(outputMessage, loggerNameT, logger_t);
  376. FormLogShow?.ShowLog(outputMessage);
  377. }
  378. }
  379. catch (Exception ex)
  380. {
  381. Logger.Error(ex, $"[{e.Content}]ERROR-SendCode", loggerNameT, logger_t);
  382. Logger.Error(ex, $"[{e.Content}]ERROR-SendCode", loggerNameE, logger_e);
  383. FormLogShow?.ShowLog($"[{e.Content}]ERROR-SendCode={ex.Message}");
  384. }
  385. }
  386. private DataRow GetGoodsLevel(string barcode, ReceiveSession e, string loggerName)
  387. {
  388. IDataAccess dataAccess = null;
  389. try
  390. {
  391. dataAccess = PLC_S_DataAccess.GetDataAccess(e);
  392. string sqlString = "select gdd.GOODSLEVELTYPEID,g.goodstypeid, inp.flowprocedureid\n" +
  393. " from tp_pm_groutingdailydetail gdd\n" +
  394. " LEFT JOIN tp_pm_inproduction inp on inp.groutingdailydetailid = gdd.groutingdailydetailid\n" +
  395. " inner join tp_mst_goods g on g.goodsid = gdd.goodsid\n" +
  396. " where gdd.barcode = :barcode";
  397. DataTable dataTable = dataAccess.ExecuteDatatable(sqlString, new CDAParameter(":barcode", barcode));
  398. if (dataTable.HasData())
  399. {
  400. return dataTable.Rows[0];
  401. }
  402. return null;
  403. }
  404. catch (Exception ex)
  405. {
  406. Logger.Error(ex, $"[{e.Content}]ERROR-GetGoodsLevel", loggerName, logger_e);
  407. FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetGoodsLevel={ex.Message}");
  408. return null;
  409. }
  410. finally
  411. {
  412. dataAccess?.Close();
  413. }
  414. }
  415. private string GetUserCodeFromWorkStation(string workStationId, ReceiveSession e, string loggerName)
  416. {
  417. try
  418. {
  419. using (IDataAccess conn = PLC_S_DataAccess.GetDataAccess())
  420. {
  421. string sqlStr = @"
  422. SELECT
  423. T.USERCODE
  424. FROM
  425. TP_MST_WORKSTATION T
  426. INNER JOIN TP_MST_USER U ON U.USERCODE = T.USERCODE
  427. WHERE
  428. T.WORKSTATIONID = :WORKSTATIONID
  429. ";
  430. object result = conn.ExecuteScalar(sqlStr, new CDAParameter(":WORKSTATIONID", workStationId));
  431. return result == null ? "0" : result.ToString();
  432. }
  433. }
  434. catch (Exception ex)
  435. {
  436. Logger.Error(ex, $"[{e.Content}]ERROR-GetUserCodeFromWorkStation", loggerName, logger_e);
  437. FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetUserCodeFromWorkStation={ex.Message}");
  438. return "0";
  439. }
  440. }
  441. private PLC_S_GL_QR GetPLC_S_GC(string flag, ReceiveSession e, string loggerName)
  442. {
  443. try
  444. {
  445. flag = M_NAME + flag;
  446. if (PLC_FLAGS.ContainsKey(flag))
  447. {
  448. return PLC_FLAGS[flag];
  449. }
  450. else
  451. {
  452. PLC_S_GL_QR plc_s = new PLC_S_GL_QR();
  453. INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_{M_NAME}.ini");
  454. plc_s.IP = ini.Read(flag, "IP");
  455. plc_s.Port = ini.Read(flag, "Port").ToInt32();
  456. plc_s.P_ID = ini.Read(flag, "P_ID").ToInt32();
  457. plc_s.USER_CODE = ini.Read(flag, "USER_CODE");
  458. plc_s.Add_Code = ini.Read(flag, "Add_Code");
  459. plc_s.Add_CodeNum = ini.Read(flag, "Add_CodeNum");
  460. plc_s.WS_ID = Convert.ToInt32(ini.Read(flag, "WS_ID"));
  461. plc_s.WS_NAME = ini.Read(flag, "WS_NAME");
  462. //plc_s.Add_F_Code = ini.Read(flag, "Add_F_Code");
  463. //plc_s.Add_F_CodeNum = ini.Read(flag, "Add_F_CodeNum");
  464. //plc_s.Add_Error = ini.Read(flag, "Add_Error");
  465. //plc_s.Add_ErrorNum = ini.Read(flag, "Add_ErrorNum");
  466. PLC_FLAGS.Add(flag, plc_s);
  467. return plc_s;
  468. }
  469. }
  470. catch (Exception ex)
  471. {
  472. Logger.Error(ex, $"[{e.Content}]ERROR-GetPLC_S_GC", loggerName, logger_e);
  473. FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetPLC_S_GC={ex.Message}");
  474. return null;
  475. }
  476. }
  477. }
  478. }