Exemplo n.º 1
0
 private void SelectNextState(IRouteState route)
 {
     if (lastSensor != null)
     {
         lastSensor.Active.Actual = false;
     }
     if (state == States.Initial)
     {
         var sensor = route.Sensors.FirstOrDefault(x => route.IsEnteringDestinationSensor(x, loc));
         if (sensor != null)
         {
             sensor.Active.Actual = true;
             lastSensor           = sensor;
             state = States.Enter;
             return;
         }
     }
     // Activate reached sensor
     {
         var sensor = route.Sensors.FirstOrDefault(x => route.IsReachedDestinationSensor(x, loc));
         if (sensor != null)
         {
             sensor.Active.Actual = true;
             lastSensor           = sensor;
             state = States.Initial;
         }
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// Default ctor
 /// </summary>
 public SensorItem(ISensor entity, ISensorState state, ItemContext context, bool interactive)
     : base(entity, false, context)
 {
     this.state = state;
     if (interactive)
     {
         MouseHandler = new EntityClickHandler(null, state);
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// Try to get the behavior for the given sensor.
 /// </summary>
 public bool TryGetBehavior(ISensorState sensor, out IRouteEventBehaviorState behavior)
 {
     if (behaviors.TryGetValue(sensor, out behavior))
     {
         behaviors.Remove(sensor); // Avoid listening to the sensor twice
         return(true);
     }
     return(false);
 }
Exemplo n.º 4
0
        public DeviceResources(IFactory factory, IHMDDevice handle)
        {
            if (factory == null || handle == null)
                throw new ArgumentNullException();

            Info = handle.Info;
            Device = handle;
            Sensor = Device.GetSensorState(0.0);

            if (Info == null)
                throw new ArgumentNullException();
            if (Device == null)
                throw new ArgumentNullException();
            if (Sensor == null)
                System.Diagnostics.Trace.TraceWarning("Unable to create Sensor");

            if (Sensor != null)
                Key = new DeviceKey(Device.SensorInfo);
        }
 /// <summary>
 /// Handle a ghost sensor event
 /// </summary>
 private void HandleGhost(ISensorState sensor)
 {
     log.Trace("HandleGhost {0}", sensor);
     // Notify about it
     if (UnexpectedSensorActivated != null)
     {
         var args = new UnexpectedSensorActivatedEventArgs(sensor);
         UnexpectedSensorActivated(this, args);
         if (args.Handled)
         {
             return;
         }
     }
     else
     {
         // Not handled, power off
         log.Warn("Ghost activation in sensor {0}, global power down.", sensor);
         railwayState.Power.Requested = false;
     }
 }
Exemplo n.º 6
0
 /// <summary>
 /// Prepare this state for use in a live railway.
 /// Make sure all relevant connections to other state objects are resolved.
 /// </summary>
 /// <returns>True if the entity is now ready for use in a live railway, false otherwise.</returns>
 protected override bool TryPrepareForUse(IStateUserInterface ui, IStatePersistence statePersistence)
 {
     sensor = RailwayState.SensorStates[Entity.Sensor];
     return(sensor != null);
 }
Exemplo n.º 7
0
 /// <summary>
 /// Does this route contains the given sensor (either as entering or reached)
 /// </summary>
 public bool Contains(ISensorState sensorState)
 {
     return(events.Any(x => x.Sensor == sensorState));
 }
Exemplo n.º 8
0
 /// <summary>
 /// Is the given sensor listed as one of the "entering destination" sensors of this route?
 /// </summary>
 public bool IsReachedDestinationSensor(ISensorState sensor, ILocState loc)
 {
     return(events.Any(x => (x.Sensor == sensor) && x.Behaviors.Any(b => b.AppliesTo(loc) && (b.StateBehavior == RouteStateBehavior.Reached))));
 }
Exemplo n.º 9
0
 /// <summary>
 /// Does this route contain an event for the given sensor for my loc?
 /// </summary>
 public bool Contains(ISensorState sensor)
 {
     return(behaviors.ContainsKey(sensor));
 }
Exemplo n.º 10
0
 /// <summary>
 /// Gets advanced info for the given sensor
 /// </summary>
 internal bool TryGetLoc(ISensorState sensorState, out FeedbackModule feedbackModule)
 {
     return(sensorIds.TryGetValue(sensorState, out feedbackModule));
 }
        /// <summary>
        /// Active state of given sensor has changed.
        /// </summary>
        private void OnSensorActiveChanged(ISensorState sensor)
        {
            if (!sensor.Active.Actual)
            {
                // Sensor became inactive
                return;
            }

            log.Trace("OnSensorActive {0}", sensor);
            var locsWithRoutes = autoLocs.Where(x => x.CurrentRoute.Actual != null).ToList();
            var locsWithSensor = locsWithRoutes.Where(x => x.CurrentRoute.Actual.Contains(sensor));
            var ghost          = true;

            // Update state of all locs
            foreach (var loc in locsWithSensor)
            {
                IRouteEventBehaviorState behavior;
                var route = loc.CurrentRoute.Actual;
                if (!route.TryGetBehavior(sensor, out behavior))
                {
                    continue;
                }
                // Save in loc
                loc.LastEventBehavior = behavior;

                // Update state
                var autoLocState = loc.AutomaticState.Actual;
                ghost = false;
                switch (behavior.StateBehavior)
                {
                case RouteStateBehavior.NoChange:
                    // No change
                    break;

                case RouteStateBehavior.Enter:
                    if (autoLocState == AutoLocState.Running)
                    {
                        loc.AutomaticState.Actual = AutoLocState.EnterSensorActivated;
                        RequestUpdate();
                    }
                    break;

                case RouteStateBehavior.Reached:
                    if ((autoLocState == AutoLocState.Running) ||
                        (autoLocState == AutoLocState.EnterSensorActivated) ||
                        (autoLocState == AutoLocState.EnteringDestination))
                    {
                        loc.AutomaticState.Actual = AutoLocState.ReachedSensorActivated;
                        RequestUpdate();
                    }
                    break;
                }

                // Update speed
                switch (behavior.SpeedBehavior)
                {
                case LocSpeedBehavior.NoChange:
                    // No change
                    break;

                case LocSpeedBehavior.Default:
                    // This is handled in the applicable states
                    break;

                case LocSpeedBehavior.Maximum:
                    loc.Speed.Requested = loc.GetMaximumSpeed(route.Route);
                    break;

                case LocSpeedBehavior.Medium:
                    loc.Speed.Requested = loc.GetMediumSpeed(route.Route);
                    break;

                case LocSpeedBehavior.Minimum:
                    loc.Speed.Requested = loc.SlowSpeed;
                    break;
                }
            }

            // Handle ghost events
            if (ghost)
            {
                // Is the sensor connected to any route, other then the routes that are active?
                var destinationBlocks = sensor.DestinationBlocks;
                var activeRoutes      = locsWithRoutes.Select(x => x.CurrentRoute.Actual).ToList();
                if (destinationBlocks.Any(x => !activeRoutes.Any(r => r.Route.Contains(x))))
                {
                    // Yes, now consider it a ghost event
                    HandleGhost(sensor);
                }
            }
        }