ConnSetting.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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. private static string INI_NAME = "DBSetting.ini";
  29. private static string INI_PATH = ApplicationInformation.GetAbsolutePath(INI_NAME);
  30. public static string INI_PATH_UserCode = null;
  31. public static string PLC_GlazingRoom = null;
  32. public static bool AutoPost = false;
  33. public static bool SE_CODE_FLAG = false;
  34. public static string[] PLCServiceSettings = new string[2];
  35. public static MESInfo MESInfo = null;
  36. public static void SetMESInfo()
  37. {
  38. INIUtil ini = INIUtil.IniFile(INI_PATH);
  39. MESInfo = new MESInfo();
  40. MESInfo.IP = ini.Read("iBOSS.MES", "IP");
  41. MESInfo.Port = ini.Read("iBOSS.MES", "Port");
  42. MESInfo.ServiceName = ini.Read("iBOSS.MES", "ServiceName");
  43. MESInfo.AccountCode = ini.Read("iBOSS.MES", "AccountCode");
  44. MESInfo.UserCode = ini.Read("iBOSS.MES", "UserCode");
  45. MESInfo.Password = ini.Read("iBOSS.MES", "Password");
  46. MESInfo.Procedures.Add(ini.Read("Procedures", "ProcedureID"));
  47. INI_PATH_UserCode = MESInfo.Password = ini.Read("iBOSS.MES", "UserCodeINIPath");
  48. if (!string.IsNullOrWhiteSpace(INI_PATH_UserCode))
  49. {
  50. MESInfo.UserCode = INIUtil.Read("iBOSS.MES", "UserCode", INI_PATH_UserCode);
  51. }
  52. MESInfo.UserCode61 = ini.Read("iBOSS.MES", "UserCode61");
  53. MESInfo.UserCode53 = ini.Read("iBOSS.MES", "UserCode53");
  54. MESInfo.UserCode81 = ini.Read("iBOSS.MES", "UserCode81");
  55. AutoPost = (ini.Read("iBOSS.MES", "AutoPost").ToLower() == "true");
  56. SE_CODE_FLAG = (ini.Read("PLCService", "SE_CODE_FLAG").ToLower() == "true");
  57. if (!string.IsNullOrWhiteSpace(MESInfo.IP))
  58. {
  59. WCFProxySetting.DefaultSetting.ResetRemoteAddress(MESInfo.IP, MESInfo.Port, MESInfo.ServiceName);
  60. }
  61. }
  62. public static void SavePLCServicePort()
  63. {
  64. INIUtil ini = INIUtil.IniFile(INI_PATH);
  65. ini.Write("PLCService", "Port", PLCServiceSettings[1]);
  66. }
  67. public static void SetOracleConnStr()
  68. {
  69. INIUtil ini = INIUtil.IniFile(INI_PATH);
  70. PLCServiceSettings[0] = ini.Read("PLCService", "AutoStart");
  71. PLCServiceSettings[1] = ini.Read("PLCService", "Port");
  72. PLC_GlazingRoom = ini.Read("PLCAddress", "GlazingRoom");
  73. string p = ini.Read("Oracle", "Password");
  74. try
  75. {
  76. p = Encryption.DecryptDES(p, "DongkeIbossprd");
  77. }
  78. catch
  79. {
  80. }
  81. _oracleString = DataAccessFactory.GetConnectionString(DataBaseType.Oracle,
  82. ini.Read("Oracle", "Server"),
  83. ini.Read("Oracle", "Port"),
  84. ini.Read("Oracle", "Database"),
  85. ini.Read("Oracle", "UserName"),
  86. p
  87. );
  88. }
  89. /// <summary>
  90. /// 初始化
  91. /// </summary>
  92. public static void Init()
  93. {
  94. _sqlitString = DataAccessFactory.GetConnectionString(DataBaseType.SQLite, ApplicationInformation.GetAbsolutePath("PLCSetting.db"));
  95. SetOracleConnStr();
  96. SetMESInfo();
  97. IDataAccess dataAccess = null;
  98. try
  99. {
  100. dataAccess = DataAccessFactory.CreateDataAccess(_sqlitString, DataBaseType.SQLite);
  101. T_Setting = dataAccess.ExecuteDatatable("select * from T_Setting");
  102. string sqlString = "SELECT t.*\n" +
  103. " , CASE\n" +
  104. " WHEN t.ConnType = '1' THEN\n" +
  105. " '长连接'\n" +
  106. " ELSE\n" +
  107. " '短连接'\n" +
  108. " END ConnTypeName\n" +
  109. " , CASE\n" +
  110. " WHEN t.Enable = -1 THEN\n" +
  111. " '禁用'\n" +
  112. " WHEN t.Enable = 0 THEN\n" +
  113. " '手动'\n" +
  114. " ELSE\n" +
  115. " '自动'\n" +
  116. " END EnableName\n" +
  117. " ,0 status \n" +
  118. " ,'未启动' statusname\n" +
  119. " ,'启用' ss\n" +
  120. " FROM T_PLC t";
  121. T_PLC = dataAccess.ExecuteDatatable(sqlString);
  122. AutoRun();
  123. }
  124. catch (Exception ex)
  125. {
  126. // TODO log
  127. LogOut.Error(null, ex);
  128. throw ex;
  129. }
  130. finally
  131. {
  132. dataAccess?.Close();
  133. }
  134. }
  135. /// <summary>
  136. /// 自动启动
  137. /// </summary>
  138. public static void AutoRun()
  139. {
  140. try
  141. {
  142. string autoRun = T_Setting.Rows[0]["AutoRun"].ToString();
  143. foreach (DataRow item in T_PLC.Rows)
  144. {
  145. string key = item["GUID"].ToString();
  146. //PLC_Do.Add(key, false);
  147. /*
  148. Timer t = new Timer();
  149. PLC_Timers.Add(key, t);
  150. //t.Tag = key;
  151. t.AutoReset = true;
  152. t.Interval = Convert.ToInt32(item["Interval"]);
  153. //t.Tick += Timer_Tick;
  154. t.Elapsed += T_Elapsed;
  155. */
  156. /*
  157. MelsecA1EAscii m = new MelsecA1EAscii(item["IP"].ToString(), Convert.ToInt32(item["Port"]));
  158. m.Always = (item["ConnType"].ToString() == "1" ? true : false);
  159. //PLC_Conn.Add(key, m);
  160. PLC_Conn.Add(t, m);
  161. */
  162. MelsecA1EAscii m1 = new MelsecA1EAscii(item["IP"].ToString(), Convert.ToInt32(item["Port"]));
  163. m1.Always = (item["ConnType"].ToString() == "1" ? true : false);
  164. PLC_ConnServer.Add(key, m1);
  165. /*
  166. //if (autoRun == "1")
  167. if (autoRun == "0")
  168. {
  169. if (item["Enable"].ToString() == "1")
  170. {
  171. item["Status"] = "1";
  172. item["StatusName"] = "已启动";
  173. item["ss"] = "停止";
  174. t.Start();
  175. //T_Elapsed(t, null);
  176. }
  177. //else
  178. //{
  179. // item["Status"] = "0";
  180. // item["StatusName"] = "停用";
  181. //}
  182. }
  183. */
  184. }
  185. }
  186. catch (Exception ex)
  187. {
  188. // TODO log
  189. LogOut.Error(null, ex);
  190. throw ex;
  191. }
  192. }
  193. /// <summary>
  194. /// PLC通信
  195. /// </summary>
  196. /// <param name="sender"></param>
  197. /// <param name="e"></param>
  198. //private static void Timer_Tick(object sender, EventArgs e)
  199. private static void T_Elapsed(object sender, ElapsedEventArgs e)
  200. {
  201. Timer t = sender as Timer;
  202. MelsecA1EAscii m = null;
  203. if (t == null)
  204. {
  205. return;
  206. }
  207. //string key = t.Tag + "";
  208. bool hasLog = true;
  209. StringBuilder lbl = new StringBuilder();
  210. string d501 = null;
  211. string code = null;
  212. try
  213. {
  214. lock (t)
  215. {
  216. //MelsecA1EAscii m = PLC_Conn[key];
  217. m = PLC_Conn[t];
  218. try
  219. {
  220. m.Always = true;
  221. //m.ConnectSocket();
  222. }
  223. catch( Exception ex)
  224. {
  225. //string j = JsonUtil.FromObject(t)
  226. // + JsonUtil.FromObject(m)
  227. // + "\r\n" + lbl;
  228. //LogOut.Error(null, ex, j);
  229. lbl.Append("PLC连接失败 : " + ex.Message);
  230. return;
  231. }
  232. lbl.Append("读取 D330 标识 : ");
  233. // 读取 D330 标识
  234. string d330 = m.GetHexData("D", 330, 1);
  235. lbl.AppendLine(d330);
  236. lbl.AppendLine(JsonUtil.FromObject(m));
  237. if (d330 != "81000001")
  238. {
  239. // TODO 调试
  240. //hasLog = false;
  241. return;
  242. }
  243. lbl.Append("读取 D341 条码 : ");
  244. // 读取 D341 条码
  245. byte[] b501 = m.GetByteData("D", 341, 6);
  246. if (b501 == null || b501.Length == 0)
  247. {
  248. SetPLCError(m, lbl, "条码为空");
  249. return;
  250. }
  251. d501 = Encoding.ASCII.GetString(b501);
  252. d501 = d501.Substring(0, 11);
  253. lbl.AppendLine(d501);
  254. lbl.AppendLine(JsonUtil.FromObject(m));
  255. lbl.Append("查询 产品编码 : ");
  256. // 查询 产品编码
  257. code = GetGoodscode(d501);
  258. lbl.AppendLine(code);
  259. bool r = false;
  260. if (string.IsNullOrEmpty(code))
  261. {
  262. lbl.AppendLine("写入 D332 产品型号长度");
  263. // 写入 D332 产品型号长度
  264. r = m.SetData("D", 332, 1, "0000");
  265. lbl.AppendLine(JsonUtil.FromObject(m));
  266. lbl.AppendLine("写入 D351 产品型号");
  267. r = m.SetData("D", 351, 10, "0000000000000000000000000000000000000000");
  268. lbl.AppendLine(JsonUtil.FromObject(m));
  269. lbl.AppendLine("写入 D330 标识");
  270. // 写入 D330 标识
  271. r = m.SetData("D", 330, 1, "0000");
  272. lbl.AppendLine(JsonUtil.FromObject(m));
  273. }
  274. else
  275. {
  276. int l = code.Length;
  277. lbl.AppendLine("写入 D332 产品型号长度");
  278. // 写入 D332 产品型号长度
  279. r = m.SetData("D", 332, 1, l.ToString("0000"));
  280. lbl.AppendLine(JsonUtil.FromObject(m));
  281. lbl.AppendLine("写入 D351 产品型号");
  282. // 写入 D351 产品编码
  283. byte[] d521 = Encoding.ASCII.GetBytes(code);
  284. r = m.SetData("D", 351, 10, d521);
  285. lbl.AppendLine(JsonUtil.FromObject(m));
  286. lbl.AppendLine("写入 D330 标识");
  287. // 写入 D330 标识
  288. //r = m.SetData("D", 330, 1, "0002");
  289. r = m.SetData("D", 330, 1, "0000");
  290. lbl.AppendLine(JsonUtil.FromObject(m));
  291. }
  292. }
  293. // TODO 正常读取不记录日志
  294. //hasLog = false;
  295. }
  296. catch (Exception ex)
  297. {
  298. SetPLCError(m, lbl, "系统异常:"+ ex.Message);
  299. string j = JsonUtil.FromObject(t)
  300. + JsonUtil.FromObject(m)
  301. + "\r\n" + lbl;
  302. LogOut.Error(null, ex, j);
  303. //throw ex;
  304. }
  305. finally
  306. {
  307. m.CloseSocket();
  308. if (hasLog)
  309. {
  310. string j = JsonUtil.FromObject(t)
  311. + JsonUtil.FromObject(m)
  312. + "\r\n" + lbl;
  313. LogOut.Debug(null, j);
  314. }
  315. }
  316. }
  317. public static void SetPLCError(MelsecA1EAscii m, StringBuilder lbl, string message)
  318. {
  319. //try
  320. //{
  321. // lbl.AppendLine("写入 D541 错误标识 : " + message);
  322. // // 写入 D541 错误标识
  323. // m.SetData("D", 541, 1, "0001");
  324. // lbl.AppendLine(JsonUtil.FromObject(m));
  325. //}
  326. //catch(Exception ex)
  327. //{
  328. // string j = JsonUtil.FromObject(m)
  329. // + "\r\n" + lbl;
  330. // LogOut.Error(null, ex, j);
  331. //}
  332. if (m == null)
  333. {
  334. return;
  335. }
  336. lbl.AppendLine("写入 D331 错误标识 : " + message);
  337. // 写入 D331 错误标识
  338. bool result = m.SetData("D", 331, 1, "0001");
  339. lbl.AppendLine(JsonUtil.FromObject(m));
  340. if (!result)
  341. {
  342. lbl.AppendLine("PLC操作失败");
  343. }
  344. }
  345. public static void SetPLCError2(MelsecA1EAscii m, StringBuilder lbl, string message)
  346. {
  347. if (m == null)
  348. {
  349. return;
  350. }
  351. lbl.AppendLine("写入 D281 错误标识 : " + message);
  352. // 写入 D281 错误标识
  353. bool result = m.SetData("D", 281, 1, "0001");
  354. lbl.AppendLine(JsonUtil.FromObject(m));
  355. if (!result)
  356. {
  357. lbl.AppendLine("PLC操作失败");
  358. }
  359. }
  360. /// <summary>
  361. /// 查询 产品编码
  362. /// </summary>
  363. /// <param name="barcode"></param>
  364. /// <returns></returns>
  365. public static string GetGoodscode(string barcode)
  366. {
  367. IDataAccess dataAccess = null;
  368. try
  369. {
  370. dataAccess = DataAccessFactory.CreateDataAccess(_oracleString, DataBaseType.Oracle);
  371. string goodscode = dataAccess.ExecuteScalar<string>(
  372. "select g.goodscode from tp_pm_groutingdailydetail g where g.barcode = '" + barcode + "'");
  373. if (goodscode == null)
  374. {
  375. return "";
  376. }
  377. string code = dataAccess.ExecuteScalar<string>(
  378. "select to_char(g.seatcovercode) from tp_mst_goods g where g.goodscode = '" + goodscode + "'");
  379. if (code == null)
  380. {
  381. return "";
  382. }
  383. return code;
  384. }
  385. catch (Exception ex)
  386. {
  387. // todo log
  388. LogOut.Error(null, ex, barcode + " :: " + _oracleString);
  389. //return "H0126L-N";
  390. return "";
  391. }
  392. finally
  393. {
  394. dataAccess?.Close();
  395. }
  396. }
  397. /// <summary>
  398. /// 验证工号
  399. /// </summary>
  400. /// <param name="usercode"></param>
  401. /// <returns></returns>
  402. public static string CheckUsercode(string usercode)
  403. {
  404. IDataAccess dataAccess = null;
  405. try
  406. {
  407. dataAccess = DataAccessFactory.CreateDataAccess(_oracleString, DataBaseType.Oracle);
  408. string id = dataAccess.ExecuteScalar<string>(
  409. "select to_char(u.userid) from tp_mst_user u where u.usercode = '" + usercode + "'");
  410. if (id == null)
  411. {
  412. return id;
  413. }
  414. id = dataAccess.ExecuteScalar<string>(
  415. "select to_char(p.procedureid) from tp_pc_procedureuser p where p.userid ="+ id
  416. + " and p.procedureid= '" + MESInfo.Procedures[0] + "'");
  417. if (id == null)
  418. {
  419. return id;
  420. }
  421. dataAccess.ExecuteNonQuery("update tp_mst_workstation t set t.usercode = '"+ usercode + "' where t.workstationid = 2215");
  422. MESInfo.UserCode = usercode;
  423. INIUtil ini = INIUtil.IniFile(INI_PATH);
  424. ini.Write("iBOSS.MES", "UserCode", usercode);
  425. // 写入
  426. if (!string.IsNullOrWhiteSpace(INI_PATH_UserCode))
  427. {
  428. INIUtil.Write("iBOSS.MES", "UserCode", usercode, INI_PATH_UserCode);
  429. }
  430. return id;
  431. }
  432. catch (Exception ex)
  433. {
  434. // todo log
  435. LogOut.Error(null, ex, usercode + " :: " + _oracleString);
  436. return null;
  437. }
  438. finally
  439. {
  440. dataAccess?.Close();
  441. }
  442. }
  443. /// <summary>
  444. /// 记录喷釉房
  445. /// </summary>
  446. /// <param name="barcode"></param>
  447. /// <returns></returns>
  448. public static bool SetGlazingRoom(string barcode, string roomcode)
  449. {
  450. IDataAccess dataAccess = null;
  451. try
  452. {
  453. dataAccess = DataAccessFactory.CreateDataAccess(_oracleString, DataBaseType.Oracle);
  454. string sql = "update tp_pm_groutingdailydetail g set g.glazingroom = '"+ roomcode + "' where g.barcode='"+ barcode + "'";
  455. int r = dataAccess.ExecuteNonQuery(sql);
  456. return (r > 0);
  457. }
  458. catch (Exception ex)
  459. {
  460. // todo log
  461. LogOut.Error(null, ex, barcode + " :: " + _oracleString);
  462. return false;
  463. }
  464. finally
  465. {
  466. dataAccess?.Close();
  467. }
  468. }
  469. /// <summary>
  470. /// 查询条码的防伪码标识和型号
  471. /// </summary>
  472. /// <param name="barcode"></param>
  473. /// <returns></returns>
  474. public static DataRow GetGoodsInfo(string barcode)
  475. {
  476. IDataAccess dataAccess = null;
  477. try
  478. {
  479. dataAccess = DataAccessFactory.CreateDataAccess(_oracleString, DataBaseType.Oracle);
  480. string sqlString = "select gdd.barcode, g.goodsid, g.goodstypeid, g.securitycodebindflag, g.securitycodecheckflag\n" +
  481. " from tp_pm_groutingdailydetail gdd\n" +
  482. " inner join tp_mst_goods g\n" +
  483. " on g.goodsid = gdd.goodsid\n" +
  484. " where gdd.barcode = '"+ barcode + "'";
  485. DataTable dt = dataAccess.ExecuteDatatable(sqlString);
  486. return (dt != null && dt.Rows.Count > 0) ? dt.Rows[0] : null;
  487. }
  488. catch (Exception ex)
  489. {
  490. // todo log
  491. LogOut.Error(null, ex, barcode + " :: " + _oracleString);
  492. return null;
  493. }
  494. finally
  495. {
  496. dataAccess?.Close();
  497. }
  498. }
  499. public static bool GetSystemSetting()
  500. {
  501. IDataAccess dataAccess = null;
  502. try
  503. {
  504. dataAccess = DataAccessFactory.CreateDataAccess(_oracleString, DataBaseType.Oracle);
  505. string se_flag = dataAccess.ExecuteScalar("select t.settingvalue from tp_mst_systemsetting t where t.settingcode = ''") + "";
  506. return (se_flag == "1");
  507. }
  508. catch (Exception ex)
  509. {
  510. // todo log
  511. LogOut.Error(null, ex);
  512. return false;
  513. }
  514. finally
  515. {
  516. dataAccess?.Close();
  517. }
  518. }
  519. public static bool DeletePLC(string guid)
  520. {
  521. IDataAccess dataAccess = null;
  522. try
  523. {
  524. dataAccess = DataAccessFactory.CreateDataAccess(_sqlitString, DataBaseType.SQLite);
  525. dataAccess.ExecuteNonQuery("delete from t_plc where guid = '" + guid + "'");
  526. /*
  527. if (PLC_Timers.ContainsKey(guid))
  528. {
  529. Timer t = PLC_Timers[guid];
  530. if (t != null)
  531. {
  532. if (PLC_Conn.ContainsKey(t))
  533. {
  534. MelsecA1EAscii m = PLC_Conn[t];
  535. if (m != null)
  536. {
  537. m.Dispose();
  538. m = null;
  539. }
  540. PLC_Conn.Remove(t);
  541. }
  542. t.Stop();
  543. t.Dispose();
  544. t = null;
  545. }
  546. PLC_Timers.Remove(guid);
  547. //PLC_ConnServer.Remove(guid);
  548. }
  549. */
  550. if (PLC_ConnServer.ContainsKey(guid))
  551. {
  552. PLC_ConnServer.Remove(guid);
  553. }
  554. //if (PLC_Conn.ContainsKey(guid))
  555. //{
  556. // MelsecA1EAscii m = PLC_Conn[guid];
  557. // if (m != null)
  558. // {
  559. // m.Dispose();
  560. // m = null;
  561. // }
  562. // PLC_Conn.Remove(guid);
  563. //}
  564. return true;
  565. }
  566. catch (Exception ex)
  567. {
  568. LogOut.Error(null, ex);
  569. throw ex;
  570. }
  571. finally
  572. {
  573. dataAccess?.Close();
  574. }
  575. }
  576. public static bool StopPLC(string guid)
  577. {
  578. try
  579. {
  580. if (PLC_Timers.ContainsKey(guid))
  581. {
  582. Timer t = PLC_Timers[guid];
  583. if (t != null)
  584. {
  585. t.Stop();
  586. }
  587. }
  588. return true;
  589. }
  590. catch (Exception ex)
  591. {
  592. LogOut.Error(null, ex);
  593. throw ex;
  594. }
  595. }
  596. public static bool StartPLC(string guid)
  597. {
  598. try
  599. {
  600. if (PLC_Timers.ContainsKey(guid))
  601. {
  602. Timer t = PLC_Timers[guid];
  603. if (t != null)
  604. {
  605. t.Start();
  606. T_Elapsed(t, null);
  607. }
  608. }
  609. return true;
  610. }
  611. catch (Exception ex)
  612. {
  613. LogOut.Error(null, ex);
  614. throw ex;
  615. }
  616. }
  617. public static void Dispose()
  618. {
  619. try
  620. {
  621. foreach (string guid in PLC_Timers.Keys)
  622. {
  623. Timer t = PLC_Timers[guid];
  624. if (t != null)
  625. {
  626. if (PLC_Conn.ContainsKey(t))
  627. {
  628. MelsecA1EAscii m = PLC_Conn[t];
  629. if (m != null)
  630. {
  631. m.Dispose();
  632. m = null;
  633. }
  634. PLC_Conn.Remove(t);
  635. }
  636. t.Stop();
  637. t.Dispose();
  638. t = null;
  639. }
  640. //PLC_Timers.Remove(guid);
  641. }
  642. PLC_Timers.Clear();
  643. PLC_ConnServer.Clear();
  644. }
  645. catch
  646. {
  647. }
  648. }
  649. public static void EditPLC(DataRow item)
  650. {
  651. IDataAccess dataAccess = null;
  652. try
  653. {
  654. dataAccess = DataAccessFactory.CreateDataAccess(_sqlitString, DataBaseType.SQLite);
  655. string sql = "update t_plc set ip = '" + item["ip"] + "'" +
  656. ", port = " + item["port"] + "" +
  657. ", Interval = " + item["Interval"] + "" +
  658. " where guid = '" + item["guid"] + "'";
  659. dataAccess.ExecuteNonQuery(sql);
  660. string guid = item["guid"].ToString();
  661. if (PLC_ConnServer.ContainsKey(guid))
  662. {
  663. MelsecA1EAscii m = PLC_ConnServer[guid];
  664. if (m != null)
  665. {
  666. m.IP = item["ip"].ToString();
  667. m.Port = Convert.ToInt32(item["port"]);
  668. //m.CloseSocket();
  669. }
  670. }
  671. if (PLC_Timers.ContainsKey(guid))
  672. {
  673. Timer t = PLC_Timers[guid];
  674. if (t != null)
  675. {
  676. if (PLC_Conn.ContainsKey(t))
  677. {
  678. MelsecA1EAscii m = PLC_Conn[t];
  679. if (m != null)
  680. {
  681. m.IP = item["ip"].ToString();
  682. m.Port = Convert.ToInt32(item["port"]);
  683. m.CloseSocket();
  684. }
  685. }
  686. int inter = Convert.ToInt32(t.Interval);
  687. int newInter = Convert.ToInt32(item["Interval"]);
  688. if (inter != newInter)
  689. {
  690. t.Interval = newInter;
  691. if (t.Enabled)
  692. {
  693. t.Stop();
  694. t.Start();
  695. T_Elapsed(t, null);
  696. }
  697. }
  698. }
  699. }
  700. }
  701. catch (Exception ex)
  702. {
  703. LogOut.Error(null, ex);
  704. throw ex;
  705. }
  706. finally
  707. {
  708. dataAccess?.Close();
  709. }
  710. }
  711. /// <summary>
  712. /// 自动工序计件
  713. /// </summary>
  714. /// <param name="barcode"></param>
  715. public static void AddWorkPiece(string barcode, string text)
  716. {
  717. if (MESInfo == null||
  718. string.IsNullOrWhiteSpace(MESInfo.IP) ||
  719. string.IsNullOrWhiteSpace(MESInfo.Port) ||
  720. string.IsNullOrWhiteSpace(MESInfo.AccountCode) ||
  721. string.IsNullOrWhiteSpace(MESInfo.UserCode)
  722. )
  723. {
  724. return;
  725. }
  726. try
  727. {
  728. string userPassword = (string.IsNullOrWhiteSpace(ConnSetting.MESInfo.Password) ?
  729. text : ConnSetting.MESInfo.Password);
  730. int procedureID = Convert.ToInt32(ConnSetting.MESInfo.Procedures[0]);
  731. SmartDeviceProxy.Instance.Invoke(p => p.AddWorkPiece(ConnSetting.MESInfo.AccountCode,
  732. ConnSetting.MESInfo.UserCode, userPassword, procedureID, barcode));
  733. }
  734. catch (Exception ex)
  735. {
  736. LogOut.Error(SmartDeviceProxy.Instance.ServiceName, ex, "AddWorkPiece");
  737. }
  738. }
  739. /// <summary>
  740. /// 自动工序计件
  741. /// </summary>
  742. /// <param name="barcode"></param>
  743. public static string AddWorkPieceByStatus(string barcode, string text, string remarks)
  744. {
  745. if (MESInfo == null ||
  746. string.IsNullOrWhiteSpace(MESInfo.IP) ||
  747. string.IsNullOrWhiteSpace(MESInfo.Port) ||
  748. string.IsNullOrWhiteSpace(MESInfo.AccountCode) ||
  749. string.IsNullOrWhiteSpace(MESInfo.UserCode)
  750. )
  751. {
  752. return "E1";
  753. }
  754. try
  755. {
  756. string userPassword = (string.IsNullOrWhiteSpace(ConnSetting.MESInfo.Password) ?
  757. text : ConnSetting.MESInfo.Password);
  758. int procedureID = Convert.ToInt32(ConnSetting.MESInfo.Procedures[0]);
  759. string scode = SmartDeviceProxy.Instance.Invoke<string>(p => p.AddWorkPieceByStatus(ConnSetting.MESInfo.AccountCode,
  760. ConnSetting.MESInfo.UserCode, userPassword, procedureID, barcode, remarks));
  761. return scode;
  762. }
  763. catch (Exception ex)
  764. {
  765. LogOut.Error(SmartDeviceProxy.Instance.ServiceName, ex, "AddWorkPiece");
  766. return "E0";
  767. }
  768. }
  769. /// <summary>
  770. /// 自动工序计件
  771. /// </summary>
  772. /// <param name="barcode"></param>
  773. public static string AddWorkPieceByStatus1(string AccountCode, string UserCode, int procedureID,
  774. string barcode, string text, string remarks)
  775. {
  776. try
  777. {
  778. string scode = SmartDeviceProxy.Instance.Invoke<string>(p => p.AddWorkPieceByStatus(AccountCode,
  779. UserCode, text, procedureID, barcode, remarks));
  780. return scode;
  781. }
  782. catch (Exception ex)
  783. {
  784. LogOut.Error(SmartDeviceProxy.Instance.ServiceName, ex, "AddWorkPiece1");
  785. return "E0";
  786. }
  787. }
  788. }
  789. }