ConnSetting.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Text;
  5. using System.Timers;
  6. //using System.Windows.Forms;
  7. using Curtain.DataAccess;
  8. using Curtain.Framework.Utils;
  9. using Curtain.Framework.Windows;
  10. using Curtain.WCF.Proxy;
  11. using Dongke.IBOSS.PRD.Basics.Library;
  12. using PLCCommunication.Proxy;
  13. namespace PCLCommunication
  14. {
  15. public class ConnSetting
  16. {
  17. private ConnSetting()
  18. {
  19. }
  20. public static Dictionary<string, Timer> PLC_Timers = new Dictionary<string, Timer>();
  21. //public static Dictionary<string, bool> PLC_Do = new Dictionary<string, bool>();
  22. public static Dictionary<Timer, MelsecA1EAscii> PLC_Conn = new Dictionary<Timer, MelsecA1EAscii>();
  23. public static Dictionary<string, MelsecA1EAscii> PLC_ConnServer = new Dictionary<string, MelsecA1EAscii>();
  24. public static DataTable T_Setting = null;
  25. public static DataTable T_PLC = null;
  26. public static string _sqlitString = null;
  27. public static string _oracleString = null;
  28. public static string INI_NAME = "DBSetting.ini";
  29. public static string INI_PATH = ApplicationInformation.GetAbsolutePath(INI_NAME);
  30. public static string[] PLCServiceSettings = new string[2];
  31. public static MESInfo MESInfo = null;
  32. public static void SetMESInfo()
  33. {
  34. INIUtil ini = INIUtil.IniFile(INI_PATH);
  35. MESInfo = new MESInfo();
  36. MESInfo.IP = ini.Read("iBOSS.MES", "IP");
  37. MESInfo.Port = ini.Read("iBOSS.MES", "Port");
  38. MESInfo.ServiceName = ini.Read("iBOSS.MES", "ServiceName");
  39. MESInfo.AccountCode = ini.Read("iBOSS.MES", "AccountCode");
  40. MESInfo.UserCode = ini.Read("iBOSS.MES", "UserCode");
  41. MESInfo.Password = ini.Read("iBOSS.MES", "Password");
  42. MESInfo.Procedures.Add(ini.Read("Procedures", "ProcedureID"));
  43. if (!string.IsNullOrWhiteSpace(MESInfo.IP))
  44. {
  45. WCFProxySetting.DefaultSetting.ResetRemoteAddress(MESInfo.IP, MESInfo.Port, MESInfo.ServiceName);
  46. }
  47. }
  48. public static void SavePLCServicePort()
  49. {
  50. INIUtil ini = INIUtil.IniFile(INI_PATH);
  51. ini.Write("PLCService", "Port", PLCServiceSettings[1]);
  52. }
  53. public static void SetOracleConnStr()
  54. {
  55. INIUtil ini = INIUtil.IniFile(INI_PATH);
  56. PLCServiceSettings[0] = ini.Read("PLCService", "AutoStart");
  57. PLCServiceSettings[1] = ini.Read("PLCService", "Port");
  58. string p = ini.Read("Oracle", "Password");
  59. try
  60. {
  61. p = Encryption.DecryptDES(p, "DongkeIbossprd");
  62. }
  63. catch
  64. {
  65. }
  66. _oracleString = DataAccessFactory.GetConnectionString(DataBaseType.Oracle,
  67. ini.Read("Oracle", "Server"),
  68. ini.Read("Oracle", "Port"),
  69. ini.Read("Oracle", "Database"),
  70. ini.Read("Oracle", "UserName"),
  71. p
  72. );
  73. }
  74. /// <summary>
  75. /// 初始化
  76. /// </summary>
  77. public static void Init()
  78. {
  79. _sqlitString = DataAccessFactory.GetConnectionString(DataBaseType.SQLite, ApplicationInformation.GetAbsolutePath("PLCSetting.db"));
  80. SetOracleConnStr();
  81. SetMESInfo();
  82. IDataAccess dataAccess = null;
  83. try
  84. {
  85. dataAccess = DataAccessFactory.CreateDataAccess(_sqlitString, DataBaseType.SQLite);
  86. T_Setting = dataAccess.ExecuteDatatable("select * from T_Setting");
  87. string sqlString = "SELECT t.*\n" +
  88. " , CASE\n" +
  89. " WHEN t.ConnType = '1' THEN\n" +
  90. " '长连接'\n" +
  91. " ELSE\n" +
  92. " '短连接'\n" +
  93. " END ConnTypeName\n" +
  94. " , CASE\n" +
  95. " WHEN t.Enable = -1 THEN\n" +
  96. " '禁用'\n" +
  97. " WHEN t.Enable = 0 THEN\n" +
  98. " '手动'\n" +
  99. " ELSE\n" +
  100. " '自动'\n" +
  101. " END EnableName\n" +
  102. " ,0 status \n" +
  103. " ,'未启动' statusname\n" +
  104. " ,'启用' ss\n" +
  105. " FROM T_PLC t";
  106. T_PLC = dataAccess.ExecuteDatatable(sqlString);
  107. AutoRun();
  108. }
  109. catch (Exception ex)
  110. {
  111. // TODO log
  112. LogOut.Error(null, ex);
  113. throw ex;
  114. }
  115. finally
  116. {
  117. dataAccess?.Close();
  118. }
  119. }
  120. /// <summary>
  121. /// 自动启动
  122. /// </summary>
  123. public static void AutoRun()
  124. {
  125. try
  126. {
  127. string autoRun = T_Setting.Rows[0]["AutoRun"].ToString();
  128. foreach (DataRow item in T_PLC.Rows)
  129. {
  130. string key = item["GUID"].ToString();
  131. //PLC_Do.Add(key, false);
  132. /*
  133. Timer t = new Timer();
  134. PLC_Timers.Add(key, t);
  135. //t.Tag = key;
  136. t.AutoReset = true;
  137. t.Interval = Convert.ToInt32(item["Interval"]);
  138. //t.Tick += Timer_Tick;
  139. t.Elapsed += T_Elapsed;
  140. */
  141. /*
  142. MelsecA1EAscii m = new MelsecA1EAscii(item["IP"].ToString(), Convert.ToInt32(item["Port"]));
  143. m.Always = (item["ConnType"].ToString() == "1" ? true : false);
  144. //PLC_Conn.Add(key, m);
  145. PLC_Conn.Add(t, m);
  146. */
  147. MelsecA1EAscii m1 = new MelsecA1EAscii(item["IP"].ToString(), Convert.ToInt32(item["Port"]));
  148. m1.Always = (item["ConnType"].ToString() == "1" ? true : false);
  149. PLC_ConnServer.Add(key, m1);
  150. /*
  151. //if (autoRun == "1")
  152. if (autoRun == "0")
  153. {
  154. if (item["Enable"].ToString() == "1")
  155. {
  156. item["Status"] = "1";
  157. item["StatusName"] = "已启动";
  158. item["ss"] = "停止";
  159. t.Start();
  160. //T_Elapsed(t, null);
  161. }
  162. //else
  163. //{
  164. // item["Status"] = "0";
  165. // item["StatusName"] = "停用";
  166. //}
  167. }
  168. */
  169. }
  170. }
  171. catch (Exception ex)
  172. {
  173. // TODO log
  174. LogOut.Error(null, ex);
  175. throw ex;
  176. }
  177. }
  178. /// <summary>
  179. /// PLC通信
  180. /// </summary>
  181. /// <param name="sender"></param>
  182. /// <param name="e"></param>
  183. //private static void Timer_Tick(object sender, EventArgs e)
  184. private static void T_Elapsed(object sender, ElapsedEventArgs e)
  185. {
  186. Timer t = sender as Timer;
  187. MelsecA1EAscii m = null;
  188. if (t == null)
  189. {
  190. return;
  191. }
  192. //string key = t.Tag + "";
  193. bool hasLog = true;
  194. StringBuilder lbl = new StringBuilder();
  195. string d501 = null;
  196. string code = null;
  197. try
  198. {
  199. lock (t)
  200. {
  201. //MelsecA1EAscii m = PLC_Conn[key];
  202. m = PLC_Conn[t];
  203. try
  204. {
  205. m.Always = true;
  206. //m.ConnectSocket();
  207. }
  208. catch( Exception ex)
  209. {
  210. //string j = JsonUtil.FromObject(t)
  211. // + JsonUtil.FromObject(m)
  212. // + "\r\n" + lbl;
  213. //LogOut.Error(null, ex, j);
  214. lbl.Append("PLC连接失败 : " + ex.Message);
  215. return;
  216. }
  217. lbl.Append("读取 D330 标识 : ");
  218. // 读取 D330 标识
  219. string d330 = m.GetHexData("D", 330, 1);
  220. lbl.AppendLine(d330);
  221. lbl.AppendLine(JsonUtil.FromObject(m));
  222. if (d330 != "81000001")
  223. {
  224. // TODO 调试
  225. //hasLog = false;
  226. return;
  227. }
  228. lbl.Append("读取 D341 条码 : ");
  229. // 读取 D341 条码
  230. byte[] b501 = m.GetByteData("D", 341, 6);
  231. if (b501 == null || b501.Length == 0)
  232. {
  233. SetPLCError(m, lbl, "条码为空");
  234. return;
  235. }
  236. d501 = Encoding.ASCII.GetString(b501);
  237. d501 = d501.Substring(0, 11);
  238. lbl.AppendLine(d501);
  239. lbl.AppendLine(JsonUtil.FromObject(m));
  240. lbl.Append("查询 产品编码 : ");
  241. // 查询 产品编码
  242. code = GetGoodscode(d501);
  243. lbl.AppendLine(code);
  244. bool r = false;
  245. if (string.IsNullOrEmpty(code))
  246. {
  247. lbl.AppendLine("写入 D332 产品型号长度");
  248. // 写入 D332 产品型号长度
  249. r = m.SetData("D", 332, 1, "0000");
  250. lbl.AppendLine(JsonUtil.FromObject(m));
  251. lbl.AppendLine("写入 D351 产品型号");
  252. r = m.SetData("D", 351, 10, "0000000000000000000000000000000000000000");
  253. lbl.AppendLine(JsonUtil.FromObject(m));
  254. lbl.AppendLine("写入 D330 标识");
  255. // 写入 D330 标识
  256. r = m.SetData("D", 330, 1, "0000");
  257. lbl.AppendLine(JsonUtil.FromObject(m));
  258. }
  259. else
  260. {
  261. int l = code.Length;
  262. lbl.AppendLine("写入 D332 产品型号长度");
  263. // 写入 D332 产品型号长度
  264. r = m.SetData("D", 332, 1, l.ToString("0000"));
  265. lbl.AppendLine(JsonUtil.FromObject(m));
  266. lbl.AppendLine("写入 D351 产品型号");
  267. // 写入 D351 产品编码
  268. byte[] d521 = Encoding.ASCII.GetBytes(code);
  269. r = m.SetData("D", 351, 10, d521);
  270. lbl.AppendLine(JsonUtil.FromObject(m));
  271. lbl.AppendLine("写入 D330 标识");
  272. // 写入 D330 标识
  273. //r = m.SetData("D", 330, 1, "0002");
  274. r = m.SetData("D", 330, 1, "0000");
  275. lbl.AppendLine(JsonUtil.FromObject(m));
  276. }
  277. }
  278. // TODO 正常读取不记录日志
  279. //hasLog = false;
  280. }
  281. catch (Exception ex)
  282. {
  283. SetPLCError(m, lbl, "系统异常:"+ ex.Message);
  284. string j = JsonUtil.FromObject(t)
  285. + JsonUtil.FromObject(m)
  286. + "\r\n" + lbl;
  287. LogOut.Error(null, ex, j);
  288. //throw ex;
  289. }
  290. finally
  291. {
  292. m.CloseSocket();
  293. if (hasLog)
  294. {
  295. string j = JsonUtil.FromObject(t)
  296. + JsonUtil.FromObject(m)
  297. + "\r\n" + lbl;
  298. LogOut.Debug(null, j);
  299. }
  300. }
  301. }
  302. public static void SetPLCError(MelsecA1EAscii m, StringBuilder lbl, string message)
  303. {
  304. //try
  305. //{
  306. // lbl.AppendLine("写入 D541 错误标识 : " + message);
  307. // // 写入 D541 错误标识
  308. // m.SetData("D", 541, 1, "0001");
  309. // lbl.AppendLine(JsonUtil.FromObject(m));
  310. //}
  311. //catch(Exception ex)
  312. //{
  313. // string j = JsonUtil.FromObject(m)
  314. // + "\r\n" + lbl;
  315. // LogOut.Error(null, ex, j);
  316. //}
  317. if (m == null)
  318. {
  319. return;
  320. }
  321. lbl.AppendLine("写入 D331 错误标识 : " + message);
  322. // 写入 D331 错误标识
  323. bool result = m.SetData("D", FrmMelsecA1EAscii.PLC_Error_Flag, 1, "0001");
  324. lbl.AppendLine(JsonUtil.FromObject(m));
  325. if (!result)
  326. {
  327. lbl.AppendLine("PLC操作失败");
  328. }
  329. }
  330. public static void SetPLCError2(MelsecA1EAscii m, StringBuilder lbl, string message)
  331. {
  332. //try
  333. //{
  334. // lbl.AppendLine("写入 D541 错误标识 : " + message);
  335. // // 写入 D541 错误标识
  336. // m.SetData("D", 541, 1, "0001");
  337. // lbl.AppendLine(JsonUtil.FromObject(m));
  338. //}
  339. //catch(Exception ex)
  340. //{
  341. // string j = JsonUtil.FromObject(m)
  342. // + "\r\n" + lbl;
  343. // LogOut.Error(null, ex, j);
  344. //}
  345. if (m == null)
  346. {
  347. return;
  348. }
  349. lbl.AppendLine("写入 D511 错误标识 : " + message);
  350. // 写入 D331 错误标识
  351. bool result = m.SetData("D", FrmMelsecA1EAscii.PLC_Error_Flag2, 1, "0001");
  352. lbl.AppendLine(JsonUtil.FromObject(m));
  353. if (!result)
  354. {
  355. lbl.AppendLine("PLC操作失败");
  356. }
  357. }
  358. /// <summary>
  359. /// 查询 产品编码
  360. /// </summary>
  361. /// <param name="barcode"></param>
  362. /// <returns></returns>
  363. public static string GetGoodscode(string barcode)
  364. {
  365. IDataAccess dataAccess = null;
  366. try
  367. {
  368. dataAccess = DataAccessFactory.CreateDataAccess(_oracleString, DataBaseType.Oracle);
  369. string goodscode = dataAccess.ExecuteScalar<string>(
  370. "select g.goodscode from tp_pm_groutingdailydetail g where g.barcode = '" + barcode + "'");
  371. if (goodscode == null)
  372. {
  373. goodscode = "";
  374. }
  375. return goodscode;
  376. }
  377. catch (Exception ex)
  378. {
  379. // todo log
  380. LogOut.Error(null, ex, barcode + " :: " + _oracleString);
  381. //return "H0126L-N";
  382. return "";
  383. }
  384. finally
  385. {
  386. dataAccess?.Close();
  387. }
  388. }
  389. public static bool DeletePLC(string guid)
  390. {
  391. IDataAccess dataAccess = null;
  392. try
  393. {
  394. dataAccess = DataAccessFactory.CreateDataAccess(_sqlitString, DataBaseType.SQLite);
  395. dataAccess.ExecuteNonQuery("delete from t_plc where guid = '" + guid + "'");
  396. /*
  397. if (PLC_Timers.ContainsKey(guid))
  398. {
  399. Timer t = PLC_Timers[guid];
  400. if (t != null)
  401. {
  402. if (PLC_Conn.ContainsKey(t))
  403. {
  404. MelsecA1EAscii m = PLC_Conn[t];
  405. if (m != null)
  406. {
  407. m.Dispose();
  408. m = null;
  409. }
  410. PLC_Conn.Remove(t);
  411. }
  412. t.Stop();
  413. t.Dispose();
  414. t = null;
  415. }
  416. PLC_Timers.Remove(guid);
  417. //PLC_ConnServer.Remove(guid);
  418. }
  419. */
  420. if (PLC_ConnServer.ContainsKey(guid))
  421. {
  422. PLC_ConnServer.Remove(guid);
  423. }
  424. //if (PLC_Conn.ContainsKey(guid))
  425. //{
  426. // MelsecA1EAscii m = PLC_Conn[guid];
  427. // if (m != null)
  428. // {
  429. // m.Dispose();
  430. // m = null;
  431. // }
  432. // PLC_Conn.Remove(guid);
  433. //}
  434. return true;
  435. }
  436. catch (Exception ex)
  437. {
  438. LogOut.Error(null, ex);
  439. throw ex;
  440. }
  441. finally
  442. {
  443. dataAccess?.Close();
  444. }
  445. }
  446. public static bool StopPLC(string guid)
  447. {
  448. try
  449. {
  450. if (PLC_Timers.ContainsKey(guid))
  451. {
  452. Timer t = PLC_Timers[guid];
  453. if (t != null)
  454. {
  455. t.Stop();
  456. }
  457. }
  458. return true;
  459. }
  460. catch (Exception ex)
  461. {
  462. LogOut.Error(null, ex);
  463. throw ex;
  464. }
  465. }
  466. public static bool StartPLC(string guid)
  467. {
  468. try
  469. {
  470. if (PLC_Timers.ContainsKey(guid))
  471. {
  472. Timer t = PLC_Timers[guid];
  473. if (t != null)
  474. {
  475. t.Start();
  476. T_Elapsed(t, null);
  477. }
  478. }
  479. return true;
  480. }
  481. catch (Exception ex)
  482. {
  483. LogOut.Error(null, ex);
  484. throw ex;
  485. }
  486. }
  487. public static void Dispose()
  488. {
  489. try
  490. {
  491. foreach (string guid in PLC_Timers.Keys)
  492. {
  493. Timer t = PLC_Timers[guid];
  494. if (t != null)
  495. {
  496. if (PLC_Conn.ContainsKey(t))
  497. {
  498. MelsecA1EAscii m = PLC_Conn[t];
  499. if (m != null)
  500. {
  501. m.Dispose();
  502. m = null;
  503. }
  504. PLC_Conn.Remove(t);
  505. }
  506. t.Stop();
  507. t.Dispose();
  508. t = null;
  509. }
  510. //PLC_Timers.Remove(guid);
  511. }
  512. PLC_Timers.Clear();
  513. PLC_ConnServer.Clear();
  514. }
  515. catch
  516. {
  517. }
  518. }
  519. public static void EditPLC(DataRow item)
  520. {
  521. IDataAccess dataAccess = null;
  522. try
  523. {
  524. dataAccess = DataAccessFactory.CreateDataAccess(_sqlitString, DataBaseType.SQLite);
  525. string sql = "update t_plc set ip = '" + item["ip"] + "'" +
  526. ", port = " + item["port"] + "" +
  527. ", Interval = " + item["Interval"] + "" +
  528. " where guid = '" + item["guid"] + "'";
  529. dataAccess.ExecuteNonQuery(sql);
  530. string guid = item["guid"].ToString();
  531. if (PLC_ConnServer.ContainsKey(guid))
  532. {
  533. MelsecA1EAscii m = PLC_ConnServer[guid];
  534. if (m != null)
  535. {
  536. m.IP = item["ip"].ToString();
  537. m.Port = Convert.ToInt32(item["port"]);
  538. //m.CloseSocket();
  539. }
  540. }
  541. if (PLC_Timers.ContainsKey(guid))
  542. {
  543. Timer t = PLC_Timers[guid];
  544. if (t != null)
  545. {
  546. if (PLC_Conn.ContainsKey(t))
  547. {
  548. MelsecA1EAscii m = PLC_Conn[t];
  549. if (m != null)
  550. {
  551. m.IP = item["ip"].ToString();
  552. m.Port = Convert.ToInt32(item["port"]);
  553. m.CloseSocket();
  554. }
  555. }
  556. int inter = Convert.ToInt32(t.Interval);
  557. int newInter = Convert.ToInt32(item["Interval"]);
  558. if (inter != newInter)
  559. {
  560. t.Interval = newInter;
  561. if (t.Enabled)
  562. {
  563. t.Stop();
  564. t.Start();
  565. T_Elapsed(t, null);
  566. }
  567. }
  568. }
  569. }
  570. }
  571. catch (Exception ex)
  572. {
  573. LogOut.Error(null, ex);
  574. throw ex;
  575. }
  576. finally
  577. {
  578. dataAccess?.Close();
  579. }
  580. }
  581. /// <summary>
  582. /// 自动工序计件
  583. /// </summary>
  584. /// <param name="barcode"></param>
  585. public static void AddWorkPiece(string barcode, string text)
  586. {
  587. if (MESInfo == null||
  588. string.IsNullOrWhiteSpace(MESInfo.IP) ||
  589. string.IsNullOrWhiteSpace(MESInfo.Port) ||
  590. string.IsNullOrWhiteSpace(MESInfo.AccountCode) ||
  591. string.IsNullOrWhiteSpace(MESInfo.UserCode)
  592. )
  593. {
  594. return;
  595. }
  596. try
  597. {
  598. string userPassword = (string.IsNullOrWhiteSpace(ConnSetting.MESInfo.Password) ?
  599. text : ConnSetting.MESInfo.Password);
  600. int procedureID = Convert.ToInt32(ConnSetting.MESInfo.Procedures[0]);
  601. SmartDeviceProxy.Instance.Invoke(p => p.AddWorkPiece(ConnSetting.MESInfo.AccountCode,
  602. ConnSetting.MESInfo.UserCode, userPassword, procedureID, barcode));
  603. }
  604. catch (Exception ex)
  605. {
  606. LogOut.Error(SmartDeviceProxy.Instance.ServiceName, ex, "AddWorkPiece");
  607. }
  608. }
  609. }
  610. }