[ADD] added checking of all folders in username directory

This commit is contained in:
Sem van der Hoeven
2020-10-29 20:35:21 +01:00
parent b98ac77261
commit baf89dac3f
4 changed files with 64 additions and 25 deletions

View File

@@ -125,13 +125,17 @@ namespace DoctorApp.Utils
{ {
// read the .bin file that is in the message // read the .bin file that is in the message
// update the view // update the view
//Debug.WriteLine($"[DOCTOR CLIENT] Got raw bike data: " + Encoding.ASCII.GetString(payloadbytes));
MainViewModel.TransferDataToClientBike(payloadbytes); MainViewModel.TransferDataToClientBike(payloadbytes);
} }
else if (DataParser.isRawDataBPMDoctor(messageBytes)) else if (DataParser.isRawDataBPMDoctor(messageBytes))
{ {
MainViewModel.TransferDataToClientBPM(payloadbytes); MainViewModel.TransferDataToClientBPM(payloadbytes);
} }
else if (DataParser.IsHistoricBikeData(messageBytes))
{
Debug.WriteLine("[DOCTOR CLIENT] got historic bike data: " + Encoding.ASCII.GetString(payloadbytes));
}
Array.Copy(totalBuffer, expectedMessageLength, totalBuffer, 0, (totalBufferReceived - expectedMessageLength)); //maybe unsafe idk Array.Copy(totalBuffer, expectedMessageLength, totalBuffer, 0, (totalBufferReceived - expectedMessageLength)); //maybe unsafe idk
totalBufferReceived -= expectedMessageLength; totalBufferReceived -= expectedMessageLength;
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);

View File

@@ -86,7 +86,7 @@ namespace DoctorApp.ViewModels
// request the historic data from the server // request the historic data from the server
this.SaveHistoricData = new RelayCommand<object>((parameter) => this.SaveHistoricData = new RelayCommand<object>((parameter) =>
{ {
this.client.sendMessage(DataParser.GetGetFileMessage(PatientInfo.Username, DateTime.Now)); this.client.sendMessage(DataParser.GetGetFileMessage(PatientInfo.Username));
// data is stored on the server // data is stored on the server
// send request to server that we want to get the current historic data from the patient // send request to server that we want to get the current historic data from the patient
// server sends this back // server sends this back

View File

@@ -243,6 +243,12 @@ namespace Util
return bytes[4] == 0x05; return bytes[4] == 0x05;
} }
public static bool IsHistoricBikeData(byte[] bytes)
{
if (bytes.Length <= 5) throw new ArgumentException("bytes too short");
return bytes[4] == 0x06;
}
/// <summary> /// <summary>
/// constructs a message with the payload, messageId and clientId /// constructs a message with the payload, messageId and clientId
@@ -473,7 +479,7 @@ namespace Util
return data; return data;
} }
public static byte[] GetGetFileMessage(string mUsername, DateTime mDateTime) public static byte[] GetGetFileMessage(string mUsername)
{ {
if (mUsername == null) if (mUsername == null)
{ {
@@ -482,7 +488,6 @@ namespace Util
dynamic data = new dynamic data = new
{ {
username = mUsername, username = mUsername,
dateTime = mDateTime.ToString("yyyy-MM-dd HH-mm-ss")
}; };
return getJsonMessage(GET_FILE, data); return getJsonMessage(GET_FILE, data);
} }
@@ -499,7 +504,7 @@ namespace Util
public static byte[] GetFileMessage(byte[] file) public static byte[] GetFileMessage(byte[] file)
{ {
return getMessage(file, 0x04); return getMessage(file, 0x06);
} }
} }
} }

View File

@@ -148,26 +148,7 @@ namespace Server
communication.SendMessageToClient(DataParser.getUsernameFromJson(payloadbytes), message); communication.SendMessageToClient(DataParser.getUsernameFromJson(payloadbytes), message);
break; break;
case DataParser.GET_FILE: case DataParser.GET_FILE:
//ugly getClientBikeData(payloadbytes);
//get the raw bike data of the user with the specified username
string username = DataParser.GetUsernameFromGetFileBytes(payloadbytes);
string path = Directory.GetCurrentDirectory() + "/" + username + "/" + DataParser.GetDateFromGetFileBytes(payloadbytes) + "/rawBike.bin";
int length = Int32.MaxValue;
try
{
FileInfo fi = new FileInfo(path);
length = (int)fi.Length;
}
catch
{
Console.WriteLine("couldn't find file " + path);
}
if (length > 10240)
break;
// we need to send it to the doctor and not to the user with the username
communication.SendMessageToClient(this.username, DataParser.GetFileMessage(File.ReadAllBytes(path)));
break; 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)}");
@@ -199,8 +180,57 @@ namespace Server
Array.Copy(payloadbytes, 0, this.BPMDataBuffer, 0, 2); Array.Copy(payloadbytes, 0, this.BPMDataBuffer, 0, 2);
} }
//this.communication.Doctor?.sendMessage(DataParser.GetRawBPMDataDoctor(payloadbytes, this.username)); //this.communication.Doctor?.sendMessage(DataParser.GetRawBPMDataDoctor(payloadbytes, this.username));
}
}
private void getClientBikeData(byte[] payloadbytes)
{
//ugly
//get the raw bike data of the user with the specified username
string username = DataParser.GetUsernameFromGetFileBytes(payloadbytes);
string path = Directory.GetCurrentDirectory() + "/" + username + "/";
string bytes = string.Empty;
StringBuilder sb = new StringBuilder(bytes);
int length = 0;
try
{
DirectoryInfo dirInf = new DirectoryInfo(Directory.GetCurrentDirectory() + "/" + username + "/");
DirectoryInfo[] directoryInfos = dirInf.GetDirectories();
for (int i = 0; i < directoryInfos.Length; i++)
{
DirectoryInfo info = directoryInfos[i];
string newPath = path + info.Name + "/rawBike.bin";
Debug.WriteLine("[SERVER CLIENT] checking for " + newPath);
sb.Append(getRawBikeDataFromFile(newPath));
if (i != directoryInfos.Length - 1) sb.Append("\n");
FileInfo fi = new FileInfo(newPath);
length += (int)fi.Length;
}
Debug.WriteLine("[SERVER CLIENT] made path to " + path);
}
catch
{
Debug.WriteLine("Folder for user " + username + " does not exist");
} }
if (length > 10240)
{
Debug.WriteLine("[SERVER CLIENT] packet was too big!");
return;
}
communication.Doctor.sendMessage(DataParser.GetFileMessage(Encoding.ASCII.GetBytes(sb.ToString())));
}
public byte[] getRawBikeDataFromFile(string path)
{
return File.ReadAllBytes(path);
} }
private bool handleLogin(byte[] payloadbytes) private bool handleLogin(byte[] payloadbytes)