/// <summary> /// Metoda wywoływana w momencie aktywowania odpowiedniego czujnika <see cref="libcore.Sensor"/> zawierajaca logikę sterowania semaforami. /// </summary> /// <param name='name'> /// Nazwa czujnika /// </param> public void Update(String name) { Logging.Instance.Send("Aktywowano sensor:" + name); if (name == "03" || name == "07") // Pociąg opuścił stację { this.isEmptyStation = true; this.p1 = true; this.p2 = true; this.boxes[0].GetLight().TurnOff(); this.boxes[3].GetLight().TurnOff(); StationMessageSender.Instance.Send(new List<bool> { p1, p2 }); Logging.Instance.Send("Pociąg opuścił stację"); if (this.trainQueue.Count > 0) { string train = this.trainQueue[0]; this.trainQueue.RemoveAt(0); this.Update(train); } } if (name == "01") // Pociąg pojawił się z prawej strony { if (!this.isEmptyStation) { this.trainQueue.Add(name); Logging.Instance.Send("Pociąg z prawej strony czeka na wjazd"); return; } else { this.train = TrainSender.Instance.Receive(); if (this.train.track == 1) { this.t1 = false; this.t2 = false; this.t3 = false; this.t4 = false; } else if (this.train.track == 2) { this.t1 = true; this.t4 = true; this.t5 = true; this.t8 = true; this.t6 = false; this.t7 = false; this.t9 = false; this.t10 = false; } else if (this.train.track == 3) { this.t1 = true; this.t4 = true; this.t5 = true; this.t8 = true; this.t9 = true; this.t10 = true; this.t11 = true; this.t12 = true; this.t6 = false; this.t7 = false; this.t9 = false; this.t10 = false; } TrackSender.Instance.Send(new List<bool> { t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12 }); Logging.Instance.Send("Zmiana zwrotnic"); if (this.train.peron == 1) // Pociąg z prawej strony chce wjechać na peron 1 { this.boxes[0].GetLight().TurnOn(); //|| Droga dla pociągu z prawej strony jest odblokowana this.boxes[1].GetLight().TurnOn(); //|| this.boxes[2].GetLight().TurnOn(); //|| this.boxes[4].GetLight().TurnOn(); //|| this.boxes[3].GetLight().TurnOff(); //|| Droga dla pociągu z lewej strony jest zablokowana this.boxes[5].GetLight().TurnOff(); //|| this.boxes[6].GetLight().TurnOff(); //|| this.boxes[7].GetLight().TurnOff(); //|| this.boxes[8].GetLight().TurnOff(); //|| this.boxes[9].GetLight().TurnOff(); //|| this.boxes[10].GetLight().TurnOff(); //|| } else if (this.train.peron == 2) // Pociąg z prawej strony chce wjechać na peron 1 { this.boxes[0].GetLight().TurnOn(); //|| Droga dla pociągu z prawej strony jest odblokowana this.boxes[2].GetLight().TurnOn(); //|| this.boxes[4].GetLight().TurnOn(); //|| this.boxes[8].GetLight().TurnOn(); //|| this.boxes[9].GetLight().TurnOn(); //|| this.boxes[1].GetLight().TurnOff(); //|| this.boxes[3].GetLight().TurnOff(); //|| Droga dla pociągu z lewej strony jest zablokowana this.boxes[5].GetLight().TurnOff(); //|| this.boxes[6].GetLight().TurnOff(); //|| this.boxes[7].GetLight().TurnOff(); //|| this.boxes[10].GetLight().TurnOff(); //|| } this.isEmptyStation = false; } } if (name == "02") { this.p1 = false; this.p2 = true; this.boxes[0].GetLight().TurnOff(); this.boxes[4].GetLight().TurnOff(); this.boxes[8].GetLight().TurnOff(); StationMessageSender.Instance.Send(new List<bool> { p1, p2 }); } if (name == "04") // Pociąg pojawił się z lewej strony { if (!this.isEmptyStation) { this.trainQueue.Add(name); Logging.Instance.Send("Pociąg z lewej strony czeka na wjazd"); return; } else { this.train = TrainSender.Instance.Receive(); if (this.train.track == 1) { this.t2 = true; this.t3 = true; this.t6 = true; this.t7 = true; this.t5 = false; this.t8 = false; } else if (this.train.track == 2) { this.t5 = false; this.t6 = false; this.t7 = false; this.t8 = false; this.t9 = false; this.t10 = false; } else if (this.train.track == 3) { this.t5 = false; this.t6 = false; this.t7 = false; this.t8 = false; this.t9 = true; this.t10 = true; this.t11 = true; this.t12 = true; } TrackSender.Instance.Send(new List<bool> { t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12 }); Logging.Instance.Send("Zmiana zwrotnic"); if (this.train.peron == 1) // Pociąg z prawej strony chce wjechać na peron 1 { this.boxes[3].GetLight().TurnOn(); //|| Droga dla pociągu z prawej strony jest odblokowana this.boxes[5].GetLight().TurnOn(); //|| this.boxes[6].GetLight().TurnOn(); //|| this.boxes[7].GetLight().TurnOn(); //|| this.boxes[0].GetLight().TurnOff(); //|| Droga dla pociągu z prawej strony jest zablokowana this.boxes[1].GetLight().TurnOff(); //|| this.boxes[2].GetLight().TurnOff(); //|| this.boxes[4].GetLight().TurnOff(); //|| this.boxes[8].GetLight().TurnOff(); //|| this.boxes[9].GetLight().TurnOff(); //|| this.boxes[10].GetLight().TurnOff(); //|| } else if (this.train.peron == 2) // Pociąg z prawej strony chce wjechać na peron 1 { this.boxes[3].GetLight().TurnOn(); //|| Droga dla pociągu z lewej strony jest odblokowana this.boxes[6].GetLight().TurnOn(); //|| this.boxes[7].GetLight().TurnOn(); //|| this.boxes[10].GetLight().TurnOn(); //|| this.boxes[0].GetLight().TurnOff(); //|| Droga dla pociągu z prawej strony jest zablokowana this.boxes[1].GetLight().TurnOff(); //|| this.boxes[2].GetLight().TurnOff(); //|| this.boxes[4].GetLight().TurnOff(); //|| this.boxes[5].GetLight().TurnOff(); //|| this.boxes[8].GetLight().TurnOff(); //|| this.boxes[9].GetLight().TurnOff(); //|| } this.isEmptyStation = false; } } if (name == "05") { this.p1 = true; this.p2 = false; this.boxes[0].GetLight().TurnOff(); this.boxes[1].GetLight().TurnOff(); this.boxes[8].GetLight().TurnOff(); StationMessageSender.Instance.Send(new List<bool> { p1, p2 }); } if (name == "06") { this.p1 = false; this.p2 = true; this.boxes[3].GetLight().TurnOff(); this.boxes[7].GetLight().TurnOff(); this.boxes[10].GetLight().TurnOff(); StationMessageSender.Instance.Send(new List<bool> { p1, p2 }); } if (name == "08") { this.p1 = false; this.p2 = false; this.boxes[3].GetLight().TurnOff(); this.boxes[5].GetLight().TurnOff(); this.boxes[10].GetLight().TurnOff(); StationMessageSender.Instance.Send(new List<bool> { p1, p2 }); } if (name == "09") { this.p1 = true; this.p2 = false; this.boxes[0].GetLight().TurnOff(); this.boxes[1].GetLight().TurnOff(); this.boxes[4].GetLight().TurnOff(); StationMessageSender.Instance.Send(new List<bool> { p1, p2 }); } if (name == "10") { } if (name == "11") { this.p1 = true; this.p2 = false; this.boxes[5].GetLight().TurnOff(); this.boxes[5].GetLight().TurnOff(); this.boxes[7].GetLight().TurnOff(); StationMessageSender.Instance.Send(new List<bool> { p1, p2 }); } if (name == "off") { this.boxes[0].GetLight().TurnOff(); this.boxes[1].GetLight().TurnOff(); this.boxes[2].GetLight().TurnOff(); this.boxes[3].GetLight().TurnOff(); this.boxes[4].GetLight().TurnOff(); this.boxes[5].GetLight().TurnOff(); this.boxes[6].GetLight().TurnOff(); this.boxes[7].GetLight().TurnOff(); this.boxes[8].GetLight().TurnOff(); this.boxes[9].GetLight().TurnOff(); this.boxes[10].GetLight().TurnOff(); this.p1 = true; this.p2 = true; StationMessageSender.Instance.Send(new List<bool> { p1, p2 }); Logging.Instance.Send("Zablokowano wszystkie semafory"); } if (name == "on") { this.boxes[0].GetLight().TurnOn(); this.boxes[1].GetLight().TurnOn(); this.boxes[2].GetLight().TurnOn(); this.boxes[3].GetLight().TurnOn(); this.boxes[4].GetLight().TurnOn(); this.boxes[5].GetLight().TurnOn(); this.boxes[6].GetLight().TurnOn(); this.boxes[7].GetLight().TurnOn(); this.boxes[8].GetLight().TurnOn(); this.boxes[9].GetLight().TurnOn(); this.boxes[10].GetLight().TurnOn(); this.p1 = true; this.p2 = true; StationMessageSender.Instance.Send(new List<bool> { p1, p2 }); Logging.Instance.Send("Odblokowano wyszyskie semafory"); } if (name == "-01") { this.boxes[0].GetLight().Switch(); Logging.Instance.Send("Zmieniono stan semafora 1"); } if (name == "-02") { this.boxes[1].GetLight().Switch(); Logging.Instance.Send("Zmieniono stan semafora 2"); } if (name == "-03") { this.boxes[2].GetLight().Switch(); Logging.Instance.Send("Zmieniono stan semafora 3"); } if (name == "-04") { this.boxes[3].GetLight().Switch(); Logging.Instance.Send("Zmieniono stan semafora 4"); } if (name == "-05") { this.boxes[4].GetLight().Switch(); Logging.Instance.Send("Zmieniono stan semafora 5"); } if (name == "-06") { this.boxes[5].GetLight().Switch(); Logging.Instance.Send("Zmieniono stan semafora 6"); } if (name == "-07") { this.boxes[6].GetLight().Switch(); Logging.Instance.Send("Zmieniono stan semafora 7"); } if (name == "-08") { this.boxes[7].GetLight().Switch(); Logging.Instance.Send("Zmieniono stan semafora 8 "); } if (name == "-09") { this.boxes[8].GetLight().Switch(); Logging.Instance.Send("Zmieniono stan semafora 9"); } if (name == "-10") { this.boxes[9].GetLight().Switch(); Logging.Instance.Send("Zmieniono stan semafora 10"); } if (name == "-11") { this.boxes[10].GetLight().Switch(); Logging.Instance.Send("Zmieniono stan semafora 11"); } List<bool> tempList = new List<bool>(); Console.WriteLine("Nowy status semaforów:\n"); foreach (var box in boxes) { tempList.Add(box.GetLight().IsOn()); Console.WriteLine("Semafor: {0} status {1}", box.GetSensor().GetName(), box.GetLight().IsOn()); } libcore.LightMessageSender.Instance.Send(tempList); }
/// <summary> /// Metoda wywołana w momencie pojawienia się wiadomości o nowych pociągach. Autor: Mateusz Tokarski /// </summary> public static void ReceiveTrain() { MessageQueue msgQ4 = new MessageQueue(MessageControlPanel.msgQName4); msgQ4.Formatter = new XmlMessageFormatter(new Type[] { typeof(libcore.Train) }); Message msg; while (StopThread == false) { if (StopThread == true) { break; } try { msg = msgQ4.Receive(new TimeSpan(0, 0, 2)); train = (libcore.Train)msg.Body; MessageControlPanel.window.Dispatcher.Invoke(new Action(UpdateTrain)); } catch (Exception ex) { } } }