added start and stop session

This commit is contained in:
shinichi
2020-09-30 15:12:10 +02:00
parent 45edbe5936
commit bd8994ad5b
4 changed files with 53 additions and 11 deletions

View File

@@ -15,6 +15,7 @@ namespace Client
private byte[] totalBuffer = new byte[1024]; private byte[] totalBuffer = new byte[1024];
private int totalBufferReceived = 0; private int totalBufferReceived = 0;
private EngineConnection engineConnection; private EngineConnection engineConnection;
private bool sessionRunning = false;
public Client() : this("localhost", 5555) public Client() : this("localhost", 5555)
@@ -40,7 +41,7 @@ namespace Client
private void OnConnect(IAsyncResult ar) private void OnConnect(IAsyncResult ar)
{ {
this.client.EndConnect(ar); this.client.EndConnect(ar);
Console.WriteLine("Verbonden!"); Console.WriteLine("TCP client Verbonden!");
this.stream = this.client.GetStream(); this.stream = this.client.GetStream();
@@ -91,6 +92,16 @@ namespace Client
tryLogin(); tryLogin();
} }
break; break;
case DataParser.START_SESSION:
this.sessionRunning = true;
byte[] startSession = DataParser.getStartSessionJson();
stream.BeginWrite(startSession, 0, startSession.Length, new AsyncCallback(OnWrite), null);
break;
case DataParser.STOP_SESSION:
this.sessionRunning = false;
byte[] stopSession = DataParser.getStopSessionJson();
stream.BeginWrite(stopSession, 0, stopSession.Length, new AsyncCallback(OnWrite), null);
break;
default: default:
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;
@@ -118,6 +129,10 @@ namespace Client
//maybe move this to other place //maybe move this to other place
public void BPM(byte[] bytes) public void BPM(byte[] bytes)
{ {
if (!sessionRunning)
{
return;
}
if (bytes == null) if (bytes == null)
{ {
throw new ArgumentNullException("no bytes"); throw new ArgumentNullException("no bytes");
@@ -128,6 +143,10 @@ namespace Client
public void Bike(byte[] bytes) public void Bike(byte[] bytes)
{ {
if (!sessionRunning)
{
return;
}
if (bytes == null) if (bytes == null)
{ {
throw new ArgumentNullException("no bytes"); throw new ArgumentNullException("no bytes");

View File

@@ -3,6 +3,7 @@ using Newtonsoft.Json.Serialization;
using System; using System;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text; using System.Text;
namespace Client namespace Client
@@ -10,7 +11,9 @@ namespace Client
public class DataParser public class DataParser
{ {
public const string LOGIN = "LOGIN"; public const string LOGIN = "LOGIN";
public const string LOGIN_RESPONSE = "LOGIN_RESPONSE"; public const string LOGIN_RESPONSE = "LOGIN RESPONSE";
public const string START_SESSION = "START SESSION";
public const string STOP_SESSION = "STOP SESSION";
/// <summary> /// <summary>
/// makes the json object with LOGIN identifier and username and password /// makes the json object with LOGIN identifier and username and password
/// </summary> /// </summary>
@@ -161,6 +164,21 @@ namespace Client
return getJsonMessage(Encoding.ASCII.GetBytes(message)); return getJsonMessage(Encoding.ASCII.GetBytes(message));
} }
public static byte[] getStartSessionJson()
{
return getJsonMessage(START_SESSION, null);
}
public static byte[] getStopSessionJson()
{
return getJsonMessage(STOP_SESSION, null);
}
public static byte[] getSetResistanceJson()
{
return null;
}
} }
} }

View File

@@ -105,7 +105,8 @@ namespace Server
this.username = username; this.username = username;
byte[] response = DataParser.getLoginResponse("OK"); byte[] response = DataParser.getLoginResponse("OK");
stream.BeginWrite(response, 0, response.Length, new AsyncCallback(OnWrite), null); stream.BeginWrite(response, 0, response.Length, new AsyncCallback(OnWrite), null);
this.saveData = new SaveData(Directory.GetCurrentDirectory() + "/" + username, sessionStart.ToString("yyyy-MM-dd HH-mm-ss")); byte[] startSession = DataParser.getStartSessionJson();
stream.BeginWrite(startSession, 0, startSession.Length, new AsyncCallback(OnWrite), null);
} }
else else
{ {
@@ -119,6 +120,12 @@ namespace Server
stream.BeginWrite(response, 0, response.Length, new AsyncCallback(OnWrite), null); stream.BeginWrite(response, 0, response.Length, new AsyncCallback(OnWrite), null);
} }
break; break;
case DataParser.START_SESSION:
this.saveData = new SaveData(Directory.GetCurrentDirectory() + "/" + this.username + "/" + sessionStart.ToString("yyyy-MM-dd HH-mm-ss"));
break;
case DataParser.STOP_SESSION:
this.saveData = null;
break;
default: default:
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;
@@ -130,11 +137,11 @@ namespace Server
Console.WriteLine(BitConverter.ToString(payloadbytes)); Console.WriteLine(BitConverter.ToString(payloadbytes));
if (payloadbytes.Length == 8) if (payloadbytes.Length == 8)
{ {
saveData.WriteDataRAWBike(payloadbytes); saveData?.WriteDataRAWBike(payloadbytes);
} }
else if (payloadbytes.Length == 2) else if (payloadbytes.Length == 2)
{ {
saveData.WriteDataRAWBPM(payloadbytes); saveData?.WriteDataRAWBPM(payloadbytes);
} }
else else
{ {

View File

@@ -8,11 +8,9 @@ namespace Server
class SaveData class SaveData
{ {
private string path; private string path;
private string filename; public SaveData(string path)
public SaveData(string path, string filename)
{ {
this.path = path; this.path = path;
this.filename = filename;
if (!Directory.Exists(path)) if (!Directory.Exists(path))
{ {
Directory.CreateDirectory(path); Directory.CreateDirectory(path);
@@ -25,7 +23,7 @@ namespace Server
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" + ".txt"))
{ {
sw.WriteLine(data); sw.WriteLine(data);
} }
@@ -37,7 +35,7 @@ namespace Server
{ {
throw new ArgumentException("data should have length of 2"); throw new ArgumentException("data should have length of 2");
} }
WriteRawData(data, this.path + "/rawBPM" + filename + ".bin"); WriteRawData(data, this.path + "/rawBPM" + ".bin");
} }
public void WriteDataRAWBike(byte[] data) public void WriteDataRAWBike(byte[] data)
@@ -46,7 +44,7 @@ namespace Server
{ {
throw new ArgumentException("data should have length of 8"); throw new ArgumentException("data should have length of 8");
} }
WriteRawData(data, this.path + "/rawBike" + filename + ".bin"); WriteRawData(data, this.path + "/rawBike" + ".bin");
} }
private void WriteRawData(byte[] data, string fileLocation) private void WriteRawData(byte[] data, string fileLocation)