private void SendBatchSwitchState(string ipAddress, int port, string swState, int status) { BravaConnection myConnection = new BravaConnection(); myConnection.BravaIP = System.Net.IPAddress.Parse(ipAddress); myConnection.BravaPort = port; BravaCodes.SwitchState[] mySwitchStates = new BravaCodes.SwitchState[8]; SetSwitchStateBatch(swState, status, mySwitchStates); SwitchbankUpdate mySwitchbankUpdate = new SwitchbankUpdate( mySwitchStates[0], mySwitchStates[1], mySwitchStates[2], mySwitchStates[3], mySwitchStates[4], mySwitchStates[5], mySwitchStates[6], mySwitchStates[7]); BravaSocket mySocket = new BravaSocket(mySwitchbankUpdate, myConnection); try { mySocket.DoTransaction(); } catch (Exception) { throw; } }
private void btnLoadShed_Click(object sender, EventArgs e) { string address = string.Empty; string unitName = grdChildOne.SelectedRows[0].Cells[2].Value.ToString(); BravaCodes.SwitchState[] mySwitchStates = new BravaCodes.SwitchState[8]; for (int x = 0; x <= 7; x++) { mySwitchStates[x] = BravaCodes.SwitchState.SwitchOff; } foreach (DataGridViewRow row in grdChildTwo.Rows) { int status = 0; int id = int.Parse(row.Cells[0].Value.ToString()); string selectedvalue = row.Cells[5].Value.ToString(); lfa.pmgmt.data.DAO.Configuration.Device deviceDAO = new data.DAO.Configuration.Device(); deviceDAO.ConnectionString = _connectionString; address = deviceDAO.GetDeviceAddress(id); string deviceSwitch = deviceDAO.GetDeviceSwitch(id); if (selectedvalue == "On") { status = 1; } else { status = 2; } SetSwitchState(deviceSwitch, status, mySwitchStates); UpdateDeviceLoadShedStatus(status, id); } SendSwitchState(address, unitName, int.Parse(ConfigurationManager.AppSettings["DefaultPort"].ToString()), mySwitchStates); DisableAllSchedules(); string loadShedMsg = "Devices successfully load shedded! "; loadShedMsg += "Please note that manual power management disables all schedules. To enable a schedule, please "; loadShedMsg += "go to the 'Schedule' management screen and re-enable the schedule."; HandleInformationMesssage(loadShedMsg); grdChildTwo.DataSource = null; grdChildTwo.Columns.Clear(); }
private void SendSwitchState(string ipAddress, int port, string swState, int status) { BravaConnection myConnection = new BravaConnection(); myConnection.BravaIP = System.Net.IPAddress.Parse(ipAddress); myConnection.BravaPort = port; BravaCodes.SwitchState[] mySwitchStates = new BravaCodes.SwitchState[8]; SetSwitchState(swState, status, mySwitchStates, ""); SwitchbankUpdate mySwitchbankUpdate = new SwitchbankUpdate( mySwitchStates[0], mySwitchStates[1], mySwitchStates[2], mySwitchStates[3], mySwitchStates[4], mySwitchStates[5], mySwitchStates[6], mySwitchStates[7]); BravaSocket mySocket = new BravaSocket(mySwitchbankUpdate, myConnection); try { //mySocket.OpenConnection(); mySocket.DoTransaction(); if (mySwitchbankUpdate.State == BravaTransaction.TransactionState.Completed) { mySwitchbankUpdate.State = BravaTransaction.TransactionState.Closed; } if (mySwitchbankUpdate.State == BravaTransaction.TransactionState.Failed) { throw new System.Net.Sockets.SocketException(10004); } } catch (System.Net.Sockets.SocketException) { mySocket.SocketClient.Close(); throw; } finally { mySocket.SocketClient.Close(); } }
// Constructors /// <summary> /// Update 8 Switches at once. Selectable Verify. /// </summary> /// <param name="SwitchState1"></param> /// <param name="SwitchState2"></param> /// <param name="SwitchState3"></param> /// <param name="SwitchState4"></param> /// <param name="SwitchState5"></param> /// <param name="SwitchState6"></param> /// <param name="SwitchState7"></param> /// <param name="SwitchState8"></param> public SwitchbankUpdate( BravaCodes.SwitchState SwitchState1, BravaCodes.SwitchState SwitchState2, BravaCodes.SwitchState SwitchState3, BravaCodes.SwitchState SwitchState4, BravaCodes.SwitchState SwitchState5, BravaCodes.SwitchState SwitchState6, BravaCodes.SwitchState SwitchState7, BravaCodes.SwitchState SwitchState8) { byte[] buffer = { (byte)BravaCodes.Tasks.Control, (byte)BravaCodes.Functions.LoadSwitch, (byte)BravaCodes.LoadSwitchRecords.SwitchBankUpdate, (byte)8 // Number of switches being updated by this command. }; #region RequestStream for Controlling the 8 Switches. m_reqStrm = new MemoryStream(16); // Write Header m_reqStrm.Write(buffer, 0, 4); // Add in the 8 switch states. // // TODO: Likely change this entire thing to some form of collection with a foreach loop instead and limits check in front. // for now it'll do pig... it'll do. m_reqStrm.WriteByte((byte)SwitchState1); m_reqStrm.WriteByte((byte)SwitchState2); m_reqStrm.WriteByte((byte)SwitchState3); m_reqStrm.WriteByte((byte)SwitchState4); m_reqStrm.WriteByte((byte)SwitchState5); m_reqStrm.WriteByte((byte)SwitchState6); m_reqStrm.WriteByte((byte)SwitchState7); m_reqStrm.WriteByte((byte)SwitchState8); // ReSynce UTC Time Base DateTime TimeBase = DateTime.Parse("01 January 2000"); DateTime UTCTime = DateTime.UtcNow; TimeSpan UTCOffset = (TimeSpan)(UTCTime - TimeBase); int reqTimeStamp = (int)UTCOffset.TotalSeconds; System.Diagnostics.Debug.WriteLine("UTC Seconds is" + reqTimeStamp.ToString()); buffer = BitConverter.GetBytes(reqTimeStamp); m_reqStrm.Write(buffer, 0, 4); // 16 bytes long #endregion this.State = TransactionState.Open; }
// Methods public BravaCodes.SwitchState[] ParseSwitchStates(int numSwitches) { BravaCodes.SwitchState[] resultStates = new BravaCodes.SwitchState[numSwitches]; // Copy response stream into a more versatile memory stream. byte[] bufferBytes = new byte[m_resStrm.Length]; m_resStrm.Seek(0, SeekOrigin.Begin); m_resStrm.Read(bufferBytes, 0, (int)m_resStrm.Length); MemoryStream resMemStr = new MemoryStream(); // First Byte: Last Record available index = Number of Records - 1. byte b_numRecords = bufferBytes[4]; // Check that input frame length matches the number of records. if (m_resStrm.Length == ((b_numRecords * BravaConstants.STATEBLOCKRECORDLENGTH) + 5)) { // Frame length matches if ((bufferBytes[0] == (byte)BravaCodes.Tasks.Response) && (bufferBytes[1] == (byte)BravaCodes.Functions.Gateway) && (bufferBytes[2] == (byte)BravaCodes.GatewayRecords.StateBlock) && (bufferBytes[3] == (byte)BravaCodes.StateBlockRecords.SwitchBankLS8)) { // Header validation OK if (numSwitches > 32) { numSwitches = 32; } for (int n = 0; n < numSwitches; n++) { resultStates[n] = (bufferBytes[0xc + n] == 0x03 ? BravaCodes.SwitchState.SwitchOff : BravaCodes.SwitchState.SwitchOn); } this.State = TransactionState.Closed; } } return(resultStates); }
private void btnLoadShed_Click(object sender, EventArgs e) { string address = string.Empty; string unitName = grdChildOne.SelectedRows[0].Cells[2].Value.ToString(); BravaCodes.SwitchState[] mySwitchStates = new BravaCodes.SwitchState[8]; for (int x = 0; x <= 7; x++) mySwitchStates[x] = BravaCodes.SwitchState.SwitchOff; foreach (DataGridViewRow row in grdChildTwo.Rows) { int status = 0; int id = int.Parse(row.Cells[0].Value.ToString()); string selectedvalue = row.Cells[5].Value.ToString(); lfa.pmgmt.data.DAO.Configuration.Device deviceDAO = new data.DAO.Configuration.Device(); deviceDAO.ConnectionString = _connectionString; address = deviceDAO.GetDeviceAddress(id); string deviceSwitch = deviceDAO.GetDeviceSwitch(id); if (selectedvalue == "On") status = 1; else status = 2; SetSwitchState(deviceSwitch, status, mySwitchStates); UpdateDeviceLoadShedStatus(status, id); } SendSwitchState(address, unitName, int.Parse(ConfigurationManager.AppSettings["DefaultPort"].ToString()), mySwitchStates); DisableAllSchedules(); string loadShedMsg = "Devices successfully load shedded! "; loadShedMsg += "Please note that manual power management disables all schedules. To enable a schedule, please "; loadShedMsg += "go to the 'Schedule' management screen and re-enable the schedule."; HandleInformationMesssage(loadShedMsg); grdChildTwo.DataSource = null; grdChildTwo.Columns.Clear(); }
private void LoadShedUnit(List <lfa.pmgmt.data.DTO.Schedule.Device> unitDevices, int defaultPort, lfa.pmgmt.data.DTO.Schedule.Unit unit, int status) { if (unit.IsActive) { string deviceAddress = string.Empty; SwitchbankUpdate mySwitchbankUpdate = null; BravaCodes.SwitchState[] mySwitchStates = new BravaCodes.SwitchState[8]; for (int x = 0; x <= 7; x++) { mySwitchStates[x] = BravaCodes.SwitchState.SwitchOff; } foreach (lfa.pmgmt.data.DTO.Schedule.Device unitDevice in unitDevices) { lfa.pmgmt.data.DAO.Configuration.Device deviceDAO = new data.DAO.Configuration.Device(); deviceDAO.ConnectionString = _connectionString; string deviceName = unitDevice.Name; int deviceConfigId = deviceDAO.GetDeviceConfigId(unit.Id_ScheduleUnit, unitDevice.Id); deviceAddress = deviceDAO.GetDeviceAddress(deviceConfigId); string deviceSwitch = deviceDAO.GetDeviceSwitch(deviceConfigId); bool switchOn = unitDevice.DeviceOn; if (switchOn) { status = 1; } else { status = 2; } SetSwitchState(deviceSwitch, status, mySwitchStates, unitDevice.Id.ToString()); } try { mySwitchbankUpdate = new SwitchbankUpdate( mySwitchStates[0], mySwitchStates[1], mySwitchStates[2], mySwitchStates[3], mySwitchStates[4], mySwitchStates[5], mySwitchStates[6], mySwitchStates[7]); SendSwitchState(deviceAddress, defaultPort, mySwitchbankUpdate); lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStatusDAO = new data.DAO.Logging.CurrentStatus(); currentStatusDAO.ConnectionString = _connectionString; currentStatusDAO.Insert(unit.Name, "OK"); } catch (System.Net.Sockets.SocketException) { System.Threading.Thread.Sleep(200); try { mySwitchbankUpdate = new SwitchbankUpdate( mySwitchStates[0], mySwitchStates[1], mySwitchStates[2], mySwitchStates[3], mySwitchStates[4], mySwitchStates[5], mySwitchStates[6], mySwitchStates[7]); SendSwitchState(deviceAddress, defaultPort, mySwitchbankUpdate); lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStatusDAO = new data.DAO.Logging.CurrentStatus(); currentStatusDAO.ConnectionString = _connectionString; currentStatusDAO.Insert(unit.Name, "OK"); } catch (System.Net.Sockets.SocketException ex) { lfa.pmgmt.data.DAO.Logging.Log logDAO = new data.DAO.Logging.Log(); logDAO.ConnectionString = _connectionString; logDAO.Insert(1, "Unit: " + unit.Name, "TCP/IP Communications Error due to: " + ex.ToString(), DateTime.Now); lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStatusDAO = new data.DAO.Logging.CurrentStatus(); currentStatusDAO.ConnectionString = _connectionString; currentStatusDAO.Insert(unit.Name, "ERR"); } } } }
private void button3_Click(object sender, EventArgs e) { BravaConnection myConnection = new BravaConnection(); myConnection.BravaIP = System.Net.IPAddress.Parse(txtIPAddr.Text); myConnection.BravaPort = Int16.Parse(txtIPPort.Text); BravaCodes.SwitchState[] mySwitchStates = new BravaCodes.SwitchState[8]; for (int x = 0; x <= 7; x++) { mySwitchStates[x] = BravaCodes.SwitchState.SwitchOff; } if (checkBox1.Checked) { mySwitchStates[0] = BravaCodes.SwitchState.SwitchOn; } if (checkBox2.Checked) { mySwitchStates[1] = BravaCodes.SwitchState.SwitchOn; } if (checkBox3.Checked) { mySwitchStates[2] = BravaCodes.SwitchState.SwitchOn; } if (checkBox4.Checked) { mySwitchStates[3] = BravaCodes.SwitchState.SwitchOn; } if (checkBox5.Checked) { mySwitchStates[4] = BravaCodes.SwitchState.SwitchOn; } if (checkBox6.Checked) { mySwitchStates[5] = BravaCodes.SwitchState.SwitchOn; } if (checkBox7.Checked) { mySwitchStates[6] = BravaCodes.SwitchState.SwitchOn; } if (checkBox8.Checked) { mySwitchStates[7] = BravaCodes.SwitchState.SwitchOn; } SwitchbankUpdate mySwitchbankUpdate = new SwitchbankUpdate( mySwitchStates[0], mySwitchStates[1], mySwitchStates[2], mySwitchStates[3], mySwitchStates[4], mySwitchStates[5], mySwitchStates[6], mySwitchStates[7]); BravaSocket mySocket = new BravaSocket(mySwitchbankUpdate, myConnection); try { mySocket.OpenConnection(); mySocket.DoTransaction(); myConnection.rqStream.Close(); } catch (Exception) { mySocket.SocketClient.Close(); throw; } }
private void sendSwitchStateCommand(System.Net.IPAddress destIPAdr, Int16 destPortNum, bool swState1, bool swState2, bool swState3, bool swState4, bool swState5, bool swState6, bool swState7, bool swState8) { BravaCodes.SwitchState[] mySwitchStates = new BravaCodes.SwitchState[8]; for (int x = 0; x <= 7; x++) mySwitchStates[x] = BravaCodes.SwitchState.SwitchOff; if (swState1) mySwitchStates[0] = BravaCodes.SwitchState.SwitchOn; if (swState2) mySwitchStates[1] = BravaCodes.SwitchState.SwitchOn; if (swState3) // cbSingleSw3.Checked) mySwitchStates[2] = BravaCodes.SwitchState.SwitchOn; if (swState4) // cbSingleSw4.Checked) mySwitchStates[3] = BravaCodes.SwitchState.SwitchOn; if (swState5) // cbSingleSw5.Checked) mySwitchStates[4] = BravaCodes.SwitchState.SwitchOn; if (swState6) // cbSingleSw6.Checked) mySwitchStates[5] = BravaCodes.SwitchState.SwitchOn; if (swState7) // cbSingleSw7.Checked) mySwitchStates[6] = BravaCodes.SwitchState.SwitchOn; if (swState8) // cbSingleSw8.Checked) mySwitchStates[7] = BravaCodes.SwitchState.SwitchOn; SwitchbankUpdate mySwitchbankUpdate = new SwitchbankUpdate( mySwitchStates[0], mySwitchStates[1], mySwitchStates[2], mySwitchStates[3], mySwitchStates[4], mySwitchStates[5], mySwitchStates[6], mySwitchStates[7]); // RequestStateBlock myReadBackVerify = new RequestStateBlock((byte)BravaCodes.StateBlockRecords.SwitchBankLS8); BravaConnection myConnection = new BravaConnection(); myConnection.BravaIP = destIPAdr; myConnection.BravaPort = destPortNum; BravaSocket mySocket = new BravaSocket(mySwitchbankUpdate, myConnection); try { mySocket.DoTransaction(); if (mySwitchbankUpdate.State == BravaTransaction.TransactionState.Completed) { mySwitchbankUpdate.State = BravaTransaction.TransactionState.Closed; //mySocket.Transaction = myReadBackVerify; //mySocket.DoTransaction(); //if (myReadBackVerify.State == BravaTransaction.TransactionState.Completed) //{ // BravaCodes.SwitchState[] readbackStates = new BravaCodes.SwitchState[8]; // readbackStates = myReadBackVerify.ParseSwitchStates(8); // // TODO: Utility function to extract the switch state from the StateBlock // if (myReadBackVerify.State == BravaTransaction.TransactionState.Closed) // { // if (mySwitchStates == readbackStates) // { // MessageBox.Show("ReadBack value differs from Set Value"); // } // } //} //else //{ // MessageBox.Show("ReadBackVerify Failed."); //} } else { MessageBox.Show("SwitchBankUpdate Failed."); } } catch (Exception) { mySocket.SocketClient.Close(); throw; } }
private void AsyncLoadShedUnit(object parameters) { try { ArrayList parms = parameters as ArrayList; List <lfa.pmgmt.data.DTO.Schedule.Device> unitDevices = parms[0] as List <lfa.pmgmt.data.DTO.Schedule.Device>; int defaultPort = (int)parms[1]; lfa.pmgmt.data.DTO.Schedule.Unit unit = parms[2] as lfa.pmgmt.data.DTO.Schedule.Unit; int status = (int)parms[3]; if (unit.IsActive) { string deviceAddress = string.Empty; SwitchbankUpdate mySwitchbankUpdate = null; BravaCodes.SwitchState[] mySwitchStates = new BravaCodes.SwitchState[8]; for (int x = 0; x <= 7; x++) { mySwitchStates[x] = BravaCodes.SwitchState.NoChange; } foreach (lfa.pmgmt.data.DTO.Schedule.Device unitDevice in unitDevices) { lfa.pmgmt.data.DAO.Configuration.Device deviceDAO = new data.DAO.Configuration.Device(); deviceDAO.ConnectionString = _connectionString; string deviceName = unitDevice.Name; int deviceConfigId = deviceDAO.GetDeviceConfigId(unit.Id_ScheduleUnit, unitDevice.Id); deviceAddress = deviceDAO.GetDeviceAddress(deviceConfigId); string deviceSwitch = deviceDAO.GetDeviceSwitch(deviceConfigId); //if (status == 0) //{ bool switchOn = unitDevice.DeviceOn; if (switchOn) { status = 1; } else { status = 2; } //} SetSwitchState(deviceSwitch, status, mySwitchStates, unitDevice.Id.ToString()); } mySwitchbankUpdate = new SwitchbankUpdate(mySwitchStates[0], mySwitchStates[1], mySwitchStates[2], mySwitchStates[3], mySwitchStates[4], mySwitchStates[5], mySwitchStates[6], mySwitchStates[7]); try { //System.Diagnostics.EventLog.WriteEntry("BUGGER", "Send 1", System.Diagnostics.EventLogEntryType.Warning); SendSwitchState(deviceAddress, defaultPort, mySwitchbankUpdate); lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStatusDAO = new data.DAO.Logging.CurrentStatus(); currentStatusDAO.ConnectionString = _connectionString; currentStatusDAO.Insert(unit.Name, "OK"); } catch (System.Net.Sockets.SocketException) { System.Threading.Thread.Sleep(200); try { mySwitchbankUpdate = new SwitchbankUpdate(mySwitchStates[0], mySwitchStates[1], mySwitchStates[2], mySwitchStates[3], mySwitchStates[4], mySwitchStates[5], mySwitchStates[6], mySwitchStates[7]); //System.Diagnostics.EventLog.WriteEntry("BUGGER", "Send Err 1", System.Diagnostics.EventLogEntryType.Warning); SendSwitchState(deviceAddress, defaultPort, mySwitchbankUpdate); lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStatusDAO = new data.DAO.Logging.CurrentStatus(); currentStatusDAO.ConnectionString = _connectionString; currentStatusDAO.Insert(unit.Name, "OK"); } catch (System.Net.Sockets.SocketException soex) { lfa.pmgmt.data.DAO.Logging.Log logDAO = new data.DAO.Logging.Log(); logDAO.ConnectionString = _connectionString; logDAO.Insert(1, "Unit: " + unit.Name, "TCP/IP Communications Error due to: " + soex.ToString(), DateTime.Now); lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStatusDAO = new data.DAO.Logging.CurrentStatus(); currentStatusDAO.ConnectionString = _connectionString; currentStatusDAO.Insert(unit.Name, "ERR"); } } } else { try { //System.Diagnostics.EventLog.WriteEntry("BUGGER", "Send 2", System.Diagnostics.EventLogEntryType.Warning); SendBatchSwitchState(unit.Address, defaultPort, string.Empty, 2); lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStatusDAO = new data.DAO.Logging.CurrentStatus(); currentStatusDAO.ConnectionString = _connectionString; currentStatusDAO.Insert(unit.Name, "OK"); } catch (System.Net.Sockets.SocketException) { System.Threading.Thread.Sleep(200); try { //System.Diagnostics.EventLog.WriteEntry("BUGGER", "Send Err 2", System.Diagnostics.EventLogEntryType.Warning); SendBatchSwitchState(unit.Address, defaultPort, string.Empty, 2); lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStatusDAO = new data.DAO.Logging.CurrentStatus(); currentStatusDAO.ConnectionString = _connectionString; currentStatusDAO.Insert(unit.Name, "OK"); } catch (System.Net.Sockets.SocketException ex) { lfa.pmgmt.data.DAO.Logging.Log logDAO = new data.DAO.Logging.Log(); logDAO.ConnectionString = _connectionString; logDAO.Insert(1, "Unit: " + unit.Name, "TCP/IP Communications Error due to: " + ex.ToString(), DateTime.Now); lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStatusDAO = new data.DAO.Logging.CurrentStatus(); currentStatusDAO.ConnectionString = _connectionString; currentStatusDAO.Insert(unit.Name, "ERR"); } } } } catch (Exception) { //File.AppendAllText(@"c:\smartpower\logerr.txt", er.ToString()); } }
private void AsyncLoadShedUnit(object parameters) { ArrayList parms = parameters as ArrayList; List<lfa.pmgmt.data.DTO.Schedule.Device> unitDevices = parms[0] as List<lfa.pmgmt.data.DTO.Schedule.Device>; int defaultPort = (int)parms[1]; lfa.pmgmt.data.DTO.Schedule.Unit unit = parms[2] as lfa.pmgmt.data.DTO.Schedule.Unit; int status = (int)parms[3]; if (unit.IsActive) { string deviceAddress = string.Empty; SwitchbankUpdate mySwitchbankUpdate = null; BravaCodes.SwitchState[] mySwitchStates = new BravaCodes.SwitchState[8]; for (int x = 0; x <= 7; x++) mySwitchStates[x] = BravaCodes.SwitchState.SwitchOff; foreach (lfa.pmgmt.data.DTO.Schedule.Device unitDevice in unitDevices) { lfa.pmgmt.data.DAO.Configuration.Device deviceDAO = new data.DAO.Configuration.Device(); deviceDAO.ConnectionString = _connectionString; string deviceName = unitDevice.Name; int deviceConfigId = deviceDAO.GetDeviceConfigId(unit.Id_ScheduleUnit, unitDevice.Id); deviceAddress = deviceDAO.GetDeviceAddress(deviceConfigId); string deviceSwitch = deviceDAO.GetDeviceSwitch(deviceConfigId); bool switchOn = unitDevice.DeviceOn; if (switchOn) { status = 1; } else { status = 2; } SetSwitchState(deviceSwitch, status, mySwitchStates, unitDevice.Id.ToString()); } mySwitchbankUpdate = new SwitchbankUpdate(mySwitchStates[0], mySwitchStates[1], mySwitchStates[2], mySwitchStates[3], mySwitchStates[4], mySwitchStates[5], mySwitchStates[6], mySwitchStates[7]); try { SendSwitchState(deviceAddress, defaultPort, mySwitchbankUpdate); lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStatusDAO = new data.DAO.Logging.CurrentStatus(); currentStatusDAO.ConnectionString = _connectionString; currentStatusDAO.Insert(unit.Name, "OK"); } catch(System.Net.Sockets.SocketException) { System.Threading.Thread.Sleep(500); try { mySwitchbankUpdate = new SwitchbankUpdate(mySwitchStates[0], mySwitchStates[1], mySwitchStates[2], mySwitchStates[3], mySwitchStates[4], mySwitchStates[5], mySwitchStates[6], mySwitchStates[7]); SendSwitchState(deviceAddress, defaultPort, mySwitchbankUpdate); lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStatusDAO = new data.DAO.Logging.CurrentStatus(); currentStatusDAO.ConnectionString = _connectionString; currentStatusDAO.Insert(unit.Name, "OK"); } catch (System.Net.Sockets.SocketException soex) { lfa.pmgmt.data.DAO.Logging.Log logDAO = new data.DAO.Logging.Log(); logDAO.ConnectionString = _connectionString; logDAO.Insert(1, "Unit: " + unit.Name, "TCP/IP Communications Error due to: " + soex.ToString(), DateTime.Now); lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStatusDAO = new data.DAO.Logging.CurrentStatus(); currentStatusDAO.ConnectionString = _connectionString; currentStatusDAO.Insert(unit.Name, "ERR"); } } } }
private void sendSwitchStateCommand(System.Net.IPAddress destIPAdr, Int16 destPortNum, bool swState1, bool swState2, bool swState3, bool swState4, bool swState5, bool swState6, bool swState7, bool swState8) { BravaCodes.SwitchState[] mySwitchStates = new BravaCodes.SwitchState[8]; for (int x = 0; x <= 7; x++) { mySwitchStates[x] = BravaCodes.SwitchState.SwitchOff; } if (swState1) { mySwitchStates[0] = BravaCodes.SwitchState.SwitchOn; } if (swState2) { mySwitchStates[1] = BravaCodes.SwitchState.SwitchOn; } if (swState3) // cbSingleSw3.Checked) { mySwitchStates[2] = BravaCodes.SwitchState.SwitchOn; } if (swState4) // cbSingleSw4.Checked) { mySwitchStates[3] = BravaCodes.SwitchState.SwitchOn; } if (swState5) // cbSingleSw5.Checked) { mySwitchStates[4] = BravaCodes.SwitchState.SwitchOn; } if (swState6) // cbSingleSw6.Checked) { mySwitchStates[5] = BravaCodes.SwitchState.SwitchOn; } if (swState7) // cbSingleSw7.Checked) { mySwitchStates[6] = BravaCodes.SwitchState.SwitchOn; } if (swState8) // cbSingleSw8.Checked) { mySwitchStates[7] = BravaCodes.SwitchState.SwitchOn; } SwitchbankUpdate mySwitchbankUpdate = new SwitchbankUpdate( mySwitchStates[0], mySwitchStates[1], mySwitchStates[2], mySwitchStates[3], mySwitchStates[4], mySwitchStates[5], mySwitchStates[6], mySwitchStates[7]); // RequestStateBlock myReadBackVerify = new RequestStateBlock((byte)BravaCodes.StateBlockRecords.SwitchBankLS8); BravaConnection myConnection = new BravaConnection(); myConnection.BravaIP = destIPAdr; myConnection.BravaPort = destPortNum; BravaSocket mySocket = new BravaSocket(mySwitchbankUpdate, myConnection); try { mySocket.DoTransaction(); if (mySwitchbankUpdate.State == BravaTransaction.TransactionState.Completed) { mySwitchbankUpdate.State = BravaTransaction.TransactionState.Closed; //mySocket.Transaction = myReadBackVerify; //mySocket.DoTransaction(); //if (myReadBackVerify.State == BravaTransaction.TransactionState.Completed) //{ // BravaCodes.SwitchState[] readbackStates = new BravaCodes.SwitchState[8]; // readbackStates = myReadBackVerify.ParseSwitchStates(8); // // TODO: Utility function to extract the switch state from the StateBlock // if (myReadBackVerify.State == BravaTransaction.TransactionState.Closed) // { // if (mySwitchStates == readbackStates) // { // MessageBox.Show("ReadBack value differs from Set Value"); // } // } //} //else //{ // MessageBox.Show("ReadBackVerify Failed."); //} } else { MessageBox.Show("SwitchBankUpdate Failed."); } } catch (Exception) { mySocket.SocketClient.Close(); throw; } }
private void sendSwitchStateCommand(System.Net.IPAddress destIPAdr, Int16 destPortNum, bool swState1, bool swState2, bool swState3, bool swState4, bool swState5, bool swState6, bool swState7, bool swState8) { BravaConnection myConnection = new BravaConnection(); myConnection.BravaIP = destIPAdr; myConnection.BravaPort = destPortNum; BravaCodes.SwitchState[] mySwitchStates = new BravaCodes.SwitchState[8]; for (int x = 0; x <= 7; x++) mySwitchStates[x] = BravaCodes.SwitchState.SwitchOff; if (swState1) mySwitchStates[0] = BravaCodes.SwitchState.SwitchOn; if (swState2) mySwitchStates[1] = BravaCodes.SwitchState.SwitchOn; if (swState3) // cbSingleSw3.Checked) mySwitchStates[2] = BravaCodes.SwitchState.SwitchOn; if (swState4) // cbSingleSw4.Checked) mySwitchStates[3] = BravaCodes.SwitchState.SwitchOn; if (swState5) // cbSingleSw5.Checked) mySwitchStates[4] = BravaCodes.SwitchState.SwitchOn; if (swState6) // cbSingleSw6.Checked) mySwitchStates[5] = BravaCodes.SwitchState.SwitchOn; if (swState7) // cbSingleSw7.Checked) mySwitchStates[6] = BravaCodes.SwitchState.SwitchOn; if (swState8) // cbSingleSw8.Checked) mySwitchStates[7] = BravaCodes.SwitchState.SwitchOn; SwitchbankUpdate mySwitchbankUpdate = new SwitchbankUpdate( mySwitchStates[0], mySwitchStates[1], mySwitchStates[2], mySwitchStates[3], mySwitchStates[4], mySwitchStates[5], mySwitchStates[6], mySwitchStates[7]); BravaSocket mySocket = new BravaSocket(mySwitchbankUpdate, myConnection); try { mySocket.OpenConnection(); mySocket.DoTransaction(); myConnection.rqStream.Close(); } catch (Exception) { mySocket.SocketClient.Close(); throw; } }
private void SendBatchSwitchState(string ipAddress, int port, string swState, int status) { BravaConnection myConnection = new BravaConnection(); myConnection.BravaIP = System.Net.IPAddress.Parse(ipAddress); myConnection.BravaPort = port; BravaCodes.SwitchState[] mySwitchStates = new BravaCodes.SwitchState[8]; SetSwitchStateBatch(swState, status, mySwitchStates); SwitchbankUpdate mySwitchbankUpdate = new SwitchbankUpdate( mySwitchStates[0], mySwitchStates[1], mySwitchStates[2], mySwitchStates[3], mySwitchStates[4], mySwitchStates[5], mySwitchStates[6], mySwitchStates[7]); BravaSocket mySocket = new BravaSocket(mySwitchbankUpdate, myConnection); try { mySocket.DoTransaction(); if (mySwitchbankUpdate.State == BravaTransaction.TransactionState.Completed) { mySwitchbankUpdate.State = BravaTransaction.TransactionState.Closed; } if (mySwitchbankUpdate.State == BravaTransaction.TransactionState.Failed) { throw new System.Net.Sockets.SocketException(10004); } } catch (System.Net.Sockets.SocketException) { mySocket.SocketClient.Close(); throw; } finally { mySocket.SocketClient.Close(); } }
private void LoadShedUnit(List<lfa.pmgmt.data.DTO.Schedule.Device> unitDevices, int defaultPort, lfa.pmgmt.data.DTO.Schedule.Unit unit, int status) { if (unit.IsActive) { string deviceAddress = string.Empty; SwitchbankUpdate mySwitchbankUpdate = null; BravaCodes.SwitchState[] mySwitchStates = new BravaCodes.SwitchState[8]; for (int x = 0; x <= 7; x++) mySwitchStates[x] = BravaCodes.SwitchState.NoChange; foreach (lfa.pmgmt.data.DTO.Schedule.Device unitDevice in unitDevices) { lfa.pmgmt.data.DAO.Configuration.Device deviceDAO = new data.DAO.Configuration.Device(); deviceDAO.ConnectionString = _connectionString; string deviceName = unitDevice.Name; int deviceConfigId = deviceDAO.GetDeviceConfigId(unit.Id_ScheduleUnit, unitDevice.Id); deviceAddress = deviceDAO.GetDeviceAddress(deviceConfigId); string deviceSwitch = deviceDAO.GetDeviceSwitch(deviceConfigId); if (status == 0) { bool switchOn = unitDevice.DeviceOn; if (switchOn) { status = 1; } else { status = 2; } } SetSwitchState(deviceSwitch, status, mySwitchStates,unitDevice.Id.ToString()); } try { mySwitchbankUpdate = new SwitchbankUpdate( mySwitchStates[0], mySwitchStates[1], mySwitchStates[2], mySwitchStates[3], mySwitchStates[4], mySwitchStates[5], mySwitchStates[6], mySwitchStates[7]); //System.Diagnostics.EventLog.WriteEntry("BUGGER", "Send 3", System.Diagnostics.EventLogEntryType.Warning); SendSwitchState(deviceAddress, defaultPort, mySwitchbankUpdate); lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStatusDAO = new data.DAO.Logging.CurrentStatus(); currentStatusDAO.ConnectionString = _connectionString; currentStatusDAO.Insert(unit.Name, "OK"); } catch(System.Net.Sockets.SocketException) { System.Threading.Thread.Sleep(200); try { mySwitchbankUpdate = new SwitchbankUpdate( mySwitchStates[0], mySwitchStates[1], mySwitchStates[2], mySwitchStates[3], mySwitchStates[4], mySwitchStates[5], mySwitchStates[6], mySwitchStates[7]); //System.Diagnostics.EventLog.WriteEntry("BUGGER", "Send Err 3", System.Diagnostics.EventLogEntryType.Warning); SendSwitchState(deviceAddress, defaultPort, mySwitchbankUpdate); lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStatusDAO = new data.DAO.Logging.CurrentStatus(); currentStatusDAO.ConnectionString = _connectionString; currentStatusDAO.Insert(unit.Name, "OK"); } catch (System.Net.Sockets.SocketException ex) { lfa.pmgmt.data.DAO.Logging.Log logDAO = new data.DAO.Logging.Log(); logDAO.ConnectionString = _connectionString; logDAO.Insert(1, "Unit: " + unit.Name, "TCP/IP Communications Error due to: " + ex.ToString(), DateTime.Now); lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStatusDAO = new data.DAO.Logging.CurrentStatus(); currentStatusDAO.ConnectionString = _connectionString; currentStatusDAO.Insert(unit.Name, "ERR"); } } } else { try { //System.Diagnostics.EventLog.WriteEntry("BUGGER", "Send 4", System.Diagnostics.EventLogEntryType.Warning); SendBatchSwitchState(unit.Address, defaultPort, string.Empty, 2); lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStatusDAO = new data.DAO.Logging.CurrentStatus(); currentStatusDAO.ConnectionString = _connectionString; currentStatusDAO.Insert(unit.Name, "OK"); } catch(System.Net.Sockets.SocketException) { System.Threading.Thread.Sleep(200); try { //System.Diagnostics.EventLog.WriteEntry("BUGGER", "Send Err 4", System.Diagnostics.EventLogEntryType.Warning); SendBatchSwitchState(unit.Address, defaultPort, string.Empty, 2); lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStatusDAO = new data.DAO.Logging.CurrentStatus(); currentStatusDAO.ConnectionString = _connectionString; currentStatusDAO.Insert(unit.Name, "OK"); } catch (System.Net.Sockets.SocketException ex) { lfa.pmgmt.data.DAO.Logging.Log logDAO = new data.DAO.Logging.Log(); logDAO.ConnectionString = _connectionString; logDAO.Insert(1, "Unit: " + unit.Name, "TCP/IP Communications Error due to: " + ex.ToString(), DateTime.Now); lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStatusDAO = new data.DAO.Logging.CurrentStatus(); currentStatusDAO.ConnectionString = _connectionString; currentStatusDAO.Insert(unit.Name, "ERR"); } } } }
private void AsyncLoadShedUnit(object parameters) { try { ArrayList parms = parameters as ArrayList; List<lfa.pmgmt.data.DTO.Schedule.Device> unitDevices = parms[0] as List<lfa.pmgmt.data.DTO.Schedule.Device>; int defaultPort = (int)parms[1]; lfa.pmgmt.data.DTO.Schedule.Unit unit = parms[2] as lfa.pmgmt.data.DTO.Schedule.Unit; int status = (int)parms[3]; if (unit.IsActive) { string deviceAddress = string.Empty; SwitchbankUpdate mySwitchbankUpdate = null; BravaCodes.SwitchState[] mySwitchStates = new BravaCodes.SwitchState[8]; for (int x = 0; x <= 7; x++) mySwitchStates[x] = BravaCodes.SwitchState.NoChange; foreach (lfa.pmgmt.data.DTO.Schedule.Device unitDevice in unitDevices) { lfa.pmgmt.data.DAO.Configuration.Device deviceDAO = new data.DAO.Configuration.Device(); deviceDAO.ConnectionString = _connectionString; string deviceName = unitDevice.Name; int deviceConfigId = deviceDAO.GetDeviceConfigId(unit.Id_ScheduleUnit, unitDevice.Id); deviceAddress = deviceDAO.GetDeviceAddress(deviceConfigId); string deviceSwitch = deviceDAO.GetDeviceSwitch(deviceConfigId); //if (status == 0) //{ bool switchOn = unitDevice.DeviceOn; if (switchOn) { status = 1; } else { status = 2; } //} SetSwitchState(deviceSwitch, status, mySwitchStates, unitDevice.Id.ToString()); } mySwitchbankUpdate = new SwitchbankUpdate(mySwitchStates[0], mySwitchStates[1], mySwitchStates[2], mySwitchStates[3], mySwitchStates[4], mySwitchStates[5], mySwitchStates[6], mySwitchStates[7]); try { //System.Diagnostics.EventLog.WriteEntry("BUGGER", "Send 1", System.Diagnostics.EventLogEntryType.Warning); SendSwitchState(deviceAddress, defaultPort, mySwitchbankUpdate); lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStatusDAO = new data.DAO.Logging.CurrentStatus(); currentStatusDAO.ConnectionString = _connectionString; currentStatusDAO.Insert(unit.Name, "OK"); } catch (System.Net.Sockets.SocketException) { System.Threading.Thread.Sleep(200); try { mySwitchbankUpdate = new SwitchbankUpdate(mySwitchStates[0], mySwitchStates[1], mySwitchStates[2], mySwitchStates[3], mySwitchStates[4], mySwitchStates[5], mySwitchStates[6], mySwitchStates[7]); //System.Diagnostics.EventLog.WriteEntry("BUGGER", "Send Err 1", System.Diagnostics.EventLogEntryType.Warning); SendSwitchState(deviceAddress, defaultPort, mySwitchbankUpdate); lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStatusDAO = new data.DAO.Logging.CurrentStatus(); currentStatusDAO.ConnectionString = _connectionString; currentStatusDAO.Insert(unit.Name, "OK"); } catch (System.Net.Sockets.SocketException soex) { lfa.pmgmt.data.DAO.Logging.Log logDAO = new data.DAO.Logging.Log(); logDAO.ConnectionString = _connectionString; logDAO.Insert(1, "Unit: " + unit.Name, "TCP/IP Communications Error due to: " + soex.ToString(), DateTime.Now); lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStatusDAO = new data.DAO.Logging.CurrentStatus(); currentStatusDAO.ConnectionString = _connectionString; currentStatusDAO.Insert(unit.Name, "ERR"); } } } else { try { //System.Diagnostics.EventLog.WriteEntry("BUGGER", "Send 2", System.Diagnostics.EventLogEntryType.Warning); SendBatchSwitchState(unit.Address, defaultPort, string.Empty, 2); lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStatusDAO = new data.DAO.Logging.CurrentStatus(); currentStatusDAO.ConnectionString = _connectionString; currentStatusDAO.Insert(unit.Name, "OK"); } catch (System.Net.Sockets.SocketException) { System.Threading.Thread.Sleep(200); try { //System.Diagnostics.EventLog.WriteEntry("BUGGER", "Send Err 2", System.Diagnostics.EventLogEntryType.Warning); SendBatchSwitchState(unit.Address, defaultPort, string.Empty, 2); lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStatusDAO = new data.DAO.Logging.CurrentStatus(); currentStatusDAO.ConnectionString = _connectionString; currentStatusDAO.Insert(unit.Name, "OK"); } catch (System.Net.Sockets.SocketException ex) { lfa.pmgmt.data.DAO.Logging.Log logDAO = new data.DAO.Logging.Log(); logDAO.ConnectionString = _connectionString; logDAO.Insert(1, "Unit: " + unit.Name, "TCP/IP Communications Error due to: " + ex.ToString(), DateTime.Now); lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStatusDAO = new data.DAO.Logging.CurrentStatus(); currentStatusDAO.ConnectionString = _connectionString; currentStatusDAO.Insert(unit.Name, "ERR"); } } } } catch (Exception) { //File.AppendAllText(@"c:\smartpower\logerr.txt", er.ToString()); } }
// Methods public BravaCodes.SwitchState[] ParseSwitchStates(int numSwitches) { BravaCodes.SwitchState[] resultStates = new BravaCodes.SwitchState[numSwitches]; // Copy response stream into a more versatile memory stream. byte[] bufferBytes = new byte[m_resStrm.Length]; m_resStrm.Seek(0, SeekOrigin.Begin); m_resStrm.Read(bufferBytes, 0, (int)m_resStrm.Length); MemoryStream resMemStr = new MemoryStream(); // First Byte: Last Record available index = Number of Records - 1. byte b_numRecords = bufferBytes[4]; // Check that input frame length matches the number of records. if (m_resStrm.Length == ((b_numRecords * BravaConstants.STATEBLOCKRECORDLENGTH) + 5)) { // Frame length matches if ((bufferBytes[0] == (byte)BravaCodes.Tasks.Response) && (bufferBytes[1] == (byte)BravaCodes.Functions.Gateway) && (bufferBytes[2] == (byte)BravaCodes.GatewayRecords.StateBlock) && (bufferBytes[3] == (byte)BravaCodes.StateBlockRecords.SwitchBankLS8)) { // Header validation OK if (numSwitches > 32) numSwitches = 32; for (int n = 0; n < numSwitches; n++) { resultStates[n] = (bufferBytes[0xc + n] == 0x03 ? BravaCodes.SwitchState.SwitchOff : BravaCodes.SwitchState.SwitchOn); } this.State = TransactionState.Closed; } } return resultStates; }
private void SendSwitchState(string ipAddress, int port, string swState, int status) { BravaConnection myConnection = new BravaConnection(); myConnection.BravaIP = System.Net.IPAddress.Parse(ipAddress); myConnection.BravaPort = port; BravaCodes.SwitchState[] mySwitchStates = new BravaCodes.SwitchState[8]; SetSwitchState(swState, status, mySwitchStates); SwitchbankUpdate mySwitchbankUpdate = new SwitchbankUpdate( mySwitchStates[0], mySwitchStates[1], mySwitchStates[2], mySwitchStates[3], mySwitchStates[4], mySwitchStates[5], mySwitchStates[6], mySwitchStates[7]); BravaSocket mySocket = new BravaSocket(mySwitchbankUpdate, myConnection); try { mySocket.OpenConnection(); mySocket.DoTransaction(); myConnection.rqStream.Close(); } catch (Exception ex) { throw ex; } finally { mySocket.SocketClient.Close(); } }
private void button3_Click(object sender, EventArgs e) { BravaConnection myConnection = new BravaConnection(); myConnection.BravaIP = System.Net.IPAddress.Parse(txtIPAddr.Text); myConnection.BravaPort = Int16.Parse(txtIPPort.Text); BravaCodes.SwitchState[] mySwitchStates = new BravaCodes.SwitchState[8]; for (int x = 0; x <= 7; x++) mySwitchStates[x] = BravaCodes.SwitchState.SwitchOff; if (checkBox1.Checked) mySwitchStates[0] = BravaCodes.SwitchState.SwitchOn; if (checkBox2.Checked) mySwitchStates[1] = BravaCodes.SwitchState.SwitchOn; if (checkBox3.Checked) mySwitchStates[2] = BravaCodes.SwitchState.SwitchOn; if (checkBox4.Checked) mySwitchStates[3] = BravaCodes.SwitchState.SwitchOn; if (checkBox5.Checked) mySwitchStates[4] = BravaCodes.SwitchState.SwitchOn; if (checkBox6.Checked) mySwitchStates[5] = BravaCodes.SwitchState.SwitchOn; if (checkBox7.Checked) mySwitchStates[6] = BravaCodes.SwitchState.SwitchOn; if (checkBox8.Checked) mySwitchStates[7] = BravaCodes.SwitchState.SwitchOn; SwitchbankUpdate mySwitchbankUpdate = new SwitchbankUpdate( mySwitchStates[0], mySwitchStates[1], mySwitchStates[2], mySwitchStates[3], mySwitchStates[4], mySwitchStates[5], mySwitchStates[6], mySwitchStates[7]); BravaSocket mySocket = new BravaSocket(mySwitchbankUpdate, myConnection); try { mySocket.OpenConnection(); mySocket.DoTransaction(); myConnection.rqStream.Close(); } catch (Exception) { mySocket.SocketClient.Close(); throw; } }
private void sendSwitchStateCommand(System.Net.IPAddress destIPAdr, Int16 destPortNum, bool swState1, bool swState2, bool swState3, bool swState4, bool swState5, bool swState6, bool swState7, bool swState8) { BravaConnection myConnection = new BravaConnection(); myConnection.BravaIP = destIPAdr; myConnection.BravaPort = destPortNum; BravaCodes.SwitchState[] mySwitchStates = new BravaCodes.SwitchState[8]; for (int x = 0; x <= 7; x++) { mySwitchStates[x] = BravaCodes.SwitchState.SwitchOff; } if (swState1) { mySwitchStates[0] = BravaCodes.SwitchState.SwitchOn; } if (swState2) { mySwitchStates[1] = BravaCodes.SwitchState.SwitchOn; } if (swState3) // cbSingleSw3.Checked) { mySwitchStates[2] = BravaCodes.SwitchState.SwitchOn; } if (swState4) // cbSingleSw4.Checked) { mySwitchStates[3] = BravaCodes.SwitchState.SwitchOn; } if (swState5) // cbSingleSw5.Checked) { mySwitchStates[4] = BravaCodes.SwitchState.SwitchOn; } if (swState6) // cbSingleSw6.Checked) { mySwitchStates[5] = BravaCodes.SwitchState.SwitchOn; } if (swState7) // cbSingleSw7.Checked) { mySwitchStates[6] = BravaCodes.SwitchState.SwitchOn; } if (swState8) // cbSingleSw8.Checked) { mySwitchStates[7] = BravaCodes.SwitchState.SwitchOn; } SwitchbankUpdate mySwitchbankUpdate = new SwitchbankUpdate( mySwitchStates[0], mySwitchStates[1], mySwitchStates[2], mySwitchStates[3], mySwitchStates[4], mySwitchStates[5], mySwitchStates[6], mySwitchStates[7]); BravaSocket mySocket = new BravaSocket(mySwitchbankUpdate, myConnection); try { mySocket.OpenConnection(); mySocket.DoTransaction(); myConnection.rqStream.Close(); } catch (Exception) { mySocket.SocketClient.Close(); throw; } }