| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039 |
- 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.Helpers;
- using Curtain.Log;
- 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");
- //dataTable.Rows.Add("GC", "产品型号", "提供产品型号的服务【】");
- //dataTable.Rows.Add("GL", "产品等级", "提供产品等级的服务【】");
- //dataTable.Rows.Add("HPG", "高压注浆", "自动高压注浆的服务【】");
- dataTable.Rows.Add("FGC_BZ", "包装工序计件", "包装工序自动计件的服务【】");
- dataTable.Rows.Add("PW", "施釉工序计件", "施釉工序自动计件的服务【】");
- //dataTable.Rows.Add("KL", "登窑", "自动登窑的服务【】");
- //dataTable.Rows.Add("KI", "入窑", "自动入窑的服务【】");
- //dataTable.Rows.Add("KUL", "卸窑", "自动卸窑的服务【】");
- //dataTable.Rows.Add("BP", "条码打印", "条码自动打印【】");
- //dataTable.Rows.Add("BP1", "条码打印(批量补打)", "条码打印(批量补打)");
- //dataTable.Rows.Add("EPTS_HPG", "高压注浆设备", "同步高压注浆设备运行参数的服务【】");
- //dataTable.Rows.Add("EPTS_KILN", "窑炉设备", "同步窑炉设备运行参数的服务【】");
- //dataTable.Rows.Add("EPTS_DRY", "烤房设备", "同步烤房设备运行参数的服务【】");
- // TODO
- 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.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;
- }
- 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.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.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.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 = true;
- 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;
- 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.IniFile($@"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.IniFile($@"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.IniFile($@"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.IniFile($@"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.IniFile($@"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.IniFile($@"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);
- }
- }
- }
- }
|