Esempio n. 1
0
        private static void SetSwitchState(string swState, int status, BravaCodes.SwitchState[] mySwitchStates)
        {
            if (swState == "Switch 1")
            {
                if (status == 1)
                {
                    mySwitchStates[0] = BravaCodes.SwitchState.SwitchOn;
                }
                else
                {
                    mySwitchStates[0] = BravaCodes.SwitchState.SwitchOff;
                }
            }

            if (swState == "Switch 2")
            {
                if (status == 1)
                {
                    mySwitchStates[1] = BravaCodes.SwitchState.SwitchOn;
                }
                else
                {
                    mySwitchStates[1] = BravaCodes.SwitchState.SwitchOff;
                }
            }

            if (swState == "Switch 3")
            {// cbSingleSw3.Checked)
                if (status == 1)
                {
                    mySwitchStates[2] = BravaCodes.SwitchState.SwitchOn;
                }
                else
                {
                    mySwitchStates[2] = BravaCodes.SwitchState.SwitchOff;
                }
            }

            if (swState == "Switch 4") // cbSingleSw4.Checked)
            {
                if (status == 1)
                {
                    mySwitchStates[3] = BravaCodes.SwitchState.SwitchOn;
                }
                else
                {
                    mySwitchStates[3] = BravaCodes.SwitchState.SwitchOff;
                }
            }

            if (swState == "Switch 5") // cbSingleSw5.Checked)
            {
                if (status == 1)
                {
                    mySwitchStates[4] = BravaCodes.SwitchState.SwitchOn;
                }
                else
                {
                    mySwitchStates[4] = BravaCodes.SwitchState.SwitchOff;
                }
            }

            if (swState == "Switch 6") // cbSingleSw6.Checked)
            {
                if (status == 1)
                {
                    mySwitchStates[5] = BravaCodes.SwitchState.SwitchOn;
                }
                else
                {
                    mySwitchStates[5] = BravaCodes.SwitchState.SwitchOff;
                }
            }

            if (swState == "Switch 7") // cbSingleSw7.Checked)
            {
                if (status == 1)
                {
                    mySwitchStates[6] = BravaCodes.SwitchState.SwitchOn;
                }
                else
                {
                    mySwitchStates[6] = BravaCodes.SwitchState.SwitchOff;
                }
            }
            if (swState == "Switch 8") // cbSingleSw8.Checked)
            {
                if (status == 1)
                {
                    mySwitchStates[7] = BravaCodes.SwitchState.SwitchOn;
                }
                else
                {
                    mySwitchStates[7] = BravaCodes.SwitchState.SwitchOff;
                }
            }
        }
Esempio n. 2
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();
            }
        }
Esempio n. 3
0
        private static void SetSwitchState(string swState, int status, BravaCodes.SwitchState[] mySwitchStates, string identifier)
        {
            if (_switchStates == null)
            {
                _switchStates = new Dictionary<string, string>();
            }

            if (_switchStates.ContainsKey(identifier))
            {
                int checkStatus = int.Parse(_switchStates[identifier].ToString());

                if (status != checkStatus)
                {
                    if (swState == "Switch 1")
                    {
                        if (status == 1)
                        {
                            mySwitchStates[0] = BravaCodes.SwitchState.SwitchOn;
                        }
                        else
                        {
                            mySwitchStates[0] = BravaCodes.SwitchState.SwitchOff;
                        }
                    }

                    if (swState == "Switch 2")
                    {
                        if (status == 1)
                        {
                            mySwitchStates[1] = BravaCodes.SwitchState.SwitchOn;
                        }
                        else
                        {
                            mySwitchStates[1] = BravaCodes.SwitchState.SwitchOff;
                        }
                    }

                    if (swState == "Switch 3")
                    {// cbSingleSw3.Checked)
                        if (status == 1)
                        {
                            mySwitchStates[2] = BravaCodes.SwitchState.SwitchOn;
                        }
                        else
                        {
                            mySwitchStates[2] = BravaCodes.SwitchState.SwitchOff;
                        }
                    }

                    if (swState == "Switch 4") // cbSingleSw4.Checked)
                    {
                        if (status == 1)
                        {
                            mySwitchStates[3] = BravaCodes.SwitchState.SwitchOn;
                        }
                        else
                        {
                            mySwitchStates[3] = BravaCodes.SwitchState.SwitchOff;
                        }
                    }

                    if (swState == "Switch 5") // cbSingleSw5.Checked)
                    {
                        if (status == 1)
                        {
                            mySwitchStates[4] = BravaCodes.SwitchState.SwitchOn;
                        }
                        else
                        {
                            mySwitchStates[4] = BravaCodes.SwitchState.SwitchOff;
                        }
                    }

                    if (swState == "Switch 6") // cbSingleSw6.Checked)
                    {
                        if (status == 1)
                        {
                            mySwitchStates[5] = BravaCodes.SwitchState.SwitchOn;
                        }
                        else
                        {
                            mySwitchStates[5] = BravaCodes.SwitchState.SwitchOff;
                        }
                    }

                    if (swState == "Switch 7") // cbSingleSw7.Checked)
                    {
                        if (status == 1)
                        {
                            mySwitchStates[6] = BravaCodes.SwitchState.SwitchOn;
                        }
                        else
                        {
                            mySwitchStates[6] = BravaCodes.SwitchState.SwitchOff;
                        }
                    }

                    if (swState == "Switch 8") // cbSingleSw8.Checked)
                    {
                        if (status == 1)
                        {
                            mySwitchStates[7] = BravaCodes.SwitchState.SwitchOn;
                        }
                        else
                        {
                            mySwitchStates[7] = BravaCodes.SwitchState.SwitchOff;
                        }
                    }

                    _switchStates.Remove(identifier);

                    System.Threading.Thread.Sleep(200);

                    _switchStates.Add(identifier, status.ToString());
                }
            }
            else
            {
                _switchStates.Add(identifier, status.ToString());

                if (swState == "Switch 1")
                {
                    if (status == 1)
                    {
                        mySwitchStates[0] = BravaCodes.SwitchState.SwitchOn;
                    }
                    else
                    {
                        mySwitchStates[0] = BravaCodes.SwitchState.SwitchOff;
                    }
                }

                if (swState == "Switch 2")
                {
                    if (status == 1)
                    {
                        mySwitchStates[1] = BravaCodes.SwitchState.SwitchOn;
                    }
                    else
                    {
                        mySwitchStates[1] = BravaCodes.SwitchState.SwitchOff;
                    }
                }

                if (swState == "Switch 3")
                {// cbSingleSw3.Checked)
                    if (status == 1)
                    {
                        mySwitchStates[2] = BravaCodes.SwitchState.SwitchOn;
                    }
                    else
                    {
                        mySwitchStates[2] = BravaCodes.SwitchState.SwitchOff;
                    }
                }

                if (swState == "Switch 4") // cbSingleSw4.Checked)
                {
                    if (status == 1)
                    {
                        mySwitchStates[3] = BravaCodes.SwitchState.SwitchOn;
                    }
                    else
                    {
                        mySwitchStates[3] = BravaCodes.SwitchState.SwitchOff;
                    }
                }

                if (swState == "Switch 5") // cbSingleSw5.Checked)
                {
                    if (status == 1)
                    {
                        mySwitchStates[4] = BravaCodes.SwitchState.SwitchOn;
                    }
                    else
                    {
                        mySwitchStates[4] = BravaCodes.SwitchState.SwitchOff;
                    }
                }

                if (swState == "Switch 6") // cbSingleSw6.Checked)
                {
                    if (status == 1)
                    {
                        mySwitchStates[5] = BravaCodes.SwitchState.SwitchOn;
                    }
                    else
                    {
                        mySwitchStates[5] = BravaCodes.SwitchState.SwitchOff;
                    }
                }

                if (swState == "Switch 7") // cbSingleSw7.Checked)
                {
                    if (status == 1)
                    {
                        mySwitchStates[6] = BravaCodes.SwitchState.SwitchOn;
                    }
                    else
                    {
                        mySwitchStates[6] = BravaCodes.SwitchState.SwitchOff;
                    }
                }

                if (swState == "Switch 8") // cbSingleSw8.Checked)
                {
                    if (status == 1)
                    {
                        mySwitchStates[7] = BravaCodes.SwitchState.SwitchOn;
                    }
                    else
                    {
                        mySwitchStates[7] = BravaCodes.SwitchState.SwitchOff;
                    }
                }
            }
        }
Esempio n. 4
0
 private static void SetSwitchStateBatch(string swState, int status, BravaCodes.SwitchState[] mySwitchStates)
 {
     mySwitchStates[0] = BravaCodes.SwitchState.SwitchOff;
        mySwitchStates[1] = BravaCodes.SwitchState.SwitchOff;
        mySwitchStates[2] = BravaCodes.SwitchState.SwitchOff;
        mySwitchStates[3] = BravaCodes.SwitchState.SwitchOff;
        mySwitchStates[4] = BravaCodes.SwitchState.SwitchOff;
        mySwitchStates[5] = BravaCodes.SwitchState.SwitchOff;
        mySwitchStates[6] = BravaCodes.SwitchState.SwitchOff;
        mySwitchStates[7] = BravaCodes.SwitchState.SwitchOff;
 }
Esempio n. 5
0
        // 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;
        }