public void SendMessage(NAE.Data.Telemetry message) { string jsonString = JsonConvert.SerializeObject(message); byte[] payload = Encoding.UTF8.GetBytes(jsonString); if (messageId == ushort.MaxValue || messageId == 0) { messageId = 1; } string resource = "http://pegasusnae.org/telemetry"; Uri requestUri = new Uri(String.Format("coaps://{0}/publish?topic={1}", CoapAuthority, resource)); CoapRequest request = new CoapRequest(messageId++, RequestMessageType.NonConfirmable, MethodType.POST, requestUri, MediaType.Json, payload); byte[] coap = request.Encode(); try { Task task = client.SendAsync(coap); Task.WhenAll(task); Trace.TraceInformation("Message sent to cloud."); Trace.TraceInformation(jsonString); } catch (Exception ex) { Trace.TraceWarning("Web Socket expection."); Trace.TraceError(ex.Message); } }
private void Udp_OnReceive(object sender, string message) { NAE.Data.Telemetry telemetry = null; try { //read the UPD message as a CSV String telemetry = NAE.Data.Telemetry.Load(message); telemetry.RunId = this.runId; if (startPoint) { this.GpsLatitudeStart = telemetry.GpsLatitude; this.GpsLongitudeStart = telemetry.GpsLongitude; startPoint = false; } telemetry.GpsLatitudeStart = this.GpsLatitudeStart; telemetry.GpsLongitudeStart = this.GpsLongitudeStart; TelemetryUpdate(telemetry); } catch (Exception ex) { Trace.TraceWarning("Fault in UPD Telemetry receive."); Trace.TraceError(ex.Message); } if (telemetry != null) { try { //forward to Web socket this.wsManager.SendMessage(telemetry); } catch (Exception ex) { } } }
public static Telemetry Load(string csvString) { int checkValue = 0; byte checkSum = 0; if (!csvString.Contains("*")) //not a valid message; no check value { return(null); } string messagePrefix = csvString.Substring(0, 2); //identifies message type if (messagePrefix != prefix) { return(null); } string messageString = csvString.Substring(2, csvString.Length - 6); //the message without identifier and check value string checkValueString = csvString.Substring(csvString.Length - 2, 2); //the check value string[] parts = messageString.Split(new char[] { ',' }); //the message parts as string array //get the check value as an int if (!int.TryParse(csvString.Substring(csvString.Length - 2, 2), NumberStyles.AllowHexSpecifier, null, out checkValue)) { return(null); } //compute the check sum checkSum = (byte)Encoding.ASCII.GetBytes(csvString.Substring(0, csvString.Length - 4)).Sum(x => (int)x); //check value should equal check value; otherwise invalid message if (checkSum != (byte)checkValue) { return(null); } int index = 0; Telemetry instance = new Telemetry(); instance.Timestamp = Convert.ToDateTime(parts[index++]); instance.GpsLatitude = Convert.ToDouble(parts[index++]); instance.GpsLongitude = Convert.ToDouble(parts[index++]); instance.GpsAltitude = Convert.ToDouble(parts[index++]); instance.GpsSpeedKph = Convert.ToDouble(parts[index++]); instance.GpsSpeedMph = Convert.ToDouble(parts[index++]); instance.GpsDirection = Convert.ToDouble(parts[index++]); instance.SatelliteFix = Convert.ToBoolean(Convert.ToInt32((parts[index++]))); instance.Satellites = Convert.ToInt32(parts[index++]); instance.Temperature = Convert.ToDouble(parts[index++]); instance.Humidity = Convert.ToDouble(parts[index++]); instance.Pressure = Convert.ToDouble(parts[index++]); instance.Altitude = Convert.ToDouble(parts[index++]); instance.LinearAccelX = Convert.ToDouble(parts[index++]); instance.LinearAccelY = Convert.ToDouble(parts[index++]); instance.LinearAccelZ = Convert.ToDouble(parts[index++]); instance.Yaw = Convert.ToDouble(parts[index++]); instance.Pitch = Convert.ToDouble(parts[index++]); instance.Roll = Convert.ToDouble(parts[index++]); instance.Sound = Convert.ToDouble(parts[index++]); instance.Voltage = Convert.ToDouble(parts[index++]); instance.Current = Convert.ToInt32(parts[index++]); return(instance); }