481 lines
16 KiB
C#
481 lines
16 KiB
C#
using Glorysoft.SECS.EQP;
|
||
using Glorysoft.SECS.EQP.Common;
|
||
using Glorysoft.SECS.EQP.Utilities;
|
||
using Newtonsoft.Json;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Windows.Forms;
|
||
using Glorysoft.EAP.Core.Constant;
|
||
using ARI.EAP.HOST.SRD;
|
||
using Glorysoft.SECS.EQP.Commands;
|
||
using Glorysoft.SECS.EQP.Message;
|
||
using ARI.EAP.HOST.Common;
|
||
using System.Threading.Tasks;
|
||
using ARI.EAP.HOST.Utilities;
|
||
using ARI.EAP.HOST.MQ;
|
||
|
||
namespace ARI.EAP.HOST
|
||
{
|
||
public partial class MainForm : Form
|
||
{
|
||
public static List<SECSContext> eqpContexts;
|
||
public static Scenario scenario;
|
||
public static Mq mq;
|
||
public static SocketClient socketClient;
|
||
|
||
public MainForm()
|
||
{
|
||
InitializeComponent();
|
||
Configuration.conf = XmlSerializeUtil.Deserialize<Configuration>(File.ReadAllText(Constants.configuerPath));
|
||
eqpContexts = new List<SECSContext>();
|
||
mq = new Mq();
|
||
socketClient = new SocketClient();
|
||
Global.MF = this;
|
||
}
|
||
|
||
private void MainForm_Load(object sender, EventArgs e)
|
||
{
|
||
this.ConnectStatus.Text = EquipmentStatus.EqConnectState;
|
||
this.MQStatus.Text = EquipmentStatus.MQstate;
|
||
this.Text = Configuration.conf.connectSetting.name;
|
||
this.editIPaddress.Text = Configuration.conf.connectSetting.remoteIp;
|
||
this.editDeviceID.Text = Configuration.conf.connectSetting.deviceId;
|
||
this.editPort.Text = Configuration.conf.connectSetting.remotePort;
|
||
EquipmentStatus.EapState = RunState.Run;
|
||
}
|
||
|
||
private void editIPaddress_KeyPress(object sender, KeyPressEventArgs e)
|
||
{
|
||
int kc = (int)e.KeyChar;
|
||
if ((kc < 48 || kc > 57) && kc != 8 && kc != 46)
|
||
{
|
||
e.Handled = true;
|
||
}
|
||
}
|
||
|
||
private void editDeviceID_KeyPress(object sender, KeyPressEventArgs e)
|
||
{
|
||
int kc = (int)e.KeyChar;
|
||
if ((kc < 48 || kc > 57) && kc != 8)
|
||
{
|
||
e.Handled = true;
|
||
}
|
||
}
|
||
|
||
private void editPort_KeyPress(object sender, KeyPressEventArgs e)
|
||
{
|
||
int kc = (int)e.KeyChar;
|
||
if ((kc < 48 || kc > 57) && kc != 8)
|
||
{
|
||
e.Handled = true;
|
||
}
|
||
}
|
||
|
||
private void editIPaddress_Leave(object sender, EventArgs e)
|
||
{
|
||
if (JudgeIPFormat(editIPaddress.Text) == true)
|
||
{
|
||
IPError.Visible = false;
|
||
}
|
||
else
|
||
{
|
||
IPError.Visible = true;
|
||
}
|
||
}
|
||
|
||
private void editDeviceID_Leave(object sender, EventArgs e)
|
||
{
|
||
if (JudgePortFormat(editDeviceID.Text) == true)
|
||
{
|
||
ErrorDeviceID.Visible = false;
|
||
}
|
||
else
|
||
{
|
||
ErrorDeviceID.Visible = true;
|
||
}
|
||
}
|
||
|
||
private void editPort_Leave(object sender, EventArgs e)
|
||
{
|
||
if (JudgePortFormat(editPort.Text) == true)
|
||
{
|
||
ErrorPort.Visible = false;
|
||
}
|
||
else
|
||
{
|
||
ErrorPort.Visible = true;
|
||
}
|
||
}
|
||
|
||
private void Connect_Click(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
//配置出错时不进行连接
|
||
if (IPError.Visible == true || ErrorPort.Visible == true || ErrorDeviceID.Visible == true)
|
||
{
|
||
MessageBox.Show("Please check configuration", "NOTICE");
|
||
return;
|
||
}
|
||
if (this.Connect.Text == "Reconnect")
|
||
{
|
||
EquipmentStatus.initStatus = false;
|
||
SimulatorInfo.Instance.Context.Close();
|
||
}
|
||
this.Connect.Text = "Reconnect";
|
||
Configuration.conf.connectSetting.deviceId = editDeviceID.Text;
|
||
Configuration.conf.connectSetting.remoteIp = editIPaddress.Text;
|
||
Configuration.conf.connectSetting.remotePort = editPort.Text;
|
||
Configuration.conf.connectSetting.name = this.Text;
|
||
XmlSerializeUtil.Serializer<Configuration>(Configuration.conf);
|
||
EquipmentStatus.EqConnectState = ConnectState.connecting;
|
||
ConnectThread();
|
||
}
|
||
catch(Exception ee)
|
||
{
|
||
MessageBox.Show(ee.Message, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
LoggerService.SYSLogger.Error(ee);
|
||
}
|
||
}
|
||
|
||
private void S1F3Request_Click(object sender, EventArgs e)
|
||
{
|
||
if (this.ConnectStatus.Text != ConnectState.connected)
|
||
{
|
||
return;
|
||
}
|
||
Command.S1F3Command(null);
|
||
}
|
||
|
||
private void S5F3_Click(object sender, EventArgs e)
|
||
{
|
||
if (this.ConnectStatus.Text != ConnectState.connected)
|
||
{
|
||
return;
|
||
}
|
||
S5F3Item item = new S5F3Item();
|
||
if (EquipmentStatus.S5F3 == "AlarmDisable")
|
||
{
|
||
|
||
item.ALED = 128;
|
||
item.ALID = 0;
|
||
Command.S5F3Command(item, null);
|
||
}
|
||
else
|
||
{
|
||
item.ALED = 0;
|
||
item.ALID = 0;
|
||
Command.S5F3Command(item, null);
|
||
}
|
||
}
|
||
|
||
public void S5F3BottonUpdate(string name)
|
||
{
|
||
if (this.ConnectStatus.Text != ConnectState.connected)
|
||
{
|
||
return;
|
||
}
|
||
Action act = delegate
|
||
{
|
||
this.S5F3.Text = name;
|
||
};
|
||
this.BeginInvoke(act);
|
||
}
|
||
|
||
private void S5F5_Click(object sender, EventArgs e)
|
||
{
|
||
if (this.ConnectStatus.Text != ConnectState.connected)
|
||
{
|
||
return;
|
||
}
|
||
Command.S5F5Command();
|
||
}
|
||
|
||
private void S7F19_Click(object sender, EventArgs e)
|
||
{
|
||
if (this.ConnectStatus.Text != ConnectState.connected)
|
||
{
|
||
return;
|
||
}
|
||
Command.S7F19Command();
|
||
}
|
||
|
||
private void S2F13_Click(object sender, EventArgs e)
|
||
{
|
||
if (this.ConnectStatus.Text != ConnectState.connected)
|
||
{
|
||
return;
|
||
}
|
||
Command.S2F13Command();
|
||
}
|
||
|
||
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
|
||
{
|
||
if (EquipmentStatus.EqConnectState.Equals(ConnectState.connected))
|
||
{
|
||
SimulatorInfo.Instance.Context.Close();
|
||
}
|
||
mq.Close();
|
||
EquipmentStatus.EapState = RunState.Stop;
|
||
}
|
||
|
||
private void Local_Click(object sender, EventArgs e)
|
||
{
|
||
if (this.ConnectStatus.Text != ConnectState.connected)
|
||
{
|
||
return;
|
||
}
|
||
try
|
||
{
|
||
S2F15Item item = new S2F15Item();
|
||
var content = Configuration.conf.commandContentCollection.findContent("S2F15_ControlState").content;
|
||
foreach (string value in content)
|
||
{
|
||
EC ec = new EC();
|
||
var ecv = Configuration.conf.sRDConfiguration.findEC(value);
|
||
ec.ECID = ecv.ecid;
|
||
item.ECs.Add(ec);
|
||
}
|
||
item.ECs[0].ECV = 3;
|
||
item.ECs[1].ECV = 1;
|
||
int tag = 1;
|
||
Command.S2F15Command(item, tag);
|
||
}
|
||
catch(Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
LoggerService.SYSLogger.Error(ex.Message);
|
||
}
|
||
}
|
||
|
||
private void Remote_Click(object sender, EventArgs e)
|
||
{
|
||
if (this.ConnectStatus.Text != ConnectState.connected)
|
||
{
|
||
return;
|
||
}
|
||
try
|
||
{
|
||
S2F15Item item = new S2F15Item();
|
||
var content = Configuration.conf.commandContentCollection.findContent("S2F15_ControlState").content;
|
||
foreach (string value in content)
|
||
{
|
||
EC ec = new EC();
|
||
var ecv = Configuration.conf.sRDConfiguration.findEC(value);
|
||
ec.ECID = ecv.ecid;
|
||
item.ECs.Add(ec);
|
||
}
|
||
item.ECs[0].ECV = 4;
|
||
item.ECs[1].ECV = 1;
|
||
int tag = 1;
|
||
Command.S2F15Command(item, tag);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
LoggerService.SYSLogger.Error(ex.Message);
|
||
}
|
||
}
|
||
|
||
#region log详细内容展示
|
||
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
|
||
{
|
||
string logDate, logFilePath, key,eventDisc,eventName;
|
||
logDate = dataGridView1.CurrentRow.Cells[0].Value.ToString();
|
||
eventName = dataGridView1.CurrentRow.Cells[2].Value.ToString();
|
||
eventDisc = dataGridView1.CurrentRow.Cells[4].Value.ToString();
|
||
key = dataGridView1.CurrentRow.Cells[5].Value.ToString();
|
||
logFilePath = logDate;
|
||
logFilePath = Application.StartupPath + @"\logs\SECSLogger\INFO\" + logFilePath.Substring(0, 10) + ".log";
|
||
LogForm LF = new LogForm(logFilePath, eventName, logDate, eventDisc, key,"SECS");
|
||
Task.Run(() =>
|
||
{
|
||
try
|
||
{
|
||
LF.ShowDialog();
|
||
}
|
||
finally
|
||
{
|
||
LF.Dispose();
|
||
}
|
||
});
|
||
|
||
}
|
||
|
||
private void dataGridView2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
|
||
{
|
||
string logDate, logFilePath, key, eventDisc, eventName;
|
||
logDate = dataGridView2.CurrentRow.Cells[0].Value.ToString();
|
||
eventName = dataGridView2.CurrentRow.Cells[2].Value.ToString();
|
||
key = dataGridView2.CurrentRow.Cells[4].Value.ToString();
|
||
eventDisc = dataGridView2.CurrentRow.Cells[3].Value.ToString();
|
||
logFilePath = logDate;
|
||
logFilePath = Application.StartupPath + @"\logs\MQLogger\INFO\" + logFilePath.Substring(0, 10) + ".log";
|
||
LogForm LF = new LogForm(logFilePath, eventName, logDate, eventDisc,key,"MQ");
|
||
Task.Run(() =>
|
||
{
|
||
try
|
||
{
|
||
LF.ShowDialog();
|
||
}
|
||
finally
|
||
{
|
||
LF.Dispose();
|
||
}
|
||
});
|
||
}
|
||
#endregion
|
||
|
||
#region IP地址合法性检测
|
||
public static bool JudgeIPFormat(string strJudgeString)
|
||
{
|
||
bool blnTest;
|
||
bool _Result = true;
|
||
|
||
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$");
|
||
blnTest = regex.IsMatch(strJudgeString);
|
||
if (blnTest == true)
|
||
{
|
||
string[] strTemp = strJudgeString.Split(new char[] { '.' }); // textBox1.Text.Split(new char[] { ‘.’ });
|
||
int nDotCount = strTemp.Length - 1; //字符串中.的数量,若.的数量小于3,则是非法的ip地址
|
||
if (3 == nDotCount)//判断字符串中.的数量
|
||
{
|
||
for (int i = 0; i < strTemp.Length; i++)
|
||
{
|
||
if (Convert.ToInt32(strTemp[i]) > 255)
|
||
{ //大于255则提示,不符合IP格式
|
||
_Result = false;
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
_Result = false;
|
||
}
|
||
}
|
||
else
|
||
{//输入非数字则提示,不符合IP格式
|
||
_Result = false;
|
||
}
|
||
return _Result;
|
||
}
|
||
#endregion
|
||
|
||
#region deviceID 和 Port 合法性检测
|
||
public static bool JudgePortFormat(string strJudgeString)
|
||
{
|
||
bool blnTest;
|
||
|
||
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("^[0-9]{1,5}$");
|
||
blnTest = regex.IsMatch(strJudgeString);
|
||
return blnTest;
|
||
}
|
||
#endregion
|
||
|
||
#region 设备连接初始化
|
||
public void ConnectThread()
|
||
{
|
||
try
|
||
{
|
||
AppSettingsConf.Conf = JsonConvert.DeserializeObject<AppSettingsConf>(File.ReadAllText(ReCache.EapSettingsFilePath),
|
||
new JsonSerializerSettings() { StringEscapeHandling = StringEscapeHandling.EscapeNonAscii });
|
||
SimulatorInfo.Instance.Scenarios = Configuration.conf.scenarioCollection;
|
||
var eqpid = Configuration.conf.connectSetting.name;
|
||
SimulatorInfo.Instance.Context = new SECSContext(eqpid, (short)Convert.ToInt32(editDeviceID.Text), editIPaddress.Text, Convert.ToInt32(editPort.Text));
|
||
SimulatorInfo.Instance.Context.Open();
|
||
}
|
||
catch (Exception ee)
|
||
{
|
||
LoggerService.SECSLogger.Error($"程序错误:{ee.Message}");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 界面log显示
|
||
public void addlog(string dir, string SxFy, string trans, string name,string key)
|
||
{
|
||
string time = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
Action act = delegate
|
||
{
|
||
int index = this.dataGridView1.Rows.Add();
|
||
this.dataGridView1.Rows[index].Cells[0].Value = time;
|
||
this.dataGridView1.Rows[index].Cells[1].Value = dir;
|
||
this.dataGridView1.Rows[index].Cells[2].Value = SxFy;
|
||
this.dataGridView1.Rows[index].Cells[3].Value = trans;
|
||
this.dataGridView1.Rows[index].Cells[4].Value = name;
|
||
this.dataGridView1.Rows[index].Cells[5].Value = key;
|
||
this.dataGridView1.FirstDisplayedScrollingRowIndex = this.dataGridView1.Rows.Count - 1;
|
||
//界面日志只保留一千行
|
||
if(this.dataGridView1.Rows.Count > 1000)
|
||
{
|
||
this.dataGridView1.Rows.RemoveAt(0);
|
||
}
|
||
};
|
||
this.BeginInvoke(act);
|
||
}
|
||
|
||
public void addMQlog(string time, string dir, string trans, string name,string transactionId)
|
||
{
|
||
Action act = delegate
|
||
{
|
||
int index = this.dataGridView2.Rows.Add();
|
||
this.dataGridView2.Rows[index].Cells[0].Value = time;
|
||
this.dataGridView2.Rows[index].Cells[1].Value = dir;
|
||
this.dataGridView2.Rows[index].Cells[2].Value = trans;
|
||
this.dataGridView2.Rows[index].Cells[3].Value = name;
|
||
this.dataGridView2.Rows[index].Cells[4].Value = transactionId;
|
||
this.dataGridView2.FirstDisplayedScrollingRowIndex = this.dataGridView2.Rows.Count - 1;
|
||
//界面日志只保留一千行
|
||
if (this.dataGridView2.Rows.Count > 1000)
|
||
{
|
||
this.dataGridView2.Rows.RemoveAt(0);
|
||
}
|
||
};
|
||
this.BeginInvoke(act);
|
||
}
|
||
#endregion
|
||
|
||
#region 界面连接状态显示
|
||
public void ConnectStatusSet(string value)
|
||
{
|
||
Action act = delegate
|
||
{
|
||
this.ConnectStatus.Text = value;
|
||
};
|
||
this.BeginInvoke(act);
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region MQ界面连接状态显示
|
||
public void MQConnectStatusSet(string value)
|
||
{
|
||
Action act = delegate
|
||
{
|
||
this.MQStatus.Text = value;
|
||
};
|
||
this.BeginInvoke(act);
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 外部调用EAP界面各按钮接口
|
||
public void pushBotton(string buttonName)
|
||
{
|
||
Button button = (Button)this.Controls.Find(buttonName, true)[0];
|
||
Action act = delegate
|
||
{
|
||
button.PerformClick();
|
||
};
|
||
this.BeginInvoke(act);
|
||
}
|
||
#endregion
|
||
}
|
||
|
||
public static class Global
|
||
{
|
||
public static MainForm MF{get;set;}
|
||
}
|
||
}
|