| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149 |
- using System;
- using System.Configuration.Install;
- using System.Data;
- using System.Drawing;
- using System.ServiceProcess;
- using System.Windows.Forms;
- using Curtain.Core;
- using Curtain.DataAccess;
- using Curtain.Extension.ExObjectConvert;
- using Curtain.Extension.ExString;
- using Curtain.Helpers;
- using Curtain.Log;
- using Curtain.Net.Sockets.PLC.Model.Melsec;
- using Curtain.Net.Sockets.PLC.Model.Siemens;
- using Curtain.Systems.Cryptography;
- using Curtain.WCF.Proxy;
- using Curtain.Windows;
- using PLC_S;
- using PLC_S.Proxy;
- namespace PLC_WF
- {
- public partial class F_Main : Form
- {
- private PLC_S_S PLC_S_S = null;
- private readonly Logger logger = Logger.CreateLogger("WF");
- public F_Main()
- {
- InitializeComponent();
- logger.FileExistDays = 30;
- logger.FilePrefix = "";
- logger.FileSuffix = "WF";
- DataTable dataTable = new DataTable("PLC_S_T");
- dataTable.Columns.Add("code");
- dataTable.Columns.Add("name");
- dataTable.Columns.Add("description");
- //xuwei add 2022-03-12
- dataTable.Rows.Add("GA", "[GA]自动", "自动识别模块");
- dataTable.Rows.Add("EPTS_GP", "打磨【房号+计件】", "");
- dataTable.Rows.Add("PW", "工序计件", "工序自动计件的服务【】");
- cboPLCST.DisplayMember = "name";
- cboPLCST.ValueMember = "code";
- this.txtPort.Tag = 1;
- cboPLCST.DataSource = dataTable;
- this.txtPort.Tag = null;
- GetSetting();
- this.btnGetDB_Click(null, null);
- this.btnGetMES_Click(null, null);
- this.btnLogLenR_Click(null, null);
- this.Text = $"{(PLC_S_S.DisplayName.IsNullOrWhiteSpace() ? PLC_S_S.ServiceName : PLC_S_S.DisplayName)}";
- }
- private void GetSetting()
- {
- PLC_S_S = PLC_S_S.Get();
- this.txtPort.Tag = 1;
- this.txtPort.Text = PLC_S_S.Port.ToString();
- if (!PLC_S_S.PLCServiceType.IsNullOrEmpty())
- {
- this.cboPLCST.SelectedValue = PLC_S_S.PLCServiceType;
- }
- this.txtPort.Tag = null;
- this.chkAutoStart.Checked = PLC_S_S.AutoStart;
- this.txtServiceName.Text = PLC_S_S.ServiceName;
- this.txtDisplayName.Text = PLC_S_S.DisplayName;
- this.txtDescription.Text = PLC_S_S.Description;
- this.txtInterval.Text = PLC_S_S.Interval + "";
- this.txtEPTS.Text = PLC_S_S.EPTS_CODE;
- }
- private void SetSetting()
- {
- //PLC_S_S PLC_S_S = PLC_S_S.Get();
- PLC_S_S.Port = this.txtPort.Text.ToInt32();
- PLC_S_S.PLCServiceType = this.cboPLCST.SelectedValue + "";
- PLC_S_S.AutoStart = this.chkAutoStart.Checked;
- PLC_S_S.ServiceName = this.txtServiceName.Text.Trim();
- PLC_S_S.DisplayName = this.txtDisplayName.Text.Trim();
- PLC_S_S.Description = this.txtDescription.Text.Trim();
- PLC_S_S.Interval = this.txtInterval.Text.Trim().ToInt32();
- PLC_S_S.EPTS_CODE = this.txtEPTS.Text.Trim();
- PLC_S_S.Set(PLC_S_S);
- }
- private void btnStart_Click(object sender, EventArgs e)
- {
- try
- {
- if (string.IsNullOrWhiteSpace(this.txtPort.Text))
- {
- MessageBox.Show("PLC服务【端口】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- this.txtPort.Focus();
- return;
- }
- //if (string.IsNullOrWhiteSpace(this.txtServiceName.Text))
- //{
- // MessageBox.Show("PLC服务【服务名】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- // this.txtServiceName.Focus();
- // return;
- //}
- //if (string.IsNullOrWhiteSpace(this.txtDisplayName.Text))
- //{
- // MessageBox.Show("PLC服务【显示名】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- // this.txtDisplayName.Focus();
- // return;
- //}
- SetSetting();
- //if (PLC_S_S.PLCServiceType == "BP1")
- //{
- // string[] s = textBox1.Lines;
- // PLC_S.ServerModel.BarcodePrint1 bb = new PLC_S.ServerModel.BarcodePrint1();
- // bb.bs = s;
- // bb.Start(PLC_S_S.Port);
- // PLC_Server.ServerSocket = bb;
- //}
- //else
- //{
- // PLC_Server.Start();
- //}
- PLC_Server.Start();
- SetESControl();
- txtStatus.Text = (PLC_Server.ServerSocket?.Started ?? false ? "启动" : "停止");
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnStop_Click(object sender, EventArgs e)
- {
- try
- {
- PLC_Server.Stop();
- SetNSControl();
- txtStatus.Text = (PLC_Server.ServerSocket?.Started ?? false ? "启动" : "停止");
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnLog_Click(object sender, EventArgs e)
- {
- try
- {
- if (PLC_Server.ServerSocket is IShowFormLog showFormLog)
- {
- if (showFormLog == null)
- {
- return;
- }
- F_Log f = showFormLog.FormLogShow as F_Log;
- if (f == null)
- {
- f = new F_Log();
- f.Text = $"{f.Text }[{PLC_Server.PLC_S_S.DisplayName}]";
- showFormLog.FormLogShow = f;
- f.ShowFormLog = showFormLog;
- }
- f.Show();
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnExePath_Click(object sender, EventArgs e)
- {
- System.Diagnostics.Process.Start(System.AppDomain.CurrentDomain.BaseDirectory);
- }
- private void btnSetSetting_Click(object sender, EventArgs e)
- {
- try
- {
- SetSetting();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnGetSetting_Click(object sender, EventArgs e)
- {
- try
- {
- GetSetting();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- //private int PLC_S_T = 0;
- private void F_Main_Shown(object sender, EventArgs e)
- {
- try
- {
- this._windowState = this.WindowState;
- RefreshForm();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void RefreshForm()
- {
- if (string.IsNullOrWhiteSpace(PLC_S_S.ServiceName))
- {
- // 新服务设置
- ////PLC_S_S.ServiceName = $"{PLC_S_S.S_Name}[{PLC_S_S.Port}]";
- ////PLC_S_S.DisplayName = $"{PLC_S_S.D_Name}[{PLC_S_S.Port}]";
- //PLC_S_S.ServiceName = $"{PLC_S_S.S_Name}[{_SS_KEY}][{txtPort.Text}]";
- //PLC_S_S.DisplayName = $"{PLC_S_S.D_Name}[{_SS_KEY}][{txtPort.Text}]";
- //this.txtServiceName.Text = PLC_S_S.ServiceName;
- //this.txtDisplayName.Text = PLC_S_S.DisplayName;
- SetNSControl();
- return;
- }
- ServiceControllerStatus? wsStatus = null;
- if (IsServiceExisted(PLC_S_S.ServiceName.ToLower()))
- {
- this.serviceController1.ServiceName = PLC_S_S.ServiceName;
- try
- {
- wsStatus = this.serviceController1.Status;
- }
- catch
- {
- // this.serviceController1.ServiceName = "";
- }
- }
- if (wsStatus == null)
- {
- // 非windows service
- if (this.chkAutoStart.Checked)
- {
- SetESControl();
- PLC_Server.Start();
- txtStatus.Text = (PLC_Server.ServerSocket?.Started ?? false ? "启动" : "停止");
- }
- else
- {
- SetNSControl();
- }
- }
- else
- {
- // windows service
- SetWSControl();
- this.txtStatus.Text = "WS:" + this.serviceController1.Status;
- }
- }
- /// <summary>
- /// WS 时,控件状态
- /// </summary>
- private void SetWSControl()
- {
- //PLC_S_T = 2;
- this.txtPort.ReadOnly = true;
- this.cboPLCST.Enabled = false;
- this.txtServiceName.ReadOnly = true;
- this.txtDisplayName.ReadOnly = true;
- this.txtDescription.ReadOnly = true;
- this.txtEPTS.ReadOnly = true;
- this.txtInterval.ReadOnly = true;
- this.btnStart.Enabled = false;
- this.btnStop.Enabled = false;
- this.btnLog.Enabled = false;
- this.btnWS_Insert.Enabled = false;
- this.btnWS_Delete.Enabled = true;
- this.btnWS_Start.Enabled = true;
- this.btnWS_Stop.Enabled = true;
- this.btnWS_ReStart.Enabled = true;
- this.btnWS_Refresh.Enabled = true;
- }
- /// <summary>
- /// es 时,控件状态
- /// </summary>
- private void SetESControl()
- {
- //PLC_S_T = 1;
- this.txtPort.ReadOnly = true;
- this.cboPLCST.Enabled = false;
- this.txtServiceName.ReadOnly = true;
- this.txtEPTS.ReadOnly = true;
- this.txtInterval.ReadOnly = true;
- this.txtDisplayName.ReadOnly = false;
- this.txtDescription.ReadOnly = false;
- this.btnStart.Enabled = false;
- this.btnStop.Enabled = true;
- this.btnLog.Enabled = true;
- this.btnWS_Insert.Enabled = false;
- this.btnWS_Delete.Enabled = false;
- this.btnWS_Start.Enabled = false;
- this.btnWS_Stop.Enabled = false;
- this.btnWS_ReStart.Enabled = false;
- this.btnWS_Refresh.Enabled = false;
- }
- /// <summary>
- /// 无服务时,控件状态
- /// </summary>
- private void SetNSControl()
- {
- //PLC_S_T = 0;
- this.txtPort.ReadOnly = false;
- this.cboPLCST.Enabled = true;
- this.txtServiceName.ReadOnly = false;
- this.txtEPTS.ReadOnly = false;
- this.txtInterval.ReadOnly = false;
- this.txtDisplayName.ReadOnly = false;
- this.txtDescription.ReadOnly = false;
- this.btnStart.Enabled = true;
- this.btnStop.Enabled = false;
- //this.btnLog.Enabled = false;
- this.btnWS_Insert.Enabled = true;
- this.btnWS_Delete.Enabled = false;
- this.btnWS_Start.Enabled = false;
- this.btnWS_Stop.Enabled = false;
- this.btnWS_ReStart.Enabled = false;
- this.btnWS_Refresh.Enabled = false;
- }
- private void F_Main_TextChanged(object sender, EventArgs e)
- {
- this.notifyIcon1.Text = this.Text;
- }
- private FormWindowState _windowState = FormWindowState.Normal;
- private bool _hasClose = false;
- private void F_Main_SizeChanged(object sender, EventArgs e)
- {
- if (this.WindowState == FormWindowState.Minimized)
- {
- //Visible = false;
- }
- else
- {
- this._windowState = this.WindowState;
- }
- }
- private void F_Main_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (!_hasClose)
- {
- DialogResult dr = MessageBox.Show("是否退出?", this.Text, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3);
- if (dr == DialogResult.Cancel)
- {
- e.Cancel = true;
- //this.WindowState = FormWindowState.Minimized;
- return;
- }
- if (dr == DialogResult.No)
- {
- e.Cancel = true;
- notifyIcon1.Visible = true;
- Visible = false;
- return;
- }
- }
- notifyIcon1.Visible = false;
- SetSetting();
- }
- private void notifyIcon1_DoubleClick(object sender, EventArgs e)
- {
- }
- private void notifyIcon1_Click(object sender, EventArgs e)
- {
- //if (!this.Visible)
- //{
- Visible = true;
- //this.TopMost = true;
- //this.WindowState = FormWindowState.Normal;
- WindowState = this._windowState;
- ShowInTaskbar = true;
- //}
- Activate();
- }
- private void txtPort_TextChanged(object sender, EventArgs e)
- {
- //if (this.txtPort.Tag == null)
- //{
- // PLC_S_S.ServiceName = $"{PLC_S_S.S_Name}[{_SS_KEY}][{txtPort.Text}]";
- // PLC_S_S.DisplayName = $"{PLC_S_S.D_Name}[{_SS_KEY}][{txtPort.Text}]";
- // this.txtServiceName.Text = PLC_S_S.ServiceName;
- // this.txtDisplayName.Text = PLC_S_S.DisplayName;
- //}
- }
- private void btnWS_Refresh_Click(object sender, EventArgs e)
- {
- try
- {
- RefreshWSStatus();
- }
- catch (Exception ex)
- {
- this.txtStatus.Text = null;
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void RefreshWSStatus()
- {
- this.serviceController1.Refresh();
- this.txtStatus.Text = "WS:" + this.serviceController1.Status;
- Application.DoEvents();
- }
- private void txtDisplayName_TextChanged(object sender, EventArgs e)
- {
- this.Text = $"{PLC_S_S.DisplayName}";
- }
- private void txtPort_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (char.IsControl(e.KeyChar) ||
- char.IsNumber(e.KeyChar)
- )
- {
- return;
- }
- e.Handled = true;
- }
- private void txtPort_KeyDown(object sender, KeyEventArgs e)
- {
- //if (char.IsControl(e.KeyData))
- //{
- // e.Handled = true;
- //}
- }
- private void btnWS_Insert_Click(object sender, EventArgs e)
- {
- try
- {
- if (string.IsNullOrWhiteSpace(this.txtPort.Text))
- {
- MessageBox.Show("PLC服务【端口】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- this.txtPort.Focus();
- return;
- }
- if (string.IsNullOrWhiteSpace(this.txtServiceName.Text))
- {
- MessageBox.Show("PLC服务【服务名】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- this.txtServiceName.Focus();
- return;
- }
- if (string.IsNullOrWhiteSpace(this.txtDisplayName.Text))
- {
- MessageBox.Show("PLC服务【显示名】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- this.txtDisplayName.Focus();
- return;
- }
- SetSetting();
- string path = ApplicationInformation.GetAbsolutePath("PLC_WS.exe");
- InstallService(path);
- SetWSControl();
- this.serviceController1.ServiceName = PLC_S_S.ServiceName;
- if (PLC_S_S.AutoStart)
- {
- this.serviceController1.Start();
- RefreshWSStatus();
- this.serviceController1.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 1, 0));
- }
- RefreshWSStatus();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnWS_Delete_Click(object sender, EventArgs e)
- {
- try
- {
- if (IsServiceExisted(PLC_S_S.ServiceName.ToLower()))
- {
- string path = ApplicationInformation.GetAbsolutePath("PLC_WS.exe");
- UninstallService(path);
- }
- SetNSControl();
- this.txtStatus.Text = null;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnWS_Start_Click(object sender, EventArgs e)
- {
- try
- {
- this.serviceController1.Start();
- RefreshWSStatus();
- this.serviceController1.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 1, 0));
- RefreshWSStatus();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnWS_Stop_Click(object sender, EventArgs e)
- {
- try
- {
- this.serviceController1.Stop();
- RefreshWSStatus();
- this.serviceController1.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 1, 0));
- RefreshWSStatus();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnWS_ReStart_Click(object sender, EventArgs e)
- {
- try
- {
- if (this.serviceController1.Status == ServiceControllerStatus.Running)
- {
- this.serviceController1.Stop();
- RefreshWSStatus();
- this.serviceController1.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 1, 0));
- RefreshWSStatus();
- }
- this.serviceController1.Start();
- RefreshWSStatus();
- this.serviceController1.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 1, 0));
- RefreshWSStatus();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- /// <summary>
- /// 判断服务是否存在
- /// </summary>
- /// <param name="serviceName"></param>
- /// <returns></returns>
- private bool IsServiceExisted(string serviceName)
- {
- ServiceController[] services = ServiceController.GetServices();
- foreach (ServiceController sc in services)
- {
- if (sc.ServiceName.ToLower() == serviceName)
- {
- return true;
- }
- }
- return false;
- }
- /// <summary>
- /// 安装服务
- /// </summary>
- /// <param name="serviceFilePath"></param>
- private void InstallService(string serviceFilePath)
- {
- using (AssemblyInstaller installer = new AssemblyInstaller())
- {
- installer.UseNewContext = true;
- installer.Path = serviceFilePath;
- //IDictionary savedState = new Hashtable();
- //installer.Install(savedState);
- //installer.Commit(savedState);
- installer.Install(null);
- installer.Commit(null);
- }
- }
- /// <summary>
- /// 卸载服务
- /// </summary>
- /// <param name="serviceFilePath"></param>
- private void UninstallService(string serviceFilePath)
- {
- using (AssemblyInstaller installer = new AssemblyInstaller())
- {
- installer.UseNewContext = true;
- installer.Path = serviceFilePath;
- installer.Uninstall(null);
- }
- }
- /// <summary>
- /// 启动服务
- /// </summary>
- /// <param name="serviceName"></param>
- private void ServiceStart(string serviceName)
- {
- using (ServiceController control = new ServiceController(serviceName))
- {
- if (control.Status == ServiceControllerStatus.Stopped)
- {
- control.Start();
- }
- }
- }
- /// <summary>
- /// 停止服务
- /// </summary>
- /// <param name="serviceName"></param>
- private void ServiceStop(string serviceName)
- {
- using (ServiceController control = new ServiceController(serviceName))
- {
- if (control.Status == ServiceControllerStatus.Running)
- {
- control.Stop();
- }
- }
- }
- private void btnRefreshForm_Click(object sender, EventArgs e)
- {
- try
- {
- RefreshForm();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnGetDB_Click(object sender, EventArgs e)
- {
- try
- {
- INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_S.ini");
- string ip = ini.Read("Oracle", "Server");
- string port = ini.Read("Oracle", "Port");
- string sid = ini.Read("Oracle", "Database");
- string username = ini.Read("Oracle", "UserName");
- string password = ini.Read("Oracle", "Password");
- if (!string.IsNullOrWhiteSpace(ip))
- {
- this.txtOracleName.Text = ip;
- }
- if (!string.IsNullOrWhiteSpace(port))
- {
- this.txtOraclePort.Text = port;
- }
- this.txtOracleDatabaseName.Text = sid;
- this.txtOracleUserName.Text = username;
- if (!string.IsNullOrEmpty(password))
- {
- try
- {
- password = SymmetricCryptogram.Decrypt(password, "DongkeIbossprd", SymmetricCryptogramType.DES);
- this.txtOraclePassword.Text = password;
- }
- catch (Exception ex1)
- {
- this.txtOraclePassword.Text = null;
- MessageBox.Show("数据库【密码】解密错误" + System.Environment.NewLine + ex1.Message,
- this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- logger.OutputError(ex1, "数据库【密码】解密错误");
- }
- }
- else
- {
- this.txtOraclePassword.Text = null;
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnSetDB_Click(object sender, EventArgs e)
- {
- try
- {
- string ip = this.txtOracleName.Text.Trim();
- string port = this.txtOraclePort.Text.Trim();
- string sid = this.txtOracleDatabaseName.Text.Trim();
- string username = this.txtOracleUserName.Text.Trim();
- string password = this.txtOraclePassword.Text;
- if (!string.IsNullOrEmpty(password))
- {
- try
- {
- password = SymmetricCryptogram.Encrypt(password, "DongkeIbossprd", SymmetricCryptogramType.DES);
- }
- catch (Exception ex1)
- {
- MessageBox.Show("数据库【密码】加密错误" + System.Environment.NewLine + ex1.Message,
- this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- logger.OutputError(ex1, "数据库【密码】加密错误");
- password = "";
- }
- }
- INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_S.ini");
- ini.Write("Oracle", "Server", ip);
- ini.Write("Oracle", "Port", port);
- ini.Write("Oracle", "Database", sid);
- ini.Write("Oracle", "UserName", username);
- ini.Write("Oracle", "Password", password);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnOracleTest_Click(object sender, EventArgs e)
- {
- try
- {
- string ip = this.txtOracleName.Text.Trim();
- string port = this.txtOraclePort.Text.Trim();
- string sid = this.txtOracleDatabaseName.Text.Trim();
- string username = this.txtOracleUserName.Text.Trim();
- string password = this.txtOraclePassword.Text;
- if (string.IsNullOrWhiteSpace(ip))
- {
- MessageBox.Show("数据库【地址】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- this.txtOracleName.Focus();
- return;
- }
- if (string.IsNullOrWhiteSpace(port))
- {
- MessageBox.Show("数据库【端口】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- this.txtOraclePort.Focus();
- return;
- }
- if (string.IsNullOrWhiteSpace(sid))
- {
- MessageBox.Show("数据库【实例】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- this.txtOracleDatabaseName.Focus();
- return;
- }
- if (string.IsNullOrWhiteSpace(username))
- {
- MessageBox.Show("数据库【用户】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- this.txtOracleUserName.Focus();
- return;
- }
- if (string.IsNullOrWhiteSpace(password))
- {
- MessageBox.Show("数据库【密码】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- this.txtOraclePassword.Focus();
- return;
- }
- IDataAccess dataAccess = DataAccess.CreateByParameters(DataBaseType.Oracle, ip, port, sid, username, password);
- try
- {
- dataAccess.Open();
- dataAccess.ExecuteScalar("select sysdate from dual");
- }
- catch (Exception ex2)
- {
- MessageBox.Show("数据库【连接】错误" + System.Environment.NewLine + ex2.Message,
- this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- logger.OutputError(ex2, "数据库【连接】错误");
- return;
- }
- DialogResult dr = MessageBox.Show("数据库【连接】成功" + System.Environment.NewLine + "【是】【否】保存设置?",
- this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Information);
- if (dr == DialogResult.Yes)
- {
- try
- {
- password = SymmetricCryptogram.Encrypt(password, "DongkeIbossprd", SymmetricCryptogramType.DES);
- }
- catch (Exception ex1)
- {
- MessageBox.Show("数据库【密码】加密错误" + System.Environment.NewLine + ex1.Message,
- this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- logger.OutputError(ex1, "数据库【密码】加密错误");
- return;
- }
- INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_S.ini");
- ini.Write("Oracle", "Server", ip);
- ini.Write("Oracle", "Port", port);
- ini.Write("Oracle", "Database", sid);
- ini.Write("Oracle", "UserName", username);
- ini.Write("Oracle", "Password", password);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void txtStatus_TextChanged(object sender, EventArgs e)
- {
- string t = txtStatus.Text;
- if (t.Contains("启动") || t.Contains("Running"))
- {
- txtStatus.ForeColor = Color.Blue;
- }
- else if (t.Contains("停止") || t.Contains("Stopped"))
- {
- txtStatus.ForeColor = Color.Red;
- }
- else
- {
- txtStatus.ForeColor = Color.Yellow;
- }
- }
- private void btnGetMES_Click(object sender, EventArgs e)
- {
- try
- {
- INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_S.ini");
- string ip = ini.Read("iBOSS.MES", "IP");
- string port = ini.Read("iBOSS.MES", "Port");
- string ServiceName = ini.Read("iBOSS.MES", "ServiceName");
- string account = ini.Read("iBOSS.MES", "AccountCode");
- string username = ini.Read("iBOSS.MES", "UserCode");
- string password = ini.Read("iBOSS.MES", "Password");
- if (!string.IsNullOrWhiteSpace(ip))
- {
- this.txtMES_IP.Text = ip;
- }
- this.txtMES_IP.Tag = ServiceName;
- if (!string.IsNullOrWhiteSpace(port))
- {
- this.txtMES_Port.Text = port;
- }
- this.txtMES_Account.Text = account;
- this.txtMES_User.Text = username;
- this.txtMES_Password.Text = password;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnSetMES_Click(object sender, EventArgs e)
- {
- try
- {
- string ip = this.txtMES_IP.Text.Trim();
- string port = this.txtMES_Port.Text.Trim();
- string account = this.txtMES_Account.Text.Trim();
- string username = this.txtMES_User.Text.Trim();
- string password = this.txtMES_Password.Text;
- INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_S.ini");
- ini.Write("iBOSS.MES", "IP", ip);
- ini.Write("iBOSS.MES", "Port", port);
- ini.Write("iBOSS.MES", "AccountCode", account);
- ini.Write("iBOSS.MES", "UserCode", username);
- ini.Write("iBOSS.MES", "Password", password);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnMESTest_Click(object sender, EventArgs e)
- {
- try
- {
- string ip = this.txtMES_IP.Text.Trim();
- string ServiceName = this.txtMES_IP.Tag + "";
- string port = this.txtMES_Port.Text.Trim();
- string account = this.txtMES_Account.Text.Trim();
- string username = this.txtMES_User.Text.Trim();
- string password = this.txtMES_Password.Text;
- if (string.IsNullOrWhiteSpace(ip))
- {
- MessageBox.Show("MES【地址】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- this.txtMES_IP.Focus();
- return;
- }
- if (string.IsNullOrWhiteSpace(ServiceName))
- {
- MessageBox.Show("MES【服务】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- this.txtMES_IP.Focus();
- return;
- }
- if (string.IsNullOrWhiteSpace(port))
- {
- MessageBox.Show("MES【端口】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- this.txtMES_Port.Focus();
- return;
- }
- if (string.IsNullOrWhiteSpace(account))
- {
- MessageBox.Show("MES【帐套】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- this.txtMES_Account.Focus();
- return;
- }
- if (string.IsNullOrWhiteSpace(username))
- {
- MessageBox.Show("MES【用户】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- this.txtMES_User.Focus();
- return;
- }
- //if (string.IsNullOrWhiteSpace(password))
- //{
- // MessageBox.Show("MES【密码】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- // this.txtMES_Password.Focus();
- // return;
- //}
- try
- {
- WCFProxySetting ps = new WCFProxySetting(ip, port, ServiceName);
- SmartDeviceProxy smartDevice = new SmartDeviceProxy(ps);
- string t = smartDevice.Invoke<string>(p => p.Test("text"));
- // TODO
- }
- catch (Exception ex1)
- {
- MessageBox.Show("MES【连接】失败" + System.Environment.NewLine + ex1.Message,
- this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- logger.OutputError(ex1, "MES【连接】失败");
- return;
- }
- DialogResult dr = MessageBox.Show("MES【连接】成功" + System.Environment.NewLine + "【是】【否】保存设置?",
- this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Information);
- if (dr == DialogResult.Yes)
- {
- INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_S.ini");
- ini.Write("iBOSS.MES", "IP", ip);
- ini.Write("iBOSS.MES", "Port", port);
- ini.Write("iBOSS.MES", "AccountCode", account);
- ini.Write("iBOSS.MES", "UserCode", username);
- ini.Write("iBOSS.MES", "Password", password);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void txtStatus_KeyPress(object sender, KeyPressEventArgs e)
- {
- e.Handled = true;
- }
- private bool _isEPTS = false;
- private string _SS_KEY = null;
- private void cboPLCST_SelectedIndexChanged(object sender, EventArgs e)
- {
- try
- {
- string key = cboPLCST.SelectedValue + "";
- if (key.StartsWith("EPTS_"))
- {
- this.lblPort.Text = "设备";
- //_SS_KEY = key.Substring(5);
- _SS_KEY = key;
- }
- else
- {
- this.lblPort.Text = "端口";
- _SS_KEY = key;
- }
- if (this.txtPort.Tag != null)
- {
- return;
- }
- if (key.StartsWith("EPTS_"))
- {
- _isEPTS = true;
- this.txtPort.Text = null;
- }
- else
- {
- if (_isEPTS)
- {
- _isEPTS = false;
- this.txtPort.Text = null;
- }
- this.txtPort_TextChanged(null, null);
- }
- //if (cboPLCST.SelectedItem is DataRowView ddd)
- //{
- // this.txtDescription.Text = ddd["description"] + "";
- //}
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- public static int PLC_LOG_LEN = 5000;
- private void btnLogLenR_Click(object sender, EventArgs e)
- {
- try
- {
- INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_S.ini");
- PLC_LOG_LEN = ini.Read("PLC_WF", "LOG_LEN").ToInt32();
- if (PLC_LOG_LEN < 1000)
- {
- PLC_LOG_LEN = 1000;
- }
- txtLogLen.Text = PLC_LOG_LEN+"";
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnLogLenW_Click(object sender, EventArgs e)
- {
- try
- {
- int len = txtLogLen.Text.ToInt32();
- if (len < 1000)
- {
- len = 1000;
- }
- PLC_LOG_LEN = len;
- txtLogLen.Text = PLC_LOG_LEN + "";
- INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_S.ini");
- ini.Write("PLC_WF", "LOG_LEN", txtLogLen.Text);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnPLC_CRW_Click(object sender, EventArgs e)
- {
- try
- {
- //string S_NAME = "SSS_1";
- //Logger logger = Logger.CreateLogger(S_NAME+ "-S");
- //Logger logger_t = Logger.CreateLogger(S_NAME + "-T");
- //Logger logger_e = Logger.CreateLogger(S_NAME + "-E");
- //Logger logger_e_out = Logger.CreateLogger(S_NAME + "-EO");
- //logger.FileNameWithoutDate = true;
- //logger.FileExistDays = 30;
- //logger.LevelOneFile = true;
- //logger.FilePrefix = null;
- //logger.FileSuffix = logger.Name;
- //logger_t.FileExistDays = 30;
- //logger_t.LevelOneFile = true;
- //logger_t.SubFolderFormat = logger_t.Name;
- //logger_e.FileExistDays = 0;
- //logger_e_out.FileExistDays = 0;
- //logger_e_out.FilePrefix = null;
- //logger_e_out.FileSuffix = S_NAME;
- //logger_e_out.Directory = "..\\_PLC_Logs";
- //logger.OutputTrace("S_start");
- //logger_t.OutputTrace("S_start");
- //logger_t.OutputWarn("S_start");
- //logger_e.OutputWarn("S_start");
- //logger_e_out.OutputWarn("S_start");
- //logger_e.OutputError(null,"S_start");
- //logger_e_out.OutputError(null,"S_start");
- switch (cboPLCType.Text)
- {
- case "MelsecA1EAscii":
- F_PLC_M<MelsecA1EAsciiModel> fm = new F_PLC_M<MelsecA1EAsciiModel>();
- fm.Show();
- break;
- case "SiemensS7_1200":
- F_PLC_M<SiemensS7_1200Model> fs = new F_PLC_M<SiemensS7_1200Model>();
- fs.Show();
- break;
- case "SimpleSocketClientModel":
- F_PLC_M<SimpleSocketClientModel> fs1 = new F_PLC_M<SimpleSocketClientModel>();
- fs1.Show();
- break;
- default:
- break;
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- }
- }
|