/// <summary>
        /// Sets the power state of the provided IPowerState given that the
        /// specified BTCmd will be executed next.  A null BTCmd indicates that
        /// there are no more pending BTCmds to be executed from any connection.
        /// </summary>
        /// <param name="ips">
        /// The IPowerState
        /// </param>
        /// <param name="conn">
        /// The BTConnection for which the BTCmd is being executed.
        /// </param>
        /// <param name="cmd">
        /// The next BTCmd to be executed, or null if there are no pending BTCmds.
        /// </param>
        public void ApplyPolicy(IPowerState ips, BTConnection conn, BTConnection.BTCmd cmd)
        {
            lock (this)
            {
                if (null != cmd)
                {   // Dispose of the Timer if it's active, then PARK the chip
                    if (null != m_powerOffTimer)
                    {
                        m_powerOffTimer.Dispose();
                        m_powerOffTimer = null;
                    }

                    PowerState state = PowerState.Park;

                    while (!ips.SetPowerState(ref state))
                    {
                        Thread.Sleep(1);
                    }
                }
                else
                {   // Then set a Timer that will turn the chip OFF if no new commands in the specified timeout
                    if (null == conn || !conn.Connected)
                    {
                        if (null == m_powerOffTimer)
                        {
                            m_powerOffTimer = new Timer(PowerOffTimerFired, ips, c_powerOffTimeout, Timeout.Infinite);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Sets the power state of the provided IPowerState given that the
        /// specified BTCmd will be executed next.  A null BTCmd indicates that
        /// there are no more pending BTCmds to be executed from any connection.
        /// </summary>
        /// <param name="ips">
        /// The IPowerState
        /// </param>
        /// <param name="conn">
        /// The BTConnection for which the BTCmd is being executed.
        /// </param>
        /// <param name="cmd">
        /// The next BTCmd to be executed, or null if there are no pending BTCmds.
        /// </param>
        public void ApplyPolicy(IPowerState ips, BTConnection conn, BTConnection.BTCmd cmd)
        {
            lock (this)
            {
                if (null != cmd)
                {   // Dispose of the Timer if it's active, then PARK the chip
                    if (null != m_powerOffTimer)
                    {
                        m_powerOffTimer.Dispose();
                        m_powerOffTimer = null;
                    }

                    PowerState state = PowerState.Park;

                    while (!ips.SetPowerState(ref state))
                    {
                        Thread.Sleep(1);
                    }
                }
                else
                {   // Then set a Timer that will turn the chip OFF if no new commands in the specified timeout
                    if (null == conn || !conn.Connected)
                    {
                        if (null == m_powerOffTimer)
                        {
                            m_powerOffTimer = new Timer(PowerOffTimerFired, ips, c_powerOffTimeout, Timeout.Infinite);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// The event handler for the power off Timer firing event.
        /// </summary>
        /// <param name="sender">
        /// The sending object (i.e. the Timer).
        /// </param>
        /// <param name="eargs">
        /// The event arguments.
        /// </param>
        public void PowerOffTimerFired(object state)
        {
            IPowerState ips = (IPowerState)state;

            lock (this)
            {
                if (null != m_powerOffTimer)
                {
                    m_powerOffTimer.Dispose();
                    m_powerOffTimer = null;

                    PowerState ps = PowerState.Off;

                    while (!ips.SetPowerState(ref ps))
                    {
                        Thread.Sleep(1);
                    }
                }
            }
        }