AntiFakeCode_BD.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Text;
  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.Melsec;
  16. using PLC_S_1.Proxy;
  17. namespace PLC_S_1.ServerModel
  18. {
  19. /// <summary>
  20. /// 2车间防伪码-绑定前扫描
  21. /// </summary>
  22. public class AntiFakeCode_BD : SocketServer<SimpleSocketServerModel>, IShowFormLog, IWindowsServer
  23. {
  24. public Dictionary<string, PLC_S_AF_BD> PLC_FLAGS = new Dictionary<string, PLC_S_AF_BD>();
  25. public const string M_NAME = "AF_BD";
  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 MES_S _mes_s = null;
  30. //private bool
  31. public int Interval
  32. {
  33. get;
  34. set;
  35. }
  36. public string EPTS_CODE
  37. {
  38. get;
  39. set;
  40. }
  41. public AntiFakeCode_BD()
  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. //ServerSocket_Received(null, null);
  61. }
  62. #region Server
  63. public IFormLogShow FormLogShow
  64. {
  65. get;
  66. set;
  67. }
  68. private void ServerSocket_ServerStarting(object sender, CancelEventArgs e)
  69. {
  70. try
  71. {
  72. logger.OutputTrace($"{M_NAME}_ServerStarting");
  73. }
  74. catch { }
  75. }
  76. private void ServerSocket_ServerStarted(object sender, EventArgs e)
  77. {
  78. try
  79. {
  80. logger.OutputTrace($"{M_NAME}_ServerStarted");
  81. }
  82. catch { }
  83. }
  84. private void ServerSocket_ServerStoping(object sender, CancelEventArgs e)
  85. {
  86. try
  87. {
  88. logger.OutputTrace($"{M_NAME}_ServerStoping");
  89. }
  90. catch { }
  91. }
  92. private void ServerSocket_ServerStoped(object sender, EventArgs e)
  93. {
  94. try
  95. {
  96. logger.OutputTrace($"{M_NAME}_ServerStoped");
  97. }
  98. catch { }
  99. }
  100. private void ServerSocket_ServerMessage(object sender, ServerMessageEventArgs e)
  101. {
  102. try
  103. {
  104. string message = e.ToString();
  105. //string cip = e?.Client?.IP;
  106. string cip = null;
  107. if (e.Type == ServerMessageType.Debug)
  108. {
  109. Logger.Debug(message, $"{M_NAME}-T[{cip}]", logger_t);
  110. }
  111. if (e.Type == ServerMessageType.Error)
  112. {
  113. Logger.Error(e.Exception, message, $"{M_NAME}-E[{cip}]", logger_e);
  114. }
  115. if (e.Type == ServerMessageType.Trace)
  116. {
  117. Logger.Trace(message, $"{M_NAME}-T[{cip}]", logger_t);
  118. }
  119. if (e.Type == ServerMessageType.Warning)
  120. {
  121. Logger.Warn(message, $"{M_NAME}-E[{cip}]", logger_e);
  122. }
  123. FormLogShow?.ShowLog("ServerMessage=" + message);
  124. }
  125. catch { }
  126. }
  127. #endregion
  128. /// <summary>
  129. /// 返回产品型号
  130. /// </summary>
  131. /// <param name="sender"></param>
  132. /// <param name="e"></param>
  133. private void ServerSocket_Received(object sender, ReceiveSession e)
  134. {
  135. PLC_S_AF_BD plc_s = null;
  136. string cIP = null;// e?.Client?.IP;
  137. string logKeyT = $"{M_NAME}-T[{cIP}]";
  138. string logKeyE = $"{M_NAME}-E[{cIP}]";
  139. SocketClient<MelsecA1EAsciiModel> plc = null;
  140. Logger LoggerT = null;
  141. try
  142. {
  143. LoggerT = Logger.CreateLogger(logKeyT, logger_t);
  144. lock (LoggerT)
  145. {
  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. // 0:扫智能,1:扫连体
  166. flag = c[0];
  167. }
  168. string anti_fake_code = null;
  169. if (c.Length > 1)
  170. {
  171. anti_fake_code = c[1];
  172. }
  173. int gt = -1;
  174. int.TryParse(flag, out gt);
  175. if (string.IsNullOrWhiteSpace(flag) || gt == -1)
  176. {
  177. outputMessage = $"[{e.Content}]接收设备代码为空";
  178. Logger.Warn(outputMessage, logKeyT, logger_t);
  179. Logger.Warn(outputMessage, logKeyE, logger_e);
  180. FormLogShow?.ShowLog(outputMessage);
  181. return;
  182. }
  183. plc_s = GetPLC_S_GC(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<MelsecA1EAsciiModel>();
  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(anti_fake_code) ||
  201. anti_fake_code == "0" ||
  202. anti_fake_code.Length != 12 ||
  203. anti_fake_code.StartsWith("0") ||
  204. anti_fake_code.StartsWith("ERROR"))
  205. {
  206. outputMessage = $"[{e.Content}]接收防伪码格式错误";
  207. Logger.Warn(outputMessage, logKeyT, logger_t);
  208. //Logger.Warn(outputMessage, logKeyE, logger_e);
  209. FormLogShow?.ShowLog(outputMessage);
  210. //SendError(plc_s, e, logKeyT, logKeyE, plc);
  211. return;
  212. }
  213. //DataRow codeRow = GetGoodsCode(barcode, e, logKeyE);
  214. //if (codeRow == null)
  215. //{
  216. // outputMessage = $"[{e.Content}]此条码[{barcode}]不存在";
  217. // Logger.Warn(outputMessage, logKeyT, logger_t);
  218. // Logger.Warn(outputMessage, logKeyE, logger_e);
  219. // FormLogShow?.ShowLog(outputMessage);
  220. // SendError(plc_s, e, logKeyT, logKeyE, plc);
  221. // return;
  222. //}
  223. //string code = codeRow["goodscode"].ToString();
  224. //string intcode = codeRow["seatcovercode"].ToString();
  225. //outputMessage = $"[{e.Content}]产品型号={code}[{intcode}]";
  226. //Logger.Trace(outputMessage, logKeyT, logger_t);
  227. //FormLogShow?.ShowLog(outputMessage);
  228. //if (string.IsNullOrWhiteSpace(intcode))
  229. //{
  230. // outputMessage = $"[{e.Content}]此条码[{barcode}]未设置型号代码";
  231. // Logger.Error(null, outputMessage, logKeyT, logger_t);
  232. // Logger.Error(null, outputMessage, logKeyE, logger_e);
  233. // FormLogShow?.ShowLog(outputMessage);
  234. // SendError(plc_s, e, logKeyT, logKeyE, plc);
  235. // return;
  236. //}
  237. PLCResult plc_r = null;
  238. //1.产品类型(int)【0:扫智能,1:扫连体,】
  239. //outputMessage = $"[{e.Content}]SET_GT=[{gt}]{plc_s.Add_GT + plc_s.Add_GTNum}";
  240. //Logger.Trace(outputMessage, logKeyT, logger_t);
  241. //FormLogShow?.ShowLog(outputMessage);
  242. //plc_r = plc.Write(plc_s.Add_GT[0], plc_s.Add_GTNum, gt);
  243. //outputMessage = $"[{e.Content}]END_GT=[{plc_r}]{plc_s.Add_GT + plc_s.Add_GTNum}";
  244. //Logger.Trace(outputMessage, logKeyT, logger_t);
  245. //FormLogShow?.ShowLog(outputMessage);
  246. string usercode;
  247. using (IDataAccess dataAccess = PLC_S_DataAccess.GetDataAccess(e))
  248. {
  249. usercode = dataAccess.ExecuteScalar<string>("select t.usercode from tp_mst_workstation t where t.workstationid = 2215");
  250. }
  251. // 0:PLC复位 1:绑定完成 2:绑定异常 3:没有条码
  252. int plc_result = 1;
  253. //4.获取条码(string)【11位条码+0】
  254. outputMessage = $"[{e.Content}]GET_Barcode=[]{plc_s.Add_AF + plc_s.Add_AFNum}";
  255. Logger.Trace(outputMessage, logKeyT, logger_t);
  256. FormLogShow?.ShowLog(outputMessage);
  257. //plc_r = plc.Write(plc_s.Add_AF, plc_s.Add_AFNum + "", anti_fake_code);
  258. PLCResult<string> plc_str = plc.Read<string>(plc_s.Add_AF, plc_s.Add_AFNum + "", 6);
  259. outputMessage = $"[{e.Content}]END_Barcode=[{plc_str}]{plc_s.Add_AF + plc_s.Add_AFNum}";
  260. string barcode = plc_str.Data;
  261. if (string.IsNullOrWhiteSpace(barcode) || barcode.Length != 12)
  262. {
  263. plc_result = 3;
  264. }
  265. else
  266. {
  267. barcode = barcode.Substring(0, 11);
  268. Logger.Trace(outputMessage, logKeyT, logger_t);
  269. FormLogShow?.ShowLog(outputMessage);
  270. string result = SmartDeviceProxy.Instance.Invoke<string>(
  271. p => p.UpdateSecurityCodeByBarcode(
  272. _mes_s.AccountCode,
  273. usercode,
  274. barcode,
  275. anti_fake_code,
  276. "3"
  277. ));
  278. outputMessage = $"[{e.Content}]SE_Result=[{result}]{anti_fake_code}";
  279. Logger.Trace(outputMessage, logKeyT, logger_t);
  280. FormLogShow?.ShowLog(outputMessage);
  281. // 返回状态(成功、失败)
  282. if (result != "OK")
  283. {
  284. if (result == "EX")
  285. {
  286. outputMessage = $"MES服务端异常(详情参见MES端日志)";
  287. }
  288. else if (result == "EU-01")
  289. {
  290. outputMessage = $"无效的帐套或工号(详情参见MES端日志)";
  291. }
  292. else
  293. {
  294. outputMessage = $"其他错误(详情参见MES端日志)[{result}]";
  295. }
  296. Logger.Error(null, outputMessage, logKeyT, logger_t);
  297. Logger.Error(null, outputMessage, logKeyE, logger_e);
  298. FormLogShow?.ShowLog(outputMessage);
  299. plc_result = 2;
  300. }
  301. }
  302. // 0:PLC复位 1:绑定完成 2:绑定异常 3:没有条码
  303. outputMessage = $"[{e.Content}]SET_F_Code=[{plc_result}]{plc_s.Add_GT_F + plc_s.Add_GT_FNum} 0:PLC复位 1:绑定完成 2:绑定异常 3:没有条码";
  304. Logger.Trace(outputMessage, logKeyT, logger_t);
  305. FormLogShow?.ShowLog(outputMessage);
  306. plc_r = plc?.Write<int>(plc_s.Add_GT_F[0], plc_s.Add_GT_FNum, plc_result);
  307. outputMessage = $"[{e.Content}]END_F_Code=[{plc_r}]{plc_s.Add_GT_F + plc_s.Add_GT_FNum}";
  308. Logger.Trace(outputMessage, logKeyT, logger_t);
  309. FormLogShow?.ShowLog(outputMessage);
  310. }
  311. }
  312. catch (Exception ex)
  313. {
  314. FormLogShow?.ShowLog($"[{e.Content}]ERROR={ex.Message}");
  315. if (plc_s != null)
  316. {
  317. Logger.Error(ex, $"[{e.Content}]ERROR", logKeyT, logger_t);
  318. Logger.Error(ex, $"[{e.Content}]ERROR", logKeyE, logger_e);
  319. SendError(plc_s, e, logKeyT, logKeyE, plc);
  320. }
  321. else
  322. {
  323. Logger.Error(ex, $"[{e.Content}]ERROR-NOFLAG", logKeyT, logger_t);
  324. Logger.Error(ex, $"[{e.Content}]ERROR-NOFLAG", logKeyE, logger_e);
  325. }
  326. }
  327. finally
  328. {
  329. LoggerT?.EndTracking();
  330. plc?.Disconnect();
  331. plc?.Close();
  332. //e.ReturnMessage("0000");
  333. }
  334. }
  335. private void SendError(PLC_S_AF_BD plc_s, ReceiveSession e, string loggerNameT,
  336. string loggerNameE, SocketClient<MelsecA1EAsciiModel> plc)
  337. {
  338. try
  339. {
  340. //using (SocketClient<SiemensS7_1200Model> plc = new SocketClient<SiemensS7_1200Model>())
  341. {
  342. // 0:PLC复位 1:绑定完成 2:绑定异常 3:没有条码
  343. string outputMessage = $"[{e.Content}]SET_Error=[2]{plc_s.Add_GT_E + plc_s.Add_GT_ENum} 0:PLC复位 1:绑定完成 2:绑定异常 3:没有条码";
  344. Logger.Trace(outputMessage, loggerNameT, logger_t);
  345. FormLogShow?.ShowLog(outputMessage);
  346. PLCResult plc_r = plc?.Write<int>(plc_s.Add_GT_E[0], plc_s.Add_GT_ENum, 2);
  347. outputMessage = $"[{e.Content}]END_Error=[{plc_r}]{plc_s.Add_GT_E + plc_s.Add_GT_ENum}";
  348. Logger.Trace(outputMessage, loggerNameT, logger_t);
  349. FormLogShow?.ShowLog(outputMessage);
  350. }
  351. }
  352. catch (Exception ex)
  353. {
  354. Logger.Error(ex, $"[{e.Content}]ERROR-SendError", loggerNameT, logger_t);
  355. Logger.Error(ex, $"[{e.Content}]ERROR-SendError", loggerNameE, logger_e);
  356. FormLogShow?.ShowLog($"[{e.Content}]ERROR-SendError={ex.Message}");
  357. }
  358. }
  359. private DataRow GetGoodsCode(string barcode, ReceiveSession e, string loggerName)
  360. {
  361. IDataAccess dataAccess = null;
  362. try
  363. {
  364. dataAccess = PLC_S_DataAccess.GetDataAccess(e);
  365. string sqlString = "select g.goodscode, g.seatcovercode\n" +
  366. " from tp_pm_groutingdailydetail gdd\n" +
  367. " inner join tp_mst_goods g\n" +
  368. " on g.goodsid = gdd.goodsid\n" +
  369. " where gdd.barcode = :barcode";
  370. DataTable dataTable = dataAccess.ExecuteDatatable(sqlString, new CDAParameter(":barcode", barcode));
  371. if (dataTable.HasData())
  372. {
  373. return dataTable.Rows[0];
  374. }
  375. return null;
  376. }
  377. catch (Exception ex)
  378. {
  379. Logger.Error(ex, $"[{e.Content}]ERROR-GetGoodsCode", loggerName, logger_e);
  380. FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetGoodsCode={ex.Message}");
  381. return null;
  382. }
  383. finally
  384. {
  385. dataAccess?.Close();
  386. }
  387. }
  388. private PLC_S_AF_BD GetPLC_S_GC(string flag, ReceiveSession e, string loggerName)
  389. {
  390. try
  391. {
  392. //flag = M_NAME + flag;
  393. flag = M_NAME;
  394. if (PLC_FLAGS.ContainsKey(flag))
  395. {
  396. return PLC_FLAGS[flag];
  397. }
  398. else
  399. {
  400. PLC_S_AF_BD plc_s = new PLC_S_AF_BD();
  401. INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_{M_NAME}.ini");
  402. plc_s.IP = ini.Read(flag, "IP");
  403. plc_s.Port = ini.Read(flag, "Port").ToInt32();
  404. //1.产品类型(int)【0:扫智能,1:扫连体,】【废弃】
  405. plc_s.Add_GT = ini.Read(flag, "Add_GT");
  406. plc_s.Add_GTNum = ini.Read(flag, "Add_GTNum").ToInt32();
  407. //2.产品型号标识(int)【1:MES写入完成,2:PLC读取完成,0:PLC复位】
  408. // 0:PLC复位 1:绑定完成 2:绑定异常 3:没有条码
  409. plc_s.Add_GT_F = ini.Read(flag, "Add_GT_F");
  410. plc_s.Add_GT_FNum = ini.Read(flag, "Add_GT_FNum").ToInt32();
  411. //3.产品型号处理异常(int)【1:产品型号异常时MES写入,0:PLC复位】
  412. // 0:PLC复位 1:绑定完成 2:绑定异常 3:没有条码
  413. plc_s.Add_GT_E = ini.Read(flag, "Add_GT_E");
  414. plc_s.Add_GT_ENum = ini.Read(flag, "Add_GT_ENum").ToInt32();
  415. //4.防伪码条码(string)【12位防伪码寄存】
  416. plc_s.Add_AF = ini.Read(flag, "Add_AF");
  417. plc_s.Add_AFNum = ini.Read(flag, "Add_AFNum").ToInt32();
  418. PLC_FLAGS.Add(flag, plc_s);
  419. return plc_s;
  420. }
  421. }
  422. catch (Exception ex)
  423. {
  424. Logger.Error(ex, $"[{e.Content}]ERROR-GetPLC_S_GC", loggerName, logger_e);
  425. FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetPLC_S_GC={ex.Message}");
  426. return null;
  427. }
  428. }
  429. }
  430. }