//thread to recieve driving commands from webserver private static void ControlReciever(object obj, object handler) { Byte[] bytes = new Byte[256]; DataHandler datahandler = (DataHandler)handler; TcpClient client = (TcpClient)obj; NetworkStream stream = client.GetStream(); while (true) { try { while ((stream.Read(bytes, 0, bytes.Length)) != 0) { string data = System.Text.Encoding.UTF8.GetString(bytes); //convert the bytes in the tcp stream into a string RobotData recievedData = JsonConvert.DeserializeObject <RobotData>(data); //deserialize the data from the string datahandler.Mode = recievedData.Mode; datahandler.Data = recievedData.DrivingCommand; } } catch (Exception e) //this usually happens when the client disconnects or times out. Will need to make the error handling for other cases instead of handling all exceptions { Console.WriteLine("Client Disconnected"); break; } } client.Close(); }
//thread to stream driving commands to the robot (deprecated), might be used in the future to send a sensor config changes if needed private static void RobotStreamer(object obj, object handler) { DataHandler datahandler = (DataHandler)handler; TcpClient client = (TcpClient)obj; NetworkStream stream = client.GetStream(); while (true) { try { RobotData data = new RobotData(); data.DrivingCommand = datahandler.Data; data.Mode = datahandler.Mode; string json = JsonConvert.SerializeObject(data); //serialize the object into a json string byte[] msg = System.Text.Encoding.UTF8.GetBytes(json); //convert the string into bytes to be sent over the TCP stream stream.Write(msg, 0, msg.Length); //send the string to the robot or sensor Thread.Sleep(100); } catch (Exception e) //this usually happens when the client disconnects or times out. Will need to make the error handling for other cases instead of handling all exceptions { Console.WriteLine("Client Disconnected"); break; } } client.Close(); }
//thread to stream sensor data to webserver/mapgen private static void SensorStreamer(object obj, object handler) { DataHandler datahandler = (DataHandler)handler; //the datahandler passed to the thread TcpClient client = (TcpClient)obj; //the tcp stream passed to the thread NetworkStream stream = client.GetStream(); while (true) { try { RobotData data = new RobotData(); //this was originally used to stream the robot's sensors to the server, but it serves the same purpose for a stationary sensor data.BatteryPercentage = datahandler.BatteryPercentage; data.DistanceSensorFront = datahandler.DistanceSensorFront; data.DistanceSensorLeft = datahandler.DistanceSensorLeft; data.DistanceSensorRight = datahandler.DistanceSensorRight; data.TemperatureReading = datahandler.TemperatureReading; data.Timestamp = datahandler.Timestamp; data.HumidityReading = datahandler.HumidityReading; data.WaterDetected = datahandler.WaterDetected; data.SmokeDetected = datahandler.SmokeDetected; data.DrivingCommand = datahandler.Data; data.Mode = datahandler.Mode; string json = JsonConvert.SerializeObject(data); //serialize the data into a string of json byte[] msg = System.Text.Encoding.UTF8.GetBytes(json); //convert the json string into bytes stream.Write(msg, 0, msg.Length); //send the bytes over the tcp stream Thread.Sleep(100); //wait .1 seconds } catch (Exception e) //this usually happens when the client disconnects or times out. Will need to make the error handling for other cases instead of handling all exceptions { Console.WriteLine("Client Disconnected"); break; } } client.Close(); }
//thread to recieve data from robot or sensor private static void SensorDataReciever(object obj, object handler) { Byte[] bytes = new Byte[256]; DataHandler datahandler = (DataHandler)handler; //the datahandler passed to the thread TcpClient client = (TcpClient)obj; //the tcp stream passed to the thread NetworkStream stream = client.GetStream(); while (true) { try { int i; while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) { string data = Encoding.UTF8.GetString(bytes, 0, i); //decode the bytes into a string RobotData recievedData = JsonConvert.DeserializeObject <RobotData>(data); //deserialize the string into a robotdata object datahandler.DistanceSensorFront = (recievedData.DistanceSensorFront); datahandler.DistanceSensorRight = (recievedData.DistanceSensorRight); datahandler.DistanceSensorLeft = (recievedData.DistanceSensorLeft); datahandler.BatteryPercentage = (recievedData.BatteryPercentage); datahandler.Timestamp = recievedData.Timestamp; datahandler.TemperatureReading = recievedData.TemperatureReading; datahandler.HumidityReading = recievedData.HumidityReading; datahandler.MotionDetected = recievedData.MotionDetected; } } catch (Exception e) //this usually happens when the client disconnects or times out. Will need to make the error handling for other cases instead of handling all exceptions { Console.WriteLine("Client Disconnected"); break; } } client.Close(); }