added function to get BPM graph data

This commit is contained in:
shinichi
2020-10-02 15:37:57 +02:00
parent 505c4907d0
commit d85c5ff935

View File

@@ -2,12 +2,16 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
namespace Server
{
class SaveData
{
private string path;
private const string jsonFilename = "/json.txt";
private const string rawBikeFilename = "/rawBike.bin";
private const string rawBPMFilename = "/rawBPM.bin";
public SaveData(string path)
{
this.path = path;
@@ -23,7 +27,7 @@ namespace Server
public void WriteDataJSON(string data)
{
using (StreamWriter sw = File.AppendText(this.path + "/json" + ".txt"))
using (StreamWriter sw = File.AppendText(this.path + jsonFilename))
{
sw.WriteLine(data);
}
@@ -35,7 +39,7 @@ namespace Server
{
throw new ArgumentException("data should have length of 2");
}
WriteRawData(data, this.path + "/rawBPM" + ".bin");
WriteRawData(data, this.path + rawBPMFilename);
}
public void WriteDataRAWBike(byte[] data)
@@ -44,7 +48,7 @@ namespace Server
{
throw new ArgumentException("data should have length of 8");
}
WriteRawData(data, this.path + "/rawBike" + ".bin");
WriteRawData(data, this.path + rawBikeFilename);
}
private void WriteRawData(byte[] data, string fileLocation)
@@ -67,6 +71,56 @@ namespace Server
}
}
/// <summary>
/// gets BPM graph data out of file.
/// if you want 100 datapoints but here are onlny 50, de last 50 datapoint will be 0
/// if you want 100 datapoints where it takes the average of 2, the last 75 will be 0
/// if the file isn't created yet it will retun null
/// </summary>
/// <param name="outputSize">the amount of data points for the output</param>
/// <param name="averageOver">the amount of data points form the file for one data point in the output</param>
/// <returns>byte array with data points from file</returns>
public byte[] getBPMgraphData(int outputSize, int averageOver)
{
if (File.Exists(this.path + rawBPMFilename))
{
FileInfo fi = new FileInfo(this.path + rawBPMFilename);
int length = (int)fi.Length;
byte[] output = new byte[outputSize];
int messageSize = 2;
int readSize = messageSize * averageOver;
byte[] readBuffer = new byte[readSize];
using (FileStream fileStream = new FileStream(this.path + rawBPMFilename, FileMode.Open, FileAccess.Read))
{
for (int i = 1; i >= outputSize; i++)
{
if (length - (i * readSize) < 0)
{
break;
}
fileStream.Read(readBuffer, length - (i * readSize), readSize);
//handling data
int total = 0;
for (int j = 0; j < averageOver; j++)
{
total += readBuffer[j * messageSize + 1];
}
output[i - 1] = (byte)(total / averageOver);
}
}
return output;
}
else
{
return null;
}
}
}
}