GoodsCode.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using Curtain.Core;
  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 Curtain.Net.Sockets.PLC.Model.Siemens;
  17. namespace PLC_S.ServerModel
  18. {
  19. /// <summary>
  20. /// 返回产品型号等信息
  21. /// </summary>
  22. public class GoodsCode : SocketServer<SimpleSocketServerModel>, IShowFormLog, IWindowsServer
  23. {
  24. public Dictionary<string, PLC_S_GC> PLC_FLAGS = new Dictionary<string, PLC_S_GC>();
  25. public const string M_NAME = "GC";
  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. public int Interval
  30. {
  31. get;
  32. set;
  33. }
  34. public string EPTS_CODE
  35. {
  36. get;
  37. set;
  38. }
  39. public GoodsCode()
  40. {
  41. logger.FileExistDays = 30;
  42. logger.FilePrefix = "";
  43. logger.FileSuffix = M_NAME;
  44. logger.LevelOneFile = true;
  45. logger.FileNameWithoutDate = true;
  46. logger_t.SubFolderFormat = "<name>";
  47. logger_t.FileExistDays = 30;
  48. logger_t.LevelOneFile = true;
  49. logger_e.FileExistDays = 0;
  50. this.Model.FormatType = CommandFormatType.StartStopChar;
  51. this.ServerStarting += ServerSocket_ServerStarting;
  52. this.ServerStarted += ServerSocket_ServerStarted;
  53. this.ServerStoping += ServerSocket_ServerStoping;
  54. this.ServerStoped += ServerSocket_ServerStoped;
  55. this.ServerMessage += ServerSocket_ServerMessage;
  56. this.Received += ServerSocket_Received;
  57. }
  58. #region Server
  59. public IFormLogShow FormLogShow
  60. {
  61. get;
  62. set;
  63. }
  64. private void ServerSocket_ServerStarting(object sender, CancelEventArgs e)
  65. {
  66. try
  67. {
  68. logger.OutputTrace($"{M_NAME}_ServerStarting");
  69. }
  70. catch { }
  71. }
  72. private void ServerSocket_ServerStarted(object sender, EventArgs e)
  73. {
  74. try
  75. {
  76. logger.OutputTrace($"{M_NAME}_ServerStarted");
  77. }
  78. catch { }
  79. }
  80. private void ServerSocket_ServerStoping(object sender, CancelEventArgs e)
  81. {
  82. try
  83. {
  84. logger.OutputTrace($"{M_NAME}_ServerStoping");
  85. }
  86. catch { }
  87. }
  88. private void ServerSocket_ServerStoped(object sender, EventArgs e)
  89. {
  90. try
  91. {
  92. logger.OutputTrace($"{M_NAME}_ServerStoped");
  93. }
  94. catch { }
  95. }
  96. private void ServerSocket_ServerMessage(object sender, ServerMessageEventArgs e)
  97. {
  98. try
  99. {
  100. string message = e.ToString();
  101. string cip = e?.Client?.IP;
  102. if (e.Type == ServerMessageType.Debug)
  103. {
  104. Logger.Debug(message, $"{M_NAME}-T[{cip}]", logger_t);
  105. }
  106. if (e.Type == ServerMessageType.Error)
  107. {
  108. Logger.Error(e.Exception, message, $"{M_NAME}-E[{cip}]", logger_e);
  109. }
  110. if (e.Type == ServerMessageType.Trace)
  111. {
  112. Logger.Trace(message, $"{M_NAME}-T[{cip}]", logger_t);
  113. }
  114. if (e.Type == ServerMessageType.Warning)
  115. {
  116. Logger.Warn(message, $"{M_NAME}-E[{cip}]", logger_e);
  117. }
  118. FormLogShow?.ShowLog("ServerMessage=" + message);
  119. }
  120. catch { }
  121. }
  122. #endregion
  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_GC 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. string outputMessage = e.ToString();
  137. Logger.Trace(outputMessage, logKeyT, logger_t);
  138. FormLogShow?.ShowLog(outputMessage);
  139. if (string.IsNullOrWhiteSpace(e.Content))
  140. {
  141. outputMessage = "接收数据为空";
  142. Logger.Warn(outputMessage, logKeyT, logger_t);
  143. Logger.Warn(outputMessage, logKeyE, logger_e);
  144. FormLogShow?.ShowLog(outputMessage);
  145. return;
  146. }
  147. outputMessage = e.Content;
  148. Logger.Trace(outputMessage, logKeyT, logger_t);
  149. FormLogShow?.ShowLog(outputMessage);
  150. string[] c = e.Content.Split('#');
  151. string flag = null;
  152. if (c.Length > 0)
  153. {
  154. flag = c[0];
  155. }
  156. string barcode = null;
  157. if (c.Length > 1)
  158. {
  159. barcode = c[1];
  160. }
  161. if (string.IsNullOrWhiteSpace(flag))
  162. {
  163. outputMessage = $"[{e.Content}]接收设备代码为空";
  164. Logger.Warn(outputMessage, logKeyT, logger_t);
  165. Logger.Warn(outputMessage, logKeyE, logger_e);
  166. FormLogShow?.ShowLog(outputMessage);
  167. return;
  168. }
  169. plc_s = GetPLC_S_GC(flag, e, logKeyE);
  170. if (plc_s == null)
  171. {
  172. outputMessage = $"[{e.Content}]设备代码[{flag}]未找到ini配置或有错误";
  173. Logger.Error(null, outputMessage, logKeyT, logger_t);
  174. Logger.Error(null, outputMessage, logKeyE, logger_e);
  175. FormLogShow?.ShowLog(outputMessage);
  176. return;
  177. }
  178. outputMessage = $"[{e.Content}]{JsonHelper.FromObject(plc_s)}";
  179. Logger.Trace(outputMessage, logKeyT, logger_t);
  180. FormLogShow?.ShowLog(outputMessage);
  181. if (string.IsNullOrWhiteSpace(barcode) || barcode == "0")
  182. {
  183. outputMessage = $"[{e.Content}]接收条码为空";
  184. Logger.Warn(outputMessage, logKeyT, logger_t);
  185. Logger.Warn(outputMessage, logKeyE, logger_e);
  186. FormLogShow?.ShowLog(outputMessage);
  187. SendError(plc_s, e, logKeyE);
  188. return;
  189. }
  190. DataRow codeRow = GetGoodsCode(barcode, e, logKeyE);
  191. if (codeRow == null)
  192. {
  193. outputMessage = $"[{e.Content}]此条码[{barcode}]不存在";
  194. Logger.Warn(outputMessage, logKeyT, logger_t);
  195. Logger.Warn(outputMessage, logKeyE, logger_e);
  196. FormLogShow?.ShowLog(outputMessage);
  197. SendError(plc_s, e, logKeyE);
  198. return;
  199. }
  200. string code = codeRow["goodscode"].ToString();
  201. string intcode = codeRow["seatcovercode"].ToString();
  202. outputMessage = $"[{e.Content}]产品型号={code}[{intcode}]";
  203. Logger.Trace(outputMessage, logKeyT, logger_t);
  204. FormLogShow?.ShowLog(outputMessage);
  205. if (string.IsNullOrWhiteSpace(intcode))
  206. {
  207. outputMessage = $"[{e.Content}]此条码[{barcode}]未设置型号代码";
  208. Logger.Error(null, outputMessage, logKeyT, logger_t);
  209. Logger.Error(null, outputMessage, logKeyE, logger_e);
  210. FormLogShow?.ShowLog(outputMessage);
  211. SendError(plc_s, e, logKeyE);
  212. return;
  213. }
  214. //using (SocketClient<SiemensS7_1200Model> plc = new SocketClient<SiemensS7_1200Model>())
  215. //{
  216. // plc.Connect(plc_s.IP, plc_s.Port);
  217. // plc.Write(plc_s.Add_Code, plc_s.Add_CodeNum, intcode.ToInt32());
  218. // plc.Write(plc_s.Add_BarCode, plc_s.Add_BarCodeNum, barcode);
  219. //}
  220. }
  221. catch (Exception ex)
  222. {
  223. FormLogShow?.ShowLog($"[{e.Content}]ERROR={ex.Message}");
  224. if (plc_s != null)
  225. {
  226. Logger.Error(ex, $"ERROR[{e.Content}]", logKeyE, logger_e);
  227. SendError(plc_s, e, logKeyE);
  228. }
  229. else
  230. {
  231. Logger.Error(ex, $"ERROR-NOFLAG[{e.Content}]", logKeyE, logger_e);
  232. }
  233. }
  234. finally
  235. {
  236. e.ReturnMessage("0000");
  237. }
  238. }
  239. private void SendError(PLC_S_GC plc_s, ReceiveSession e, string loggerName)
  240. {
  241. try
  242. {
  243. //using (SocketClient<SiemensS7_1200Model> plc = new SocketClient<SiemensS7_1200Model>())
  244. //{
  245. // plc.Connect(plc_s.IP, plc_s.Port);
  246. // plc.Write(plc_s.Add_Error, plc_s.Add_ErrorNum, 1);
  247. //}
  248. }
  249. catch (Exception ex)
  250. {
  251. //Logger.Error(ex, $"ERROR[{e.Content}]", $"{M_NAME}-E[{e?.Client?.IP}]", logger_e);
  252. Logger.Error(ex, $"ERROR[{e.Content}]", loggerName, logger_e);
  253. FormLogShow?.ShowLog("SendError=" + ex.Message);
  254. }
  255. }
  256. private DataRow GetGoodsCode(string barcode, ReceiveSession e, string loggerName)
  257. {
  258. IDataAccess dataAccess = null;
  259. try
  260. {
  261. dataAccess = PLC_S_DataAccess.GetDataAccess(e);
  262. string sqlString = "select g.goodscode, g.seatcovercode\n" +
  263. " from tp_pm_groutingdailydetail gdd\n" +
  264. " inner join tp_mst_goods g\n" +
  265. " on g.goodsid = gdd.goodsid\n" +
  266. " where gdd.barcode = :barcode";
  267. DataTable dataTable = dataAccess.ExecuteDatatable(sqlString, new CDAParameter(":barcode", barcode));
  268. if (dataTable.HasData())
  269. {
  270. return dataTable.Rows[0];
  271. }
  272. return null;
  273. }
  274. catch (Exception ex)
  275. {
  276. //Logger.Error(ex, $"ERROR[{e.Content}]", $"{M_NAME}-E[{e?.Client?.IP}]", logger_e);
  277. Logger.Error(ex, $"ERROR[{e.Content}]", loggerName, logger_e);
  278. FormLogShow?.ShowLog("GetGoodsCode=" + ex.Message);
  279. return null;
  280. }
  281. finally
  282. {
  283. dataAccess?.Close();
  284. }
  285. }
  286. private PLC_S_GC GetPLC_S_GC(string flag, ReceiveSession e, string loggerName)
  287. {
  288. try
  289. {
  290. flag = "GC" + flag;
  291. if (PLC_FLAGS.ContainsKey(flag))
  292. {
  293. return PLC_FLAGS[flag];
  294. }
  295. else
  296. {
  297. PLC_S_GC plc_s = new PLC_S_GC();
  298. INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_{M_NAME}.ini");
  299. plc_s.IP = ini.Read(flag, "IP");
  300. plc_s.Port = ini.Read(flag, "Port").ToInt32();
  301. plc_s.Add_Code = ini.Read(flag, "Add_Code");
  302. plc_s.Add_CodeNum = ini.Read(flag, "Add_CodeNum");
  303. plc_s.Add_BarCode = ini.Read(flag, "Add_BarCode");
  304. plc_s.Add_BarCodeNum = ini.Read(flag, "Add_BarCodeNum");
  305. plc_s.Add_Error = ini.Read(flag, "Add_Error");
  306. plc_s.Add_ErrorNum = ini.Read(flag, "Add_ErrorNum");
  307. PLC_FLAGS.Add(flag, plc_s);
  308. return plc_s;
  309. }
  310. }
  311. catch (Exception ex)
  312. {
  313. //Logger.Error(ex, $"ERROR[{e.Content}]", $"{M_NAME}-E[{e?.Client?.IP}]", logger_e);
  314. Logger.Error(ex, $"ERROR[{e.Content}]", loggerName, logger_e);
  315. FormLogShow?.ShowLog("GetPLC_S_GC=" + ex.Message);
  316. return null;
  317. }
  318. }
  319. }
  320. }