Merge branch 'binairy-writer' into develop

This commit is contained in:
shinichi
2020-09-30 14:25:29 +02:00
2 changed files with 51 additions and 11 deletions

View File

@@ -123,15 +123,23 @@ namespace Server
Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}"); Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}");
break; break;
} }
Array.Copy(message, 5, payloadbytes, 0, message.Length - 5); saveData?.WriteDataJSON(Encoding.ASCII.GetString(payloadbytes));
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payloadbytes));
saveData.WriteDataJSON(Encoding.ASCII.GetString(payloadbytes));
} }
else if (DataParser.isRawData(message)) else if (DataParser.isRawData(message))
{ {
Console.WriteLine(BitConverter.ToString(message)); Console.WriteLine(BitConverter.ToString(payloadbytes));
saveData.WriteDataRAW(ByteArrayToString(message)); 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));
}
} }

View File

@@ -22,21 +22,53 @@ namespace Server
/// <summary> /// <summary>
/// Every line is a new data entry /// Every line is a new data entry
/// </summary> /// </summary>
public void WriteDataJSON(string data) 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); 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();
} }
} }
} }
} }