H_Grouting.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. using Dongke.IBOSS.PRD.WCF.DataModels;
  18. using Newtonsoft.Json.Linq;
  19. using PLC_S.Proxy;
  20. namespace PLC_S.ServerModel
  21. {
  22. /// <summary>
  23. /// 高压注浆登记
  24. /// </summary>
  25. public class H_Grouting : SocketServer<SimpleSocketServerModel>, IShowFormLog, IWindowsServer
  26. {
  27. public static Dictionary<string, PLC_S_HPG> PLC_FLAGS = new Dictionary<string, PLC_S_HPG>();
  28. public const string M_NAME = "HPG";
  29. private MES_S _mes_s = null;
  30. private readonly Logger logger = Logger.CreateLogger(M_NAME);
  31. private readonly LogInfo logger_t = new LogInfo();
  32. private readonly LogInfo logger_e = new LogInfo();
  33. public int Interval
  34. {
  35. get;
  36. set;
  37. }
  38. public string EPTS_CODE
  39. {
  40. get;
  41. set;
  42. }
  43. public H_Grouting()
  44. {
  45. logger.FileExistDays = 30;
  46. logger.FilePrefix = "";
  47. logger.FileSuffix = M_NAME;
  48. logger.LevelOneFile = true;
  49. logger.FileNameWithoutDate = true;
  50. logger_t.SubFolderFormat = "<name>";
  51. logger_t.FileExistDays = 30;
  52. logger_t.LevelOneFile = true;
  53. logger_e.FileExistDays = 0;
  54. this.Model.FormatType = CommandFormatType.StartStopChar;
  55. this.ServerStarting += ServerSocket_ServerStarting;
  56. this.ServerStarted += ServerSocket_ServerStarted;
  57. this.ServerStoping += ServerSocket_ServerStoping;
  58. this.ServerStoped += ServerSocket_ServerStoped;
  59. this.ServerMessage += ServerSocket_ServerMessage;
  60. this.Received += ServerSocket_Received;
  61. _mes_s = MES_S.Get();
  62. }
  63. #region Server
  64. public IFormLogShow FormLogShow
  65. {
  66. get;
  67. set;
  68. }
  69. private void ServerSocket_ServerStarting(object sender, CancelEventArgs e)
  70. {
  71. try
  72. {
  73. logger.OutputTrace($"{M_NAME}_ServerStarting");
  74. }
  75. catch { }
  76. }
  77. private void ServerSocket_ServerStarted(object sender, EventArgs e)
  78. {
  79. try
  80. {
  81. logger.OutputTrace($"{M_NAME}_ServerStarted");
  82. }
  83. catch { }
  84. }
  85. private void ServerSocket_ServerStoping(object sender, CancelEventArgs e)
  86. {
  87. try
  88. {
  89. logger.OutputTrace($"{M_NAME}_ServerStoping");
  90. }
  91. catch { }
  92. }
  93. private void ServerSocket_ServerStoped(object sender, EventArgs e)
  94. {
  95. try
  96. {
  97. logger.OutputTrace($"{M_NAME}_ServerStoped");
  98. }
  99. catch { }
  100. }
  101. private void ServerSocket_ServerMessage(object sender, ServerMessageEventArgs e)
  102. {
  103. try
  104. {
  105. string message = e.ToString();
  106. string cip = e?.Client?.IP;
  107. if (e.Type == ServerMessageType.Debug)
  108. {
  109. Logger.Debug(message, $"{M_NAME}-T[{cip}]", logger_t);
  110. }
  111. if (e.Type == ServerMessageType.Error)
  112. {
  113. Logger.Error(e.Exception, message, $"{M_NAME}-E[{cip}]", logger_e);
  114. }
  115. if (e.Type == ServerMessageType.Trace)
  116. {
  117. Logger.Trace(message, $"{M_NAME}-T[{cip}]", logger_t);
  118. }
  119. if (e.Type == ServerMessageType.Warning)
  120. {
  121. Logger.Warn(message, $"{M_NAME}-E[{cip}]", logger_e);
  122. }
  123. FormLogShow?.ShowLog("ServerMessage=" + message);
  124. }
  125. catch { }
  126. }
  127. #endregion
  128. /// <summary>
  129. /// 返回产品型号
  130. /// </summary>
  131. /// <param name="sender"></param>
  132. /// <param name="e"></param>
  133. private void ServerSocket_Received(object sender, ReceiveSession e)
  134. {
  135. PLC_S_HPG plc_s = null;
  136. string cIP = e?.Client?.IP;
  137. string logKeyT = $"{M_NAME}-T[{cIP}]";
  138. string logKeyE = $"{M_NAME}-E[{cIP}]";
  139. SocketClient<SiemensS7_1200Model> plc = null;
  140. try
  141. {
  142. string outputMessage = e.ToString();
  143. Logger.Trace(outputMessage, logKeyT, logger_t);
  144. FormLogShow?.ShowLog(outputMessage);
  145. if (string.IsNullOrWhiteSpace(e.Content))
  146. {
  147. outputMessage = "接收数据为空";
  148. Logger.Warn(outputMessage, logKeyT, logger_t);
  149. Logger.Warn(outputMessage, logKeyE, logger_e);
  150. FormLogShow?.ShowLog(outputMessage);
  151. return;
  152. }
  153. outputMessage = e.Content;
  154. Logger.Trace(outputMessage, logKeyT, logger_t);
  155. FormLogShow?.ShowLog(outputMessage);
  156. string flag = e.Content;
  157. //string[] c = e.Content.Split('#');
  158. //
  159. //if (c.Length > 0)
  160. //{
  161. // flag = c[0];
  162. //}
  163. //string barcode = null;
  164. //if (c.Length > 1)
  165. //{
  166. // barcode = c[1];
  167. //}
  168. if (string.IsNullOrWhiteSpace(flag))
  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. plc_s = GetPLC_S_HPG(flag, e, logKeyE);
  177. if (plc_s == null)
  178. {
  179. outputMessage = $"[{e.Content}]设备代码[{flag}]未找到ini配置或有错误";
  180. Logger.Error(null, outputMessage, logKeyT, logger_t);
  181. Logger.Error(null, outputMessage, logKeyE, logger_e);
  182. FormLogShow?.ShowLog(outputMessage);
  183. return;
  184. }
  185. outputMessage = $"[{e.Content}]PLC_Connect={JsonHelper.FromObject(plc_s)}";
  186. Logger.Trace(outputMessage, logKeyT, logger_t);
  187. FormLogShow?.ShowLog(outputMessage);
  188. plc = new SocketClient<SiemensS7_1200Model>();
  189. plc.Connect(plc_s.IP, plc_s.Port);
  190. // 查询当前成型工位对应的成型工号,对应的高压线号,
  191. DataRow dataRow = GetGUserCode(plc_s, e, logKeyE);
  192. string usercode = dataRow["usercode"].ToString();
  193. //string H_LineCode = dataRow["H_LineCode"].ToString();
  194. //int H_LineIndex = dataRow["H_LineIndex"].ToInt32();
  195. //int H_BatchNo = dataRow["H_BatchNo"].ToInt32();
  196. //int H_BatchIndex = dataRow["H_BatchIndex"].ToInt32();
  197. // 设置注浆或未注浆原因
  198. string lineCode = "D04A01";
  199. JObject jGroutingData = new JObject();
  200. jGroutingData.Add("GROUTINGDATE", DateTime.Now.ToString("yyyy-MM-dd"));
  201. jGroutingData.Add("GROUTINGLINECODE", lineCode);
  202. jGroutingData.Add("GROUTINGTIMES", 1);
  203. JArray jGROUTINGLINEDETAIL = new JArray();
  204. JObject jToken = new JObject();
  205. jToken.Add("GROUTINGMOULDCODE", "D04A01-001");
  206. jToken.Add("GROUTINGFLAG", "1");
  207. jToken.Add("NOGROUTINGRREASON", "null");
  208. jGROUTINGLINEDETAIL.Add(jToken);
  209. jToken = new JObject();
  210. jToken.Add("GROUTINGMOULDCODE", "D04A01-002");
  211. jToken.Add("GROUTINGFLAG", "0");
  212. jToken.Add("NOGROUTINGRREASON", "47");
  213. jGROUTINGLINEDETAIL.Add(jToken);
  214. jGroutingData.Add("GROUTINGLINEDETAIL", jGROUTINGLINEDETAIL);
  215. string groutingData = jGroutingData.ToString();
  216. //MES_S _mes_s = MES_S.Get();
  217. ServiceResultEntity result = SmartDeviceProxy.Instance.Invoke<ServiceResultEntity>(
  218. p => p.SetHGrouting(
  219. _mes_s.AccountCode,
  220. usercode,
  221. lineCode,
  222. groutingData,
  223. null));
  224. // 返回状态(成功、失败)
  225. if (result.Status != ServiceResultStatus.Success)
  226. {
  227. if (result.Status == ServiceResultStatus.SystemError)
  228. {
  229. outputMessage = $"MES服务端异常(详情参见MES端日志)";
  230. }
  231. else if (result.Message == "EU-01")
  232. {
  233. outputMessage = $"无效的帐套或工号(详情参见MES端日志)";
  234. }
  235. else
  236. {
  237. outputMessage = $"其他错误(详情参见MES端日志)[{result}]";
  238. }
  239. Logger.Error(null, outputMessage, logKeyT, logger_t);
  240. Logger.Error(null, outputMessage, logKeyE, logger_e);
  241. FormLogShow?.ShowLog(outputMessage);
  242. SendError(plc_s, e, logKeyT, logKeyE, plc);
  243. return;
  244. }
  245. // 读取注浆参数
  246. // 插入注浆参数
  247. int gd_id = result.Result.ToInt32();
  248. int r = SetHGroutingParas(plc_s, e, logKeyE, gd_id, dataRow, null);
  249. }
  250. catch (Exception ex)
  251. {
  252. FormLogShow?.ShowLog($"[{e.Content}]ERROR={ex.Message}");
  253. if (plc_s != null)
  254. {
  255. Logger.Error(ex, $"[{e.Content}]ERROR", logKeyT, logger_t);
  256. Logger.Error(ex, $"[{e.Content}]ERROR", logKeyE, logger_e);
  257. SendError(plc_s, e, logKeyT, logKeyE, plc);
  258. }
  259. else
  260. {
  261. Logger.Error(ex, $"[{e.Content}]ERROR-NOFLAG", logKeyT, logger_t);
  262. Logger.Error(ex, $"[{e.Content}]ERROR-NOFLAG", logKeyE, logger_e);
  263. }
  264. }
  265. finally
  266. {
  267. e.ReturnMessage("0000");
  268. }
  269. }
  270. private void SendError(PLC_S_HPG plc_s, ReceiveSession e, string loggerNameT,
  271. string loggerNameE, SocketClient<SiemensS7_1200Model> plc)
  272. {
  273. try
  274. {
  275. //using (SocketClient<SiemensS7_1200Model> plc = new SocketClient<SiemensS7_1200Model>())
  276. {
  277. string outputMessage = $"[{e.Content}]SET_ErrorNum=[1]{plc_s.Add_Error + plc_s.Add_ErrorNum}";
  278. Logger.Trace(outputMessage, loggerNameT, logger_t);
  279. FormLogShow?.ShowLog(outputMessage);
  280. plc?.Write<short>(plc_s.Add_Error, plc_s.Add_ErrorNum, (short)1);
  281. outputMessage = $"[{e.Content}]END_ErrorNum=[1]{plc_s.Add_Error + plc_s.Add_ErrorNum}";
  282. Logger.Trace(outputMessage, loggerNameT, logger_t);
  283. FormLogShow?.ShowLog(outputMessage);
  284. }
  285. }
  286. catch (Exception ex)
  287. {
  288. Logger.Error(ex, $"[{e.Content}]ERROR-SendError", loggerNameT, logger_t);
  289. Logger.Error(ex, $"[{e.Content}]ERROR-SendError", loggerNameE, logger_e);
  290. FormLogShow?.ShowLog($"[{e.Content}]ERROR-SendError={ex.Message}");
  291. }
  292. }
  293. private PLC_S_HPG GetPLC_S_HPG(string flag, ReceiveSession e, string loggerName)
  294. {
  295. try
  296. {
  297. flag = M_NAME + flag;
  298. if (PLC_FLAGS.ContainsKey(flag))
  299. {
  300. return PLC_FLAGS[flag];
  301. }
  302. else
  303. {
  304. PLC_S_HPG plc_s = new PLC_S_HPG();
  305. INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_{M_NAME}.ini");
  306. plc_s.GL_ID = ini.Read(flag, "GL_ID").ToInt32();
  307. plc_s.GL_CODE = ini.Read(flag, "GL_CODE");
  308. plc_s.GL_H_CODE = ini.Read(flag, "GL_H_CODE");
  309. plc_s.GL_H_INDEX = ini.Read(flag, "GL_H_INDEX").ToInt32();
  310. plc_s.WORKSTATIONID = ini.Read(flag, "WORKSTATIONID").ToInt32();
  311. plc_s.NOGROUTINGRREASON = ini.Read(flag, "NOGROUTINGRREASON").ToInt32();
  312. plc_s.IP = ini.Read(flag, "IP");
  313. plc_s.Port = ini.Read(flag, "Port").ToInt32();
  314. plc_s.Add_Code = ini.Read(flag, "Add_Code");
  315. plc_s.Add_CodeNum = ini.Read(flag, "Add_CodeNum");
  316. plc_s.Add_Error = ini.Read(flag, "Add_Error");
  317. plc_s.Add_ErrorNum = ini.Read(flag, "Add_ErrorNum");
  318. PLC_FLAGS.Add(flag, plc_s);
  319. return plc_s;
  320. }
  321. }
  322. catch (Exception ex)
  323. {
  324. Logger.Error(ex, $"[{e.Content}]ERROR-GetPLC_S_HPG", loggerName, logger_e);
  325. FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetPLC_S_HPG={ex.Message}");
  326. return null;
  327. }
  328. }
  329. private DataRow GetGUserCode(PLC_S_HPG plc_s, ReceiveSession e, string loggerName)
  330. {
  331. IDataAccess dataAccess = null;
  332. try
  333. {
  334. dataAccess = PLC_S_DataAccess.GetDataAccess(e);
  335. string sqlString = @"
  336. SELECT t.usercode
  337. ,gl.h_linecode
  338. ,gl.h_lineindex
  339. ,gl.h_batchno
  340. ,gl.h_batchindex
  341. FROM tp_mst_workstation t
  342. INNER JOIN tp_pc_groutingline gl
  343. ON gl.groutinglineid = t.groutinglineid
  344. WHERE t.workstationtypeid = 301
  345. AND t.workstationid = @workstationid@
  346. AND t.groutinglineid = @groutinglineid@";
  347. CDAParameter[] ps = new CDAParameter[]
  348. {
  349. new CDAParameter("groutinglineid", plc_s.GL_ID),
  350. new CDAParameter("workstationid", plc_s.WORKSTATIONID),
  351. };
  352. DataTable dataTable = dataAccess.ExecuteDatatable(sqlString, ps);
  353. if (dataTable.HasData())
  354. {
  355. return dataTable.Rows[0];
  356. }
  357. return null;
  358. }
  359. catch (Exception ex)
  360. {
  361. Logger.Error(ex, $"[{e.Content}]ERROR-GetGUserCode", loggerName, logger_e);
  362. FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetGUserCode={ex.Message}");
  363. return null;
  364. }
  365. finally
  366. {
  367. dataAccess?.Close();
  368. }
  369. }
  370. private int SetHGroutingParas(PLC_S_HPG plc_s, ReceiveSession e, string loggerName,
  371. int GroutingDailyID, DataRow dataRow, object jj)
  372. {
  373. IDataAccess dataAccess = null;
  374. try
  375. {
  376. dataAccess = PLC_S_DataAccess.GetDataAccess(e);
  377. string sqlString = @"
  378. INSERT INTO TP_PM_H_GroutingParas
  379. (GroutingDailyID
  380. ,GroutingLineID
  381. ,GroutingLineCode
  382. ,H_LineCode
  383. ,H_LineIndex
  384. ,H_BatchNo
  385. ,H_BatchIndex
  386. --,a
  387. )
  388. VALUES
  389. (@GroutingDailyID@
  390. ,@GroutingLineID@
  391. ,@GroutingLineCode@
  392. ,@H_LineCode@
  393. ,@H_LineIndex@
  394. ,@H_BatchNo@
  395. ,@H_BatchIndex@
  396. --,1
  397. )";
  398. CDAParameter[] ps = new CDAParameter[]
  399. {
  400. new CDAParameter("GroutingDailyID", GroutingDailyID),
  401. new CDAParameter("GroutingLineID", plc_s.GL_ID),
  402. new CDAParameter("GroutingLineCode", plc_s.GL_CODE),
  403. new CDAParameter("H_LineCode", plc_s.GL_H_CODE),
  404. new CDAParameter("H_LineIndex", plc_s.GL_H_INDEX),
  405. new CDAParameter("H_BatchNo", dataRow["H_BatchNo"]),
  406. new CDAParameter("H_BatchIndex", dataRow["H_BatchIndex"]),
  407. //new CDAParameter("H_BatchIndex", dataRow["H_BatchIndex"]),
  408. };
  409. return dataAccess.ExecuteNonQuery(sqlString, ps);
  410. }
  411. catch (Exception ex)
  412. {
  413. Logger.Error(ex, $"[{e.Content}]ERROR-SetHGroutingParas", loggerName, logger_e);
  414. FormLogShow?.ShowLog($"[{e.Content}]ERROR-SetHGroutingParas={ex.Message}");
  415. return -1;
  416. }
  417. finally
  418. {
  419. dataAccess?.Close();
  420. }
  421. }
  422. }
  423. }