// method to load telemetry stream data // returns TelemetryModel which maps onto json data by properties public static async Task <BlueTelemetryModel> LoadTelemetry(string base_url, bool uia = false) { // start with empty string for url string url = ""; // decide whether to get recent suit data or dcu/uia recent data if (uia) { // set api url to obtain recent DCU/UIA data //url = base_url + "api/simulation/uiastate"; url = $"{ base_url }api/simulation/uiastate"; Console.WriteLine("url: " + url); } else { // set api url to obtain recent suit data url = base_url + "api/simulation/state"; //url = $"{ base_url }api/simulation/state"; //Console.WriteLine("url: " + url); } // new request using a single open api client and gets this as response using (HttpResponseMessage res = await Data.api.GetAsync(url)) { // if response is successful, read back data if (res.IsSuccessStatusCode) { // //TelemetryModel model = JsonConvert.DeserializeObject<TelemetryModel>(res); // create new object for telemetry data // takes data into as json and maps onto telemetry model, ignoring properties that don't match BlueTelemetryModel telemetry = await res.Content.ReadAsAsync <BlueTelemetryModel>(); // add each entry to list for future testing telemetryData.Add(telemetry); // return the mapped data return(telemetry); } // else, unsuccessful response else { // throw an exception (with reason) if response isn't succesful throw new Exception(res.ReasonPhrase); } // close/dispose of port when done to avoid bad memory management } }
// call this method to post dummy data to server public static void TestVitalsPost() { // //PortName = "COM7"; // PortNameMac = "dev/cu.HC-05-DevB"; //PortName = "dev/cu.HC-05-DevB" // Set in Windows Settings->Bluetooth->More Bluetooth Settings->COM Ports (OUTGOING) // create a new serial port listener with given baud rate on given windows port SerialPort serialPort = new SerialPort { BaudRate = 9600, PortName = "/dev/cu.DSDTECHHC-05-DevB" // Set in Windows Settings->Bluetooth->More Bluetooth Settings->COM Ports (OUTGOING) //PortName = "dev/cu.HC-05-DevB" // Set in Windows Settings->Bluetooth->More Bluetooth Settings->COM Ports (OUTGOING) }; serialPort.Open(); // double counter = 60.0; // post 4 random vital measurements to server while (counter < 64.0) { // send to arduino through bluetooth comm //serialPort.WriteLine("PC counter: " + (counter++)); // POST to server var postVitals = new BlueTelemetryModel { heartrate = counter, oxygen = 60.0, temperature = 70.0 }; _ = PostJsonHttpClient(postVitals); // update ctr counter++; // check serial port connection and buffer for data every second Thread.Sleep(1000); } }
// send vitals as a json to mongoDB // from: https://www.stevejgordon.co.uk/sending-and-receiving-json-using-httpclient-with-system-net-http-json public static async Task PostJsonHttpClient(BlueTelemetryModel postVitals) { // get the initialized client HttpClient httpClient = Data.api; // //BlueTelemetryModel postVitals = new BlueTelemetryModel(); // api post request url string url = "http://127.0.0.1:3002/api/vital"; //string url = "http://127.0.0.1:3002/api/temp"; //string url = "http://127.0.0.1:3002/api/oxygen"; //string url = "http://127.0.0.1:3002/api/heartrate"; Console.WriteLine("BLUETOOTH POST REQUEST: " + url); // send vital json and wait for response from server var postResponse = await httpClient.PostAsJsonAsync(url, postVitals); // postResponse.EnsureSuccessStatusCode(); }
// //public static SerialPort serialPort = new SerialPort(); //public SerialPort serialPort { get; set; } //public int baudrate { get; set; } //public string portname { get; set; } //public bool connected { get; set; } // constructor initializes serial comm and reads (as own thread) until broken public Bluetooth() { // //PortName = "COM7"; // PortNameMac = "dev/cu.HC-05-DevB"; //PortName = "dev/cu.HC-05-DevB" // Set in Windows Settings->Bluetooth->More Bluetooth Settings->COM Ports (OUTGOING) // create a new serial port listener with given baud rate on given windows port SerialPort serialPort = new SerialPort { BaudRate = 9600, PortName = "COM5" //PortName = "/dev/cu.DSDTECHHC-05-DevB" // Set in Windows Settings->Bluetooth->More Bluetooth Settings->COM Ports (OUTGOING) //PortName = "dev/cu.HC-05-DevB" // Set in Windows Settings->Bluetooth->More Bluetooth Settings->COM Ports (OUTGOING) }; serialPort.Open(); // //int counter = 0; // save serial data //double data = 0.0; var data = "0.0"; //BlueTelemetryModel postVitals = new BlueTelemetryModel(); BlueTelemetryModel postVitals = new BlueTelemetryModel { heartrate = Convert.ToDouble(data), oxygen = Convert.ToDouble(data), temperature = Convert.ToDouble(data) }; //_ = PostJsonHttpClient(postVitals); // while a serial port connection exists while (serialPort.IsOpen) { // if serial port contains data, extract while (serialPort.BytesToRead > 0) { //Console.Write(Convert.ToChar(serialPort.ReadChar())); //Console.Write(serialPort.ReadChar()); //Console.Write(serialPort.ReadExisting()); // save data from serial port into variable and convert to string in format of double data = serialPort.ReadExisting() + ".0"; //data = Convert.ToChar(serialPort.ReadChar()); //data = Convert.ToChar(serialPort.ReadChar()); //Console.Write(Convert.ToChar(serialPort.ReadLine())); //Console.Write(Convert.ToChar(serialPort.ReadLine())); //Console.WriteLine((double) data); //Console.WriteLine(data); //postVitals = { BlueTelemetryModel.temperature = data }; postVitals.temperature = Convert.ToDouble(data); Console.WriteLine(postVitals.temperature); _ = PostJsonHttpClient(postVitals); } // send to arduino through bluetooth comm //serialPort.WriteLine("PC counter: " + (counter++)); // update ctr //counter++; // check serial port connection and buffer for data every second Thread.Sleep(1000); // if serial port closes, break loop } }