GoodsLevel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. namespace PLC_S.ServerModel
  15. {
  16. /// <summary>
  17. /// 返回产品型号等信息
  18. /// </summary>
  19. public class GoodsLevel : SocketServer<SimpleSocketServerModel>, IShowFormLog, IWindowsServer
  20. {
  21. public static Dictionary<string, PLC_S_GC> PLC_FLAGS = new Dictionary<string, PLC_S_GC>();
  22. public const string M_NAME = "GL";
  23. private readonly Logger logger = Logger.CreateLogger(M_NAME);
  24. private readonly LogInfo logger_t = new LogInfo();
  25. private readonly LogInfo logger_e = new LogInfo();
  26. public GoodsLevel()
  27. {
  28. logger.FileExistDays = 30;
  29. logger.FilePrefix = "";
  30. logger.FileSuffix = M_NAME;
  31. logger.LevelOneFile = true;
  32. logger.FileNameWithoutDate = true;
  33. logger_t.SubFolderFormat = "<name>";
  34. logger_t.FileExistDays = 30;
  35. logger_t.LevelOneFile = true;
  36. logger_e.FileExistDays = 0;
  37. this.Model.FormatType = CommandFormatType.StartStopChar;
  38. this.ServerStarting += ServerSocket_ServerStarting;
  39. this.ServerStarted += ServerSocket_ServerStarted;
  40. this.ServerStoping += ServerSocket_ServerStoping;
  41. this.ServerStoped += ServerSocket_ServerStoped;
  42. this.ServerMessage += ServerSocket_ServerMessage;
  43. this.Received += ServerSocket_Received;
  44. }
  45. #region Server
  46. public IFormLogShow FormLogShow
  47. {
  48. get;
  49. set;
  50. }
  51. private void ServerSocket_ServerStarting(object sender, CancelEventArgs e)
  52. {
  53. try
  54. {
  55. logger.OutputTrace($"{M_NAME}_ServerStarting");
  56. }
  57. catch { }
  58. }
  59. private void ServerSocket_ServerStarted(object sender, EventArgs e)
  60. {
  61. try
  62. {
  63. logger.OutputTrace($"{M_NAME}_ServerStarted");
  64. }
  65. catch { }
  66. }
  67. private void ServerSocket_ServerStoping(object sender, CancelEventArgs e)
  68. {
  69. try
  70. {
  71. logger.OutputTrace($"{M_NAME}_ServerStoping");
  72. }
  73. catch { }
  74. }
  75. private void ServerSocket_ServerStoped(object sender, EventArgs e)
  76. {
  77. try
  78. {
  79. logger.OutputTrace($"{M_NAME}_ServerStoped");
  80. }
  81. catch { }
  82. }
  83. #endregion
  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. /// <summary>
  111. /// 返回产品型号
  112. /// </summary>
  113. /// <param name="sender"></param>
  114. /// <param name="e"></param>
  115. private void ServerSocket_Received(object sender, ReceiveSession e)
  116. {
  117. PLC_S_GC plc_s = null;
  118. string cIP = e?.Client?.IP;
  119. string logKeyT = $"{M_NAME}-T[{cIP}]";
  120. string logKeyE = $"{M_NAME}-E[{cIP}]";
  121. try
  122. {
  123. string outputMessage = e.ToString();
  124. Logger.Trace(outputMessage, logKeyT, logger_t);
  125. FormLogShow?.ShowLog(outputMessage);
  126. if (string.IsNullOrWhiteSpace(e.Content))
  127. {
  128. outputMessage = "接收数据为空";
  129. Logger.Warn(outputMessage, logKeyT, logger_t);
  130. Logger.Warn(outputMessage, logKeyE, logger_e);
  131. FormLogShow?.ShowLog(outputMessage);
  132. return;
  133. }
  134. outputMessage = e.Content;
  135. Logger.Trace(outputMessage, logKeyT, logger_t);
  136. FormLogShow?.ShowLog(outputMessage);
  137. string[] c = e.Content.Split('#');
  138. string flag = null;
  139. if (c.Length > 0)
  140. {
  141. flag = c[0];
  142. }
  143. string barcode = null;
  144. if (c.Length > 1)
  145. {
  146. barcode = c[1];
  147. }
  148. if (string.IsNullOrWhiteSpace(flag))
  149. {
  150. outputMessage = $"[{e.Content}]接收设备代码为空";
  151. Logger.Warn(outputMessage, logKeyT, logger_t);
  152. Logger.Warn(outputMessage, logKeyE, logger_e);
  153. FormLogShow?.ShowLog(outputMessage);
  154. return;
  155. }
  156. plc_s = GetPLC_S_GL(flag, e, logKeyE);
  157. if (plc_s == null)
  158. {
  159. outputMessage = $"[{e.Content}]设备代码[{flag}]未找到ini配置或有错误";
  160. Logger.Error(null, outputMessage, logKeyT, logger_t);
  161. Logger.Error(null, outputMessage, logKeyE, logger_e);
  162. FormLogShow?.ShowLog(outputMessage);
  163. return;
  164. }
  165. outputMessage = $"[{e.Content}]{JsonHelper.FromObject(plc_s)}";
  166. Logger.Trace(outputMessage, logKeyT, logger_t);
  167. FormLogShow?.ShowLog(outputMessage);
  168. if (string.IsNullOrWhiteSpace(barcode) || barcode == "0")
  169. {
  170. outputMessage = $"[{e.Content}]接收条码为空";
  171. Logger.Warn(outputMessage, logKeyT, logger_t);
  172. Logger.Warn(outputMessage, logKeyE, logger_e);
  173. FormLogShow?.ShowLog(outputMessage);
  174. return;
  175. }
  176. DataRow codeRow = GetGoodsCodeLevel(barcode, e, logKeyE);
  177. if (codeRow == null)
  178. {
  179. outputMessage = $"[{e.Content}]此条码[{barcode}]不存在";
  180. Logger.Warn(outputMessage, logKeyT, logger_t);
  181. Logger.Warn(outputMessage, logKeyE, logger_e);
  182. FormLogShow?.ShowLog(outputMessage);
  183. SendError(plc_s, e, logKeyE);
  184. return;
  185. }
  186. string code = codeRow["goodscode"].ToString();
  187. string intcode = codeRow["seatcovercode"].ToString();
  188. string level = codeRow["goodslevel"].ToString();
  189. string levelName = codeRow["goodslevelname"].ToString();
  190. outputMessage = $"[{e.Content}]产品型号={code}[{intcode}] 等级={levelName}[{level}]";
  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<MelsecA1EAsciiModel> plc = new SocketClient<MelsecA1EAsciiModel>())
  203. {
  204. plc.Connect(plc_s.IP, plc_s.Port);
  205. plc.Write(plc_s.Add_Code, plc_s.Add_CodeNum, intcode.ToInt32());
  206. }
  207. }
  208. catch (Exception ex)
  209. {
  210. FormLogShow?.ShowLog($"[{e.Content}]ERROR={ex.Message}");
  211. if (plc_s != null)
  212. {
  213. Logger.Error(ex, $"ERROR[{e.Content}]", logKeyE, logger_e);
  214. SendError(plc_s, e, logKeyE);
  215. }
  216. else
  217. {
  218. Logger.Error(ex, $"ERROR-NOFLAG[{e.Content}]", logKeyE, logger_e);
  219. }
  220. }
  221. finally
  222. {
  223. e.ReturnMessage("0000");
  224. }
  225. }
  226. private void SendError(PLC_S_GC plc_s, ReceiveSession e, string loggerName)
  227. {
  228. try
  229. {
  230. using (SocketClient<MelsecA1EAsciiModel> plc = new SocketClient<MelsecA1EAsciiModel>())
  231. {
  232. plc.Connect(plc_s.IP, plc_s.Port);
  233. plc.Write(plc_s.Add_Error[0], plc_s.Add_ErrorNum, 1);
  234. }
  235. }
  236. catch (Exception ex)
  237. {
  238. //Logger.Error(ex, $"ERROR[{e.Content}]", $"{M_NAME}-E[{e?.Client?.IP}]", logger_e);
  239. Logger.Error(ex, $"ERROR[{e.Content}]", loggerName, logger_e);
  240. FormLogShow?.ShowLog("SendError=" + ex.Message);
  241. }
  242. }
  243. private DataRow GetGoodsCodeLevel(string barcode, ReceiveSession e, string loggerName)
  244. {
  245. IDataAccess dataAccess = null;
  246. try
  247. {
  248. dataAccess = PLC_S_DataAccess.GetDataAccess(e);
  249. // TODO
  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_GL(string flag, ReceiveSession e, string loggerName)
  275. {
  276. try
  277. {
  278. if (PLC_FLAGS.ContainsKey(flag))
  279. {
  280. return PLC_FLAGS[flag];
  281. }
  282. else
  283. {
  284. PLC_S_GC plc_s = new PLC_S_GC();
  285. INIHelper ini = INIHelper.IniFile($@"PLC_S_INI\PLC_S_{M_NAME}.ini");
  286. plc_s.IP = ini.Read(flag, "IP");
  287. plc_s.Port = ini.Read(flag, "Port").ToInt32();
  288. plc_s.Add_Code = ini.Read(flag, "Add_Code");
  289. plc_s.Add_CodeNum = ini.Read(flag, "Add_CodeNum").ToInt32();
  290. plc_s.Add_Error = ini.Read(flag, "Add_Error");
  291. plc_s.Add_ErrorNum = ini.Read(flag, "Add_ErrorNum").ToInt32();
  292. //PLC_FLAGS.Add(flag, plc_s);
  293. return plc_s;
  294. }
  295. }
  296. catch (Exception ex)
  297. {
  298. //Logger.Error(ex, $"ERROR[{e.Content}]", $"{M_NAME}-E[{e?.Client?.IP}]", logger_e);
  299. Logger.Error(ex, $"ERROR[{e.Content}]", loggerName, logger_e);
  300. FormLogShow?.ShowLog("GetPLC_S_GL=" + ex.Message);
  301. return null;
  302. }
  303. }
  304. }
  305. }