F_Main.cs 42 KB

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