diff --git a/Server/Client.cs b/Server/Client.cs index 5e385be..fc4baca 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -123,15 +123,23 @@ namespace Server Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}"); break; } - Array.Copy(message, 5, payloadbytes, 0, message.Length - 5); - dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payloadbytes)); - saveData.WriteDataJSON(Encoding.ASCII.GetString(payloadbytes)); - + saveData?.WriteDataJSON(Encoding.ASCII.GetString(payloadbytes)); } else if (DataParser.isRawData(message)) { - Console.WriteLine(BitConverter.ToString(message)); - saveData.WriteDataRAW(ByteArrayToString(message)); + Console.WriteLine(BitConverter.ToString(payloadbytes)); + if (payloadbytes.Length == 8) + { + saveData.WriteDataRAWBike(payloadbytes); + } + else if (payloadbytes.Length == 2) + { + saveData.WriteDataRAWBPM(payloadbytes); + } + else + { + Console.WriteLine("received raw data with weird lenght " + BitConverter.ToString(payloadbytes)); + } } diff --git a/Server/SaveData.cs b/Server/SaveData.cs index 0286bde..4163471 100644 --- a/Server/SaveData.cs +++ b/Server/SaveData.cs @@ -22,21 +22,53 @@ namespace Server /// /// Every line is a new data entry /// - + public void WriteDataJSON(string data) { - using (StreamWriter sw = File.AppendText(this.path + "/json"+filename+".txt")) + using (StreamWriter sw = File.AppendText(this.path + "/json" + filename + ".txt")) { sw.WriteLine(data); } } - public void WriteDataRAW(string data) + public void WriteDataRAWBPM(byte[] data) { - using (StreamWriter sw = File.AppendText(this.path + "/raw" + filename + ".txt")) + if (data.Length != 2) { - sw.WriteLine(data); + throw new ArgumentException("data should have length of 2"); + } + WriteRawData(data, this.path + "/rawBPM" + filename + ".bin"); + } + + public void WriteDataRAWBike(byte[] data) + { + if (data.Length != 8) + { + throw new ArgumentException("data should have length of 8"); + } + WriteRawData(data, this.path + "/rawBike" + filename + ".bin"); + } + + private void WriteRawData(byte[] data, string fileLocation) + { + int length = 0; + try + { + FileInfo fi = new FileInfo(fileLocation); + length = (int)fi.Length; + } + catch + { + // do nothing + } + using (BinaryWriter sw = new BinaryWriter(File.Open(fileLocation, FileMode.Create))) + { + sw.Seek(length, SeekOrigin.End); + sw.Write(data); + sw.Flush(); } } + + } }