GoodsPiece_JM3.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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:工序异常,0:复位)等信息(3#精坯出库-金马)【废弃,逻辑同GoodsPiece_QR2】
  21. /// </summary>
  22. public class GoodsPiece_JM3 : SocketServer<SimpleSocketServerModel>, IShowFormLog, IWindowsServer
  23. {
  24. /// <summary>
  25. /// ini 实体
  26. /// </summary>
  27. public Dictionary<string, PLC_S_GP_QR> PLC_FLAGS = new Dictionary<string, PLC_S_GP_QR>();
  28. /// <summary>
  29. /// 对接点编码
  30. /// </summary>
  31. public const string M_NAME = "GP_QR";
  32. private MES_S _mes_s = null;
  33. private readonly Logger logger = Logger.CreateLogger(M_NAME);
  34. private readonly LogInfo logger_t = new LogInfo();
  35. private readonly LogInfo logger_e = new LogInfo();
  36. public int Interval
  37. {
  38. get;
  39. set;
  40. }
  41. public string EPTS_CODE
  42. {
  43. get;
  44. set;
  45. }
  46. public GoodsPiece_JM3()
  47. {
  48. logger.FileExistDays = 30;
  49. logger.FilePrefix = "";
  50. logger.FileSuffix = M_NAME;
  51. logger.LevelOneFile = true;
  52. logger.FileNameWithoutDate = true;
  53. logger_t.SubFolderFormat = "<name>";
  54. logger_t.FileExistDays = 30;
  55. logger_t.LevelOneFile = true;
  56. logger_e.FileExistDays = 10;
  57. this.Model.FormatType = CommandFormatType.StartStopChar;
  58. this.ServerStarting += ServerSocket_ServerStarting;
  59. this.ServerStarted += ServerSocket_ServerStarted;
  60. this.ServerStoping += ServerSocket_ServerStoping;
  61. this.ServerStoped += ServerSocket_ServerStoped;
  62. this.ServerMessage += ServerSocket_ServerMessage;
  63. this.Received += ServerSocket_Received;
  64. _mes_s = MES_S.Get();
  65. }
  66. #region Server
  67. public IFormLogShow FormLogShow
  68. {
  69. get;
  70. set;
  71. }
  72. private void ServerSocket_ServerStarting(object sender, CancelEventArgs e)
  73. {
  74. try
  75. {
  76. logger.OutputTrace($"{M_NAME}_ServerStarting");
  77. }
  78. catch { }
  79. }
  80. private void ServerSocket_ServerStarted(object sender, EventArgs e)
  81. {
  82. try
  83. {
  84. logger.OutputTrace($"{M_NAME}_ServerStarted");
  85. }
  86. catch { }
  87. }
  88. private void ServerSocket_ServerStoping(object sender, CancelEventArgs e)
  89. {
  90. try
  91. {
  92. logger.OutputTrace($"{M_NAME}_ServerStoping");
  93. }
  94. catch { }
  95. }
  96. private void ServerSocket_ServerStoped(object sender, EventArgs e)
  97. {
  98. try
  99. {
  100. logger.OutputTrace($"{M_NAME}_ServerStoped");
  101. }
  102. catch { }
  103. }
  104. private void ServerSocket_ServerMessage(object sender, ServerMessageEventArgs e)
  105. {
  106. try
  107. {
  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. /// <summary>
  132. /// 返回产品型号
  133. /// </summary>
  134. /// <param name="sender"></param>
  135. /// <param name="e"></param>
  136. private void ServerSocket_Received(object sender, ReceiveSession e)
  137. {
  138. PLC_S_GP_QR plc_s = null;
  139. string cIP = e?.Client?.IP;
  140. string logKeyT = $"{M_NAME}-T[{cIP}]";
  141. string logKeyE = $"{M_NAME}-E[{cIP}]";
  142. SocketClient<SiemensS7_1200Model> plc = null;
  143. Logger LoggerT = Logger.CreateLogger(logKeyT, logger_t);
  144. lock (LoggerT)
  145. {
  146. try
  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. SendCode(plc_s, e, logKeyT, logKeyE, plc, 2);
  159. return;
  160. }
  161. outputMessage = e.Content;
  162. Logger.Trace(outputMessage, logKeyT, logger_t);
  163. FormLogShow?.ShowLog(outputMessage);
  164. string[] c = e.Content.Split('#');
  165. string flag = null;
  166. if (c.Length > 0)
  167. {
  168. flag = c[0];
  169. }
  170. string barcode = null;
  171. if (c.Length > 1)
  172. {
  173. barcode = c[1];
  174. }
  175. if (string.IsNullOrWhiteSpace(flag))
  176. {
  177. outputMessage = $"[{e.Content}]接收设备代码为空";
  178. Logger.Warn(outputMessage, logKeyT, logger_t);
  179. Logger.Warn(outputMessage, logKeyE, logger_e);
  180. FormLogShow?.ShowLog(outputMessage);
  181. SendCode(plc_s, e, logKeyT, logKeyE, plc, 2);
  182. return;
  183. }
  184. plc_s = GetPLC_S_GP(flag, e, logKeyE);
  185. if (plc_s == null || string.IsNullOrWhiteSpace(plc_s.IP))
  186. {
  187. outputMessage = $"[{e.Content}]设备代码[{flag}]未找到ini配置或有错误";
  188. Logger.Error(null, outputMessage, logKeyT, logger_t);
  189. Logger.Error(null, outputMessage, logKeyE, logger_e);
  190. FormLogShow?.ShowLog(outputMessage);
  191. SendCode(plc_s, e, logKeyT, logKeyE, plc, 2);
  192. return;
  193. }
  194. outputMessage = $"[{e.Content}]PLC_Connect={JsonHelper.FromObject(plc_s)}";
  195. Logger.Trace(outputMessage, logKeyT, logger_t);
  196. FormLogShow?.ShowLog(outputMessage);
  197. plc = new SocketClient<SiemensS7_1200Model>();
  198. plc.Connect(plc_s.IP, plc_s.Port);
  199. if (string.IsNullOrWhiteSpace(barcode) ||
  200. barcode == "0" ||
  201. barcode.Length != 11 ||
  202. barcode.StartsWith("0"))
  203. {
  204. outputMessage = $"[{e.Content}]接收条码格式错误";
  205. Logger.Warn(outputMessage, logKeyT, logger_t);
  206. Logger.Warn(outputMessage, logKeyE, logger_e);
  207. FormLogShow?.ShowLog(outputMessage);
  208. SendCode(plc_s, e, logKeyT, logKeyE, plc, 2);
  209. return;
  210. }
  211. // logic begin
  212. //string usercode = "";
  213. string p_id = "";
  214. short plc_flag = 1;
  215. using (IDataAccess dataAccess = PLC_S_DataAccess.GetDataAccess())
  216. {
  217. //string sqlString = "select gdd.GOODSLEVELTYPEID,g.goodstypeid, inp.flowprocedureid\n" +
  218. //" from tp_pm_groutingdailydetail gdd\n" +
  219. //" LEFT JOIN tp_pm_inproduction inp on inp.groutingdailydetailid = gdd.groutingdailydetailid\n" +
  220. //" inner join tp_mst_goods g on g.goodsid = gdd.goodsid\n" +
  221. //" where gdd.barcode = :barcode";
  222. string sqlString = "select gdd.flowprocedureid\n" +
  223. " from tp_pm_inproduction gdd\n" +
  224. " where gdd.barcode = :barcode";
  225. DataTable dataTable = dataAccess.ExecuteDatatable(sqlString, new CDAParameter(":barcode", barcode));
  226. DataRow codeRow = null;
  227. if (dataTable.HasData())
  228. {
  229. codeRow = dataTable.Rows[0];
  230. }
  231. if (codeRow == null)
  232. {
  233. outputMessage = $"[{e.Content}]此条码[{barcode}]不存在";
  234. Logger.Warn(outputMessage, logKeyT, logger_t);
  235. Logger.Warn(outputMessage, logKeyE, logger_e);
  236. FormLogShow?.ShowLog(outputMessage);
  237. SendCode(plc_s, e, logKeyT, logKeyE, plc, 3);
  238. return;
  239. }
  240. p_id = codeRow["flowprocedureid"].ToString();
  241. //// TODO 固定工号 or 班组工号?
  242. //// 刮边工号
  243. //sqlString = @"
  244. // SELECT t.usercode, u.userid
  245. // FROM tp_mst_workstation t
  246. // inner join tp_mst_user u on u.usercode = t.usercode
  247. // WHERE t.workstationtypeid = 3201
  248. // AND t.workstationid = @workstationid@
  249. // ";
  250. //CDAParameter[] ps = new CDAParameter[]
  251. //{
  252. // new CDAParameter("workstationid", plc_s.WS_ID),
  253. //};
  254. //int userid = 0;
  255. //dataTable = dataAccess.ExecuteDatatable(sqlString, ps);
  256. //if (dataTable.HasData())
  257. //{
  258. // usercode = dataTable.Rows[0]["usercode"].ToString();
  259. // userid = dataTable.Rows[0]["userid"].ToInt32();
  260. //}
  261. //outputMessage = $"[{e.Content}]DB_UserCode=[{usercode}]";
  262. //Logger.Trace(outputMessage, logKeyT, logger_t);
  263. //FormLogShow?.ShowLog(outputMessage);
  264. }
  265. if (p_id != plc_s.P_ID.ToString())
  266. {
  267. // 3# 工序计件
  268. string result = SmartDeviceProxy.Instance.Invoke<string>(
  269. p => p.AddWorkPieceByStatus3(
  270. _mes_s.AccountCode,
  271. plc_s.USER_CODE,
  272. //usercode,
  273. null,
  274. plc_s.P_ID,
  275. barcode,
  276. null));
  277. outputMessage = $"[{e.Content}]MES_Result=[{result}]";
  278. Logger.Trace(outputMessage, logKeyT, logger_t);
  279. FormLogShow?.ShowLog(outputMessage);
  280. // 返回状态(成功、失败)
  281. if (result != "OK")
  282. {
  283. plc_flag = 3;
  284. if (result == "EE")
  285. {
  286. plc_flag = 4;
  287. outputMessage = $"MES服务端异常(详情参见MES端日志)";
  288. }
  289. else if (result == "EU-01")
  290. {
  291. outputMessage = $"无效的帐套或工号(详情参见MES端日志)";
  292. }
  293. else
  294. {
  295. outputMessage = $"其他错误(详情参见MES端日志)[{result}]";
  296. }
  297. Logger.Error(null, outputMessage, logKeyT, logger_t);
  298. Logger.Error(null, outputMessage, logKeyE, logger_e);
  299. FormLogShow?.ShowLog(outputMessage);
  300. SendCode(plc_s, e, logKeyT, logKeyE, plc, plc_flag);
  301. return;
  302. }
  303. }
  304. //1:通过
  305. SendCode(plc_s, e, logKeyT, logKeyE, plc, 1);
  306. }
  307. catch (Exception ex)
  308. {
  309. FormLogShow?.ShowLog($"[{e.Content}]ERROR={ex.Message}");
  310. if (plc_s != null)
  311. {
  312. Logger.Error(ex, $"[{e.Content}]ERROR", logKeyT, logger_t);
  313. Logger.Error(ex, $"[{e.Content}]ERROR", logKeyE, logger_e);
  314. if (plc?.Socket?.Connected ?? false)
  315. {
  316. SendCode(plc_s, e, logKeyT, logKeyE, plc, 4);
  317. }
  318. }
  319. else
  320. {
  321. Logger.Error(ex, $"[{e.Content}]ERROR-NOFLAG", logKeyT, logger_t);
  322. Logger.Error(ex, $"[{e.Content}]ERROR-NOFLAG", logKeyE, logger_e);
  323. }
  324. }
  325. finally
  326. {
  327. LoggerT?.EndTracking();
  328. plc?.Disconnect();
  329. plc?.Close();
  330. //e.ReturnMessage("0000");
  331. }
  332. }
  333. }
  334. private void SendCode(PLC_S_GP_QR plc_s, ReceiveSession e, string loggerNameT,
  335. string loggerNameE, SocketClient<SiemensS7_1200Model> plc, short code)
  336. {
  337. try
  338. {
  339. //using (SocketClient<SiemensS7_1200Model> plc = new SocketClient<SiemensS7_1200Model>())
  340. {
  341. // 扫码结果:【1:通过,2.条码格式错误,3:条码不可到达,4:工序异常,0:复位】
  342. string outputMessage = $"[{e.Content}]SET_CODE=[{code}]{plc_s.Add_Code + plc_s.Add_CodeNum}【1:通过,2.条码格式错误,3:条码不可到达,4:工序异常】";
  343. Logger.Trace(outputMessage, loggerNameT, logger_t);
  344. FormLogShow?.ShowLog(outputMessage);
  345. PLCResult p_r = plc?.Write<short>(plc_s.Add_Code, plc_s.Add_CodeNum, code);
  346. outputMessage = $"[{e.Content}]END_CODE=[{p_r}]{plc_s.Add_Code + plc_s.Add_CodeNum}";
  347. Logger.Trace(outputMessage, loggerNameT, logger_t);
  348. FormLogShow?.ShowLog(outputMessage);
  349. }
  350. }
  351. catch (Exception ex)
  352. {
  353. Logger.Error(ex, $"[{e.Content}]ERROR-SendCode", loggerNameT, logger_t);
  354. Logger.Error(ex, $"[{e.Content}]ERROR-SendCode", loggerNameE, logger_e);
  355. FormLogShow?.ShowLog($"[{e.Content}]ERROR-SendCode={ex.Message}");
  356. }
  357. }
  358. private PLC_S_GP_QR GetPLC_S_GP(string flag, ReceiveSession e, string loggerName)
  359. {
  360. try
  361. {
  362. flag = M_NAME + flag;
  363. if (PLC_FLAGS.ContainsKey(flag))
  364. {
  365. return PLC_FLAGS[flag];
  366. }
  367. else
  368. {
  369. PLC_S_GP_QR plc_s = new PLC_S_GP_QR();
  370. INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_{M_NAME}.ini");
  371. plc_s.IP = ini.Read(flag, "IP");
  372. plc_s.Port = ini.Read(flag, "Port").ToInt32();
  373. plc_s.P_ID = ini.Read(flag, "P_ID").ToInt32();
  374. plc_s.WS_ID = ini.Read(flag, "WS_ID").ToInt32();
  375. plc_s.USER_CODE = ini.Read(flag, "USER_CODE");
  376. plc_s.Add_Code = ini.Read(flag, "Add_Code");
  377. plc_s.Add_CodeNum = ini.Read(flag, "Add_CodeNum");
  378. plc_s.Add_BarCode = ini.Read(flag, "Add_BarCode");
  379. plc_s.Add_BarCodeNum = ini.Read(flag, "Add_BarCodeNum");
  380. plc_s.Add_GoodsType = ini.Read(flag, "Add_GoodsType");
  381. plc_s.Add_GoodsTypeNum = ini.Read(flag, "Add_GoodsTypeNum");
  382. PLC_FLAGS.Add(flag, plc_s);
  383. return plc_s;
  384. }
  385. }
  386. catch (Exception ex)
  387. {
  388. Logger.Error(ex, $"[{e.Content}]ERROR-GetPLC_S_GP", loggerName, logger_e);
  389. FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetPLC_S_GP={ex.Message}");
  390. return null;
  391. }
  392. }
  393. }
  394. }