GoodsAuto.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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.Siemens;
  15. using PLC_S.Proxy;
  16. namespace PLC_S.ServerModel
  17. {
  18. /// <summary>
  19. /// 自动返回产品型号等信息 xuwei add 2022-01-10
  20. /// </summary>
  21. public class GoodsAuto : SocketServer<SimpleSocketServerModel>, IShowFormLog, IWindowsServer
  22. {
  23. public Dictionary<string, PLC_S_GA> PLC_FLAGS = new Dictionary<string, PLC_S_GA>();
  24. public const string M_NAME = "GA";
  25. private MES_S _mes_s = null;
  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 GoodsAuto()
  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. _mes_s = MES_S.Get();
  59. }
  60. #region Server
  61. public IFormLogShow FormLogShow
  62. {
  63. get;
  64. set;
  65. }
  66. private void ServerSocket_ServerStarting(object sender, CancelEventArgs e)
  67. {
  68. try
  69. {
  70. _S_STOP = false;
  71. logger.OutputTrace($"{M_NAME}_ServerStarting");
  72. }
  73. catch { }
  74. }
  75. private void ServerSocket_ServerStarted(object sender, EventArgs e)
  76. {
  77. try
  78. {
  79. _S_STOP = false;
  80. logger.OutputTrace($"{M_NAME}_ServerStarted");
  81. }
  82. catch { }
  83. }
  84. private void ServerSocket_ServerStoping(object sender, CancelEventArgs e)
  85. {
  86. try
  87. {
  88. _S_STOP = false;
  89. logger.OutputTrace($"{M_NAME}_ServerStoping");
  90. }
  91. catch { }
  92. }
  93. private void ServerSocket_ServerStoped(object sender, EventArgs e)
  94. {
  95. try
  96. {
  97. _S_STOP = false;
  98. logger.OutputTrace($"{M_NAME}_ServerStoped");
  99. }
  100. catch { }
  101. }
  102. private void ServerSocket_ServerMessage(object sender, ServerMessageEventArgs e)
  103. {
  104. try
  105. {
  106. _S_STOP = false;
  107. string message = e.ToString();
  108. string cip = e?.Client?.IP;
  109. if (e.Type == ServerMessageType.Debug)
  110. {
  111. Logger.Debug(message, $"{M_NAME}-T[{cip}]", logger_t);
  112. }
  113. if (e.Type == ServerMessageType.Error)
  114. {
  115. Logger.Error(e.Exception, message, $"{M_NAME}-E[{cip}]", logger_e);
  116. }
  117. if (e.Type == ServerMessageType.Trace)
  118. {
  119. Logger.Trace(message, $"{M_NAME}-T[{cip}]", logger_t);
  120. }
  121. if (e.Type == ServerMessageType.Warning)
  122. {
  123. Logger.Warn(message, $"{M_NAME}-E[{cip}]", logger_e);
  124. }
  125. FormLogShow?.ShowLog("ServerMessage=" + message);
  126. }
  127. catch { }
  128. }
  129. #endregion
  130. /// <summary>
  131. /// 返回产品型号
  132. /// </summary>
  133. /// <param name="sender"></param>
  134. /// <param name="e"></param>
  135. private void ServerSocket_Received(object sender, ReceiveSession e)
  136. {
  137. PLC_S_GA plc_s = null;
  138. string cIP = e?.Client?.IP;
  139. string logKeyT = $"{M_NAME}-T[{cIP}]";
  140. string logKeyE = $"{M_NAME}-E[{cIP}]";
  141. _S_STOP = false;
  142. SocketClient<SiemensS7_1200Model> plc = null;
  143. Logger LoggerT = null;
  144. try
  145. {
  146. LoggerT = Logger.CreateLogger(logKeyT, logger_t);
  147. lock (LoggerT)
  148. {
  149. LoggerT.BeginTracking();
  150. string outputMessage = e.ToString();
  151. Logger.Trace(outputMessage, logKeyT, logger_t);
  152. FormLogShow?.ShowLog(outputMessage);
  153. #region 【必须】判断接收数据是否为空
  154. if (string.IsNullOrWhiteSpace(e.Content))
  155. {
  156. outputMessage = "接收数据为空";
  157. Logger.Warn(outputMessage, logKeyT, logger_t);
  158. Logger.Warn(outputMessage, logKeyE, logger_e);
  159. FormLogShow?.ShowLog(outputMessage);
  160. return;
  161. }
  162. #endregion
  163. #region 【必须】判断接收设备代码是否为空
  164. outputMessage = e.Content;
  165. Logger.Trace(outputMessage, logKeyT, logger_t);
  166. FormLogShow?.ShowLog(outputMessage);
  167. string[] c = e.Content.Split('#');
  168. string flag = null;
  169. if (c.Length > 0)
  170. {
  171. flag = c[0];
  172. }
  173. string barCode = null;
  174. if (c.Length > 1)
  175. {
  176. barCode = c[1];
  177. }
  178. if (string.IsNullOrWhiteSpace(flag))
  179. {
  180. outputMessage = $"[{e.Content}]接收设备代码为空";
  181. Logger.Warn(outputMessage, logKeyT, logger_t);
  182. Logger.Warn(outputMessage, logKeyE, logger_e);
  183. FormLogShow?.ShowLog(outputMessage);
  184. return;
  185. }
  186. #endregion
  187. #region 【必须】读取INI配置文件
  188. plc_s = GetPLC_S_GA(flag, e, logKeyE);
  189. if (plc_s == null)
  190. {
  191. outputMessage = $"[{e.Content}]设备代码[{flag}]未找到ini配置或有错误";
  192. Logger.Error(null, outputMessage, logKeyT, logger_t);
  193. Logger.Error(null, outputMessage, logKeyE, logger_e);
  194. FormLogShow?.ShowLog(outputMessage);
  195. return;
  196. }
  197. #endregion
  198. #region 【必须】建立PLC连接
  199. if (!string.IsNullOrEmpty(plc_s.PLC_IP))
  200. {
  201. outputMessage = $"[{e.Content}]PLC_Connect={JsonHelper.FromObject(plc_s)}";
  202. Logger.Trace(outputMessage, logKeyT, logger_t);
  203. FormLogShow?.ShowLog(outputMessage);
  204. plc = new SocketClient<SiemensS7_1200Model>();
  205. plc.Connect(plc_s.PLC_IP, plc_s.PLC_PORT);
  206. }
  207. else
  208. {
  209. Logger.Trace("PLC地址未设置,测试数据使用!", logKeyT, logger_t);
  210. }
  211. PLCResult p_r;
  212. #endregion
  213. #region 【必须】判断接收条码格式是否正确
  214. if (string.IsNullOrWhiteSpace(barCode) ||
  215. barCode == "0" ||
  216. barCode.Length != 11 ||
  217. barCode.StartsWith("0") ||
  218. barCode.StartsWith("ERROR")
  219. )
  220. {
  221. outputMessage = $"[{e.Content}]接收条码格式错误";
  222. Logger.Warn(outputMessage, logKeyT, logger_t);
  223. Logger.Warn(outputMessage, logKeyE, logger_e);
  224. FormLogShow?.ShowLog(outputMessage);
  225. SendCode(plc_s, e, logKeyT, logKeyE, plc, 2);
  226. return;
  227. }
  228. #endregion
  229. #region 【可选】产品型号写入
  230. if (!string.IsNullOrEmpty(plc_s.PLC_GOODSCODE_CODE) && !string.IsNullOrEmpty(plc_s.PLC_GOODSCODE_CODENUM))
  231. {
  232. string goodsCode = GetGoodsCode(barCode, e, logKeyE);
  233. if (goodsCode == null)
  234. {
  235. outputMessage = $"[{e.Content}]此条码[{barCode}]不存在";
  236. Logger.Warn(outputMessage, logKeyT, logger_t);
  237. Logger.Warn(outputMessage, logKeyE, logger_e);
  238. FormLogShow?.ShowLog(outputMessage);
  239. if (!string.IsNullOrEmpty(plc_s.PLC_IP)) SendCode(plc_s, e, logKeyT, logKeyE, plc, 2);
  240. return;
  241. }
  242. if (!string.IsNullOrEmpty(plc_s.PLC_IP))
  243. {
  244. outputMessage = $"[{e.Content}]SET_GOODSCODE=[{goodsCode}]{plc_s.PLC_GOODSCODE_CODE + plc_s.PLC_GOODSCODE_CODENUM}";
  245. Logger.Trace(outputMessage, logKeyT, logger_t);
  246. FormLogShow?.ShowLog(outputMessage);
  247. p_r = plc.Write<string>(plc_s.PLC_GOODSCODE_CODE, plc_s.PLC_GOODSCODE_CODENUM, goodsCode);
  248. outputMessage = $"[{e.Content}]END_GOODSCODE=[{p_r}]{plc_s.PLC_GOODSCODE_CODE + plc_s.PLC_GOODSCODE_CODENUM}";
  249. Logger.Trace(outputMessage, logKeyT, logger_t);
  250. FormLogShow?.ShowLog(outputMessage);
  251. }
  252. else
  253. {
  254. outputMessage = $"[{e.Content}]产品型号=[{goodsCode}]";
  255. Logger.Trace(outputMessage, logKeyT, logger_t);
  256. FormLogShow?.ShowLog(outputMessage);
  257. }
  258. }
  259. #endregion
  260. #region 【可选】产品型号代码写入
  261. if (!string.IsNullOrEmpty(plc_s.PLC_SEATCOVER_CODE) && !string.IsNullOrEmpty(plc_s.PLC_SEATCOVER_CODENUM))
  262. {
  263. string intCode = GetSeatCoverCode(barCode, e, logKeyE);
  264. if (intCode == null)
  265. {
  266. outputMessage = $"[{e.Content}]此条码[{barCode}]不存在";
  267. Logger.Warn(outputMessage, logKeyT, logger_t);
  268. Logger.Warn(outputMessage, logKeyE, logger_e);
  269. FormLogShow?.ShowLog(outputMessage);
  270. if (!string.IsNullOrEmpty(plc_s.PLC_IP)) SendCode(plc_s, e, logKeyT, logKeyE, plc, 2);
  271. return;
  272. }
  273. if (string.IsNullOrWhiteSpace(intCode))
  274. {
  275. outputMessage = $"[{e.Content}]此条码[{barCode}]未设置型号代码";
  276. Logger.Error(null, outputMessage, logKeyT, logger_t);
  277. Logger.Error(null, outputMessage, logKeyE, logger_e);
  278. FormLogShow?.ShowLog(outputMessage);
  279. if (!string.IsNullOrEmpty(plc_s.PLC_IP)) SendCode(plc_s, e, logKeyT, logKeyE, plc, 2);
  280. return;
  281. }
  282. if (!string.IsNullOrEmpty(plc_s.PLC_IP))
  283. {
  284. outputMessage = $"[{e.Content}]SET_SEATCOVER=[{intCode}]{plc_s.PLC_SEATCOVER_CODE + plc_s.PLC_SEATCOVER_CODENUM}";
  285. Logger.Trace(outputMessage, logKeyT, logger_t);
  286. FormLogShow?.ShowLog(outputMessage);
  287. p_r = plc.Write<short>(plc_s.PLC_SEATCOVER_CODE, plc_s.PLC_SEATCOVER_CODENUM, (short)(intCode.ToInt32()));
  288. outputMessage = $"[{e.Content}]END_SEATCOVER=[{p_r}]{plc_s.PLC_SEATCOVER_CODE + plc_s.PLC_SEATCOVER_CODENUM}";
  289. Logger.Trace(outputMessage, logKeyT, logger_t);
  290. FormLogShow?.ShowLog(outputMessage);
  291. }
  292. else
  293. {
  294. outputMessage = $"[{e.Content}]产品型号=[{intCode}]";
  295. Logger.Trace(outputMessage, logKeyT, logger_t);
  296. FormLogShow?.ShowLog(outputMessage);
  297. }
  298. }
  299. #endregion
  300. #region 【可选】产品类型写入
  301. if (!string.IsNullOrEmpty(plc_s.PLC_GOODSTYPE_CODE) && !string.IsNullOrEmpty(plc_s.PLC_GOODSTYPE_CODENUM))
  302. {
  303. Int16 goodsType = Convert.ToInt16(GetGoodsType(barCode, e, logKeyT));
  304. if (!string.IsNullOrEmpty(plc_s.PLC_IP))
  305. {
  306. outputMessage = $"[{e.Content}]SET_GoodsType=[{goodsType}]{plc_s.PLC_GOODSTYPE_CODE + plc_s.PLC_GOODSTYPE_CODENUM}";
  307. Logger.Trace(outputMessage, logKeyT, logger_t);
  308. FormLogShow?.ShowLog(outputMessage);
  309. p_r = plc?.Write<short>(plc_s.PLC_GOODSTYPE_CODE, plc_s.PLC_GOODSTYPE_CODENUM, goodsType);
  310. outputMessage = $"[{e.Content}]END_GoodsType=[{p_r.ToString()}]{plc_s.PLC_GOODSTYPE_CODE + plc_s.PLC_GOODSTYPE_CODENUM}";
  311. Logger.Trace(outputMessage, logKeyT, logger_t);
  312. FormLogShow?.ShowLog(outputMessage);
  313. }
  314. else
  315. {
  316. outputMessage = $"[{e.Content}]产品类型=[{goodsType}]";
  317. Logger.Trace(outputMessage, logKeyT, logger_t);
  318. FormLogShow?.ShowLog(outputMessage);
  319. }
  320. }
  321. #endregion
  322. #region 【可选】产品条码写入
  323. if (!string.IsNullOrEmpty(plc_s.PLC_BARCODE_CODE) && !string.IsNullOrEmpty(plc_s.PLC_BARCODE_CODENUM))
  324. {
  325. if (!string.IsNullOrEmpty(plc_s.PLC_IP))
  326. {
  327. outputMessage = $"[{e.Content}]SET_BarCode=[{barCode}]{plc_s.PLC_BARCODE_CODE + plc_s.PLC_BARCODE_CODENUM}";
  328. Logger.Trace(outputMessage, logKeyT, logger_t);
  329. FormLogShow?.ShowLog(outputMessage);
  330. p_r = plc.Write<string>(plc_s.PLC_BARCODE_CODE, plc_s.PLC_BARCODE_CODENUM, barCode);
  331. outputMessage = $"[{e.Content}]END_BarCode=[{p_r}]{plc_s.PLC_BARCODE_CODE + plc_s.PLC_BARCODE_CODENUM}";
  332. Logger.Trace(outputMessage, logKeyT, logger_t);
  333. FormLogShow?.ShowLog(outputMessage);
  334. }
  335. else
  336. {
  337. outputMessage = $"[{e.Content}]产品条码=[{barCode}]";
  338. Logger.Trace(outputMessage, logKeyT, logger_t);
  339. FormLogShow?.ShowLog(outputMessage);
  340. }
  341. }
  342. #endregion
  343. #region 【可选】半检返工标识写入
  344. if (!string.IsNullOrEmpty(plc_s.PLC_SEMIREWORK_CODE) && !string.IsNullOrEmpty(plc_s.PLC_SEMIREWORK_CODENUM))
  345. {
  346. Int16 goodsType = Convert.ToInt16(GetSemiReWork(barCode, e, logKeyT));
  347. if (!string.IsNullOrEmpty(plc_s.PLC_IP))
  348. {
  349. outputMessage = $"[{e.Content}]SET_SemiRework=[{goodsType}]{plc_s.PLC_SEMIREWORK_CODE + plc_s.PLC_SEMIREWORK_CODENUM}";
  350. Logger.Trace(outputMessage, logKeyT, logger_t);
  351. FormLogShow?.ShowLog(outputMessage);
  352. p_r = plc?.Write<short>(plc_s.PLC_SEMIREWORK_CODE, plc_s.PLC_SEMIREWORK_CODENUM, goodsType);
  353. outputMessage = $"[{e.Content}]END_SemiRework=[{p_r.ToString()}]{plc_s.PLC_SEMIREWORK_CODE + plc_s.PLC_SEMIREWORK_CODENUM}";
  354. Logger.Trace(outputMessage, logKeyT, logger_t);
  355. FormLogShow?.ShowLog(outputMessage);
  356. }
  357. else
  358. {
  359. outputMessage = $"[{e.Content}]半检返工标识=[{goodsType}]";
  360. Logger.Trace(outputMessage, logKeyT, logger_t);
  361. FormLogShow?.ShowLog(outputMessage);
  362. }
  363. }
  364. #endregion
  365. #region 【可选】产品等级写入
  366. if (!string.IsNullOrEmpty(plc_s.PLC_GOODSLEVEL_CODE) && !string.IsNullOrEmpty(plc_s.PLC_GOODSLEVEL_CODENUM))
  367. {
  368. Int16 goodsLevel = Convert.ToInt16(GetGoodsLevel(barCode, e, logKeyT));
  369. if (!string.IsNullOrEmpty(plc_s.PLC_IP))
  370. {
  371. outputMessage = $"[{e.Content}]SET_GoodsLevel=[{goodsLevel}]{plc_s.PLC_GOODSLEVEL_CODE + plc_s.PLC_GOODSLEVEL_CODENUM}";
  372. Logger.Trace(outputMessage, logKeyT, logger_t);
  373. FormLogShow?.ShowLog(outputMessage);
  374. p_r = plc?.Write<short>(plc_s.PLC_GOODSLEVEL_CODE, plc_s.PLC_GOODSLEVEL_CODENUM, goodsLevel);
  375. outputMessage = $"[{e.Content}]END_GoodsLevel=[{p_r.ToString()}]{plc_s.PLC_GOODSLEVEL_CODE + plc_s.PLC_GOODSLEVEL_CODENUM}";
  376. Logger.Trace(outputMessage, logKeyT, logger_t);
  377. FormLogShow?.ShowLog(outputMessage);
  378. }
  379. else
  380. {
  381. outputMessage = $"[{e.Content}]产品等级=[{goodsLevel}]";
  382. Logger.Trace(outputMessage, logKeyT, logger_t);
  383. FormLogShow?.ShowLog(outputMessage);
  384. }
  385. }
  386. #endregion
  387. #region 【可选】工序验证
  388. if (!string.IsNullOrEmpty(plc_s.PROCEDURE_CHECKID))
  389. {
  390. Int16 plc_flag = Convert.ToInt16(GetProcedureCheck(barCode, plc_s.PROCEDURE_CHECKID, e, logKeyT, logKeyE));
  391. if (!string.IsNullOrEmpty(plc_s.PLC_IP))
  392. {
  393. SendCode(plc_s, e, logKeyT, logKeyE, plc, plc_flag);
  394. }
  395. else
  396. {
  397. outputMessage = $"[{e.Content}]工序=[{plc_s.PROCEDURE_CHECKID}]工序验证=[{plc_flag}]";
  398. Logger.Trace(outputMessage, logKeyT, logger_t);
  399. FormLogShow?.ShowLog(outputMessage);
  400. }
  401. return;
  402. }
  403. #endregion
  404. #region 【可选】工序计件
  405. if (!string.IsNullOrEmpty(plc_s.PROCEDURE_ID) && (!string.IsNullOrEmpty(plc_s.PROCEDURE_USERCODE) || !string.IsNullOrEmpty(plc_s.WORKSTATION_ID)) )
  406. {
  407. string p_id = GetFlowId(barCode, e, logKeyE);
  408. Int16 plc_flag = 0;
  409. if (p_id != plc_s.PROCEDURE_ID.ToString())
  410. {
  411. string userCode = "";
  412. //默认读取userCode
  413. if(!string.IsNullOrEmpty(plc_s.PROCEDURE_USERCODE))
  414. {
  415. userCode = plc_s.PROCEDURE_USERCODE;
  416. }
  417. //优先从工位取userCode
  418. if (!string.IsNullOrEmpty(plc_s.WORKSTATION_ID))
  419. {
  420. userCode = GetUserCodeFromWorkStation(plc_s.WORKSTATION_ID, e, logKeyE);
  421. outputMessage = $"[{e.Content}]DB_UserCode=[{userCode}]";
  422. Logger.Trace(outputMessage, logKeyT, logger_t);
  423. FormLogShow?.ShowLog(outputMessage);
  424. }
  425. //工序计件
  426. string result = SmartDeviceProxy.Instance.Invoke<string>(
  427. p => p.AddWorkPieceByStatus3(
  428. _mes_s.AccountCode,
  429. userCode,
  430. null,
  431. Convert.ToInt32(plc_s.PROCEDURE_ID),
  432. barCode,
  433. null));
  434. outputMessage = $"[{e.Content}]工序=[{plc_s.PROCEDURE_ID}]工号=[{userCode}]工序计件=[{result}]";
  435. Logger.Trace(outputMessage, logKeyT, logger_t);
  436. FormLogShow?.ShowLog(outputMessage);
  437. // 返回状态(成功、失败)
  438. if (result != "OK")
  439. {
  440. plc_flag = 3;
  441. if (result == "EE")
  442. {
  443. plc_flag = 4;
  444. outputMessage = $"MES服务端异常(详情参见MES端日志)";
  445. }
  446. else if (result == "EU-01")
  447. {
  448. outputMessage = $"无效的帐套或工号(详情参见MES端日志)";
  449. }
  450. else
  451. {
  452. outputMessage = $"其他错误(详情参见MES端日志)[{result}]";
  453. }
  454. Logger.Error(null, outputMessage, logKeyT, logger_t);
  455. Logger.Error(null, outputMessage, logKeyE, logger_e);
  456. FormLogShow?.ShowLog(outputMessage);
  457. if (!string.IsNullOrEmpty(plc_s.PLC_IP))
  458. {
  459. SendCode(plc_s, e, logKeyT, logKeyE, plc, plc_flag);
  460. }
  461. return;
  462. }
  463. }
  464. }
  465. #endregion
  466. #region 【可选】写入PLC扫码结果 【1:通过,2:扫码异常,3:工序异常,0:复位】
  467. if (!string.IsNullOrEmpty(plc_s.PLC_FLAG_CODE) && !string.IsNullOrEmpty(plc_s.PLC_FLAG_CODENUM))
  468. {
  469. if (!string.IsNullOrEmpty(plc_s.PLC_IP))
  470. {
  471. SendCode(plc_s, e, logKeyT, logKeyE, plc, 1);
  472. }
  473. else
  474. {
  475. outputMessage = $"[{e.Content}]PLC标识=[1]";
  476. Logger.Trace(outputMessage, logKeyT, logger_t);
  477. FormLogShow?.ShowLog(outputMessage);
  478. }
  479. }
  480. #endregion
  481. }
  482. }
  483. catch (Exception ex)
  484. {
  485. FormLogShow?.ShowLog($"[{e.Content}]ERROR={ex.Message}");
  486. if (plc_s != null)
  487. {
  488. Logger.Error(ex, $"[{e.Content}]ERROR", logKeyT, logger_t);
  489. Logger.Error(ex, $"[{e.Content}]ERROR", logKeyE, logger_e);
  490. SendCode(plc_s, e, logKeyT, logKeyE, plc, 2);
  491. }
  492. else
  493. {
  494. Logger.Error(ex, $"[{e.Content}]ERROR-NOFLAG", logKeyT, logger_t);
  495. Logger.Error(ex, $"[{e.Content}]ERROR-NOFLAG", logKeyE, logger_e);
  496. }
  497. }
  498. finally
  499. {
  500. LoggerT?.EndTracking();
  501. plc?.Disconnect();
  502. plc?.Close();
  503. }
  504. }
  505. private void SendCode(PLC_S_GA plc_s, ReceiveSession e, string loggerNameT, string loggerNameE, SocketClient<SiemensS7_1200Model> plc, short code)
  506. {
  507. try
  508. {
  509. string outputMessage = $"[{e.Content}]SET_FLAG_CODE=[{code}]{plc_s.PLC_FLAG_CODE + plc_s.PLC_FLAG_CODENUM}";
  510. Logger.Trace(outputMessage, loggerNameT, logger_t);
  511. FormLogShow?.ShowLog(outputMessage);
  512. PLCResult p_r = plc?.Write<short>(plc_s.PLC_FLAG_CODE, plc_s.PLC_FLAG_CODENUM, (short)code);
  513. outputMessage = $"[{e.Content}]SET_FLAG_CODE=[{p_r}]{plc_s.PLC_FLAG_CODE + plc_s.PLC_FLAG_CODENUM}";
  514. Logger.Trace(outputMessage, loggerNameT, logger_t);
  515. FormLogShow?.ShowLog(outputMessage);
  516. }
  517. catch (Exception ex)
  518. {
  519. Logger.Error(ex, $"[{e.Content}]ERROR-SendCode", loggerNameT, logger_t);
  520. Logger.Error(ex, $"[{e.Content}]ERROR-SendCode", loggerNameE, logger_e);
  521. FormLogShow?.ShowLog($"[{e.Content}]ERROR-SendCode={ex.Message}");
  522. }
  523. }
  524. private string GetGoodsCode(string barCode, ReceiveSession e, string loggerName)
  525. {
  526. try
  527. {
  528. using (IDataAccess conn = PLC_S_DataAccess.GetDataAccess(e))
  529. {
  530. string sqlStr = @"
  531. SELECT
  532. G.GOODSCODE
  533. FROM
  534. TP_PM_GROUTINGDAILYDETAIL GDD
  535. WHERE
  536. GDD.BARCODE = :BARCODE
  537. ";
  538. object result = conn.ExecuteScalar(sqlStr, new CDAParameter(":BARCODE", barCode));
  539. return result == null ? "" : result.ToString();
  540. }
  541. }
  542. catch (Exception ex)
  543. {
  544. Logger.Error(ex, $"[{e.Content}]ERROR-GetGoodsCode", loggerName, logger_e);
  545. FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetGoodsCode={ex.Message}");
  546. return null;
  547. }
  548. }
  549. private string GetSeatCoverCode(string barCode, ReceiveSession e, string loggerName)
  550. {
  551. try
  552. {
  553. using (IDataAccess conn = PLC_S_DataAccess.GetDataAccess(e))
  554. {
  555. string sqlStr = @"
  556. SELECT
  557. G.SEATCOVERCODE
  558. FROM
  559. TP_PM_GROUTINGDAILYDETAIL GDD
  560. INNER JOIN TP_MST_GOODS G ON G.GOODSID = GDD.GOODSID
  561. WHERE
  562. GDD.BARCODE = :BARCODE
  563. ";
  564. object result = conn.ExecuteScalar(sqlStr, new CDAParameter(":BARCODE", barCode));
  565. return result == null ? "" : result.ToString();
  566. }
  567. }
  568. catch (Exception ex)
  569. {
  570. Logger.Error(ex, $"[{e.Content}]ERROR-GetSeatCoverCode", loggerName, logger_e);
  571. FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetSeatCoverCode={ex.Message}");
  572. return null;
  573. }
  574. }
  575. private string GetGoodsType(string barCode, ReceiveSession e, string loggerName)
  576. {
  577. try
  578. {
  579. using (IDataAccess conn = PLC_S_DataAccess.GetDataAccess(e))
  580. {
  581. string sqlStr = @"
  582. SELECT
  583. -- G.GOODSCODE,
  584. -- G.SEATCOVERCODE,
  585. CASE
  586. WHEN T.GOODSTYPECODE = '001001001' THEN 2 --连体
  587. WHEN T.GOODSTYPECODE = '001001002' THEN 3 --分体
  588. WHEN T.GOODSTYPECODE = '001001003' THEN 1 --智能
  589. WHEN T.GOODSTYPECODE = '001001004' THEN 1 --智能高压
  590. ELSE 0 --其他
  591. END AS GOODSTYPE
  592. FROM
  593. TP_PM_GROUTINGDAILYDETAIL GDD
  594. INNER JOIN TP_MST_GOODS G ON G.GOODSID = GDD.GOODSID
  595. INNER JOIN TP_MST_GOODSTYPE T ON G.GOODSTYPEID = T.GOODSTYPEID
  596. WHERE
  597. GDD.BARCODE = :BARCODE --'10006800247'
  598. ";
  599. string goodsType = conn.ExecuteScalar(sqlStr, new CDAParameter(":BARCODE", barCode)).ToString();
  600. return goodsType;
  601. }
  602. }
  603. catch (Exception ex)
  604. {
  605. Logger.Error(ex, $"[{e.Content}]ERROR-GetGoodsType", loggerName, logger_e);
  606. FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetGoodsType={ex.Message}");
  607. return "0";
  608. }
  609. }
  610. private string GetFlowId(string barCode, ReceiveSession e, string loggerName)
  611. {
  612. try
  613. {
  614. using (IDataAccess conn = PLC_S_DataAccess.GetDataAccess())
  615. {
  616. string sqlStr = @"
  617. SELECT
  618. GDD.FLOWPROCEDUREID
  619. FROM
  620. TP_PM_INPRODUCTION GDD
  621. WHERE
  622. GDD.BARCODE = :BARCODE
  623. ";
  624. object result = conn.ExecuteScalar(sqlStr, new CDAParameter(":BARCODE", barCode));
  625. return result == null ? "0" : result.ToString();
  626. }
  627. }
  628. catch (Exception ex)
  629. {
  630. Logger.Error(ex, $"[{e.Content}]ERROR-GetFlowId", loggerName, logger_e);
  631. FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetFlowId={ex.Message}");
  632. return "0";
  633. }
  634. }
  635. private string GetUserCodeFromWorkStation(string workStationId, ReceiveSession e, string loggerName)
  636. {
  637. try
  638. {
  639. using (IDataAccess conn = PLC_S_DataAccess.GetDataAccess())
  640. {
  641. string sqlStr = @"
  642. SELECT
  643. T.USERCODE
  644. FROM
  645. TP_MST_WORKSTATION T
  646. INNER JOIN TP_MST_USER U ON U.USERCODE = T.USERCODE
  647. WHERE
  648. T.WORKSTATIONID = :WORKSTATIONID
  649. ";
  650. object result = conn.ExecuteScalar(sqlStr, new CDAParameter(":WORKSTATIONID", workStationId));
  651. return result == null ? "0" : result.ToString();
  652. }
  653. }
  654. catch (Exception ex)
  655. {
  656. Logger.Error(ex, $"[{e.Content}]ERROR-GetUserCodeFromWorkStation", loggerName, logger_e);
  657. FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetUserCodeFromWorkStation={ex.Message}");
  658. return "0";
  659. }
  660. }
  661. private string GetSemiReWork(string barCode, ReceiveSession e, string loggerName)
  662. {
  663. try
  664. {
  665. using (IDataAccess conn = PLC_S_DataAccess.GetDataAccess())
  666. {
  667. string sqlStr = @"
  668. SELECT
  669. P.ISREWORKFLAG
  670. FROM
  671. TP_PM_INPRODUCTION P
  672. WHERE
  673. P.BARCODE = :BARCODE
  674. ";
  675. object result = conn.ExecuteScalar(sqlStr, new CDAParameter(":BARCODE", barCode));
  676. //返工标识(0:正常,1:半检返工中, 2:半检返修)
  677. //没找到记录表明产品报废写 3
  678. string resultFlag = "0";
  679. if(result !=null)
  680. {
  681. if (result.ToString() == "0") resultFlag = "1"; //正常
  682. if (result.ToString() == "1" || result.ToString() == "2") resultFlag = "2";//返工
  683. }
  684. else
  685. {
  686. resultFlag = "3";//报废
  687. }
  688. return resultFlag;
  689. }
  690. }
  691. catch (Exception ex)
  692. {
  693. Logger.Error(ex, $"[{e.Content}]ERROR-GetSemiReWork", loggerName, logger_e);
  694. FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetSemiReWork={ex.Message}");
  695. return "0";
  696. }
  697. }
  698. private string GetGoodsLevel(string barCode, ReceiveSession e, string loggerName)
  699. {
  700. try
  701. {
  702. using (IDataAccess conn = PLC_S_DataAccess.GetDataAccess())
  703. {
  704. string sqlStr = @"
  705. SELECT
  706. GDD.GOODSLEVELTYPEID
  707. FROM
  708. TP_PM_GROUTINGDAILYDETAIL GDD
  709. WHERE
  710. GDD.BARCODE = :BARCODE
  711. ";
  712. object result = conn.ExecuteScalar(sqlStr, new CDAParameter(":BARCODE", barCode));
  713. return result == null ? "0" : result.ToString();
  714. }
  715. }
  716. catch (Exception ex)
  717. {
  718. Logger.Error(ex, $"[{e.Content}]ERROR-GetGoodsLevel", loggerName, logger_e);
  719. FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetGoodsLevel={ex.Message}");
  720. return "0";
  721. }
  722. }
  723. private string GetProcedureCheck(string barCode,string procedureId, ReceiveSession e, string loggerNameT, string loggerNameE)
  724. {
  725. try
  726. {
  727. using (IDataAccess dataAccess = PLC_S_DataAccess.GetDataAccess())
  728. {
  729. CDAParameter[] parameters = new CDAParameter[]
  730. {
  731. new CDAParameter("in_procedureid", procedureId),
  732. new CDAParameter("in_barcode", barCode),
  733. new CDAParameter("out_errType",DataType.Int32, ParameterDirection.Output),
  734. new CDAParameter("out_errMsg",DataType.VarChar, 4000, ParameterDirection.Output),
  735. new CDAParameter("out_goodsID",DataType.Int32, ParameterDirection.Output),
  736. new CDAParameter("out_goodsCode",DataType.VarChar, 400, ParameterDirection.Output),
  737. new CDAParameter("out_goodsType",DataType.Int32, ParameterDirection.Output),
  738. };
  739. int ii = dataAccess.ExecuteSPNonQuery("PRO_PM_CheckBarcode_PLC", parameters);
  740. Int16 plc_flag = 0;
  741. if (!int.TryParse(parameters[2].Value + "", out int errtype))
  742. {
  743. errtype = 4;
  744. }
  745. // 其他情况显示MES异常
  746. if (errtype == 4)
  747. {
  748. plc_flag = 4;
  749. }
  750. else if (errtype == 0)
  751. {
  752. plc_flag = 1;
  753. }
  754. else // errtype == 1 || errtype == 2 || errtype == 3
  755. {
  756. // 无效条码,显示不可到达
  757. plc_flag = 3;
  758. }
  759. string outputMessage = $"[{e.Content}]MESSAGE=[{errtype}][{parameters[3].Value}]";
  760. Logger.Trace(outputMessage, loggerNameT, logger_t);
  761. FormLogShow?.ShowLog(outputMessage);
  762. return plc_flag.ToString();
  763. }
  764. }
  765. catch (Exception ex)
  766. {
  767. Logger.Error(ex, $"[{e.Content}]ERROR-GetProcedureCheck", loggerNameE, logger_e);
  768. FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetProcedureCheck={ex.Message}");
  769. return "0";
  770. }
  771. }
  772. private PLC_S_GA GetPLC_S_GA(string flag, ReceiveSession e, string loggerName)
  773. {
  774. try
  775. {
  776. flag = M_NAME + flag;
  777. if (PLC_FLAGS.ContainsKey(flag))
  778. {
  779. return PLC_FLAGS[flag];
  780. }
  781. else
  782. {
  783. PLC_S_GA plc_s = new PLC_S_GA();
  784. INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_{M_NAME}.ini");
  785. plc_s.PLC_IP = ini.Read(flag, "PLC_IP");
  786. plc_s.PLC_PORT = ini.Read(flag, "PLC_PORT").ToInt32();
  787. plc_s.PLC_FLAG_CODE = ini.Read(flag, "PLC_FLAG_CODE");
  788. plc_s.PLC_FLAG_CODENUM = ini.Read(flag, "PLC_FLAG_CODENUM");
  789. plc_s.PLC_SEATCOVER_CODE = ini.Read(flag, "PLC_SEATCOVER_CODE");
  790. plc_s.PLC_SEATCOVER_CODENUM = ini.Read(flag, "PLC_SEATCOVER_CODENUM");
  791. plc_s.PLC_GOODSCODE_CODE = ini.Read(flag, "PLC_GOODSCODE_CODE");
  792. plc_s.PLC_GOODSCODE_CODENUM = ini.Read(flag, "PLC_GOODSCODE_CODENUM");
  793. plc_s.PLC_GOODSTYPE_CODE = ini.Read(flag, "PLC_GOODSTYPE_CODE");
  794. plc_s.PLC_GOODSTYPE_CODENUM = ini.Read(flag, "PLC_GOODSTYPE_CODENUM");
  795. plc_s.PLC_SEMIREWORK_CODE = ini.Read(flag, "PLC_SEMIREWORK_CODE");
  796. plc_s.PLC_SEMIREWORK_CODENUM = ini.Read(flag, "PLC_SEMIREWORK_CODENUM");
  797. plc_s.PLC_GOODSLEVEL_CODE = ini.Read(flag, "PLC_GOODSLEVEL_CODE");
  798. plc_s.PLC_GOODSLEVEL_CODENUM = ini.Read(flag, "PLC_GOODSLEVEL_CODENUM");
  799. plc_s.PROCEDURE_ID = ini.Read(flag, "PROCEDURE_ID");
  800. plc_s.PROCEDURE_USERCODE = ini.Read(flag, "PROCEDURE_USERCODE");
  801. plc_s.WORKSTATION_ID = ini.Read(flag, "WORKSTATION_ID");
  802. plc_s.WORKSTATION_NAME = ini.Read(flag, "WORKSTATION_NAME");
  803. PLC_FLAGS.Add(flag, plc_s);
  804. return plc_s;
  805. }
  806. }
  807. catch (Exception ex)
  808. {
  809. Logger.Error(ex, $"[{e.Content}]ERROR-GetPLC_S_GA", loggerName, logger_e);
  810. FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetPLC_S_GA={ex.Message}");
  811. return null;
  812. }
  813. }
  814. }
  815. }