From 6f1ab57fe4e3f1cbdefc6773858b6972f027c7a7 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 30 Sep 2020 13:00:13 +0200 Subject: [PATCH 1/3] saving in binairy format took forever --- Server/Client.cs | 3 +-- Server/SaveData.cs | 23 ++++++++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/Server/Client.cs b/Server/Client.cs index 5e385be..78abf58 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -123,7 +123,6 @@ 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)); @@ -131,7 +130,7 @@ namespace Server else if (DataParser.isRawData(message)) { Console.WriteLine(BitConverter.ToString(message)); - saveData.WriteDataRAW(ByteArrayToString(message)); + saveData.WriteDataRAW(message); } diff --git a/Server/SaveData.cs b/Server/SaveData.cs index 0286bde..ffaaf90 100644 --- a/Server/SaveData.cs +++ b/Server/SaveData.cs @@ -22,20 +22,33 @@ 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 WriteDataRAW(byte[] data) { - using (StreamWriter sw = File.AppendText(this.path + "/raw" + filename + ".txt")) + int length = 0; + try { - sw.WriteLine(data); + FileInfo fi = new FileInfo(this.path + "/raw" + filename + ".bin"); + length = (int)fi.Length; + } + catch + { + + } + using (BinaryWriter sw = new BinaryWriter(File.Open(this.path + "/raw" + filename + ".bin", FileMode.Create))) + { + + Console.WriteLine("head position " + sw.Seek(length, SeekOrigin.End)); + sw.Write(data); + sw.Flush(); } } } From 82f2d6b71c513ff03d59b2ff4bb64676690dff5c Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 30 Sep 2020 14:17:26 +0200 Subject: [PATCH 2/3] saving bike and bpm data in separate files --- Server/Client.cs | 15 +++++++++++++-- Server/SaveData.cs | 29 ++++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/Server/Client.cs b/Server/Client.cs index 78abf58..30d7efc 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -129,8 +129,19 @@ namespace Server } else if (DataParser.isRawData(message)) { - Console.WriteLine(BitConverter.ToString(message)); - saveData.WriteDataRAW(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 ffaaf90..be5412b 100644 --- a/Server/SaveData.cs +++ b/Server/SaveData.cs @@ -31,22 +31,41 @@ namespace Server } } - public void WriteDataRAW(byte[] data) + public void WriteDataRAWBPM(byte[] data) { int length = 0; try { - FileInfo fi = new FileInfo(this.path + "/raw" + filename + ".bin"); + FileInfo fi = new FileInfo(this.path + "/rawBPM" + filename + ".bin"); length = (int)fi.Length; } catch { - + // do nothing } - using (BinaryWriter sw = new BinaryWriter(File.Open(this.path + "/raw" + filename + ".bin", FileMode.Create))) + using (BinaryWriter sw = new BinaryWriter(File.Open(this.path + "/rawBPM" + filename + ".bin", FileMode.Create))) { + sw.Seek(length, SeekOrigin.End); + sw.Write(data); + sw.Flush(); + } + } - Console.WriteLine("head position " + sw.Seek(length, SeekOrigin.End)); + public void WriteDataRAWBike(byte[] data) + { + int length = 0; + try + { + FileInfo fi = new FileInfo(this.path + "/rawBike" + filename + ".bin"); + length = (int)fi.Length; + } + catch + { + // do nothing + } + using (BinaryWriter sw = new BinaryWriter(File.Open(this.path + "/rawBike" + filename + ".bin", FileMode.Create))) + { + sw.Seek(length, SeekOrigin.End); sw.Write(data); sw.Flush(); } From a65c36b8d12d07a6d9e752bd6c63539a813cd9a1 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 30 Sep 2020 14:24:38 +0200 Subject: [PATCH 3/3] fix bug and more efficient code --- Server/Client.cs | 4 +--- Server/SaveData.cs | 42 +++++++++++++++++++++--------------------- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/Server/Client.cs b/Server/Client.cs index 30d7efc..fc4baca 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -123,9 +123,7 @@ namespace Server Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}"); break; } - 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)) { diff --git a/Server/SaveData.cs b/Server/SaveData.cs index be5412b..4163471 100644 --- a/Server/SaveData.cs +++ b/Server/SaveData.cs @@ -32,18 +32,36 @@ namespace Server } public void WriteDataRAWBPM(byte[] data) + { + if (data.Length != 2) + { + 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(this.path + "/rawBPM" + filename + ".bin"); + FileInfo fi = new FileInfo(fileLocation); length = (int)fi.Length; } catch { // do nothing } - using (BinaryWriter sw = new BinaryWriter(File.Open(this.path + "/rawBPM" + filename + ".bin", FileMode.Create))) + using (BinaryWriter sw = new BinaryWriter(File.Open(fileLocation, FileMode.Create))) { sw.Seek(length, SeekOrigin.End); sw.Write(data); @@ -51,24 +69,6 @@ namespace Server } } - public void WriteDataRAWBike(byte[] data) - { - int length = 0; - try - { - FileInfo fi = new FileInfo(this.path + "/rawBike" + filename + ".bin"); - length = (int)fi.Length; - } - catch - { - // do nothing - } - using (BinaryWriter sw = new BinaryWriter(File.Open(this.path + "/rawBike" + filename + ".bin", FileMode.Create))) - { - sw.Seek(length, SeekOrigin.End); - sw.Write(data); - sw.Flush(); - } - } + } }