BarcodePrint1.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using Curtain.Core;
  6. using Curtain.Extension.ExObjectConvert;
  7. using Curtain.Framework.Json;
  8. using Curtain.Helpers;
  9. using Curtain.Log;
  10. using Curtain.Net.Sockets.PLC;
  11. using Curtain.Net.Sockets.PLC.Model;
  12. using Curtain.Net.Sockets.PLC.Model.Melsec;
  13. using PLC_S.Proxy;
  14. namespace PLC_S.ServerModel
  15. {
  16. /// <summary>
  17. /// 条码打印
  18. /// </summary>
  19. public class BarcodePrint1 : SocketServer<SimpleSocketServerModel>, IShowFormLog, IWindowsServer
  20. {
  21. public static Dictionary<string, PLC_S_BP> PLC_FLAGS = new Dictionary<string, PLC_S_BP>();
  22. public const string M_NAME = "BP";
  23. private MES_S _mes_s = null;
  24. private readonly Logger logger = Logger.CreateLogger(M_NAME);
  25. private readonly LogInfo logger_t = new LogInfo();
  26. private readonly LogInfo logger_e = new LogInfo();
  27. public int Interval
  28. {
  29. get;
  30. set;
  31. }
  32. public string EPTS_CODE
  33. {
  34. get;
  35. set;
  36. }
  37. public BarcodePrint1()
  38. {
  39. logger.FileExistDays = 30;
  40. logger.FilePrefix = "";
  41. logger.FileSuffix = M_NAME;
  42. logger_t.LevelOneFile = true;
  43. logger.FileNameWithoutDate = true;
  44. logger_t.SubFolderFormat = "<name>";
  45. logger_t.FileExistDays = 30;
  46. logger_t.LevelOneFile = true;
  47. logger_e.FileExistDays = 0;
  48. this.Model.FormatType = CommandFormatType.StartStopChar;
  49. this.ServerStarting += ServerSocket_ServerStarting;
  50. this.ServerStarted += ServerSocket_ServerStarted;
  51. this.ServerStoping += ServerSocket_ServerStoping;
  52. this.ServerStoped += ServerSocket_ServerStoped;
  53. this.ServerMessage += ServerSocket_ServerMessage;
  54. this.Received += ServerSocket_Received;
  55. _mes_s = MES_S.Get();
  56. }
  57. #region Server
  58. public IFormLogShow FormLogShow
  59. {
  60. get;
  61. set;
  62. }
  63. private void ServerSocket_ServerStarting(object sender, CancelEventArgs e)
  64. {
  65. try
  66. {
  67. logger.OutputTrace($"{M_NAME}_ServerStarting");
  68. }
  69. catch { }
  70. }
  71. private void ServerSocket_ServerStarted(object sender, EventArgs e)
  72. {
  73. try
  74. {
  75. logger.OutputTrace($"{M_NAME}_ServerStarted");
  76. }
  77. catch { }
  78. }
  79. private void ServerSocket_ServerStoping(object sender, CancelEventArgs e)
  80. {
  81. try
  82. {
  83. logger.OutputTrace($"{M_NAME}_ServerStoping");
  84. }
  85. catch { }
  86. }
  87. private void ServerSocket_ServerStoped(object sender, EventArgs e)
  88. {
  89. try
  90. {
  91. logger.OutputTrace($"{M_NAME}_ServerStoped");
  92. }
  93. catch { }
  94. }
  95. private void ServerSocket_ServerMessage(object sender, ServerMessageEventArgs e)
  96. {
  97. try
  98. {
  99. string message = e.ToString();
  100. string cip = e?.Client?.IP;
  101. if (e.Type == ServerMessageType.Debug)
  102. {
  103. Logger.Debug(message, $"{M_NAME}-T[{cip}]", logger_t);
  104. }
  105. if (e.Type == ServerMessageType.Error)
  106. {
  107. Logger.Error(e.Exception, message, $"{M_NAME}-E[{cip}]", logger_e);
  108. }
  109. if (e.Type == ServerMessageType.Trace)
  110. {
  111. Logger.Trace(message, $"{M_NAME}-T[{cip}]", logger_t);
  112. }
  113. if (e.Type == ServerMessageType.Warning)
  114. {
  115. Logger.Warn(message, $"{M_NAME}-E[{cip}]", logger_e);
  116. }
  117. FormLogShow?.ShowLog("ServerMessage=" + message);
  118. }
  119. catch { }
  120. }
  121. #endregion
  122. public string[] bs = new string[1];
  123. /// <summary>
  124. /// 返回产品型号
  125. /// </summary>
  126. /// <param name="sender"></param>
  127. /// <param name="e"></param>
  128. private void ServerSocket_Received(object sender, ReceiveSession e)
  129. {
  130. PLC_S_BP plc_s = null;
  131. string cIP = e?.Client?.IP;
  132. string logKeyT = $"{M_NAME}-T[{cIP}]";
  133. string logKeyE = $"{M_NAME}-E[{cIP}]";
  134. try
  135. {
  136. //foreach (string b in bs)
  137. //{
  138. // string result1 = SmartDeviceProxy.Instance.Invoke<string>(
  139. // p => p.PrintBarcodeTest(
  140. // _mes_s.AccountCode,
  141. // "DONGKEL",
  142. // b,
  143. // null,
  144. // 1,
  145. // null, 2));
  146. //}
  147. //return;
  148. string outputMessage = e.ToString();
  149. Logger.Trace(outputMessage, logKeyT, logger_t);
  150. FormLogShow?.ShowLog(outputMessage);
  151. if (string.IsNullOrWhiteSpace(e.Content))
  152. {
  153. outputMessage = "接收数据为空";
  154. Logger.Warn(outputMessage, logKeyT, logger_t);
  155. Logger.Warn(outputMessage, logKeyE, logger_e);
  156. FormLogShow?.ShowLog(outputMessage);
  157. return;
  158. }
  159. outputMessage = e.Content;
  160. Logger.Trace(outputMessage, logKeyT, logger_t);
  161. FormLogShow?.ShowLog(outputMessage);
  162. string flag = null;
  163. string[] c = e.Content.Split('#');
  164. if (c.Length > 0)
  165. {
  166. flag = c[0];
  167. }
  168. string barcode = null;
  169. if (c.Length > 1)
  170. {
  171. barcode = c[1];
  172. }
  173. if (string.IsNullOrWhiteSpace(flag))
  174. {
  175. outputMessage = $"[{e.Content}]接收设备代码为空";
  176. Logger.Warn(outputMessage, logKeyT, logger_t);
  177. Logger.Warn(outputMessage, logKeyE, logger_e);
  178. FormLogShow?.ShowLog(outputMessage);
  179. return;
  180. }
  181. plc_s = GetPLC_S_BP(flag, e, logKeyE);
  182. if (plc_s == null)
  183. {
  184. outputMessage = $"[{e.Content}]设备代码[{flag}]未找到ini配置或有错误";
  185. Logger.Error(null, outputMessage, logKeyT, logger_t);
  186. Logger.Error(null, outputMessage, logKeyE, logger_e);
  187. FormLogShow?.ShowLog(outputMessage);
  188. return;
  189. }
  190. outputMessage = $"[{e.Content}]{JsonHelper.FromObject(plc_s)}";
  191. Logger.Trace(outputMessage, logKeyT, logger_t);
  192. FormLogShow?.ShowLog(outputMessage);
  193. //MES_S _mes_s = MES_S.Get();
  194. string result = SmartDeviceProxy.Instance.Invoke<string>(
  195. p => p.PrintBarcode(
  196. _mes_s.AccountCode,
  197. plc_s.USER_CDOE,
  198. barcode,
  199. null,
  200. plc_s.COPIES,
  201. null));
  202. // 返回状态(成功、失败)
  203. if (result == "OK")
  204. {
  205. //using (SocketClient<MelsecA1EAsciiModel> plc = new SocketClient<MelsecA1EAsciiModel>())
  206. //{
  207. // plc.Connect(plc_s.IP, plc_s.Port);
  208. // plc.Write(plc_s.Add_Code[0], plc_s.Add_CodeNum, 1);
  209. //}
  210. return;
  211. }
  212. if (result == "EE")
  213. {
  214. outputMessage = $"MES服务端异常(详情参见MES端日志)";
  215. }
  216. else if (result == "EU-01")
  217. {
  218. outputMessage = $"无效的帐套或工号(详情参见MES端日志)";
  219. }
  220. else
  221. {
  222. outputMessage = $"其他错误(详情参见MES端日志)[{result}]";
  223. }
  224. Logger.Error(null, outputMessage, logKeyT, logger_t);
  225. Logger.Error(null, outputMessage, logKeyE, logger_e);
  226. FormLogShow?.ShowLog(outputMessage);
  227. SendError(plc_s, e, logKeyE);
  228. }
  229. catch (Exception ex)
  230. {
  231. FormLogShow?.ShowLog($"[{e.Content}]ERROR={ex.Message}");
  232. if (plc_s != null)
  233. {
  234. Logger.Error(ex, $"ERROR[{e.Content}]", logKeyE, logger_e);
  235. SendError(plc_s, e, logKeyE);
  236. }
  237. else
  238. {
  239. Logger.Error(ex, $"ERROR-NOFLAG[{e.Content}]", logKeyE, logger_e);
  240. }
  241. }
  242. finally
  243. {
  244. e.ReturnMessage("0000");
  245. }
  246. }
  247. private void SendError(PLC_S_BP plc_s, ReceiveSession e, string loggerName)
  248. {
  249. try
  250. {
  251. //using (SocketClient<MelsecA1EAsciiModel> plc = new SocketClient<MelsecA1EAsciiModel>())
  252. //{
  253. // plc.Connect(plc_s.IP, plc_s.Port);
  254. // plc.Write(plc_s.Add_Error[0], plc_s.Add_ErrorNum, 1);
  255. //}
  256. }
  257. catch (Exception ex)
  258. {
  259. //Logger.Error(ex, $"ERROR[{e.Content}]", $"{M_NAME}-E[{e?.Client?.IP}]", logger_e);
  260. Logger.Error(ex, $"ERROR[{e.Content}]", loggerName, logger_e);
  261. FormLogShow?.ShowLog("SendError=" + ex.Message);
  262. }
  263. }
  264. private PLC_S_BP GetPLC_S_BP(string flag, ReceiveSession e, string loggerName)
  265. {
  266. try
  267. {
  268. if (PLC_FLAGS.ContainsKey(flag))
  269. {
  270. return PLC_FLAGS[flag];
  271. }
  272. else
  273. {
  274. PLC_S_BP plc_s = new PLC_S_BP();
  275. INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_{M_NAME}.ini");
  276. plc_s.PRINTER_ID = ini.Read(flag, "PRINTER_ID").ToInt32();
  277. plc_s.USER_CDOE = ini.Read(flag, "USER_CDOE");
  278. plc_s.COPIES = ini.Read(flag, "COPIES").ToInt32();
  279. plc_s.IP = ini.Read(flag, "IP");
  280. plc_s.Port = ini.Read(flag, "Port").ToInt32();
  281. plc_s.Add_Code = ini.Read(flag, "Add_Code");
  282. plc_s.Add_CodeNum = ini.Read(flag, "Add_CodeNum");
  283. plc_s.Add_Error = ini.Read(flag, "Add_Error");
  284. plc_s.Add_ErrorNum = ini.Read(flag, "Add_ErrorNum");
  285. //PLC_FLAGS.Add(flag, gcplc);
  286. return plc_s;
  287. }
  288. }
  289. catch (Exception ex)
  290. {
  291. //Logger.Error(ex, $"ERROR[{e.Content}]", $"{M_NAME}-E[{e?.Client?.IP}]", logger_e);
  292. Logger.Error(ex, $"ERROR[{e.Content}]", loggerName, logger_e);
  293. FormLogShow?.ShowLog("GetPLC_S_BP=" + ex.Message);
  294. return null;
  295. }
  296. }
  297. }
  298. }