private string GetSwitchStatus() { string st = ""; try { for (short i = 0; i < objSwitch.MaxSwitch; i++) { st = objSwitch.GetSwitchName(i).ToString() + "=" + objSwitch.GetSwitch(i).ToString(); txtInfo.Text += st + Environment.NewLine; } } catch (Exception Ex) { txtInfo.Text += Ex.ToString() + Environment.NewLine; } return st; }
/// <summary> /// Get switch power wrapper /// </summary> public bool?PowerGet(byte PORT_NUM, string PORT_NAME) { //log enter Logging.AddLog(System.Reflection.MethodBase.GetCurrentMethod().Name + " [" + PORT_NAME + "] enter", LogLevel.Trace); bool?val = null; //if previously switch was connected, try to get value if (Connected_flag) { try { val = objSwitch.GetSwitch(PORT_NUM); } catch (NotConnectedException ex) { //Not connected? try to recconect for N times Logging.AddLog("Switch driver not connected exception [" + ex.Message + "]. Try to reconnect", LogLevel.Activity, Highlight.Error); //MessageBox.Show(ex.ToString()); bool reconnF = true; int i = 1; while (reconnF) { Connect = true; //try to connect reconnF = (i++ <= SWITCH_MAX_RECCONNECT_ATTEMPTS) && !Connected_flag; } //reconnect result ok? if (Connected_flag) { //try to get switch again Logging.AddLog("Switch driver reconnected", LogLevel.Activity); try { val = objSwitch.GetSwitch(PORT_NUM); } catch (Exception ex2) { //if again exception - give up val = null; Logging.AddLog("Couldn't get switch [" + PORT_NUM + "] value [" + ex2.Message + "]", LogLevel.Important); Logging.AddLog(MethodBase.GetCurrentMethod().Name + " exception [" + ex2.ToString() + "]", LogLevel.Debug, Highlight.Error); } } else { val = null; Logging.AddLog("Switch driver couldn't be reconnected after " + SWITCH_MAX_RECCONNECT_ATTEMPTS + " attempts", LogLevel.Activity); } } catch (Exception ex) { val = null; Logging.AddLog("Get " + PORT_NAME + " unknown exception [" + ex.Message + "]!", LogLevel.Important, Highlight.Error); Logging.AddLog("" + PORT_NAME + " exception details: " + ex.ToString(), LogLevel.Debug, Highlight.Debug); Connected_flag = false; } } else { //switch wasn't even connected val = null; Logging.AddLog(System.Reflection.MethodBase.GetCurrentMethod().Name + " [" + PORT_NAME + "]: Switch wasn't connected, exiting", LogLevel.Trace); } Logging.AddLog(System.Reflection.MethodBase.GetCurrentMethod().Name + " [" + PORT_NAME + "] return value: " + val.ToString(), LogLevel.Trace); return(val); }
/// <summary> /// Update fields /// </summary> private void UpdateFields() { if (IsConnected) { DriverName.Text = driver.Name; DriverInfo.Text = driver.DriverInfo; DriverVersion.Text = driver.DriverVersion; DriverDescription.Text = driver.Description; txtNumberOfSwitches.Text = driver.MaxSwitch.ToString(); txtIPaddr.Text = driver.Action("IPAddress", ""); txtCacheConnection.Text = driver.Action("GetCacheParameter", "CacheCheckConnection"); txtCacheSensorData.Text = driver.Action("GetCacheParameter", "CacheSensorState"); txtTimeout.Text = driver.Action("GetTimeout", ""); string stro = ""; for (int i = 1; i <= driver.MaxSwitch; i++) { try { //((TextBox)this.Controls.Find("txtOutput" + i, true)[0]).Text = driver.GetSwitch((short)(i - 1)).ToString(); ((TextBox)this.Controls.Find("txtOutName" + i, true)[0]).Text = driver.GetSwitchName((short)(i - 1)).ToString(); ((TextBox)this.Controls.Find("txtOutDesc" + i, true)[0]).Text = driver.GetSwitchDescription((short)(i - 1)).ToString(); stro = driver.Action("GetSwitchROFlag", (i - 1).ToString()).ToString(); ((TextBox)this.Controls.Find("txtOutput" + i, true)[0]).Text = stro; ((CheckBox)this.Controls.Find("chkOut" + i, true)[0]).Text = driver.GetSwitchName((short)(i - 1)).ToString(); ((CheckBox)this.Controls.Find("chkOut" + i, true)[0]).Checked = driver.GetSwitch((short)(i - 1)); toolTip1.SetToolTip(((CheckBox)this.Controls.Find("chkOut" + i, true)[0]), driver.GetSwitchDescription((short)(i - 1))); } catch (ASCOM.NotConnectedException ex) { txtLog.AppendText(Environment.NewLine); txtLog.AppendText("Oops, error. Press ok and we will try to reconnect" + Environment.NewLine + ex.ToString()); txtLog.AppendText(Environment.NewLine); //MessageBox.Show("Oops, error. Press ok and we will try to reconnect" + Environment.NewLine + ex.ToString()); toolStripStatusLabel1.Text = "try to reconnect" + " at " + DateTime.Now; txtLog.AppendText("try to reconnect" + " at " + DateTime.Now); txtLog.AppendText(Environment.NewLine); try { Connect(); } catch { toolStripStatusLabel1.Text = "Couldn't reconnect" + " at " + DateTime.Now; txtLog.AppendText("Couldn't reconnect" + " at " + DateTime.Now); txtLog.AppendText(Environment.NewLine); return; } } catch (Exception ex) { txtLog.AppendText(Environment.NewLine); txtLog.AppendText("Unknown exception" + Environment.NewLine + ex.ToString()); txtLog.AppendText(Environment.NewLine); MessageBox.Show("Unknown exception [" + i + "][" + stro + "]" + Environment.NewLine + ex.ToString()); } } } }