Develop #10

Merged
SemvdH merged 229 commits from develop into master 2020-10-29 22:50:49 +00:00
10 changed files with 159 additions and 33 deletions
Showing only changes of commit 9f7a01170b - Show all commits

View File

@@ -185,7 +185,7 @@ namespace ClientApp.Utils
else if (DataParser.isRawDataBikeServer(messageBytes))
{
Console.WriteLine($"Received data: {BitConverter.ToString(payloadbytes)}");
}
}
totalBufferReceived -= expectedMessageLength;
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);

View File

@@ -16,7 +16,7 @@ namespace ClientApp.ViewModels
public string Username { get; set; }
public ICommand LoginCommand { get; set; }
public bool LoginStatus { get; set; }
public bool LoginStatus { get; set; } = false;
public bool InvertedLoginStatus { get; set; }

View File

@@ -19,9 +19,7 @@
<Label Content="Password" HorizontalContentAlignment="Center"/>
<PasswordBox x:Name="Password" Width="120" Util:FocusAdvancement.AdvancesByEnterKey="True"/>
<Button x:Name="Login" Content="Login" Command="{Binding LoginCommand}" CommandParameter="{Binding ElementName=Password}" Margin="0,20,0,0" Width="120"/>
<Popup IsOpen="{Binding InvertedLoginStatus}" PopupAnimation = "Slide" HorizontalAlignment="Center">
<Label Content="Login failed" Foreground="Red" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Background="Transparent"/>
</Popup>
<Label Content="Login failed" Foreground="Red" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Background="Transparent" IsEnabled="{Binding InvertedLoginStatus}"/>
</StackPanel>
</DockPanel>
</Page>

View File

@@ -25,6 +25,7 @@
<PackageReference Include="AsyncAwaitBestPractices.MVVM" Version="4.3.0" />
<PackageReference Include="LiveCharts.Wpf" Version="0.9.7" />
<PackageReference Include="MvvmLightLibsStd10" Version="5.4.1.1" />
<PackageReference Include="Prism.Wpf" Version="8.0.0.1909" />
<PackageReference Include="PropertyChanged.Fody" Version="3.2.9" />
</ItemGroup>

View File

@@ -1,9 +1,11 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using DoctorApp.ViewModels;
using Microsoft.Win32;
using ProftaakRH;
using Util;
@@ -129,6 +131,23 @@ namespace DoctorApp.Utils
{
MainViewModel.TransferDataToClientBPM(payloadbytes);
}
else if (DataParser.IsHistoricBikeData(messageBytes))
{
// todo change to name of patient
string name = MainViewModel.Tabs[0].PatientInfo.Username;
string result = "PATIENT INFO FOR " + name + "\n\n";
for (int i = 0; i < payloadbytes.Length; i += 8)
{
byte[] res = new byte[8];
Array.Copy(payloadbytes, i, res, 0, 8);
result += handleBikeHistory(res);
}
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/" + name + ".txt";
File.WriteAllText(path,result);
}
Array.Copy(totalBuffer, expectedMessageLength, totalBuffer, 0, (totalBufferReceived - expectedMessageLength)); //maybe unsafe idk
totalBufferReceived -= expectedMessageLength;
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
@@ -140,11 +159,40 @@ namespace DoctorApp.Utils
}
/// <summary>
/// starts sending a message to the server
/// </summary>
/// <param name="message">the message to send</param>
public void sendMessage(byte[] message)
private string handleBikeHistory(byte[] bytes)
{
string res = string.Empty;
switch (bytes[0])
{
case 0x10:
res += "Time elapsed: " + bytes[2] / 4 + "s\n";
res += "Distance traveled: " + bytes[3] + "\n";
int input = bytes[4] | (bytes[5] << 8);
res += $"Speed {input * 0.01}m/s\n";
res += $"Heart rate: { Convert.ToString(bytes[6], 10)}";
break;
case 0x19:
res += $"RPM: {bytes[2]}\n";
int accumPower = bytes[3] | (bytes[4] << 8);
res += $"Accumulated power: {accumPower} watt\n";
int instantPower = (bytes[5]) | (bytes[6] & 0b00001111) << 8;
if (instantPower != 0xFFF)
{
res += $"Instant power: {instantPower} watt\n";
}
break;
}
return res + "\n";
}
/// <summary>
/// starts sending a message to the server
/// </summary>
/// <param name="message">the message to send</param>
public void sendMessage(byte[] message)
{
stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
}

View File

@@ -41,6 +41,8 @@ namespace DoctorApp.ViewModels
public ICommand SetResistance { get; set; }
public ICommand SaveHistoricData { get; set; }
public MainWindowViewModel MainWindowViewModel { get; set; }
private Client client;
@@ -81,13 +83,23 @@ namespace DoctorApp.ViewModels
PatientInfo.Resistance = float.Parse(((TextBox)parameter).Text);
});
// request the historic data from the server
this.SaveHistoricData = new RelayCommand<object>((parameter) =>
{
this.client.sendMessage(DataParser.GetGetFileMessage(PatientInfo.Username));
// data is stored on the server
// send request to server that we want to get the current historic data from the patient
// server sends this back
// we parse it
});
}
public void BPMData(byte [] bytes)
public void BPMData(byte[] bytes)
{
//TODO
//Parsen van de data you fuck
if(bytes[0] == 0x00)
if (bytes[0] == 0x00)
{
}
@@ -95,13 +107,13 @@ namespace DoctorApp.ViewModels
{
PatientInfo.BPM = bytes[1];
if (MySelectedItem == "BPM")
{
{
Chart.NewValue(PatientInfo.BPM);
}
}
}
public void BikeData(byte[] bytes)

View File

@@ -18,16 +18,20 @@
<RowDefinition Height="180*"/>
<RowDefinition Height="180*"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.RowSpan="2" Margin="0,0,0,22">
<StackPanel.Resources>
<Style TargetType="{x:Type Label}">
<Setter Property="Margin" Value="0,0,20,0"/>
</Style>
</StackPanel.Resources>
<Label Content="{Binding Path=PatientInfo.Username}"/>
<Label Content="{Binding Path=PatientInfo.Status}"/>
</StackPanel>
<StackPanel Margin="0,10,0,0" Grid.RowSpan="2" Grid.Row="1">
<Grid Grid.RowSpan="2" Margin="0,0,0,22">
<StackPanel Orientation="Horizontal" >
<StackPanel.Resources>
<Style TargetType="{x:Type Label}">
<Setter Property="Margin" Value="0,0,20,0"/>
</Style>
</StackPanel.Resources>
<Label Content="{Binding Path=PatientInfo.Username}"/>
<Label Content="{Binding Path=PatientInfo.Status}"/>
</StackPanel>
<Button Content="Save Historic Data" HorizontalAlignment="Right" VerticalAlignment="Center" Padding="5" Command="{Binding SaveHistoricData}"/>
</Grid>
<StackPanel Margin="0,10,0,0" Grid.Row="2">
<StackPanel.Resources>
<Style TargetType="{x:Type DockPanel}">
<Setter Property="Margin" Value="0,20,0,0"/>

View File

@@ -10,7 +10,7 @@
AllowsTransparency="True"
MinHeight="{Binding MinimumHeight}"
MinWidth="{Binding MinimumWidth}"
Title="MainWindow" Height="500" Width="900">
Title="MainWindow" Height="800" Width="1500">
<Window.Resources>
<Style TargetType="{x:Type local:MainWindow}">
<Setter Property="Template">

View File

@@ -23,6 +23,7 @@ namespace Util
public const string DISCONNECT = "DISCONNECT";
public const string LOGIN_DOCTOR = "LOGIN DOCTOR";
public const string MESSAGE = "MESSAGE";
public const string GET_FILE = "GET FILE";
/// <summary>
/// makes the json object with LOGIN identifier and username and password
/// </summary>
@@ -72,7 +73,7 @@ namespace Util
return getName(bytes, 2, bytes.Length - 2);
}
private static string getName(byte[] bytes , int offset, int lenght)
private static string getName(byte[] bytes, int offset, int lenght)
{
byte[] nameArray = new byte[lenght];
Array.Copy(bytes, offset, nameArray, 0, lenght);
@@ -242,6 +243,12 @@ namespace Util
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>
/// constructs a message with the payload, messageId and clientId
@@ -286,7 +293,7 @@ namespace Util
public static byte[] GetRawBPMDataDoctor(byte[] payload, string username)
{
return GetRawDataDoctor(payload,username,0x05);
return GetRawDataDoctor(payload, username, 0x05);
}
private static byte[] GetRawDataDoctor(byte[] payload, string username, byte messageID)
@@ -295,8 +302,8 @@ namespace Util
byte[] nameArray = Encoding.ASCII.GetBytes(username);
byte[] total = new byte[nameArray.Length + payload.Length];
Array.Copy(payload, 0, total, 0, payload.Length);
Array.Copy(nameArray,0,total,payload.Length,nameArray.Length);
return getMessage(total,messageID);
Array.Copy(nameArray, 0, total, payload.Length, nameArray.Length);
return getMessage(total, messageID);
}
/// <summary>
@@ -337,14 +344,14 @@ namespace Util
};
return getJsonMessage(STOP_SESSION, data);
}
/// <summary>
/// gets the message to set the resistance of the given user with the given resistance.
/// </summary>
/// <param name="user">the username to set the resistance of.</param>
/// <param name="mResistance">the resistance value to set</param>
/// <returns>a byte array containing a json messsage to set the user's resistance, using our protocol.</returns>
public static byte[] getSetResistanceJson(string user,float mResistance)
public static byte[] getSetResistanceJson(string user, float mResistance)
{
dynamic data = new
{
@@ -436,7 +443,7 @@ namespace Util
/// <returns>the chat message in the json message</returns>
public static string getChatMessageFromJson(byte[] json)
{
return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.chat;
return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.chat;
}
/// <summary>
@@ -472,6 +479,32 @@ namespace Util
return data;
}
public static byte[] GetGetFileMessage(string mUsername)
{
if (mUsername == null)
{
throw new ArgumentNullException("username null");
}
dynamic data = new
{
username = mUsername,
};
return getJsonMessage(GET_FILE, data);
}
public static string GetUsernameFromGetFileBytes(byte[] json)
{
return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.username;
}
public static string GetDateFromGetFileBytes(byte[] json)
{
return ((string)((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.dateTime);
}
public static byte[] GetFileMessage(byte[] file)
{
return getMessage(file, 0x06);
}
}
}

View File

@@ -147,6 +147,9 @@ namespace Server
case DataParser.MESSAGE:
communication.SendMessageToClient(DataParser.getUsernameFromJson(payloadbytes), message);
break;
case DataParser.GET_FILE:
getClientBikeData(payloadbytes);
break;
default:
Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}");
break;
@@ -177,8 +180,35 @@ namespace Server
Array.Copy(payloadbytes, 0, this.BPMDataBuffer, 0, 2);
}
//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);
try
{
DirectoryInfo dirInf = new DirectoryInfo(Directory.GetCurrentDirectory() + "/" + username + "/");
DirectoryInfo[] directoryInfos = dirInf.GetDirectories();
DirectoryInfo latest = directoryInfos[directoryInfos.Length - 1];
path = path + latest.Name + "/rawBike.bin";
FileInfo fi = new FileInfo(path);
if ((int)fi.Length >= 1024) return;
}
catch (Exception e)
{
Debug.WriteLine("[SERVER CLIENT] excetion while trying to get raw bike data: " + e.Message);
}
Debug.WriteLine("[SERVER CLIENT] about to send " +sb.ToString());
communication.Doctor.sendMessage(DataParser.GetFileMessage(File.ReadAllBytes(path)));
}
private bool handleLogin(byte[] payloadbytes)