using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using DotNetSAPI; using System.ServiceModel; using System.ServiceModel.Channels; using System.Runtime.Serialization; using CommonLib; using System.Speech.Synthesis; namespace wcfControlMonitorClient { public partial class FrmClient : Form, WcfControlMonitor.IControlMonitorCallback { //protected override CreateParams CreateParams //{ // get // { // CreateParams cp = base.CreateParams; // cp.ExStyle |= 0x02000000;//WS_CLIPCHILDREN 两个窗体叠加时,重叠部分不重绘 // return cp; // } //} Model.MDevice devinfo; int PLC = 0; DateTime lastupdate = DateTime.Now; FrmDeviceStatus _deviceStatus = new FrmDeviceStatus(); FrmWorkstationStatus _worstation = new FrmWorkstationStatus(); StringBuilder sql = new StringBuilder(); DBFactory.DBOperator dbo = CStaticClass.dbo; DBFactory.DBOperator dbo1 = new DBFactory.DBOperator(); //public SpVoice Voice = new SpVoice(); public Model.MDevice[] devlist; public int devsn = 0; private static FrmClient _formInstance; public static FrmClient FormInstance { get { if (_formInstance == null) { _formInstance = new FrmClient(); } return _formInstance; } set { _formInstance = value; } } public FrmClient() { CommonClassLib.CCarryConvert.WriteDarkCasket("Login", "操作日志", "FrmClient()", "", ""); InitializeComponent(); try { //初始化管理和调度交互状态 Model.CGetInfo.AddIOControlStatus(); Model.CGetInfo.AddDeviceKindInfo(); Model.CGetInfo.AddDeviceInfo(); Model.CGetInfo.AddDeviceErrors(); devlist = Model.CGetInfo.DeviceInfo.Values.ToArray(); DataView dv0 = dbo.ExceSQL("SELECT distinct F_S7Connection,F_DBWGetLength FROM T_Base_Device WHERE (F_DeviceKindIndex = 28)").Tables[0].DefaultView; if (dv0.Count > 0) { for (int i = 0; i < dv0.Count; i++) { if (CStaticClass.MutiS7ConnReturns.ContainsKey(dv0[i]["F_S7Connection"].ToString()) == false) { CStaticClass.MutiS7ConnReturns.Add(dv0[i]["F_S7Connection"].ToString(), new int[Convert.ToInt32(dv0[i]["F_DBWGetLength"])]); } } } CStaticClass.SystemName = CommonClassLib.AppSettings.GetValue("SystemName"); this.notifyIcon1.Text = CStaticClass.SystemName; this.Text = CStaticClass.SystemName; OPCClient.CCommonOPCClient.Hostname = CommonClassLib.AppSettings.GetValue("HostName"); OPCClient.CCommonOPCClient.ProgID = CommonClassLib.AppSettings.GetValue("OPCProgID"); #region 客户端订阅初始化20120428 DataView dvx; DataView dv = dbo.ExceSQL("SELECT F_ReadSubscription, F_DeviceIndex, F_DBW2Address, F_DBWGetLength, F_S7Connection, F_SplitByte FROM T_Base_Device WHERE (F_ReadSubscription IS NOT NULL) AND (F_ReadSubscription =F_DeviceIndex) ORDER BY F_ReadSubscription").Tables[0].DefaultView; for (int i = 0; i < dv.Count; i++) { dvx = dbo.ExceSQL(string.Format("SELECT (MAX(F_DBW2Address) + F_DBWGetLength) AS maxdbw2 FROM T_Base_Device WHERE (F_ReadSubscription = '{0}') AND (F_DeviceKindIndex <> 33) GROUP BY F_DBWGetLength ORDER BY maxdbw2 DESC", dv[i]["F_ReadSubscription"])).Tables[0].DefaultView; if (dvx.Count > 0) { if (CStaticClass.MutiReadSubscriptionDeviceStateReturns.ContainsKey(dv[i]["F_ReadSubscription"].ToString()) == false) { CStaticClass.MutiReadSubscriptionDeviceStateReturns.Add(dv[i]["F_ReadSubscription"].ToString(), new int[Convert.ToInt32(dvx[0]["maxdbw2"]) - Convert.ToInt32(dv[i]["F_DBW2Address"])]); } } } dv = dbo.ExceSQL("SELECT F_ReadSubscription, F_DeviceIndex, F_DBW2Address, F_DBWGetLength, F_S7Connection, F_SplitByte FROM T_Base_Device WHERE (F_ReadSubscription IS NOT NULL) AND (F_DeviceKindIndex=33) ORDER BY F_ReadSubscription").Tables[0].DefaultView; for (int i = 0; i < dv.Count; i++) { if (CStaticClass.MutiReadSubscriptionDeviceSplitReturns.ContainsKey(dv[i]["F_ReadSubscription"].ToString()) == false) { CStaticClass.MutiReadSubscriptionDeviceSplitReturns.Add(dv[i]["F_ReadSubscription"].ToString(), new int[Convert.ToInt32(dv[i]["F_DBWGetLength"])]); } } CommonClassLib.CCarryConvert.WriteDarkCasket("Login", "操作日志", "FrmClient()-准备CreateSubscriptionGroup", "", ""); OPCClient.CCommonOPCClient.CreateSubscriptionGroup(); CommonClassLib.CCarryConvert.WriteDarkCasket("Login", "操作日志", "FrmClient()-CreateSubscriptionGroup", "", ""); OPCClient.CCommonOPCClient.UpdateDB += new CUpdateDBEventHandler(CCommon_UpdateDB); if (OPCClient.CCommonOPCClient.OpcError != null) { MessageBox.Show(OPCClient.CCommonOPCClient.OpcError); } } catch (Exception ex) { CommonClassLib.CCarryConvert.WriteDarkCasket("Login", "操作日志", "FrmClient()-catch", ex.StackTrace, ex.Message); throw ex; } #endregion } /// /// 客户端事件响应20120428 /// /// /// void CCommon_UpdateDB(object sender, CUpdateDBChangeEventArgs e) { if (CStaticClass.WCFOK == true && !CStaticClass.client.IsDisposed) { string errtext = string.Empty; this.BeginInvoke(new MethodInvoker(delegate() { DealWithDeviceState(out errtext, e.Deviceindex, e.SplitbyteValue, e.Devicestates); })); } } /// /// 客户端事件响应处理函数20120428 /// /// /// /// /// /// bool DealWithDeviceState(out string errtext, int deviceindex, byte[] splitbyteValue, byte[] devicestates) {//deviceindex设备组代表的订阅,首先把设备组按照F_DBW2Address由小到大排序,拆分到具体每个设备 if ((splitbyteValue == null) && (devicestates == null)) {//20120420 errtext = OPCClient.CCommonOPCClient.OpcError; SendMessage(string.Format("tsStatus@FrmClient.DealWithDeviceState时,订阅组:{0}发生异常,{1}!", deviceindex, errtext)); return false; } errtext = string.Empty; DataView dv = new DataView(); DataView dvsp = new DataView(); DataView dvbc = new DataView(); int devicebegin = 0; int DBW2Addressbegin = 0; int SplitBytebegin = 0; string S7Connection = string.Empty; int SplitByteLength = 0; try { StringBuilder sql = new StringBuilder(string.Format("SELECT F_DeviceKindIndex, F_DeviceIndex, F_DBW2Address, F_DBWGetLength, F_SplitByte,F_S7Connection FROM T_Base_Device WHERE (F_DeviceKindIndex <>33) AND (F_ReadSubscription = '{0}') AND (F_DBW2Address IS NOT NULL) ORDER BY F_DBW2Address", deviceindex)); dv = dbo1.ExceSQL(sql.ToString()).Tables[0].DefaultView; if (dv.Count > 0) { devicebegin = Convert.ToInt32(dv[0]["F_DeviceIndex"]); DBW2Addressbegin = Convert.ToInt32(dv[0]["F_DBW2Address"]); if (Convert.ToInt32(dv[0]["F_DeviceKindIndex"]) != 7 && Convert.ToInt32(dv[0]["F_DeviceKindIndex"]) != 24 && Convert.ToInt32(dv[0]["F_DeviceKindIndex"]) != 35 && Convert.ToInt32(dv[0]["F_DeviceKindIndex"]) != 8 && Convert.ToInt32(dv[0]["F_DeviceKindIndex"]) != 40) { SplitBytebegin = Convert.ToInt32(dv[0]["F_SplitByte"]); // 光电开关起始 } S7Connection = dv[0]["F_S7Connection"].ToString(); sql = new StringBuilder(string.Format("SELECT F_DeviceIndex, F_DBW2Address, F_DBWGetLength, F_SplitByte,F_S7Connection FROM T_Base_Device WHERE (F_DeviceKindIndex =33) AND (F_ReadSubscription = '{0}') ORDER BY F_DBW2Address", deviceindex)); dvsp = dbo1.ExceSQL(sql.ToString()).Tables[0].DefaultView; if (dvsp.Count > 0) { SplitByteLength = Convert.ToInt32(dvsp[0]["F_DBWGetLength"]); } } else { sql = new StringBuilder(string.Format("SELECT F_DeviceIndex, F_DBW2Address, F_DBWGetLength, F_SplitByte,F_S7Connection FROM T_Base_Device WHERE (F_DeviceIndex = '{0}') AND (F_DBW2Address IS NOT NULL) ORDER BY F_DBW2Address", deviceindex)); dv = dbo1.ExceSQL(sql.ToString()).Tables[0].DefaultView; if (dv.Count > 0) { devicebegin = Convert.ToInt32(dv[0]["F_DeviceIndex"]); DBW2Addressbegin = Convert.ToInt32(dv[0]["F_DBW2Address"]); SplitBytebegin = Convert.ToInt32(dv[0]["F_SplitByte"]); S7Connection = dv[0]["F_S7Connection"].ToString(); sql = new StringBuilder(string.Format("SELECT F_DeviceIndex, F_DBW2Address, F_DBWGetLength, F_SplitByte,F_S7Connection FROM T_Base_Device WHERE (F_DeviceKindIndex =33) AND (F_ReadSubscription = '{0}') ORDER BY F_DBW2Address", deviceindex)); dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView; if (dv.Count > 0) { SplitByteLength = Convert.ToInt32(dv[0]["F_DBWGetLength"]); } } else {//20120420 errtext = string.Format("FrmClient.DealWithDeviceState时,订阅组:{0}对应的设备索引不存在!", deviceindex); SendMessage(string.Format("tsStatus@FrmClient.DealWithDeviceState时,订阅组:{0}对应的设备索引不存在!", deviceindex)); return false; } } if (splitbyteValue != null) { Array.Copy(splitbyteValue, CStaticClass.MutiReadSubscriptionDeviceSplitReturns[deviceindex.ToString()], splitbyteValue.Length); dvsp = dbo1.ExceSQL(string.Format("SELECT F_DeviceIndex, F_DBW2Address, F_DBWGetLength, F_SplitByte FROM T_Base_Device WHERE (F_DeviceKindIndex <>31) AND(F_DeviceKindIndex <>33) AND (F_S7Connection = '{0}') AND (F_DBW2Address IS NOT NULL and F_SplitByte IS NOT NULL and F_SplitByte >={1} and F_SplitByte <{1}+{2}) ORDER BY F_DBW2Address", S7Connection, SplitBytebegin, SplitByteLength)).Tables[0].DefaultView; for (int i = 0; i < dvsp.Count; i++) { devinfo = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(dvsp[i]["F_DeviceIndex"])); int delta = Convert.ToInt32(dvsp[i]["F_SplitByte"]) - SplitBytebegin; if (delta < 0) continue; devinfo.SplitByte_0 = (splitbyteValue[delta] & 1) == 1 ? 1 : 0; devinfo.SplitByte_1 = (splitbyteValue[delta] & 2) == 2 ? 1 : 0; devinfo.SplitByte_2 = (splitbyteValue[delta] & 4) == 4 ? 1 : 0; devinfo.SplitByte_3 = (splitbyteValue[delta] & 8) == 8 ? 1 : 0; devinfo.SplitByte_4 = (splitbyteValue[delta] & 16) == 16 ? 1 : 0; devinfo.SplitByte_5 = (splitbyteValue[delta] & 32) == 32 ? 1 : 0; devinfo.SplitByte_6 = (splitbyteValue[delta] & 64) == 64 ? 1 : 0; devinfo.SplitByte_7 = (splitbyteValue[delta] & 128) == 128 ? 1 : 0; //if (devinfo.DeviceIndex == 12103 && devinfo.SplitByte_6 == 0) //{ // label35.Text = "否"; //} //if (devinfo.DeviceIndex == 12103 && devinfo.SplitByte_6 == 1) //{ // label35.Text = "是"; //} //if (devinfo.DeviceIndex == 12101 && devinfo.SplitByte_6 == 0) //{ // label36.Text = "否"; //} //if (devinfo.DeviceIndex == 12101 && devinfo.SplitByte_6 == 1) //{ // label36.Text = "是"; //} Model.CGetInfo.SetDeviceInfo(devinfo); } } int tempdb2addr = 0; StringBuilder sss = new StringBuilder(); StringBuilder barcode = new StringBuilder(); if (devicestates != null) { Array.Copy(devicestates, CStaticClass.MutiReadSubscriptionDeviceStateReturns[deviceindex.ToString()], devicestates.Length); for (int di = 0; di < dv.Count; di++) { int devidx = Convert.ToInt32(dv[di]["F_DeviceIndex"]); devinfo = Model.CGetInfo.GetDeviceInfo(devidx); int taskindex = 0; int state = 0; //以下代码重新开发CStaticClass.MutiS7ConnReturns改为按照F_ReadSubscription if (devinfo.Dbw2Getlength > 3) { tempdb2addr = Convert.ToInt32(dv[di]["F_DBW2Address"]) - DBW2Addressbegin; taskindex = (devicestates[tempdb2addr + 2] << 8) + devicestates[tempdb2addr + 3]; switch (devinfo.DeviceKind) { case 1: devinfo.SplitByte_0 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 7] & 1) == 1 ? 1 : 0; devinfo.SplitByte_1 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 7] & 2) == 2 ? 1 : 0; devinfo.SplitByte_2 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 7] & 4) == 4 ? 1 : 0; devinfo.SplitByte_3 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 7] & 8) == 8 ? 1 : 0; devinfo.SplitByte_4 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 7] & 16) == 16 ? 1 : 0; devinfo.SplitByte_5 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 7] & 32) == 32 ? 1 : 0; devinfo.SplitByte_6 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 7] & 64) == 64 ? 1 : 0; devinfo.SplitByte_7 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 7] & 128) == 128 ? 1 : 0; break; case 2: if (devinfo.IfCorrelDoubleFork == "1") { devinfo.SplitByte_0 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 4] & 1) == 1 ? 1 : 0; devinfo.SplitByte_1 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 4] & 2) == 2 ? 1 : 0; devinfo.SplitByte_2 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 4] & 4) == 4 ? 1 : 0; devinfo.SplitByte_3 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 4] & 8) == 8 ? 1 : 0; devinfo.SplitByte_4 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 4] & 16) == 16 ? 1 : 0; devinfo.SplitByte_5 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 4] & 32) == 32 ? 1 : 0; devinfo.SplitByte_6 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 4] & 64) == 64 ? 1 : 0; devinfo.SplitByte_7 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 4] & 128) == 128 ? 1 : 0; } else { devinfo.SplitByte_0 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 6] & 1) == 1 ? 1 : 0; devinfo.SplitByte_1 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 6] & 2) == 2 ? 1 : 0; devinfo.SplitByte_2 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 6] & 4) == 4 ? 1 : 0; devinfo.SplitByte_3 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 6] & 8) == 8 ? 1 : 0; devinfo.SplitByte_4 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 6] & 16) == 16 ? 1 : 0; devinfo.SplitByte_5 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 6] & 32) == 32 ? 1 : 0; devinfo.SplitByte_6 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 6] & 64) == 64 ? 1 : 0; devinfo.SplitByte_7 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 6] & 128) == 128 ? 1 : 0; //if (devinfo.DeviceIndex == 12103 && devinfo.SplitByte_6 == 0) //{ // label35.Text = "否"; //} //if (devinfo.DeviceIndex == 12103 && devinfo.SplitByte_6 == 1) //{ // label35.Text = "是"; //} //if (devinfo.DeviceIndex == 12101 && devinfo.SplitByte_6 == 0) //{ // label36.Text = "否"; //} //if (devinfo.DeviceIndex == 12101 && devinfo.SplitByte_6 == 1) //{ // label36.Text = "是"; //} } break; case 4: devinfo.SplitByte_0 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 12] & 1) == 1 ? 1 : 0; devinfo.SplitByte_1 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 12] & 2) == 2 ? 1 : 0; devinfo.SplitByte_2 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 12] & 4) == 4 ? 1 : 0; devinfo.SplitByte_3 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 12] & 8) == 8 ? 1 : 0; devinfo.SplitByte_4 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 12] & 16) == 16 ? 1 : 0; devinfo.SplitByte_5 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 12] & 32) == 32 ? 1 : 0; devinfo.SplitByte_6 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 12] & 64) == 64 ? 1 : 0; devinfo.SplitByte_7 = (devicestates[tempdb2addr + (int)devinfo.Dbw2Getlength - 12] & 128) == 128 ? 1 : 0; break; default: break; } if (devinfo.Dbw2Getlength > 1) { state = devicestates[tempdb2addr + 1]; } } if (devinfo.DeviceIndex == 888) { devinfo.XCoor = devicestates[tempdb2addr + 0]; } int devkind = devinfo.DeviceKind; if (state >= 30) { devinfo.ErrorCode = state; devinfo.RunState = 2; } else {//0,1,2 if (state == 2) { devinfo.RunState = 5; } else { devinfo.RunState = state; } devinfo.ErrorCode = state; } devinfo.TaskNo = taskindex; if ((devinfo.DeviceKind == 1) || (devinfo.DeviceKind == 4)) { devinfo.XCoor = devicestates[tempdb2addr + 7] + (devicestates[tempdb2addr + 6] << 8) + (devicestates[tempdb2addr + 5] << 16) + (devicestates[tempdb2addr + 4] << 32);//X坐标 //devinfo.XCoor = devicestates[tempdb2addr + 12] ;//X坐标 if (devinfo.DeviceKind == 1) { devinfo.YCoor = devicestates[tempdb2addr + 11] + (devicestates[tempdb2addr + 10] << 8) + (devicestates[tempdb2addr + 9] << 16) + (devicestates[tempdb2addr + 8] << 32);//Y坐标 //devinfo.YCoor = devicestates[tempdb2addr + 13] ;//Y坐标 } } Model.CGetInfo.SetDeviceInfo(devinfo); } } return true; } catch (Exception ex) { SendMessage(string.Format("tsStatus@FrmClient.DealWithDeviceState时{0}", ex.StackTrace + ex.Message)); errtext = string.Format("tsStatus@FrmClient.DealWithDeviceState时{0}", ex.StackTrace + ex.Message); return false; } finally { dv.Dispose(); dvbc.Dispose(); dvsp.Dispose(); } } #region wcf回调 public void SendDeviceInfo(Model.MDevice devinfo) { try { if (CStaticClass.WCFOK == false) return; if (this.IsDisposed) return; Model.CGetInfo.SetDeviceInfo(devinfo); } catch (Exception ex) { throw ex; } } public IAsyncResult BeginSendDeviceInfo(Model.MDevice devinfo, AsyncCallback acb, object state) { return state as IAsyncResult; } public void EndSendDeviceInfo(IAsyncResult ar) { } public IAsyncResult BeginSendMessage(string message, AsyncCallback acb, object state) { return state as IAsyncResult; } public void EndSendMessage(IAsyncResult ar) { } public void SendMessage(string message) { char[] cc = new char[1] { '@' }; string[] strsplit = message.Split(cc); if (strsplit.GetLength(0) > 1) {//更新指定控件 if (strsplit[0] == "tsStatus") { tsStatus.Text = strsplit[1]; } else if (strsplit[0] == "notifyIcon1") { this.BeginInvoke(new MethodInvoker(delegate() { notifyIcon1.BalloonTipText = strsplit[1]; notifyIcon1.ShowBalloonTip(10000, "警告:", strsplit[1], ToolTipIcon.Warning); })); } } //else //{//刷新数据显示 // this.dgvManager.BeginInvoke(new MethodInvoker(delegate() // { // sql.Remove(0, sql.Length); // sql.Append("select * from V_Manage_Task where ").Append(CStaticClass.Manstatus); // this.dgvManager.DataSource = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView; // })); // this.dgvMonitor.BeginInvoke(new MethodInvoker(delegate() // { // sql.Remove(0, sql.Length); // sql.Append("select * from V_Monitor_Task where ").Append(CStaticClass.Monstatus).Append(" order by 设备指令索引 asc "); // this.dgvMonitor.DataSource = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView; // })); //} } //20140824richard public void SendOrderMessage(Model.MonitorInfo mi) { this.BeginInvoke(new MethodInvoker(delegate() { try { if (CStaticClass.UserID.ToUpper() == "SA") { CStaticClass.Order = mi.Orderturnon; CStaticClass.ObtainManageTask = mi.Obtaintask; if (CStaticClass.Order == true) { ClickOrderTurnOn(); } else { ClickOrderTurnOff(); } if (CStaticClass.ObtainManageTask == true) { ClickObtainTask(); } else { ClickStopObtainTask(); } } } catch (Exception ex) { if (tsStatus.Text.IndexOf("接收【获取任务和命令开关】广播时:" + ex.Message) < 0) { tsStatus.ForeColor = Color.Red; this.tsStatus.Text += "接收【获取任务和命令开关】广播时:" + ex.Message; } } })); } public IAsyncResult BeginSendOrderMessage(Model.MonitorInfo mi, AsyncCallback callBack, object state) { return state as IAsyncResult; } public void EndSendOrderMessage(IAsyncResult ar) { } //20140824richard #endregion private void Client_FormClosing(object sender, FormClosingEventArgs e) { if (MessageBox.Show("您确认要关闭" + CStaticClass.SystemName + "吗?", "操作提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK) { e.Cancel = true; return; } ClientLogout(); OPCClient.CCommonOPCClient.DisConnectOPCServer();//20120428 CommonClassLib.AppSettings.dbo.Close(); CommonClassLib.AppSettings.dbo1.Close(); CommonClassLib.AppSettings.dboM.Close(); FrmLogin.FormInstance.Close(); } void LogCallBack(IAsyncResult ar) { try { string strErr = string.Empty; if (CStaticClass.WcfControl.EndLogoutClient(out strErr, ar) == true) { //if (ar.AsyncState.ToString() == "RegisterClientCallBack") //{ // CStaticClass.WcfControl.BeginRegisterClient(CStaticClass.UserID, "客户端:" + CStaticClass.ClientID + ",用户名:" + CStaticClass.UserID, new AsyncCallback(RegisterClientCallBack), null); // return; //} } CStaticClass.WCFOK = false; } catch (Exception ex) { MessageBox.Show(ex.Message, "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } public void ClientLogout() { try { string errtext = string.Empty; CStaticClass.WcfControl.BeginLogoutClient("客户端:" + CStaticClass.ClientID + ",用户名:" + CStaticClass.UserID, new AsyncCallback(LogCallBack), ""); } catch (Exception ex) { MessageBox.Show(ex.Message, "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void toolStripMenuItem1_Click(object sender, EventArgs e) { FrmHandCommand.FormInstance.Show(); } private void toolStripMenuItem2_Click(object sender, EventArgs e) { FrmHandAssociateCommand.FormInstance.Show(); } private void MIManageShow_Click(object sender, EventArgs e) { FrmAutoCommand.FormInstance.Show(); } private void MIAltMonitor_Click(object sender, EventArgs e) { FrmModifyMonitor.FormInstance.Show(); } private void MIAltManange_Click(object sender, EventArgs e) { FrmModifyManage.FormInstance.Show(); } // SpeechSynthesizer synth = new SpeechSynthesizer(); private void notifyIcon1_BalloonTipShown(object sender, EventArgs e) { try { //SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync; //SpVoice Voice = new SpVoice(); //Voice.Speak(notifyIcon1.BalloonTipText, SpFlags); synth.Speak(notifyIcon1.BalloonTipText); } catch { } } private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) { if (CStaticClass.WCFOK == true) { this.TopMost = true; this.Visible = true; this.TopMost = false; this.WindowState = FormWindowState.Maximized; } } private void tsStatus_TextChanged(object sender, EventArgs e) { } private void TSMrefresh_Click(object sender, EventArgs e) { sql.Remove(0, sql.Length); sql.Append("select * from V_Manage_Task where ").Append(CStaticClass.Manstatus); this.dgvManager.DataSource = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView; } private void TSMMonitorRefresh_Click(object sender, EventArgs e) { sql.Remove(0, sql.Length); sql.Append("select * from V_Monitor_Task where ").Append(CStaticClass.Monstatus).Append(" order by 设备指令索引 asc "); this.dgvMonitor.DataSource = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView; } private void rbmanAll_CheckedChanged(object sender, EventArgs e) { RadioButton rb = (RadioButton)sender; if (rb.Checked == true) { CStaticClass.Manstatus = " F_Status>=-1 "; } TSMrefresh_Click(sender, e); } private void rbmanRun_CheckedChanged(object sender, EventArgs e) { RadioButton rb = (RadioButton)sender; if (rb.Checked == true) { CStaticClass.Manstatus = " F_Status=1 "; } TSMrefresh_Click(sender, e); } private void rbmanWait_CheckedChanged(object sender, EventArgs e) { RadioButton rb = (RadioButton)sender; if (rb.Checked == true) { CStaticClass.Manstatus = " F_Status=0 "; } TSMrefresh_Click(sender, e); } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { try { string df = ""; switch (this.comboBox1.Text) { case "条码": df = "条码"; break; case "调度任务索引": df = "调度任务索引"; break; case "设备指令索引": df = "设备指令索引"; break; default: return; } //20100208 sql.Remove(0, sql.Length); sql.Append("select distinct " + df + " from V_Monitor_Task "); DataView dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView; if (dv.Count > 0) { this.textBox1.DataSource = null; this.textBox1.DisplayMember = df; this.textBox1.ValueMember = df; this.textBox1.DataSource = dv; } } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void button1_Click(object sender, EventArgs e) { try { if (this.textBox1.Text.Trim().Length == 0) return; if (this.comboBox1.Text == "条码") { CStaticClass.Monstatus = " 条码='" + this.textBox1.Text.Trim() + "' "; } if (this.comboBox1.Text == "调度任务索引") { CStaticClass.Monstatus = " 调度任务索引='" + this.textBox1.Text.Trim() + "' "; } if (this.comboBox1.Text == "设备指令索引") { CStaticClass.Monstatus = " 设备指令索引='" + this.textBox1.Text.Trim() + "' "; } TSMMonitorRefresh_Click(sender, e); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void button2_Click(object sender, EventArgs e) { CStaticClass.Monstatus = " F_Status>=-1 "; TSMMonitorRefresh_Click(sender, e); } public void MonitorRefresh() {//20110222 sql.Remove(0, sql.Length); sql.Append("select * from V_Monitor_Task where ").Append(CStaticClass.Monstatus).Append(" order by 设备指令索引 asc "); this.dgvMonitor.DataSource = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView; } public void ManagerRefresh() {//20110222 sql.Remove(0, sql.Length); sql.Append("select * from V_Manage_Task where ").Append(CStaticClass.Manstatus); this.dgvManager.DataSource = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView; } void BuildHandTaskCallBack(IAsyncResult ar) { string errtext = string.Empty; if (CStaticClass.WcfControl.EndBuildHandTask(out errtext, ar) == false) { //MessageBox.Show(errtext, "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void Client_Load(object sender, EventArgs e) { try { //for (int i = 0; i <= 1000; i++) //{ // Model.HandTask ht = new Model.HandTask(11001, 1, 0, "0", "00-00-00", "-", 0, 0); // CStaticClass.WcfControl.BeginBuildHandTask(ht, new AsyncCallback(BuildHandTaskCallBack), ht); //} #region 添加输送机、穿梭车和堆垛机的单击事件 foreach (Control ctrl in this.plAccessorial1.Controls) { if (ctrl.GetType().ToString() == "System.Windows.Forms.Button") { ctrl.Click += new EventHandler(ctrl_Click); } if ((ctrl.GetType().ToString() == "System.Windows.Forms.Label") && (ctrl.Tag != null) && (ctrl.ForeColor == Color.Red)) { ctrl.Click += new EventHandler(ctrl_ClickLabel); } if ((ctrl.GetType().ToString() == "System.Windows.Forms.PictureBox") && (ctrl.Enabled == true) ) { if (ctrl.Name.IndexOf("pic_") < 0 && ctrl.Name.IndexOf("sto_") < 0) { ctrl.Click += new EventHandler(ctrl_ClickPic); } else { ctrl.Click += new EventHandler(ctrl_Click_1); ctrl.MouseEnter += new EventHandler(Btn_MouseEnter_Stop); } } } foreach (Control ctrl in this.plAccessorial2.Controls) { if (ctrl.GetType().ToString() == "System.Windows.Forms.Button") { ctrl.Click += new EventHandler(ctrl_Click); } if ((ctrl.GetType().ToString() == "System.Windows.Forms.Label") && (ctrl.Tag != null) && (ctrl.ForeColor == Color.Red)) { ctrl.Click += new EventHandler(ctrl_ClickLabel); } if ((ctrl.GetType().ToString() == "System.Windows.Forms.PictureBox") && (ctrl.Enabled == true)) { if (ctrl.Name.IndexOf("pic_") < 0 && ctrl.Name.IndexOf("sto_") < 0) { ctrl.Click += new EventHandler(ctrl_ClickPic); } else { ctrl.Click += new EventHandler(ctrl_Click_1); ctrl.MouseEnter += new EventHandler(Btn_MouseEnter_Stop); } } } foreach (Control ctrl in this.plAccessorial3.Controls) { if (ctrl.GetType().ToString() == "System.Windows.Forms.Button") { ctrl.Click += new EventHandler(ctrl_Click); } if ((ctrl.GetType().ToString() == "System.Windows.Forms.Label") && (ctrl.Tag != null) && (ctrl.ForeColor == Color.Red)) { ctrl.Click += new EventHandler(ctrl_ClickLabel); } if ((ctrl.GetType().ToString() == "System.Windows.Forms.PictureBox") && (ctrl.Enabled == true)) { if (ctrl.Name.IndexOf("pic_") < 0 && ctrl.Name.IndexOf("sto_") < 0) { ctrl.Click += new EventHandler(ctrl_ClickPic); } else { ctrl.Click += new EventHandler(ctrl_Click_1); ctrl.MouseEnter += new EventHandler(Btn_MouseEnter_Stop); } } } #endregion TSMrefresh_Click(sender, e); TSMMonitorRefresh_Click(sender, e); //this.MMmasterAdmin.Visible = true; //if (CStaticClass.UserID.ToUpper() == "SA") //{ // this.MMmasterAdmin.Visible = true; //} //else //{ // this.MMmasterAdmin.Visible = false; //} } catch (Exception ex) { throw (ex); } } private void MIDeviceState_Click(object sender, EventArgs e) { FrmDeviceInfoEdit.FormInstance.Show(); } private void MIRouteInfo_Click(object sender, EventArgs e) { FrmRouteEdit.FormInstance.Show(); } private void MIWaitTime_Click(object sender, EventArgs e) { FrmWaitTime.FormInstance.Show(); } private void MIMonitorConnectInfo_Click(object sender, EventArgs e) { FrmConnectDB.FormInstance.ConnectType = "Monitor"; FrmConnectDB.FormInstance.Show(); } private void MIIOStation_Click(object sender, EventArgs e) { FrmIOStation.FormInstance.Show(); } private void timer1_Tick(object sender, EventArgs e) { if (CStaticClass.WCFOK == false) { if (CStaticClass.WcfControl != null) { if (CStaticClass.WcfControl.State == CommunicationState.Faulted) { CStaticClass.WCFOK = false; ReConnectAndRegisterClient(); } } return; } try { if (label33.Text == "") { if (CStaticClass.UserID != "") label33.Text = dbo.GetSingle("select f_username from T_Base_Users where f_userid='" + CStaticClass.UserID + "'").ToString(); } //CStaticClass.WcfControl.BeginHeartbeat(new AsyncCallback(CallBackHeartbeat), null); if (CStaticClass.WcfControl != null) { if (CStaticClass.WcfControl.State == CommunicationState.Faulted) { CStaticClass.WCFOK = false; ReConnectAndRegisterClient(); } else { CStaticClass.WcfControl.BeginHeartbeat(new AsyncCallback(CallBackHeartbeat), null); } } //devsn++; //if (devsn < devlist.Count()) //{ // CStaticClass.WcfControl.BeginSendAllDeviceStates(devlist[devsn].DeviceIndex, new AsyncCallback(SendAllDeviceStatesCallback), null); //} //else //{ // devsn = 0; // CStaticClass.WcfControl.BeginSendAllDeviceStates(devlist[devsn].DeviceIndex, new AsyncCallback(SendAllDeviceStatesCallback), null); //} } catch (Exception ex) { if (tsStatus.Text.IndexOf("BeginHeartbeat时:" + ex.Message) < 0) { tsStatus.ForeColor = Color.Red; this.tsStatus.Text += "BeginHeartbeat时:" + ex.Message; } CStaticClass.WCFOK = false; ReConnectAndRegisterClient(); } } public void SendAllDeviceStatesCallback(IAsyncResult ar) { try { Model.MDevice dev; dev = CStaticClass.WcfControl.EndSendAllDeviceStates(ar); if (dev != null) { Model.CGetInfo.SetDeviceInfo(dev); } devsn++; if (devsn < devlist.Count()) { CStaticClass.WcfControl.BeginSendAllDeviceStates(devlist[devsn].DeviceIndex, new AsyncCallback(SendAllDeviceStatesCallback), null); } else { //devsn=0; //CStaticClass.WcfControl.BeginSendAllDeviceStates(devlist[devsn].DeviceIndex, new AsyncCallback(SendAllDeviceStatesCallback), null); } } catch (Exception ex) { this.BeginInvoke(new MethodInvoker(delegate() { if (tsStatus.Text.IndexOf("SendAllDeviceStatesCallback时:" + ex.Message) < 0) { tsStatus.ForeColor = Color.Red; this.tsStatus.Text += "SendAllDeviceStatesCallback时:" + ex.Message; } })); ReRegisterClient(); } } private void MITransportTimeout_Click(object sender, EventArgs e) { FrmTransportTimeout.FormInstance.Show(); } private void MISeeDarket_Click(object sender, EventArgs e) { FrmBrowseDarkCasket.FormInstance.Show(); } private void MIDarketFileSet_Click(object sender, EventArgs e) { FrmDarkCasket.FormInstance.Show(); } private void MIAutoTaskHistory_Click(object sender, EventArgs e) { FrmBrowseAotoTaskLog.FormInstance.Show(); } private void MIUserInfoManage_Click(object sender, EventArgs e) { FrmUsers.FormInstance.Show(); } private void MIAltPassword_Click(object sender, EventArgs e) { FrmPassword.FormInstance.Show(); } private void MMExitSystem_Click(object sender, EventArgs e) { this.Close(); } private void toolStripMenuItem10_Click(object sender, EventArgs e) { this.Visible = false; //this.WindowState = FormWindowState.Minimized; } private void toolStripMenuItem8_Click(object sender, EventArgs e) { this.Visible = true; this.WindowState = FormWindowState.Maximized; } private void toolStripMenuItem9_Click(object sender, EventArgs e) { this.Close(); } private void tabPage1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; g.TranslateTransform(this.tabPage1.AutoScrollPosition.X, this.tabPage1.AutoScrollPosition.Y); g.Dispose(); } private void ctrl_Click(object sender, EventArgs e) { Button btn = (Button)sender; _deviceStatus.DeviceIndex = Convert.ToInt32(btn.Tag); _deviceStatus.ShowDialog(); } private void ctrl_ClickTxt(object sender, EventArgs e) { TextBox txt = (TextBox)sender; _worstation.DeviceIndex = Convert.ToInt32(txt.Tag); _worstation.ShowDialog(); } private void ctrl_ClickPic(object sender, EventArgs e) { PictureBox btn = (PictureBox)sender; _deviceStatus.DeviceIndex = Convert.ToInt32(btn.Tag); _deviceStatus.ShowDialog(); } private void ctrl_ClickLabel(object sender, EventArgs e) { Label label = (Label)sender; _deviceStatus.DeviceIndex = Convert.ToInt32(label.Tag); _deviceStatus.ShowDialog(); } private void timerFlash_Tick(object sender, EventArgs e) { string[] DS; char[] dd = new char[1] { '.' }; Model.MDevice device; TimeSpan ts = DateTime.Now.TimeOfDay; DataView dv; DataView dvJY; this.SuspendLayout(); //richard.liu20140806 try { if (CStaticClass.WCFOK == true) { sql.Remove(0, sql.Length);//richard.liu20140806 sql.Append("SELECT F_DeviceIndex FROM T_Base_Device WHERE (F_LockedState = - 1) "); dvJY = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;//richard.liu20140806 Panel pp = plAccessorial1; PictureBox stack1 ; PictureBox stack2 ; PictureBox stack3 ; PictureBox stack4 ; //PictureBox RGV1=bt15001; //PictureBox RGV2 = bt15002; //PictureBox RGV3 = bt25001; TextBox tw1 ; TextBox tw2 ; TextBox tw3 ; TextBox tw4; stack1 = bt11001; stack2 = bt11002; stack3 = bt11003; stack4 = bt11004; tw1 = t11001; tw2 = t11002; tw3 = t11003; tw4 = t11004; pp = plAccessorial1; if (this.tabControl1.SelectedIndex == 1) { stack1 = bt110012; stack2 = bt110022; stack3 = bt110032; stack4 = bt110042; tw1 = t110012; tw2 = t110022; tw3 = t110032; tw4 = t110042; pp = plAccessorial2; } else if (this.tabControl1.SelectedIndex == 2) { stack1 = bt110013; stack2 = bt110023; stack3 = bt110033; stack4 = bt110043; tw1 = t110013; tw2 = t110023; tw3 = t110033; tw4 = t110043; pp = plAccessorial3; } #region 每层楼监视画面 #region 堆垛机、RGV foreach (PictureBox ctrl in pp.Controls.OfType()) { if (ctrl.Enabled == false ) continue; if (ctrl.Name.IndexOf("pic_") >= 0 || ctrl.Name.IndexOf("sto_") >= 0) continue; device = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(ctrl.Tag)); if (device == null) {//20100108 if (tsStatus.Text.IndexOf(ctrl.Name + "控件对应的设备索引:" + ctrl.Tag + "在数据库没有记录!") < 0) { tsStatus.ForeColor = Color.Red; this.tsStatus.Text += ctrl.Name + "控件对应的设备索引:" + ctrl.Tag + "在数据库没有记录!"; } continue; } #region 动画 if (device.DeviceKind == 1) {//堆垛机 if (pp== plAccessorial1) { switch (device.DeviceIndex) { case 11001: stack1.Location = new Point((int)(1104 - 20 * (device.XCoor - 1)), stack1.Location.Y); tw1.Location = new Point(stack1.Location.X + 10, tw1.Location.Y); break; case 11002: stack2.Location = new Point((int)(1104 - 20 * (device.XCoor - 1)), stack2.Location.Y); tw2.Location = new Point(stack2.Location.X + 10, tw2.Location.Y); break; case 11003: stack3.Location = new Point((int)(1104 - 15 * (device.XCoor - 1)), stack3.Location.Y); tw3.Location = new Point(stack3.Location.X + 10, tw3.Location.Y); break; case 11004: stack4.Location = new Point((int)(1104 - 15 * (device.XCoor - 1)), stack4.Location.Y); tw4.Location = new Point(stack4.Location.X + 10, tw4.Location.Y); break; } } if (pp == plAccessorial2) { switch (device.DeviceIndex) { case 11001: stack1.Location = new Point((int)(1192 - 20 * (device.XCoor - 1)), stack1.Location.Y); tw1.Location = new Point(stack1.Location.X + 10, tw1.Location.Y); break; case 11002: stack2.Location = new Point((int)(1192 - 20 * (device.XCoor - 1)), stack2.Location.Y); tw2.Location = new Point(stack2.Location.X + 10, tw2.Location.Y); break; case 11003: stack3.Location = new Point((int)(1192 - 15 * (device.XCoor - 1)), stack3.Location.Y); tw3.Location = new Point(stack3.Location.X + 10, tw3.Location.Y); break; case 11004: stack4.Location = new Point((int)(1192 - 15 * (device.XCoor - 1)), stack4.Location.Y); tw4.Location = new Point(stack4.Location.X + 10, tw4.Location.Y); break; } } if (pp == plAccessorial3) { switch (device.DeviceIndex) { case 11001: stack1.Location = new Point((int)(1198 - 20 * (device.XCoor - 1)), stack1.Location.Y); tw1.Location = new Point(stack1.Location.X + 10, tw1.Location.Y); break; case 11002: stack2.Location = new Point((int)(1198 - 20 * (device.XCoor - 1)), stack2.Location.Y); tw2.Location = new Point(stack2.Location.X + 10, tw2.Location.Y); break; case 11003: stack3.Location = new Point((int)(1198 - 15 * (device.XCoor - 1)), stack3.Location.Y); tw3.Location = new Point(stack3.Location.X + 10, tw3.Location.Y); break; case 11004: stack4.Location = new Point((int)(1198 - 15 * (device.XCoor - 1)), stack4.Location.Y); tw4.Location = new Point(stack4.Location.X + 10, tw4.Location.Y); break; } } } #endregion #region 设置设备状态 if (ctrl.Enabled == false) continue;//20150130 bianzhi switch (device.RunState) { //空闲 case 0: ctrl.BackColor = Color.MediumOrchid; break; //运行 case 1: ctrl.BackColor = Color.Lime; break; //故障 case 2: ctrl.BackColor = Color.Red; break; //离线 case 3: ctrl.BackColor = Color.LightYellow; break; //停用 case 4: ctrl.BackColor = Color.Maroon; break; //20090920动作完成 case 5: ctrl.BackColor = Color.SeaGreen;//MediumOrchid; break; } #endregion #region 设备被禁用 //richar.liu20140806 dvJY.Sort = "F_DeviceIndex";//20150320 宁波地铁 bianzhi if (dvJY.Find(device.DeviceIndex) >= 0) { ctrl.BackColor = Color.Maroon; } #endregion } #endregion #region 类输送机 foreach (Button ctrl in pp.Controls.OfType