// <summary> // The following commands are internally used in the server // </summary> public dynamic debug(int commandID, int myserverID, int senderID, int receiverID, ComData comdata, DeviceRouting devices) { string CurrentTime = DateTime.Now.ToString("HH:mm:ss"); DeviceRegistry sender = devices.FindByID(receiverID); if (sender != null) {//has sender that is acknowledged by the server { string message1 = ""; comdata.Get(3, ref message1); string message2 = ""; comdata.Get(4, ref message2); Console.WriteLine("'" + sender.Name + "' at " + CurrentTime + ": " + message1 + '\n' + message2); } } else { Console.WriteLine("Incoming command doesn't have proper sender at " + CurrentTime); } return(null); }
public Form1() { InitializeComponent(); mega = new Arduino("", 19200); DataInterpretor dataman = new DataInterpretor(); dataman.addString("{1,\"hallo, kaas\",5}"); ComData comdata = dataman.getComData(); int param1 = 0; string param2 = ""; int param3 = 0; comdata.Get(0, ref param1); comdata.Get(1, ref param2); comdata.Get(1, ref param3); Debug.Write(param1 + "," + param2 + "," + param3); /*StringEater eater = new StringEater(); * * eater.String("hallo there dude"); * eater.eatUntil(test); * * Debug.Write(eater.buffer);*/ tmrConnectionCheck.Enabled = true; }
/// <summary> /// Incoming messages from clientss are processed here. /// </summary> /// <param name="message">Stringified version of ComData</param> public void SendMessage(string message) { //Process Stringified ComData data.addString(message); //Is it really usable (as ComData) if (data.usableInput) { //Get the comdata ComData comdata = data.getComData(); int senderID = 0; int receiverID = 0; int commandID = 0; string CurrentTime = DateTime.Now.ToString("HH:mm:ss"); comdata.Get(0, ref senderID); comdata.Get(1, ref receiverID); comdata.Get(2, ref commandID); //Is the command valid? if (commandID < 1) { MessagesList.Add(Commandslist.GenerateErrorMessage(senderID, 1, "Invalid command")); return; } //A message has been received by something, find device DeviceRegistry receiver = devices.FindByID(receiverID); //The receiver has not been registered in this system if (receiver == null) { //And the receiver is not supposed to be 'everyone' if (receiverID != -1) { //then stop the code.. //Add the message to return error to sender MessagesList.Add(Commandslist.GenerateErrorMessage(senderID, 1, "Invalid receiver")); return; } } //If this message is meant for this server if (receiverID == myserverID || receiverID == -1) { //Does not require a valid senderID //Switch base handshake commands dynamic result = Commandslist.ExecuteCommand(commandID, myserverID, senderID, receiverID, comdata, devices); //if result is a command if (result is ComData) { int param1 = 0; result.Get(0, ref param1); Console.WriteLine(param1); Console.WriteLine(message); MessagesList.Add(result); } } else//wasn't meant for this server { //TODO make code to transmit command to target } } else { string CurrentTime = DateTime.Now.ToString("HH:mm:ss"); Console.WriteLine("corrupted message received at " + CurrentTime); } }
public void Add(ComData data, Communication handler) { if (!Program.MasterServer.Connected)//Then if connection with master server is there { return; } int target = 0; data.Get(0, ref target); int sender = 0; data.Get(1, ref sender); int commandid = 0; data.Get(2, ref commandid); //Reporter.AddReport(4, handler.DeviceName+ ": "+ data.ToString()); switch (handler.devicetype.ToLower()) { case "arduino": case "i/o": { if (target == -1 || target == Program.MasterServer.serverID) { Program.MasterServer.Send(data); return; } else if (target == handler.comID || sender == handler.comID) //coming or going from device { executable(data, handler); } if (target == handler.comID) { if (commandid != 3 || target != sender) { handler.Send(data); } } break; } case "server": case "pc": { if (target == -1) //is -1 when { foreach (Communication device in Program.LocalDevices) { if (device.Connected) { data.Set(0, device.comID); device.unhandledcommands.executable(data, device); } } } else if (target == handler.comID) { executable(data, handler); } else { foreach (Communication device in Program.LocalDevices) { if (device.Connected && target == device.comID) { device.unhandledcommands.executable(data, device); } } } break; } } }