FGC_BZ.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Threading;
  7. using Curtain.Core;
  8. using Curtain.DataAccess;
  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.Melsec;
  15. using Curtain.Net.Sockets.PLC.Model.Siemens;
  16. using PLC_S.Proxy;
  17. namespace PLC_S.ServerModel
  18. {
  19. /// <summary>
  20. /// 计件(中陶,机械臂施釉)
  21. /// </summary>
  22. public class FGC_BZ : SocketServer<SimpleSocketServerModel>, IShowFormLog, IWindowsServer
  23. {
  24. //public Dictionary<string, PLC_S_PW> PLC_FLAGS = new Dictionary<string, PLC_S_PW>();
  25. //public Dictionary<string, PLC_S_GC> PLC_FLAGS_GC = new Dictionary<string, PLC_S_GC>();
  26. public const string M_NAME = "FGC_BZ";
  27. private readonly MES_S _mes_s = null;
  28. private readonly Logger logger = Logger.CreateLogger(M_NAME);
  29. private readonly LogInfo logger_t = new LogInfo();
  30. private readonly LogInfo logger_e = new LogInfo();
  31. private FGC_BZ_INI _FGC_BZ_INI = null;
  32. private bool _S_STOP = false;
  33. public FGC_BZ()
  34. {
  35. logger.FileExistDays = 30;
  36. logger.FilePrefix = "";
  37. logger.FileSuffix = M_NAME;
  38. logger_t.LevelOneFile = true;
  39. logger.FileNameWithoutDate = true;
  40. logger_t.SubFolderFormat = "<name>";
  41. logger_t.FileExistDays = 30;
  42. logger_t.LevelOneFile = true;
  43. logger_e.FileExistDays = 0;
  44. this.Model.FormatType = CommandFormatType.StartStopChar;
  45. this.ServerStarting += ServerSocket_ServerStarting;
  46. this.ServerStarted += ServerSocket_ServerStarted;
  47. this.ServerStoping += ServerSocket_ServerStoping;
  48. this.ServerStoped += ServerSocket_ServerStoped;
  49. this.ServerMessage += ServerSocket_ServerMessage;
  50. this.Received += ServerSocket_Received;
  51. _mes_s = MES_S.Get();
  52. _FGC_BZ_INI = FGC_BZ_INI.Get();
  53. }
  54. #region Server
  55. public IFormLogShow FormLogShow
  56. {
  57. get;
  58. set;
  59. }
  60. private void ServerSocket_ServerStarting(object sender, CancelEventArgs e)
  61. {
  62. try
  63. {
  64. _S_STOP = false;
  65. logger.OutputTrace($"{M_NAME}_ServerStarting");
  66. }
  67. catch { }
  68. }
  69. private void ServerSocket_ServerStarted(object sender, EventArgs e)
  70. {
  71. try
  72. {
  73. _S_STOP = false;
  74. logger.OutputTrace($"{M_NAME}_ServerStarted");
  75. }
  76. catch { }
  77. }
  78. private void ServerSocket_ServerStoping(object sender, CancelEventArgs e)
  79. {
  80. try
  81. {
  82. _S_STOP = true;
  83. logger.OutputTrace($"{M_NAME}_ServerStoping");
  84. }
  85. catch { }
  86. }
  87. private void ServerSocket_ServerStoped(object sender, EventArgs e)
  88. {
  89. try
  90. {
  91. _S_STOP = true;
  92. logger.OutputTrace($"{M_NAME}_ServerStoped");
  93. }
  94. catch { }
  95. }
  96. private void ServerSocket_ServerMessage(object sender, ServerMessageEventArgs e)
  97. {
  98. try
  99. {
  100. _S_STOP = true;
  101. string message = e.ToString();
  102. string cip = e?.Client?.IP;
  103. if (e.Type == ServerMessageType.Debug)
  104. {
  105. Logger.Debug(message, $"{M_NAME}-T[{cip}]", logger_t);
  106. }
  107. if (e.Type == ServerMessageType.Error)
  108. {
  109. Logger.Error(e.Exception, message, $"{M_NAME}-E[{cip}]", logger_e);
  110. }
  111. if (e.Type == ServerMessageType.Trace)
  112. {
  113. Logger.Trace(message, $"{M_NAME}-T[{cip}]", logger_t);
  114. }
  115. if (e.Type == ServerMessageType.Warning)
  116. {
  117. Logger.Warn(message, $"{M_NAME}-E[{cip}]", logger_e);
  118. }
  119. FormLogShow?.ShowLog("ServerMessage=" + message);
  120. }
  121. catch { }
  122. }
  123. #endregion
  124. private string _RECEIVED_CODE = null;
  125. /// <summary>
  126. /// 返回产品型号
  127. /// </summary>
  128. /// <param name="sender"></param>
  129. /// <param name="e"></param>
  130. private void ServerSocket_Received(object sender, ReceiveSession e)
  131. {
  132. //string receivedCode = Guid.NewGuid().ToString();
  133. //_RECEIVED_CODE = receivedCode;
  134. //_S_STOP = true;
  135. //lock (this)
  136. {
  137. string cIP = e?.Client?.IP;
  138. string logKeyT = $"{M_NAME}-T[{cIP}]";
  139. string logKeyE = $"{M_NAME}-E[{cIP}]";
  140. //if (_RECEIVED_CODE != receivedCode)
  141. //{
  142. // string outputMessage = $"[{e.Content}]不是当前请求,退出排队";
  143. // Logger.Warn(outputMessage, logKeyT, logger_t);
  144. // Logger.Warn(outputMessage, logKeyE, logger_e);
  145. // FormLogShow?.ShowLog(outputMessage);
  146. // return;
  147. //}
  148. //_S_STOP = false;
  149. //PLC_S_PW plc_s = null;
  150. //SocketClient<SiemensS7_1200Model> plc = null;
  151. try
  152. {
  153. string outputMessage = e.ToString();
  154. Logger.Trace(outputMessage, logKeyT, logger_t);
  155. FormLogShow?.ShowLog(outputMessage);
  156. if (string.IsNullOrWhiteSpace(e.Content))
  157. {
  158. outputMessage = "接收数据为空";
  159. Logger.Warn(outputMessage, logKeyT, logger_t);
  160. Logger.Warn(outputMessage, logKeyE, logger_e);
  161. FormLogShow?.ShowLog(outputMessage);
  162. SendError(e, logKeyT, logKeyE);
  163. return;
  164. }
  165. outputMessage = e.Content;
  166. Logger.Trace(outputMessage, logKeyT, logger_t);
  167. FormLogShow?.ShowLog(outputMessage);
  168. string flag = null;
  169. string[] c = e.Content.Split('#');
  170. if (c.Length > 0)
  171. {
  172. flag = c[0];
  173. }
  174. string barcode = null;
  175. if (c.Length > 1)
  176. {
  177. barcode = c[1];
  178. }
  179. if (string.IsNullOrWhiteSpace(barcode) || barcode == "0" || barcode.StartsWith("0"))
  180. {
  181. outputMessage = $"[{e.Content}]接收条码为空";
  182. Logger.Warn(outputMessage, logKeyT, logger_t);
  183. Logger.Warn(outputMessage, logKeyE, logger_e);
  184. FormLogShow?.ShowLog(outputMessage);
  185. SendError(e, logKeyT, logKeyE);
  186. return;
  187. }
  188. outputMessage = JsonHelper.FromObject(_FGC_BZ_INI);
  189. Logger.Trace(outputMessage, logKeyT, logger_t);
  190. FormLogShow?.ShowLog(outputMessage);
  191. string usercode = _FGC_BZ_INI.USER_CODE;//"XC01";
  192. int p_id = _FGC_BZ_INI.P_ID;//15;
  193. outputMessage = $"[{e.Content}]AddWorkPieceByStatus3=[P_ID:{p_id}]";
  194. Logger.Trace(outputMessage, logKeyT, logger_t);
  195. FormLogShow?.ShowLog(outputMessage);
  196. string result = SmartDeviceProxy.Instance.Invoke<string>(
  197. p => p.AddWorkPieceByStatus3(
  198. _mes_s.AccountCode,
  199. usercode,
  200. null,
  201. p_id,
  202. barcode,
  203. null));
  204. //string result = "OK";
  205. // 返回状态(成功、失败)
  206. if (result == "OK")
  207. {
  208. outputMessage = $"[{e.Content}]result=[OK]";
  209. Logger.Trace(outputMessage, logKeyT, logger_t);
  210. FormLogShow?.ShowLog(outputMessage);
  211. return;
  212. }
  213. if (result == "EE")
  214. {
  215. outputMessage = $"[{e.Content}]result=[{result}]MES服务端异常(详情参见MES端日志)";
  216. }
  217. else if (result == "EU-01")
  218. {
  219. outputMessage = $"[{e.Content}]result=[{result}]无效的帐套或工号(详情参见MES端日志)";
  220. }
  221. else
  222. {
  223. outputMessage = $"[{e.Content}]result=[{result}]其他错误(详情参见MES端日志)";
  224. }
  225. Logger.Warn(outputMessage, logKeyT, logger_t);
  226. Logger.Warn(outputMessage, logKeyE, logger_e);
  227. FormLogShow?.ShowLog(outputMessage);
  228. SendError(e, logKeyT, logKeyE);
  229. }
  230. catch (Exception ex)
  231. {
  232. FormLogShow?.ShowLog($"[{e.Content}]ERROR={ex.Message}");
  233. SendError(e, logKeyT, logKeyE);
  234. }
  235. finally
  236. {
  237. //e.ReturnMessage("0000");
  238. }
  239. }
  240. }
  241. //private void SendError(PLC_S_PW plc_s, ReceiveSession e, string loggerNameT,
  242. // string loggerNameE, SocketClient<SiemensS7_1200Model> plc)
  243. private void SendError(ReceiveSession e, string loggerNameT, string loggerNameE)
  244. {
  245. try
  246. {
  247. using (SocketClient<SimpleSocketClientModel> plc = new SocketClient<SimpleSocketClientModel>())
  248. {
  249. plc.Connect(_FGC_BZ_INI.SMT_IP, _FGC_BZ_INI.SMT_PORT);
  250. string outputMessage = $"[{e.Content}]SET_ErrorNum=[{_FGC_BZ_INI.SMT_ECODE}]";
  251. Logger.Trace(outputMessage, loggerNameT, logger_t);
  252. FormLogShow?.ShowLog(outputMessage);
  253. PLCMessage cm = new PLCMessage();
  254. cm.Command = _FGC_BZ_INI.SMT_ECODE;
  255. plc.DoCommandOneWay(cm);
  256. outputMessage = $"[{e.Content}]END_ErrorNum=[{_FGC_BZ_INI.SMT_ECODE}]";
  257. Logger.Trace(outputMessage, loggerNameT, logger_t);
  258. FormLogShow?.ShowLog(outputMessage);
  259. }
  260. }
  261. catch (Exception ex)
  262. {
  263. Logger.Error(ex, $"[{e.Content}]ERROR-SendError", loggerNameT, logger_t);
  264. Logger.Error(ex, $"[{e.Content}]ERROR-SendError", loggerNameE, logger_e);
  265. FormLogShow?.ShowLog($"[{e.Content}]ERROR-SendError={ex.Message}");
  266. }
  267. }
  268. public class FGC_BZ_INI
  269. {
  270. //包装入仓工序ID、生产工号
  271. public int P_ID = 15;
  272. public string USER_CODE = "XC01";
  273. //扫描头IP、端口、错误命令
  274. public string SMT_IP = "10.0.8.11";
  275. public int SMT_PORT = 1000;
  276. public string SMT_ECODE = "111111";
  277. public static FGC_BZ_INI Get()
  278. {
  279. FGC_BZ_INI plc_s = new FGC_BZ_INI();
  280. INIHelper ini = INIHelper.IniFile($@"PLC_S_INI\FGC_BZ.ini");
  281. plc_s.P_ID = ini.Read("FGC_BZ", "P_ID").ToInt32();
  282. plc_s.USER_CODE = ini.Read("FGC_BZ", "USER_CODE");
  283. plc_s.SMT_IP = ini.Read("FGC_BZ", "SMT_IP");
  284. plc_s.SMT_PORT = ini.Read("FGC_BZ", "SMT_PORT").ToInt32();
  285. plc_s.SMT_ECODE = ini.Read("FGC_BZ", "SMT_ECODE");
  286. return plc_s;
  287. }
  288. }
  289. }
  290. }