Ejemplo n.º 1
0
    private void SetBoatLightState(LaneTypes lanetype, int groupID, int componentID, BoatAndTrainLightState newState)
    {
        //Finds all warning lights with certain properties
        List <BoatLight> boatlightList = boatlights.FindAll(l => l.laneType == lanetype && l.GroupID == groupID && l.ComponentID == componentID);

        //No warning light found
        if (boatlightList == null)
        {
            Debug.LogError($"ERROR: Boatlight with groupID: {groupID} and componentID: {componentID} not found");
            return;
        }

        foreach (var light in boatlightList)
        {
            if (newState != light.state)
            {
                light.state = newState;
            }
        }
    }
Ejemplo n.º 2
0
    // Update is called once per frame
    void Update()
    {
        //Switch between states at random intervals if not listening to controller
        if (!M2QTTController.Instance.ListenToController)
        {
            currentTime += Time.deltaTime;

            if (currentTime >= timeTillChange)
            {
                currentTime -= timeTillChange;

                if (state == BoatAndTrainLightState.Red)
                {
                    state = BoatAndTrainLightState.Green;
                }
                else if (state == BoatAndTrainLightState.Green)
                {
                    state = BoatAndTrainLightState.Red;
                }
            }

            //Set new random interval
            timeTillChange = Random.Range(4f, 18f);
        }

        bool barrierActive = barrier.IsActive;

        if (state == BoatAndTrainLightState.Red && !barrierActive)
        {
            //Light is red, activate the barrier
            barrier.IsActive = true;
        }
        else if (state == BoatAndTrainLightState.Green && barrierActive)
        {
            //Light is green, deactivate the barrier
            barrier.IsActive = false;
        }
    }
Ejemplo n.º 3
0
    //Handles MQTT messages when they are received
    public void HandleMessage(string topic, string message)
    {
        //Parse and store topic information
        TopicInformation info = ParseTopic(topic);

        switch (info.componentType)
        {
        case ComponentTypes.traffic_light:
            //Handle traffic lights
            //Try setting new traffic light state
            try
            {
                TrafficLightState newState = (TrafficLightState)System.Enum.Parse(typeof(TrafficLightState), message);
                SetTrafficLightState(info.laneType, info.groupID, info.componentID, newState);
            }
            catch
            {
                Debug.LogError($"ERROR: Tried setting invalid traffic light state: '{message}' for traffic light with GroupID: {info.groupID} and ComponentID: {info.componentID}");
            }
            break;

        case ComponentTypes.warning_light:
            //Handle warning lights
            //Try setting new warning light state
            try
            {
                WarningLightState newState = (WarningLightState)System.Enum.Parse(typeof(WarningLightState), message);
                SetWarningLightState(info.laneType, info.groupID, info.componentID, newState);
            }
            catch
            {
                Debug.LogError($"ERROR: Tried setting invalid warning light state: '{message}' for warning light with GroupID: {info.groupID} and ComponentID: {info.componentID}");
            }
            break;

        case ComponentTypes.boat_light:
            //Handle boat lights
            //Try setting new boat light state
            try
            {
                BoatAndTrainLightState newState = (BoatAndTrainLightState)System.Enum.Parse(typeof(BoatAndTrainLightState), message);
                SetBoatLightState(info.laneType, info.groupID, info.componentID, newState);
            }
            catch
            {
                Debug.LogError($"ERROR: Tried setting invalid boat light state: '{message}' for boat light with GroupID: {info.groupID} and ComponentID: {info.componentID}");
            }
            break;

        case ComponentTypes.sensor:     //Don't have to handle sensor data
            break;

        case ComponentTypes.barrier:
            //Handle barriers
            //Try setting new barrier state
            try
            {
                BarrierState newState = (BarrierState)System.Enum.Parse(typeof(BarrierState), message);
                SetBarrierState(info.laneType, info.groupID, info.componentID, newState);
            }
            catch
            {
                Debug.LogError($"ERROR: Tried setting invalid barrier state: '{message}' for barrier with GroupID: {info.groupID} and ComponentID: {info.componentID}");
            }
            break;

        case ComponentTypes.deck:
            //Handle deck(bridge)
            //Try setting new deck(bridge) state
            try
            {
                DeckState newState = (DeckState)System.Enum.Parse(typeof(DeckState), message);
                SetDeckState(info.laneType, info.groupID, info.componentID, newState);
            }
            catch
            {
                Debug.LogError($"ERROR: Tried setting invalid deck(bridge) state: '{message}' for deck with GroupID: {info.groupID} and ComponentID: {info.componentID}");
            }
            break;

        case ComponentTypes.train_light:
            //Handle train_light
            //Try setting new train_light state
            try
            {
                BoatAndTrainLightState newState = (BoatAndTrainLightState)System.Enum.Parse(typeof(BoatAndTrainLightState), message);
                SetTrainLightState(info.laneType, info.groupID, info.componentID, newState);
            }
            catch
            {
                Debug.LogError($"ERROR: Tried setting invalid train_light state: '{message}' for train_light with GroupID: {info.groupID} and ComponentID: {info.componentID}");
            }
            break;
        }
    }