GoodsCode.cs 12 KB

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