Exemplo n.º 1
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)
        {
            // Resolve command station
            var cs = RailwayState.SelectCommandStation(Entity);

            if (cs == null)
            {
                return(false);
            }
            cs.AddInput(this);

            // Resolve destination blocks
            var routes = RailwayState.RouteStates.Where(x => x.Contains(this));

            destinationBlocks = routes.Select(x => x.To).ToList();
            if (Entity.Block != null)
            {
                var blockState = RailwayState.BlockStates[Entity.Block];
                if ((blockState != null) && (!destinationBlocks.Contains(blockState)))
                {
                    destinationBlocks.Add(blockState);
                }
            }

            return(true);
        }
Exemplo n.º 2
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)
        {
            if (!base.TryPrepareForUse(ui, statePersistence))
            {
                return(false);
            }
            var splb = lb as SerialPortLocoBuffer;

            if (splb != null)
            {
                var portNames = SerialPort.GetPortNames();
                if (!portNames.Contains(splb.PortName))
                {
                    var portName = ui.ChooseComPortName(this);
                    if (portNames.Contains(portName))
                    {
                        // Now we have an available port name
                        splb.PortName = portName;
                    }
                    else
                    {
                        // No available port name provided, we cannot be used.
                        return(false);
                    }
                }
            }
            return(true);
        }
Exemplo n.º 3
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)
        {
            if (!base.TryPrepareForUse(ui, statePersistence))
            {
                return(false);
            }
            try
            {
                client = new MqttClient(Entity.HostName);
                client.MqttMsgPublishReceived += onMqttMsgPublishReceived;
                client.MqttMsgPublished       += onMqttMsgPublished;
                client.ConnectionClosed       += onMqttConnectionClosed;

                // Power on
                client.Connect(clientID);

                // Subscribe to topics
                var topics = getSubscribeTopics();
                var qos    = topics.Select(x => MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE).ToArray();
                client.Subscribe(topics, qos);
            }
            catch (Exception ex)
            {
                Log.Error("Failed to connect to MQTT server: " + ex);
                return(false);
            }
            return(true);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Prepare this state for use in a live railway.
 /// Make sure all relevant connections to other state objects are resolved.
 /// Check the IsReadyForUse property afterwards if it has succeeded.
 /// </summary>
 internal void PrepareForUse(IStateUserInterface ui, IStatePersistence statePersistence)
 {
     if (!readyForUse)
     {
         readyForUse = TryPrepareForUse(ui, statePersistence);
     }
 }
Exemplo n.º 5
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)
        {
            if (!base.TryPrepareForUse(ui, statePersistence))
            {
                return(false);
            }
            var portNames = SerialPort.GetPortNames();

            if (!portNames.Contains(Entity.ComPortName))
            {
                var portName = ui.ChooseComPortName(this);
                if (portNames.Contains(portName))
                {
                    // Now we have an available port name
                    client.PortName = portName;
                }
                else
                {
                    // No available port name provided, we cannot be used.
                    return(false);
                }
            }
            // Request for status, this triggers the open process.
            PostWorkerAction(() => client.Status());
            return(true);
        }
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)
        {
            // Do not change the order of initialization
            commandStationStates.Cast <EntityState>().Foreach(x => x.PrepareForUse(ui, statePersistence));
            locStates.Cast <EntityState>().Foreach(x => x.PrepareForUse(ui, statePersistence));
            junctionStates.Cast <EntityState>().Foreach(x => x.PrepareForUse(ui, statePersistence));
            signalStates.Cast <EntityState>().Foreach(x => x.PrepareForUse(ui, statePersistence));
            blockStates.Cast <EntityState>().Foreach(x => x.PrepareForUse(ui, statePersistence));
            blockGroupStates.Cast <EntityState>().Foreach(x => x.PrepareForUse(ui, statePersistence));
            routeStates.Cast <EntityState>().Foreach(x => x.PrepareForUse(ui, statePersistence));
            sensorStates.Cast <EntityState>().Foreach(x => x.PrepareForUse(ui, statePersistence));
            outputStates.Cast <EntityState>().Foreach(x => x.PrepareForUse(ui, statePersistence));

            // Wrap up
            blockStates.Cast <BlockState>().Foreach(x => x.FinalizePrepare());
            routeStates.Cast <RouteState>().Foreach(x => x.FinalizePrepare());

            // Attach power event
            foreach (var csState in commandStationStates)
            {
                csState.Power.ActualChanged += (s, _) => power.OnActualChanged();
            }

            return(true);
        }
Exemplo n.º 7
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)
 {
     if (!base.TryPrepareForUse(ui, statePersistence))
     {
         return(false);
     }
     return(true);
 }
Exemplo n.º 8
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)
 {
     if (!base.TryPrepareForUse(ui, statePersistence))
     {
         return(false);
     }
     RailwayState.ModelTime.ActualChanged += (s, _) => OnTimeChanged();
     return(true);
 }
Exemplo n.º 9
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)
 {
     if (!base.TryPrepareForUse(ui, statePersistence))
     {
         return(false);
     }
     CommandStation.AddInput(busyInput);
     return(true);
 }
Exemplo n.º 10
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)
        {
            blocks.Clear();
            var myBlockEntities = Entity.Module.Blocks.Where(x => x.BlockGroup == Entity);

            blocks.AddRange(myBlockEntities.Select(x => RailwayState.BlockStates[x]));

            return(true);
        }
        /// <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)
        {
            var stream = Entity.Sound;

            if (stream != null)
            {
                sound = ui.SoundPlayer.Create(stream);
            }
            return(true);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Default ctor
        /// </summary>
        internal AppState(IStateUserInterface ui, MainForm mainForm)
        {
            Application.ThreadException += (s, x) => OnErrorPowerDown(x.Exception);
            Application.Idle            += (s, x) => OnApplicationIdle();
            this.ui       = ui;
            this.mainForm = mainForm;
            var persistence = new StatePersistence();

            statePersistence = persistence;
            server           = new Core.Server.Impl.Server();
        }
Exemplo n.º 13
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)
        {
            if (!base.TryPrepareForUse(ui, statePersistence))
            {
                return(false);
            }
            try
            {
                discoveryBroadcaster.Start();
            }
            catch (Exception ex)
            {
                Log.Error("Failed to start discovery broadcaster: " + ex);
                return(false);
            }
            try
            {
                localWorkerService = new LocalWorkerServiceImpl(this, Log);
                grpcServer         = new Server
                {
                    Services = { LocalWorkerService.BindService(localWorkerService) },
                    Ports    = { new ServerPort("0.0.0.0", Entity.APIPort, ServerCredentials.Insecure) }
                };
                grpcServer.Start();
            }
            catch (Exception ex)
            {
                Log.Error("Failed to start GRPC server: " + ex);
                return(false);
            }

            try
            {
                client = new MqttClient(Entity.HostName);
                client.MqttMsgPublishReceived += onMqttMsgPublishReceived;
                client.MqttMsgPublished       += onMqttMsgPublished;
                client.ConnectionClosed       += onMqttConnectionClosed;

                // Power on
                client.Connect(clientID);

                // Subscribe to topics
                var topics = getSubscribeTopics();
                var qos    = topics.Select(x => MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE).ToArray();
                client.Subscribe(topics, qos);
            }
            catch (Exception ex)
            {
                Log.Error("Failed to connect to MQTT server: " + ex);
                return(false);
            }
            return(true);
        }
Exemplo n.º 14
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)
 {
     if (Entity.Loc != null)
     {
         loc = RailwayState.LocStates[Entity.Loc];
         if (loc == null)
         {
             return(false);
         }
     }
     return(true);
 }
Exemplo n.º 15
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)
        {
            var cs = RailwayState.SelectCommandStation(Entity);

            if (cs == null)
            {
                return(false);
            }
            this.statePersistence = statePersistence;
            cs.AddLoc(this);
            commandStation = cs;
            return(true);
        }
Exemplo n.º 16
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)
        {
            if (!base.TryPrepareForUse(ui, statePersistence))
            {
                return(false);
            }
            var cs = SelectCommandStation();

            if (cs == null)
            {
                return(false);
            }
            cs.AddJunction(this);
            commandStation = cs;
            RailwayState.Power.ActualChanged += OnPowerActualChanged;
            return(true);
        }
Exemplo n.º 17
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)
        {
            var index = 0;

            while (index < actions.Count)
            {
                var x = (EntityState)actions[index];
                x.PrepareForUse(ui, statePersistence);
                if (!x.IsReadyForUse)
                {
                    actions.RemoveAt(index);
                }
                else
                {
                    index++;
                }
            }
            return(actions.Any());
        }
Exemplo n.º 18
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)
        {
            // Load state
            this.statePersistence = statePersistence;
            bool closed;

            if (statePersistence.TryGetBlockState(RailwayState, this, out closed))
            {
                Closed.Requested = closed;
            }
            junctions.Clear();
            var myJunctionEntities = Entity.Module.Junctions.Where(x => x.Block == Entity);

            junctions.AddRange(myJunctionEntities.Select(x => RailwayState.JunctionStates[x]));

            var groupEntity = Entity.BlockGroup;

            blockGroup = (groupEntity != null) ? RailwayState.BlockGroupStates[groupEntity] : null;

            return(true);
        }
Exemplo n.º 19
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)
        {
            var fromBlock = routes[0].From as IBlock;

            if (fromBlock == null)
            {
                return(false);
            }
            from = RailwayState.BlockStates[fromBlock];
            var lastRoute = routes[routes.Length - 1];
            var toBlock   = lastRoute.To as IBlock;

            if (toBlock == null)
            {
                return(false);
            }
            to = RailwayState.BlockStates[toBlock];
            destinationReachedTrigger.PrepareForUse(ui, statePersistence);

            foreach (var route in routes)
            {
                foreach (var item in route.CrossingJunctions)
                {
                    var junctionState = RailwayState.JunctionStates[item.Junction];
                    var state         = item.Accept(Default <JunctionWithStateBuilder> .Instance, junctionState);
                    if (state == null)
                    {
                        return(false);
                    }
                    crossingJunctions.Add(state);
                    hasNonStraightSwitches |= state.IsNonStraight;
                }
            }

            events.ForEach(x => x.PrepareForUse(ui, statePersistence));
            permissions.PrepareForUse(ui, statePersistence);


            return(true);
        }
Exemplo n.º 20
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)
        {
            // Resolve block.
            var entity = Entity;

            block = (entity.Block != null) ? RailwayState.BlockStates[entity.Block] : null;
            if (block == null)
            {
                return(false);
            }

            // Resolve command station
            var cs = RailwayState.SelectCommandStation(Entity);

            if (cs == null)
            {
                return(false);
            }

            commandStation = cs;
            commandStation.AddSignal(this);
            return(true);
        }
        /// <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)
        {
            if (!base.TryPrepareForUse(ui, statePersistence))
            {
                return(false);
            }

            /*var portNames = SerialPort.GetPortNames();
             * if (!portNames.Contains(Entity.ComPortName))
             * {
             *  var portName = ui.ChooseComPortName(this);
             *  if (portNames.Contains(portName))
             *  {
             *      // Now we have an available port name
             *      sender.PortName = portName;
             *  }
             *  else
             *  {
             *      // No available port name provided, we cannot be used.
             *      return false;
             *  }
             * }*/
            return(true);
        }
Exemplo n.º 22
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 abstract bool TryPrepareForUse(IStateUserInterface ui, IStatePersistence statePersistence);
Exemplo n.º 23
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)
 {
     activateTrigger.PrepareForUse(ui, statePersistence);
     deActivateTrigger.PrepareForUse(ui, statePersistence);
     return(base.TryPrepareForUse(ui, statePersistence));
 }
Exemplo n.º 24
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.º 25
0
 /// <summary>
 /// Prepare this state for use in a live railway.
 /// Make sure all relevant connections to other state objects are resolved.
 /// Check the IsReadyForUse property afterwards if it has succeeded.
 /// </summary>
 void IRailwayState.PrepareForUse(IStateUserInterface ui, IStatePersistence statePersistence)
 {
     PrepareForUse(ui, statePersistence);
 }
Exemplo n.º 26
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)
 {
     return(true);
 }
 /// <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)
 {
     junction = RailwayState.JunctionStates[Entity.Junction] as IInitializationJunctionState;
     return(junction != null);
 }