F_Main.cs 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. using System;
  2. using System.Configuration.Install;
  3. using System.Data;
  4. using System.Drawing;
  5. using System.ServiceProcess;
  6. using System.Windows.Forms;
  7. using Curtain.Core;
  8. using Curtain.DataAccess;
  9. using Curtain.Helpers;
  10. using Curtain.Log;
  11. using Curtain.Systems.Cryptography;
  12. using Curtain.WCF.Proxy;
  13. using Curtain.Windows;
  14. using PLC_S;
  15. using PLC_S.Proxy;
  16. namespace PLC_WF
  17. {
  18. public partial class F_Main : Form
  19. {
  20. private PLC_S_S PLC_S_S = null;
  21. private readonly Logger logger = Logger.CreateLogger("WF");
  22. public F_Main()
  23. {
  24. InitializeComponent();
  25. logger.FileExistDays = 30;
  26. logger.FilePrefix = "";
  27. logger.FileSuffix = "WF";
  28. DataTable dataTable = new DataTable("PLC_S_T");
  29. dataTable.Columns.Add("code");
  30. dataTable.Columns.Add("name");
  31. dataTable.Columns.Add("description");
  32. //dataTable.Rows.Add("GC", "产品型号", "提供产品型号的服务【】");
  33. //dataTable.Rows.Add("GL", "产品等级", "提供产品等级的服务【】");
  34. //dataTable.Rows.Add("HPG", "高压注浆", "自动高压注浆的服务【】");
  35. dataTable.Rows.Add("FGC_BZ", "包装工序计件", "包装工序自动计件的服务【】");
  36. dataTable.Rows.Add("PW", "施釉工序计件", "施釉工序自动计件的服务【】");
  37. //dataTable.Rows.Add("KL", "登窑", "自动登窑的服务【】");
  38. //dataTable.Rows.Add("KI", "入窑", "自动入窑的服务【】");
  39. //dataTable.Rows.Add("KUL", "卸窑", "自动卸窑的服务【】");
  40. //dataTable.Rows.Add("BP", "条码打印", "条码自动打印【】");
  41. //dataTable.Rows.Add("BP1", "条码打印(批量补打)", "条码打印(批量补打)");
  42. //dataTable.Rows.Add("EPTS_HPG", "高压注浆设备", "同步高压注浆设备运行参数的服务【】");
  43. //dataTable.Rows.Add("EPTS_KILN", "窑炉设备", "同步窑炉设备运行参数的服务【】");
  44. //dataTable.Rows.Add("EPTS_DRY", "烤房设备", "同步烤房设备运行参数的服务【】");
  45. // TODO
  46. cboPLCST.DisplayMember = "name";
  47. cboPLCST.ValueMember = "code";
  48. this.txtPort.Tag = 1;
  49. cboPLCST.DataSource = dataTable;
  50. this.txtPort.Tag = null;
  51. GetSetting();
  52. this.btnGetDB_Click(null, null);
  53. this.btnGetMES_Click(null, null);
  54. this.Text = $"{(PLC_S_S.DisplayName.IsNullOrWhiteSpace() ? PLC_S_S.ServiceName : PLC_S_S.DisplayName)}";
  55. }
  56. private void GetSetting()
  57. {
  58. PLC_S_S = PLC_S_S.Get();
  59. this.txtPort.Tag = 1;
  60. this.txtPort.Text = PLC_S_S.Port.ToString();
  61. if (!PLC_S_S.PLCServiceType.IsNullOrEmpty())
  62. {
  63. this.cboPLCST.SelectedValue = PLC_S_S.PLCServiceType;
  64. }
  65. this.txtPort.Tag = null;
  66. this.chkAutoStart.Checked = PLC_S_S.AutoStart;
  67. this.txtServiceName.Text = PLC_S_S.ServiceName;
  68. this.txtDisplayName.Text = PLC_S_S.DisplayName;
  69. this.txtDescription.Text = PLC_S_S.Description;
  70. }
  71. private void SetSetting()
  72. {
  73. //PLC_S_S PLC_S_S = PLC_S_S.Get();
  74. PLC_S_S.Port = this.txtPort.Text.ToInt32();
  75. PLC_S_S.PLCServiceType = this.cboPLCST.SelectedValue + "";
  76. PLC_S_S.AutoStart = this.chkAutoStart.Checked;
  77. PLC_S_S.ServiceName = this.txtServiceName.Text.Trim();
  78. PLC_S_S.DisplayName = this.txtDisplayName.Text.Trim();
  79. PLC_S_S.Description = this.txtDescription.Text.Trim();
  80. PLC_S_S.Set(PLC_S_S);
  81. }
  82. private void btnStart_Click(object sender, EventArgs e)
  83. {
  84. try
  85. {
  86. if (string.IsNullOrWhiteSpace(this.txtPort.Text))
  87. {
  88. MessageBox.Show("PLC服务【端口】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  89. this.txtPort.Focus();
  90. return;
  91. }
  92. //if (string.IsNullOrWhiteSpace(this.txtServiceName.Text))
  93. //{
  94. // MessageBox.Show("PLC服务【服务名】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  95. // this.txtServiceName.Focus();
  96. // return;
  97. //}
  98. //if (string.IsNullOrWhiteSpace(this.txtDisplayName.Text))
  99. //{
  100. // MessageBox.Show("PLC服务【显示名】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  101. // this.txtDisplayName.Focus();
  102. // return;
  103. //}
  104. SetSetting();
  105. //if (PLC_S_S.PLCServiceType == "BP1")
  106. //{
  107. // string[] s = textBox1.Lines;
  108. // PLC_S.ServerModel.BarcodePrint1 bb = new PLC_S.ServerModel.BarcodePrint1();
  109. // bb.bs = s;
  110. // bb.Start(PLC_S_S.Port);
  111. // PLC_Server.ServerSocket = bb;
  112. //}
  113. //else
  114. //{
  115. // PLC_Server.Start();
  116. //}
  117. PLC_Server.Start();
  118. SetESControl();
  119. txtStatus.Text = (PLC_Server.ServerSocket?.Started ?? false ? "启动" : "停止");
  120. }
  121. catch (Exception ex)
  122. {
  123. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  124. }
  125. }
  126. private void btnStop_Click(object sender, EventArgs e)
  127. {
  128. try
  129. {
  130. PLC_Server.Stop();
  131. SetNSControl();
  132. txtStatus.Text = (PLC_Server.ServerSocket?.Started ?? false ? "启动" : "停止");
  133. }
  134. catch (Exception ex)
  135. {
  136. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  137. }
  138. }
  139. private void btnLog_Click(object sender, EventArgs e)
  140. {
  141. try
  142. {
  143. if (PLC_Server.ServerSocket is IShowFormLog showFormLog)
  144. {
  145. if (showFormLog == null)
  146. {
  147. return;
  148. }
  149. F_Log f = showFormLog.FormLogShow as F_Log;
  150. if (f == null)
  151. {
  152. f = new F_Log();
  153. f.Text = $"{f.Text }[{PLC_Server.PLC_S_S.DisplayName}]";
  154. showFormLog.FormLogShow = f;
  155. f.ShowFormLog = showFormLog;
  156. }
  157. f.Show();
  158. }
  159. }
  160. catch (Exception ex)
  161. {
  162. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  163. }
  164. }
  165. private void btnExePath_Click(object sender, EventArgs e)
  166. {
  167. System.Diagnostics.Process.Start(System.AppDomain.CurrentDomain.BaseDirectory);
  168. }
  169. private void btnSetSetting_Click(object sender, EventArgs e)
  170. {
  171. try
  172. {
  173. SetSetting();
  174. }
  175. catch (Exception ex)
  176. {
  177. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  178. }
  179. }
  180. private void btnGetSetting_Click(object sender, EventArgs e)
  181. {
  182. try
  183. {
  184. GetSetting();
  185. }
  186. catch (Exception ex)
  187. {
  188. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  189. }
  190. }
  191. //private int PLC_S_T = 0;
  192. private void F_Main_Shown(object sender, EventArgs e)
  193. {
  194. try
  195. {
  196. this._windowState = this.WindowState;
  197. RefreshForm();
  198. }
  199. catch (Exception ex)
  200. {
  201. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  202. }
  203. }
  204. private void RefreshForm()
  205. {
  206. if (string.IsNullOrWhiteSpace(PLC_S_S.ServiceName))
  207. {
  208. // 新服务设置
  209. //PLC_S_S.ServiceName = $"{PLC_S_S.S_Name}[{PLC_S_S.Port}]";
  210. //PLC_S_S.DisplayName = $"{PLC_S_S.D_Name}[{PLC_S_S.Port}]";
  211. PLC_S_S.ServiceName = $"{PLC_S_S.S_Name}[{_SS_KEY}][{txtPort.Text}]";
  212. PLC_S_S.DisplayName = $"{PLC_S_S.D_Name}[{_SS_KEY}][{txtPort.Text}]";
  213. this.txtServiceName.Text = PLC_S_S.ServiceName;
  214. this.txtDisplayName.Text = PLC_S_S.DisplayName;
  215. SetNSControl();
  216. return;
  217. }
  218. ServiceControllerStatus? wsStatus = null;
  219. if (IsServiceExisted(PLC_S_S.ServiceName.ToLower()))
  220. {
  221. this.serviceController1.ServiceName = PLC_S_S.ServiceName;
  222. try
  223. {
  224. wsStatus = this.serviceController1.Status;
  225. }
  226. catch
  227. {
  228. // this.serviceController1.ServiceName = "";
  229. }
  230. }
  231. if (wsStatus == null)
  232. {
  233. // 非windows service
  234. if (this.chkAutoStart.Checked)
  235. {
  236. SetESControl();
  237. PLC_Server.Start();
  238. txtStatus.Text = (PLC_Server.ServerSocket?.Started ?? false ? "启动" : "停止");
  239. }
  240. else
  241. {
  242. SetNSControl();
  243. }
  244. }
  245. else
  246. {
  247. // windows service
  248. SetWSControl();
  249. this.txtStatus.Text = "WS:" + this.serviceController1.Status;
  250. }
  251. }
  252. /// <summary>
  253. /// WS 时,控件状态
  254. /// </summary>
  255. private void SetWSControl()
  256. {
  257. //PLC_S_T = 2;
  258. this.txtPort.ReadOnly = true;
  259. this.cboPLCST.Enabled = false;
  260. //this.txtServiceName.ReadOnly = true;
  261. this.txtDisplayName.ReadOnly = true;
  262. this.txtDescription.ReadOnly = true;
  263. this.btnStart.Enabled = false;
  264. this.btnStop.Enabled = false;
  265. this.btnLog.Enabled = false;
  266. this.btnWS_Insert.Enabled = false;
  267. this.btnWS_Delete.Enabled = true;
  268. this.btnWS_Start.Enabled = true;
  269. this.btnWS_Stop.Enabled = true;
  270. this.btnWS_ReStart.Enabled = true;
  271. this.btnWS_Refresh.Enabled = true;
  272. }
  273. /// <summary>
  274. /// es 时,控件状态
  275. /// </summary>
  276. private void SetESControl()
  277. {
  278. //PLC_S_T = 1;
  279. this.txtPort.ReadOnly = true;
  280. this.cboPLCST.Enabled = false;
  281. //this.txtServiceName.ReadOnly = true;
  282. this.txtDisplayName.ReadOnly = false;
  283. this.txtDescription.ReadOnly = false;
  284. this.btnStart.Enabled = false;
  285. this.btnStop.Enabled = true;
  286. this.btnLog.Enabled = true;
  287. this.btnWS_Insert.Enabled = false;
  288. this.btnWS_Delete.Enabled = false;
  289. this.btnWS_Start.Enabled = false;
  290. this.btnWS_Stop.Enabled = false;
  291. this.btnWS_ReStart.Enabled = false;
  292. this.btnWS_Refresh.Enabled = false;
  293. }
  294. /// <summary>
  295. /// 无服务时,控件状态
  296. /// </summary>
  297. private void SetNSControl()
  298. {
  299. //PLC_S_T = 0;
  300. this.txtPort.ReadOnly = false;
  301. this.cboPLCST.Enabled = true;
  302. //this.txtServiceName.ReadOnly = true;
  303. this.txtDisplayName.ReadOnly = false;
  304. this.txtDescription.ReadOnly = false;
  305. this.btnStart.Enabled = true;
  306. this.btnStop.Enabled = false;
  307. this.btnLog.Enabled = false;
  308. this.btnWS_Insert.Enabled = true;
  309. this.btnWS_Delete.Enabled = false;
  310. this.btnWS_Start.Enabled = false;
  311. this.btnWS_Stop.Enabled = false;
  312. this.btnWS_ReStart.Enabled = false;
  313. this.btnWS_Refresh.Enabled = false;
  314. }
  315. private void F_Main_TextChanged(object sender, EventArgs e)
  316. {
  317. this.notifyIcon1.Text = this.Text;
  318. }
  319. private FormWindowState _windowState = FormWindowState.Normal;
  320. private bool _hasClose = false;
  321. private void F_Main_SizeChanged(object sender, EventArgs e)
  322. {
  323. if (this.WindowState == FormWindowState.Minimized)
  324. {
  325. //Visible = false;
  326. }
  327. else
  328. {
  329. this._windowState = this.WindowState;
  330. }
  331. }
  332. private void F_Main_FormClosing(object sender, FormClosingEventArgs e)
  333. {
  334. if (!_hasClose)
  335. {
  336. DialogResult dr = MessageBox.Show("是否退出?", this.Text, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3);
  337. if (dr == DialogResult.Cancel)
  338. {
  339. e.Cancel = true;
  340. return;
  341. }
  342. if (dr == DialogResult.No)
  343. {
  344. e.Cancel = true;
  345. notifyIcon1.Visible = true;
  346. Visible = false;
  347. return;
  348. }
  349. }
  350. notifyIcon1.Visible = false;
  351. SetSetting();
  352. }
  353. private void notifyIcon1_DoubleClick(object sender, EventArgs e)
  354. {
  355. }
  356. private void notifyIcon1_Click(object sender, EventArgs e)
  357. {
  358. //if (!this.Visible)
  359. //{
  360. Visible = true;
  361. //this.TopMost = true;
  362. //this.WindowState = FormWindowState.Normal;
  363. WindowState = this._windowState;
  364. ShowInTaskbar = true;
  365. //}
  366. Activate();
  367. }
  368. private void txtPort_TextChanged(object sender, EventArgs e)
  369. {
  370. if (this.txtPort.Tag == null)
  371. {
  372. PLC_S_S.ServiceName = $"{PLC_S_S.S_Name}[{_SS_KEY}][{txtPort.Text}]";
  373. PLC_S_S.DisplayName = $"{PLC_S_S.D_Name}[{_SS_KEY}][{txtPort.Text}]";
  374. this.txtServiceName.Text = PLC_S_S.ServiceName;
  375. this.txtDisplayName.Text = PLC_S_S.DisplayName;
  376. }
  377. }
  378. private void btnWS_Refresh_Click(object sender, EventArgs e)
  379. {
  380. try
  381. {
  382. RefreshWSStatus();
  383. }
  384. catch (Exception ex)
  385. {
  386. this.txtStatus.Text = null;
  387. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  388. }
  389. }
  390. private void RefreshWSStatus()
  391. {
  392. this.serviceController1.Refresh();
  393. this.txtStatus.Text = "WS:" + this.serviceController1.Status;
  394. Application.DoEvents();
  395. }
  396. private void txtDisplayName_TextChanged(object sender, EventArgs e)
  397. {
  398. this.Text = $"{PLC_S_S.DisplayName}";
  399. }
  400. private void txtPort_KeyPress(object sender, KeyPressEventArgs e)
  401. {
  402. if (char.IsControl(e.KeyChar) ||
  403. char.IsNumber(e.KeyChar)
  404. )
  405. {
  406. return;
  407. }
  408. e.Handled = true;
  409. }
  410. private void txtPort_KeyDown(object sender, KeyEventArgs e)
  411. {
  412. //if (char.IsControl(e.KeyData))
  413. //{
  414. // e.Handled = true;
  415. //}
  416. }
  417. private void btnWS_Insert_Click(object sender, EventArgs e)
  418. {
  419. try
  420. {
  421. if (string.IsNullOrWhiteSpace(this.txtPort.Text))
  422. {
  423. MessageBox.Show("PLC服务【端口】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  424. this.txtPort.Focus();
  425. return;
  426. }
  427. if (string.IsNullOrWhiteSpace(this.txtServiceName.Text))
  428. {
  429. MessageBox.Show("PLC服务【服务名】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  430. this.txtServiceName.Focus();
  431. return;
  432. }
  433. if (string.IsNullOrWhiteSpace(this.txtDisplayName.Text))
  434. {
  435. MessageBox.Show("PLC服务【显示名】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  436. this.txtDisplayName.Focus();
  437. return;
  438. }
  439. SetSetting();
  440. string path = ApplicationInformation.GetAbsolutePath("PLC_WS.exe");
  441. InstallService(path);
  442. SetWSControl();
  443. this.serviceController1.ServiceName = PLC_S_S.ServiceName;
  444. if (PLC_S_S.AutoStart)
  445. {
  446. this.serviceController1.Start();
  447. RefreshWSStatus();
  448. this.serviceController1.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 1, 0));
  449. }
  450. RefreshWSStatus();
  451. }
  452. catch (Exception ex)
  453. {
  454. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  455. }
  456. }
  457. private void btnWS_Delete_Click(object sender, EventArgs e)
  458. {
  459. try
  460. {
  461. if (IsServiceExisted(PLC_S_S.ServiceName.ToLower()))
  462. {
  463. string path = ApplicationInformation.GetAbsolutePath("PLC_WS.exe");
  464. UninstallService(path);
  465. }
  466. SetNSControl();
  467. this.txtStatus.Text = null;
  468. }
  469. catch (Exception ex)
  470. {
  471. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  472. }
  473. }
  474. private void btnWS_Start_Click(object sender, EventArgs e)
  475. {
  476. try
  477. {
  478. this.serviceController1.Start();
  479. RefreshWSStatus();
  480. this.serviceController1.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 1, 0));
  481. RefreshWSStatus();
  482. }
  483. catch (Exception ex)
  484. {
  485. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  486. }
  487. }
  488. private void btnWS_Stop_Click(object sender, EventArgs e)
  489. {
  490. try
  491. {
  492. this.serviceController1.Stop();
  493. RefreshWSStatus();
  494. this.serviceController1.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 1, 0));
  495. RefreshWSStatus();
  496. }
  497. catch (Exception ex)
  498. {
  499. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  500. }
  501. }
  502. private void btnWS_ReStart_Click(object sender, EventArgs e)
  503. {
  504. try
  505. {
  506. if (this.serviceController1.Status == ServiceControllerStatus.Running)
  507. {
  508. this.serviceController1.Stop();
  509. RefreshWSStatus();
  510. this.serviceController1.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 1, 0));
  511. RefreshWSStatus();
  512. }
  513. this.serviceController1.Start();
  514. RefreshWSStatus();
  515. this.serviceController1.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 1, 0));
  516. RefreshWSStatus();
  517. }
  518. catch (Exception ex)
  519. {
  520. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  521. }
  522. }
  523. /// <summary>
  524. /// 判断服务是否存在
  525. /// </summary>
  526. /// <param name="serviceName"></param>
  527. /// <returns></returns>
  528. private bool IsServiceExisted(string serviceName)
  529. {
  530. ServiceController[] services = ServiceController.GetServices();
  531. foreach (ServiceController sc in services)
  532. {
  533. if (sc.ServiceName.ToLower() == serviceName)
  534. {
  535. return true;
  536. }
  537. }
  538. return false;
  539. }
  540. /// <summary>
  541. /// 安装服务
  542. /// </summary>
  543. /// <param name="serviceFilePath"></param>
  544. private void InstallService(string serviceFilePath)
  545. {
  546. using (AssemblyInstaller installer = new AssemblyInstaller())
  547. {
  548. installer.UseNewContext = true;
  549. installer.Path = serviceFilePath;
  550. //IDictionary savedState = new Hashtable();
  551. //installer.Install(savedState);
  552. //installer.Commit(savedState);
  553. installer.Install(null);
  554. installer.Commit(null);
  555. }
  556. }
  557. /// <summary>
  558. /// 卸载服务
  559. /// </summary>
  560. /// <param name="serviceFilePath"></param>
  561. private void UninstallService(string serviceFilePath)
  562. {
  563. using (AssemblyInstaller installer = new AssemblyInstaller())
  564. {
  565. installer.UseNewContext = true;
  566. installer.Path = serviceFilePath;
  567. installer.Uninstall(null);
  568. }
  569. }
  570. /// <summary>
  571. /// 启动服务
  572. /// </summary>
  573. /// <param name="serviceName"></param>
  574. private void ServiceStart(string serviceName)
  575. {
  576. using (ServiceController control = new ServiceController(serviceName))
  577. {
  578. if (control.Status == ServiceControllerStatus.Stopped)
  579. {
  580. control.Start();
  581. }
  582. }
  583. }
  584. /// <summary>
  585. /// 停止服务
  586. /// </summary>
  587. /// <param name="serviceName"></param>
  588. private void ServiceStop(string serviceName)
  589. {
  590. using (ServiceController control = new ServiceController(serviceName))
  591. {
  592. if (control.Status == ServiceControllerStatus.Running)
  593. {
  594. control.Stop();
  595. }
  596. }
  597. }
  598. private void btnRefreshForm_Click(object sender, EventArgs e)
  599. {
  600. try
  601. {
  602. RefreshForm();
  603. }
  604. catch (Exception ex)
  605. {
  606. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  607. }
  608. }
  609. private void btnGetDB_Click(object sender, EventArgs e)
  610. {
  611. try
  612. {
  613. INIHelper ini = INIHelper.IniFile($@"PLC_S_INI\PLC_S_S.ini");
  614. string ip = ini.Read("Oracle", "Server");
  615. string port = ini.Read("Oracle", "Port");
  616. string sid = ini.Read("Oracle", "Database");
  617. string username = ini.Read("Oracle", "UserName");
  618. string password = ini.Read("Oracle", "Password");
  619. if (!string.IsNullOrWhiteSpace(ip))
  620. {
  621. this.txtOracleName.Text = ip;
  622. }
  623. if (!string.IsNullOrWhiteSpace(port))
  624. {
  625. this.txtOraclePort.Text = port;
  626. }
  627. this.txtOracleDatabaseName.Text = sid;
  628. this.txtOracleUserName.Text = username;
  629. if (!string.IsNullOrEmpty(password))
  630. {
  631. try
  632. {
  633. password = SymmetricCryptogram.Decrypt(password, "DongkeIbossprd", SymmetricCryptogramType.DES);
  634. this.txtOraclePassword.Text = password;
  635. }
  636. catch (Exception ex1)
  637. {
  638. this.txtOraclePassword.Text = null;
  639. MessageBox.Show("数据库【密码】解密错误" + System.Environment.NewLine + ex1.Message,
  640. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  641. logger.OutputError(ex1, "数据库【密码】解密错误");
  642. }
  643. }
  644. else
  645. {
  646. this.txtOraclePassword.Text = null;
  647. }
  648. }
  649. catch (Exception ex)
  650. {
  651. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  652. }
  653. }
  654. private void btnSetDB_Click(object sender, EventArgs e)
  655. {
  656. try
  657. {
  658. string ip = this.txtOracleName.Text.Trim();
  659. string port = this.txtOraclePort.Text.Trim();
  660. string sid = this.txtOracleDatabaseName.Text.Trim();
  661. string username = this.txtOracleUserName.Text.Trim();
  662. string password = this.txtOraclePassword.Text;
  663. if (!string.IsNullOrEmpty(password))
  664. {
  665. try
  666. {
  667. password = SymmetricCryptogram.Encrypt(password, "DongkeIbossprd", SymmetricCryptogramType.DES);
  668. }
  669. catch (Exception ex1)
  670. {
  671. MessageBox.Show("数据库【密码】加密错误" + System.Environment.NewLine + ex1.Message,
  672. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  673. logger.OutputError(ex1, "数据库【密码】加密错误");
  674. password = "";
  675. }
  676. }
  677. INIHelper ini = INIHelper.IniFile($@"PLC_S_INI\PLC_S_S.ini");
  678. ini.Write("Oracle", "Server", ip);
  679. ini.Write("Oracle", "Port", port);
  680. ini.Write("Oracle", "Database", sid);
  681. ini.Write("Oracle", "UserName", username);
  682. ini.Write("Oracle", "Password", password);
  683. }
  684. catch (Exception ex)
  685. {
  686. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  687. }
  688. }
  689. private void btnOracleTest_Click(object sender, EventArgs e)
  690. {
  691. try
  692. {
  693. string ip = this.txtOracleName.Text.Trim();
  694. string port = this.txtOraclePort.Text.Trim();
  695. string sid = this.txtOracleDatabaseName.Text.Trim();
  696. string username = this.txtOracleUserName.Text.Trim();
  697. string password = this.txtOraclePassword.Text;
  698. if (string.IsNullOrWhiteSpace(ip))
  699. {
  700. MessageBox.Show("数据库【地址】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  701. this.txtOracleName.Focus();
  702. return;
  703. }
  704. if (string.IsNullOrWhiteSpace(port))
  705. {
  706. MessageBox.Show("数据库【端口】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  707. this.txtOraclePort.Focus();
  708. return;
  709. }
  710. if (string.IsNullOrWhiteSpace(sid))
  711. {
  712. MessageBox.Show("数据库【实例】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  713. this.txtOracleDatabaseName.Focus();
  714. return;
  715. }
  716. if (string.IsNullOrWhiteSpace(username))
  717. {
  718. MessageBox.Show("数据库【用户】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  719. this.txtOracleUserName.Focus();
  720. return;
  721. }
  722. if (string.IsNullOrWhiteSpace(password))
  723. {
  724. MessageBox.Show("数据库【密码】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  725. this.txtOraclePassword.Focus();
  726. return;
  727. }
  728. IDataAccess dataAccess = DataAccess.CreateByParameters(DataBaseType.Oracle, ip, port, sid, username, password);
  729. try
  730. {
  731. dataAccess.Open();
  732. dataAccess.ExecuteScalar("select sysdate from dual");
  733. }
  734. catch (Exception ex2)
  735. {
  736. MessageBox.Show("数据库【连接】错误" + System.Environment.NewLine + ex2.Message,
  737. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  738. logger.OutputError(ex2, "数据库【连接】错误");
  739. return;
  740. }
  741. DialogResult dr = MessageBox.Show("数据库【连接】成功" + System.Environment.NewLine + "【是】【否】保存设置?",
  742. this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Information);
  743. if (dr == DialogResult.Yes)
  744. {
  745. try
  746. {
  747. password = SymmetricCryptogram.Encrypt(password, "DongkeIbossprd", SymmetricCryptogramType.DES);
  748. }
  749. catch (Exception ex1)
  750. {
  751. MessageBox.Show("数据库【密码】加密错误" + System.Environment.NewLine + ex1.Message,
  752. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  753. logger.OutputError(ex1, "数据库【密码】加密错误");
  754. return;
  755. }
  756. INIHelper ini = INIHelper.IniFile($@"PLC_S_INI\PLC_S_S.ini");
  757. ini.Write("Oracle", "Server", ip);
  758. ini.Write("Oracle", "Port", port);
  759. ini.Write("Oracle", "Database", sid);
  760. ini.Write("Oracle", "UserName", username);
  761. ini.Write("Oracle", "Password", password);
  762. }
  763. }
  764. catch (Exception ex)
  765. {
  766. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  767. }
  768. }
  769. private void txtStatus_TextChanged(object sender, EventArgs e)
  770. {
  771. string t = txtStatus.Text;
  772. if (t.Contains("启动") || t.Contains("Running"))
  773. {
  774. txtStatus.ForeColor = Color.Blue;
  775. }
  776. else if (t.Contains("停止") || t.Contains("Stopped"))
  777. {
  778. txtStatus.ForeColor = Color.Red;
  779. }
  780. else
  781. {
  782. txtStatus.ForeColor = Color.Yellow;
  783. }
  784. }
  785. private void btnGetMES_Click(object sender, EventArgs e)
  786. {
  787. try
  788. {
  789. INIHelper ini = INIHelper.IniFile($@"PLC_S_INI\PLC_S_S.ini");
  790. string ip = ini.Read("iBOSS.MES", "IP");
  791. string port = ini.Read("iBOSS.MES", "Port");
  792. string ServiceName = ini.Read("iBOSS.MES", "ServiceName");
  793. string account = ini.Read("iBOSS.MES", "AccountCode");
  794. string username = ini.Read("iBOSS.MES", "UserCode");
  795. string password = ini.Read("iBOSS.MES", "Password");
  796. if (!string.IsNullOrWhiteSpace(ip))
  797. {
  798. this.txtMES_IP.Text = ip;
  799. }
  800. this.txtMES_IP.Tag = ServiceName;
  801. if (!string.IsNullOrWhiteSpace(port))
  802. {
  803. this.txtMES_Port.Text = port;
  804. }
  805. this.txtMES_Account.Text = account;
  806. this.txtMES_User.Text = username;
  807. this.txtMES_Password.Text = password;
  808. }
  809. catch (Exception ex)
  810. {
  811. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  812. }
  813. }
  814. private void btnSetMES_Click(object sender, EventArgs e)
  815. {
  816. try
  817. {
  818. string ip = this.txtMES_IP.Text.Trim();
  819. string port = this.txtMES_Port.Text.Trim();
  820. string account = this.txtMES_Account.Text.Trim();
  821. string username = this.txtMES_User.Text.Trim();
  822. string password = this.txtMES_Password.Text;
  823. INIHelper ini = INIHelper.IniFile($@"PLC_S_INI\PLC_S_S.ini");
  824. ini.Write("iBOSS.MES", "IP", ip);
  825. ini.Write("iBOSS.MES", "Port", port);
  826. ini.Write("iBOSS.MES", "AccountCode", account);
  827. ini.Write("iBOSS.MES", "UserCode", username);
  828. ini.Write("iBOSS.MES", "Password", password);
  829. }
  830. catch (Exception ex)
  831. {
  832. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  833. }
  834. }
  835. private void btnMESTest_Click(object sender, EventArgs e)
  836. {
  837. try
  838. {
  839. string ip = this.txtMES_IP.Text.Trim();
  840. string ServiceName = this.txtMES_IP.Tag + "";
  841. string port = this.txtMES_Port.Text.Trim();
  842. string account = this.txtMES_Account.Text.Trim();
  843. string username = this.txtMES_User.Text.Trim();
  844. string password = this.txtMES_Password.Text;
  845. if (string.IsNullOrWhiteSpace(ip))
  846. {
  847. MessageBox.Show("MES【地址】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  848. this.txtMES_IP.Focus();
  849. return;
  850. }
  851. if (string.IsNullOrWhiteSpace(ServiceName))
  852. {
  853. MessageBox.Show("MES【服务】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  854. this.txtMES_IP.Focus();
  855. return;
  856. }
  857. if (string.IsNullOrWhiteSpace(port))
  858. {
  859. MessageBox.Show("MES【端口】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  860. this.txtMES_Port.Focus();
  861. return;
  862. }
  863. if (string.IsNullOrWhiteSpace(account))
  864. {
  865. MessageBox.Show("MES【帐套】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  866. this.txtMES_Account.Focus();
  867. return;
  868. }
  869. if (string.IsNullOrWhiteSpace(username))
  870. {
  871. MessageBox.Show("MES【用户】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  872. this.txtMES_User.Focus();
  873. return;
  874. }
  875. //if (string.IsNullOrWhiteSpace(password))
  876. //{
  877. // MessageBox.Show("MES【密码】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  878. // this.txtMES_Password.Focus();
  879. // return;
  880. //}
  881. try
  882. {
  883. WCFProxySetting ps = new WCFProxySetting(ip, port, ServiceName);
  884. SmartDeviceProxy smartDevice = new SmartDeviceProxy(ps);
  885. string t = smartDevice.Invoke<string>(p => p.Test("text"));
  886. // TODO
  887. }
  888. catch (Exception ex1)
  889. {
  890. MessageBox.Show("MES【连接】失败" + System.Environment.NewLine + ex1.Message,
  891. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  892. logger.OutputError(ex1, "MES【连接】失败");
  893. return;
  894. }
  895. DialogResult dr = MessageBox.Show("MES【连接】成功" + System.Environment.NewLine + "【是】【否】保存设置?",
  896. this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Information);
  897. if (dr == DialogResult.Yes)
  898. {
  899. INIHelper ini = INIHelper.IniFile($@"PLC_S_INI\PLC_S_S.ini");
  900. ini.Write("iBOSS.MES", "IP", ip);
  901. ini.Write("iBOSS.MES", "Port", port);
  902. ini.Write("iBOSS.MES", "AccountCode", account);
  903. ini.Write("iBOSS.MES", "UserCode", username);
  904. ini.Write("iBOSS.MES", "Password", password);
  905. }
  906. }
  907. catch (Exception ex)
  908. {
  909. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  910. }
  911. }
  912. private void txtStatus_KeyPress(object sender, KeyPressEventArgs e)
  913. {
  914. e.Handled = true;
  915. }
  916. private bool _isEPTS = false;
  917. private string _SS_KEY = null;
  918. private void cboPLCST_SelectedIndexChanged(object sender, EventArgs e)
  919. {
  920. try
  921. {
  922. string key = cboPLCST.SelectedValue + "";
  923. if (key.StartsWith("EPTS_"))
  924. {
  925. this.lblPort.Text = "设备";
  926. //_SS_KEY = key.Substring(5);
  927. _SS_KEY = key;
  928. }
  929. else
  930. {
  931. this.lblPort.Text = "端口";
  932. _SS_KEY = key;
  933. }
  934. if (this.txtPort.Tag != null)
  935. {
  936. return;
  937. }
  938. if (key.StartsWith("EPTS_"))
  939. {
  940. _isEPTS = true;
  941. this.txtPort.Text = null;
  942. }
  943. else
  944. {
  945. if (_isEPTS)
  946. {
  947. _isEPTS = false;
  948. this.txtPort.Text = null;
  949. }
  950. this.txtPort_TextChanged(null, null);
  951. }
  952. if (cboPLCST.SelectedItem is DataRowView ddd)
  953. {
  954. this.txtDescription.Text = ddd["description"] + "";
  955. }
  956. }
  957. catch (Exception ex)
  958. {
  959. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  960. }
  961. }
  962. }
  963. }