Ejemplo n.º 1
0
        /// <summary>
        /// Disambigates signals from various switches and acts upon them.  Disambiguation
        /// logic has not been implemented.  this is  TODO. 
        /// Maintains a list of switches that are currently held down.  When the swithches
        /// are released, it checks to see how long they were held and then triggers an
        /// event.
        /// </summary>
        /// <param name="switches">switch collection</param>
        /// <param name="switchObj">Which switch to act on</param>
        private void disambiguateAndAct(Dictionary<String, IActuatorSwitch> switches, IActuatorSwitch switchObj)
        {
            IActuatorSwitch switchActivated = null;
            long elapsedTime = 0;

            switch (switchObj.Action)
            {
                case SwitchAction.Down:
                    lock (switches)
                    {
                        if (!switches.ContainsKey(switchObj.Name))
                        {
                            Log.Debug("switches does not contain " + switchObj.Name + ". adding it");

                            // add it to the current list of active switches
                            switches.Add(switchObj.Name, switchObj);

                            switchObj.AcceptTimer.Restart();
                        }
                        else
                        {
                            Log.Debug("switches already contains " + switchObj.Name);
                        }
                    }

                    break;

                case SwitchAction.Up:
                    // remove from the list of currently accepted switches
                    lock (switches)
                    {
                        switchObj.RegisterSwitchUp();
                        if (switches.ContainsKey(switchObj.Name))
                        {
                            Log.Debug("switches contains " + switchObj.Name);
                            var activeSwitch = switches[switchObj.Name];

                            elapsedTime = (activeSwitch != null && activeSwitch.AcceptTimer.IsRunning) ? activeSwitch.AcceptTimer.ElapsedMilliseconds : 0;

                            if (switchObj.Actuate &&
                                activeSwitch != null &&
                                activeSwitch.AcceptTimer.IsRunning &&
                                activeSwitch.AcceptTimer.ElapsedMilliseconds >= CoreGlobals.AppPreferences.AcceptTime)
                            {
                                Log.Debug("Switch accepted!");
                                switchActivated = switchObj;
                            }
                            else
                            {
                                Log.Debug("Switch not found or actuate is false or timer not running or elapsedTime < accept time");
                            }

                            switches.Remove(switchObj.Name);
                        }
                        else
                        {
                            Log.Debug("switches does not contain " + switchObj.Name);
                        }
                    }

                    break;

                case SwitchAction.Trigger:
                    lock (switches)
                    {
                        if (switches.ContainsKey(switchObj.Name))
                        {
                            switches.Remove(switchObj.Name);
                        }
                    }

                    if (switchObj.Actuate)
                    {
                        switchActivated = switchObj;
                    }

                    break;
            }

            AuditLog.Audit(new AuditEventSwitchActuate(switchObj.Name, switchObj.Action.ToString(), switchObj.Actuator.Name, switchObj.Tag, elapsedTime));

            if (switchActivated != null)
            {
                act(switchObj);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Disambigates signals from various switches and acts upon them.  Disambiguation
        /// logic has not been implemented.  this is  TODO.
        /// Maintains a list of switches that are currently held down.  When the swithches
        /// are released, it checks to see how long they were held and then triggers an
        /// event.
        /// </summary>
        /// <param name="switches">switch collection</param>
        /// <param name="switchObj">Which switch to act on</param>
        private void disambiguateAndAct(Dictionary <String, IActuatorSwitch> switches, IActuatorSwitch switchObj)
        {
            IActuatorSwitch switchActivated = null;
            long            elapsedTime     = 0;

            switch (switchObj.Action)
            {
            case SwitchAction.Down:
                lock (switches)
                {
                    if (!switches.ContainsKey(switchObj.Name))
                    {
                        Log.Debug("switches does not contain " + switchObj.Name + ". adding it");

                        // add it to the current list of active switches
                        switches.Add(switchObj.Name, switchObj);

                        switchObj.AcceptTimer.Restart();
                    }
                    else
                    {
                        Log.Debug("switches already contains " + switchObj.Name);
                    }
                }

                break;

            case SwitchAction.Up:
                // remove from the list of currently accepted switches
                lock (switches)
                {
                    switchObj.RegisterSwitchUp();
                    if (switches.ContainsKey(switchObj.Name))
                    {
                        Log.Debug("switches contains " + switchObj.Name);
                        var activeSwitch = switches[switchObj.Name];

                        elapsedTime = (activeSwitch != null && activeSwitch.AcceptTimer.IsRunning) ? activeSwitch.AcceptTimer.ElapsedMilliseconds : 0;

                        if (switchObj.Actuate &&
                            activeSwitch != null &&
                            activeSwitch.AcceptTimer.IsRunning &&
                            activeSwitch.AcceptTimer.ElapsedMilliseconds >= CoreGlobals.AppPreferences.AcceptTime)
                        {
                            Log.Debug("Switch accepted!");
                            switchActivated = switchObj;
                        }
                        else
                        {
                            Log.Debug("Switch not found or actuate is false or timer not running or elapsedTime < accept time");
                        }

                        switches.Remove(switchObj.Name);
                    }
                    else
                    {
                        Log.Debug("switches does not contain " + switchObj.Name);
                    }
                }

                break;

            case SwitchAction.Trigger:
                lock (switches)
                {
                    if (switches.ContainsKey(switchObj.Name))
                    {
                        switches.Remove(switchObj.Name);
                    }
                }

                if (switchObj.Actuate)
                {
                    switchActivated = switchObj;
                }

                break;
            }

            AuditLog.Audit(new AuditEventSwitchActuate(switchObj.Name, switchObj.Action.ToString(), switchObj.Actuator.Name, switchObj.Tag, elapsedTime));

            if (switchActivated != null)
            {
                act(switchObj);
            }
        }