public TrainStationProcessor(System.Windows.Forms.Label label) { rand = new Random(); ip = "localhost:8000"; if(!File.Exists("ip.txt")) { File.Create("ip.txt"); } if(File.Exists("ip.txt")) { try { string temp = File.ReadAllText("ip.txt"); if (!string.IsNullOrEmpty(temp) && !string.IsNullOrWhiteSpace(temp) && temp.Length > 2) { ip = temp; } } catch(Exception) { } } if (!localOnly) { _service = new TrafficMessageClient("BasicHttpBinding_ITrafficMessage", "http://" + ip + "/MEX/MessageService"); //_service = new TrafficMessageClient(); _inMessage = new ServerMessage(); _outMessage = new ServerMessage(); } string[] comports = System.IO.Ports.SerialPort.GetPortNames(); if (comports.Length > 0) { _arduino = new Arduino(0, System.IO.Ports.SerialPort.GetPortNames()[0]); _ledControl = new LedControl(_arduino, 6, 50); try { _arduino.Connect(); _ledControl.InitializeLedStrip(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } //_timer = new Timer(500.0); //_timer.Elapsed += Tick; _timer = new System.Windows.Forms.Timer(); _timer.Interval = 127; _timer.Tick += _timer_Tick; _timer.Enabled = true; _timer.Start(); }
public bool SendMessageEx(ServerMessage message, IPEndPoint id) { TrafficClient client; if (id == null || message == null || message.EndPoint == null || !trafficMessages.TryGetValue(message.EndPoint, out client)) { return false; } IPEndPoint to = message.EndPoint; message.EndPoint = id; client.Messages.Add(message); //Console.WriteLine("SendMessageEx::('" + id + "', '" + to + "', '" + System.Text.Encoding.ASCII.GetString(message.Data) + "');"); return true; }
public bool RetrieveMessageEx(out ServerMessage message, IPEndPoint id) { TrafficClient client; if (id == null || !trafficMessages.TryGetValue(id, out client) || client.Messages.Count == 0) { message = null; return false; } message = client.Messages[0]; client.Messages.RemoveAt(0); //Console.WriteLine("RetrieveMessageEx::('" + id.ToString() + "');"); return true; }
//private void Tick(Object source, ElapsedEventArgs args) private void _timer_Tick(object sender, EventArgs e) { foreach (KeyValuePair <Arduino, IPEndPoint> train in new Dictionary <Arduino, IPEndPoint>(_ARDUINO2IP)) { try { IPEndPoint endpoint = _ARDUINO2IP[train.Key]; Train realTrain = _ARDUINO2TRAIN[train.Key]; if (train.Key.IsConnected) { //retrieve serial messages { Message message = new Message(); while (train.Key.Read(message) > 0) { //perform connection of arduino to the system switch ((Actions)message.Action) { case Actions.TRAIN_CONNECTME: { string name = System.Text.Encoding.ASCII.GetString(message.Data).Replace("\0", ""); IPEndPoint connection = TrafficMessage.ConnectMeEx(ObjectType.Train, name, train.Value); if (connection != null) { _ARDUINO2IP[train.Key] = connection; endpoint = connection; train.Key.Write(new Message((byte)Actions.TRAIN_CONNECTIONOK)); administration.Add(realTrain); } else { train.Key.Write(new Message((byte)Actions.TRAIN_CONNECTIONFAIL)); } } break; case Actions.TRAIN_I_AM_GOING_TO_NEXT_STATION: realTrain.UpdateNextStation(); break; //TODO case for whatsmyconnectionstatus default: //else if not connection just send the message to the next station { ServerMessage sm = new ServerMessage(); if (realTrain.NextStation != null && realTrain.NextStation.Address != null) { sm.EndPoint = realTrain.NextStation.Address; sm.TrainMessage = message; TrafficMessage.SendMessageEx(sm, train.Value); } } break; } message = new Message(); } } //send new messages to arduino (train) { List <ServerMessage> messages = new List <ServerMessage>(); if (TrafficMessage.RetrieveAllMessagesEx(out messages, train.Value)) { foreach (ServerMessage sm in messages) { if (sm.TrainMessage != null) { train.Key.Write(sm.TrainMessage); } } } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
public RP6Control() { _sMessage = new ServerMessage(null, null, new Message()); endPoint = new IPEndPoint(new IPAddress(0x7F000001), 9873); }
public RP6Control() { _sMessage = new ServerMessage(null, null, new Message()); endPoint = new IPEndPoint(new IPAddress(0x7F000001), 9873 ); }
//private void Tick(Object source, ElapsedEventArgs args) private void _timer_Tick(object sender, EventArgs e) { foreach(KeyValuePair<Arduino, IPEndPoint> train in new Dictionary<Arduino, IPEndPoint>(_ARDUINO2IP)) { try { IPEndPoint endpoint = _ARDUINO2IP[train.Key]; Train realTrain = _ARDUINO2TRAIN[train.Key]; if (train.Key.IsConnected) { //retrieve serial messages { Message message = new Message(); while (train.Key.Read(message) > 0) { //perform connection of arduino to the system switch ((Actions)message.Action) { case Actions.TRAIN_CONNECTME: { string name = System.Text.Encoding.ASCII.GetString(message.Data).Replace("\0", ""); IPEndPoint connection = TrafficMessage.ConnectMeEx(ObjectType.Train, name, train.Value); if (connection != null) { _ARDUINO2IP[train.Key] = connection; endpoint = connection; train.Key.Write(new Message((byte)Actions.TRAIN_CONNECTIONOK)); administration.Add(realTrain); } else { train.Key.Write(new Message((byte)Actions.TRAIN_CONNECTIONFAIL)); } } break; case Actions.TRAIN_I_AM_GOING_TO_NEXT_STATION: realTrain.UpdateNextStation(); break; //TODO case for whatsmyconnectionstatus default: //else if not connection just send the message to the next station { ServerMessage sm = new ServerMessage(); if (realTrain.NextStation != null && realTrain.NextStation.Address != null) { sm.EndPoint = realTrain.NextStation.Address; sm.TrainMessage = message; TrafficMessage.SendMessageEx(sm, train.Value); } } break; } message = new Message(); } } //send new messages to arduino (train) { List<ServerMessage> messages = new List<ServerMessage>(); if (TrafficMessage.RetrieveAllMessagesEx(out messages, train.Value)) { foreach (ServerMessage sm in messages) { if (sm.TrainMessage != null) { train.Key.Write(sm.TrainMessage); } } } } } } catch(Exception ex) { Console.WriteLine(ex.Message); } } }