From a65c36b8d12d07a6d9e752bd6c63539a813cd9a1 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 30 Sep 2020 14:24:38 +0200 Subject: [PATCH] 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(); - } - } + } }