private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                dataGridView1.Columns.Clear();

                ComboItem item = comboBox1.SelectedItem as ComboItem;

                lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStats = new data.DAO.Logging.CurrentStatus();
                currentStats.ConnectionString = _connectionString;

                _selectedZone = item.Id;
                List <lfa.pmgmt.data.DTO.Logging.CurrentStatus> statusList = currentStats.List(_selectedZone);

                dataGridView1.DataSource = statusList;

                DataGridViewImageColumn iColumn = new DataGridViewImageColumn();
                dataGridView1.Columns.Add(iColumn);

                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if (row.Cells[2].Value.ToString().Equals("OK"))
                    {
                        DataGridViewImageCell column = row.Cells[3] as DataGridViewImageCell;

                        column.Value = Image.FromFile("OK.png");
                    }
                    else
                    {
                        DataGridViewImageCell column = row.Cells[3] as DataGridViewImageCell;
                        column.Value = Image.FromFile("NotOK.png");
                    }

                    row.Height = 40;
                }

                dataGridView1.Columns[0].Visible = false;
                dataGridView1.Columns[1].Width   = 400;
                dataGridView1.Columns[2].Visible = false;
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                dataGridView1.Columns.Clear();

                ComboItem item = comboBox1.SelectedItem as ComboItem;

                _selectedZone = item.Id;
                lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStats = new data.DAO.Logging.CurrentStatus();
                currentStats.ConnectionString = _connectionString;

                List<lfa.pmgmt.data.DTO.Logging.CurrentStatus> statusList = currentStats.List(_selectedZone);

                dataGridView1.DataSource = statusList;

                DataGridViewImageColumn iColumn = new DataGridViewImageColumn();

                dataGridView1.Columns.Add(iColumn);

                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if (row.Cells[2].Value.ToString().Equals("OK"))
                    {
                        DataGridViewImageCell column = row.Cells[3] as DataGridViewImageCell;
                        column.Value = Image.FromFile("OK.png");
                    }
                    else
                    {
                        DataGridViewImageCell column = row.Cells[3] as DataGridViewImageCell;
                        column.Value = Image.FromFile("NotOK.png");
                    }
                    row.Height = 40;
                }

                dataGridView1.Columns[0].Visible = false;
                dataGridView1.Columns[1].Width = 400;
                dataGridView1.Columns[2].Visible = false;
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }
Beispiel #3
0
        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");
                    }
                }
            }
        }
Beispiel #4
0
        private void SendSwitchState(string ipAddress, string unitName, int port, BravaCodes.SwitchState[] mySwitchStates)
        {
            BravaConnection myConnection = new BravaConnection();

            myConnection.BravaIP = System.Net.IPAddress.Parse(ipAddress);

            myConnection.BravaPort = port;

            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;

                    lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStatusDAO = new data.DAO.Logging.CurrentStatus();
                    currentStatusDAO.ConnectionString = _connectionString;
                    currentStatusDAO.Insert(unitName, "OK");
                }

                if (mySwitchbankUpdate.State == BravaTransaction.TransactionState.Failed)
                {
                    lfa.pmgmt.data.DAO.Logging.Log logDAO = new data.DAO.Logging.Log();
                    logDAO.ConnectionString = _connectionString;
                    logDAO.Insert(1, "Unit: " + unitName, "TCP/IP Communications Error due to: BRAVA API or Connection error.", DateTime.Now);

                    lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStatusDAO = new data.DAO.Logging.CurrentStatus();
                    currentStatusDAO.ConnectionString = _connectionString;
                    currentStatusDAO.Insert(unitName, "ERR");
                }
            }
            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: " + unitName, "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(unitName, "ERR");
            }
            catch (Exception nex)
            {
                HandleException(nex);
            }
            finally
            {
                mySocket.SocketClient.Close();
            }
        }
Beispiel #5
0
        private void SendSwitchState(string ipAddress, string unitName, int port, BravaCodes.SwitchState[] mySwitchStates)
        {
            BravaConnection myConnection = new BravaConnection();

            myConnection.BravaIP = System.Net.IPAddress.Parse(ipAddress);

            myConnection.BravaPort = port;

            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;

                    lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStatusDAO = new data.DAO.Logging.CurrentStatus();
                    currentStatusDAO.ConnectionString = _connectionString;
                    currentStatusDAO.Insert(unitName, "OK");
                }

                if (mySwitchbankUpdate.State == BravaTransaction.TransactionState.Failed)
                {
                    lfa.pmgmt.data.DAO.Logging.Log logDAO = new data.DAO.Logging.Log();
                    logDAO.ConnectionString = _connectionString;
                    logDAO.Insert(1, "Unit: " + unitName, "TCP/IP Communications Error due to: BRAVA API or Connection error.", DateTime.Now);

                    lfa.pmgmt.data.DAO.Logging.CurrentStatus currentStatusDAO = new data.DAO.Logging.CurrentStatus();
                    currentStatusDAO.ConnectionString = _connectionString;
                    currentStatusDAO.Insert(unitName, "ERR");
                }
            }
            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: " + unitName, "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(unitName, "ERR");
            }
            catch (Exception nex)
            {
                HandleException(nex);
            }
            finally
            {
                mySocket.SocketClient.Close();
            }
        }
Beispiel #6
0
        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());
            }
        }
Beispiel #7
0
        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");
                    }
                }
            }
        }
Beispiel #8
0
        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());
            }
        }
Beispiel #9
0
        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");
                    }
                }
            }
        }