// 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 == WarningLightState.On) { state = WarningLightState.Off; } else if (state == WarningLightState.Off) { state = WarningLightState.On; } } //Set new random interval timeTillChange = Random.Range(4f, 18f); } bool barrierActive = barrier.IsActive; if (state == WarningLightState.On && !barrierActive) { //Light is red, activate the barrier barrier.IsActive = true; } else if (state == WarningLightState.Off && barrierActive) { //Light is green, deactivate the barrier barrier.IsActive = false; } }
//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; } }
private void SetWarningLightState(LaneTypes lanetype, int groupID, int componentID, WarningLightState newState) { //Finds all warning lights with certain properties List <WarningLight> warninglightList = warninglights.FindAll(l => l.laneType == lanetype && l.GroupID == groupID && l.ComponentID == componentID); //No warning light found if (warninglightList == null) { Debug.LogError($"ERROR: Warninglight with groupID: {groupID} and componentID: {componentID} not found"); return; } foreach (var light in warninglightList) { if (newState != light.state) { light.state = newState; } } }