Develop #10

Merged
SemvdH merged 229 commits from develop into master 2020-10-29 22:50:49 +00:00
12 changed files with 319 additions and 36 deletions
Showing only changes of commit 3ea42b65ed - Show all commits

View File

@@ -7,6 +7,14 @@
<ApplicationIcon>Images\Logo\icon1.ico</ApplicationIcon> <ApplicationIcon>Images\Logo\icon1.ico</ApplicationIcon>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<None Remove="Images\CoolBackground.jpg" /> <None Remove="Images\CoolBackground.jpg" />
<None Remove="Images\Icons\CheckMark.png" /> <None Remove="Images\Icons\CheckMark.png" />

View File

@@ -181,7 +181,7 @@ namespace ClientApp.Utils
break; break;
} }
} }
else if (DataParser.isRawData(messageBytes)) else if (DataParser.isRawDataBikeServer(messageBytes))
{ {
Console.WriteLine($"Received data: {BitConverter.ToString(payloadbytes)}"); Console.WriteLine($"Received data: {BitConverter.ToString(payloadbytes)}");
} }
@@ -228,7 +228,7 @@ namespace ClientApp.Utils
{ {
throw new ArgumentNullException("no bytes"); throw new ArgumentNullException("no bytes");
} }
byte[] message = DataParser.GetRawDataMessage(bytes); byte[] message = DataParser.GetRawBPMDataMessageServer(bytes);
if (engineConnection.Connected && engineConnection.FollowingRoute) if (engineConnection.Connected && engineConnection.FollowingRoute)
{ {
@@ -255,7 +255,7 @@ namespace ClientApp.Utils
{ {
throw new ArgumentNullException("no bytes"); throw new ArgumentNullException("no bytes");
} }
byte[] message = DataParser.GetRawDataMessage(bytes); byte[] message = DataParser.GetRawBikeDataMessageServer(bytes);
bool canSendToEngine = engineConnection.Connected && engineConnection.FollowingRoute; bool canSendToEngine = engineConnection.Connected && engineConnection.FollowingRoute;
switch (bytes[0]) switch (bytes[0])
{ {

View File

@@ -23,6 +23,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="AsyncAwaitBestPractices.MVVM" Version="4.3.0" /> <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="MvvmLightLibsStd10" Version="5.4.1.1" />
<PackageReference Include="PropertyChanged.Fody" Version="3.2.9" /> <PackageReference Include="PropertyChanged.Fody" Version="3.2.9" />
</ItemGroup> </ItemGroup>

127
DoctorApp/Models/Chart.cs Normal file
View File

@@ -0,0 +1,127 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using LiveCharts;
using LiveCharts.Configurations;
using Util;
namespace DoctorApp.Models
{
class Chart : ObservableObject,INotifyPropertyChanged
{
private double _axisMax;
private double _axisMin;
private double _trend;
public event PropertyChangedEventHandler PropertyChanged;
public ChartValues<MeasureModel> ChartValues { get; set; }
public Func<double, string> DateTimeFormatter { get; set; }
public PatientInfo PatientInfo { get; set; }
public double AxisStep { get; set; }
public double AxisUnit { get; set; }
public double AxisMax
{
get { return _axisMax; }
set
{
_axisMax = value;
OnPropertyChanged("AxisMax");
}
}
public double AxisMin
{
get { return _axisMin; }
set
{
_axisMin = value;
OnPropertyChanged("AxisMin");
}
}
public bool IsReading { get; set; }
public Chart(PatientInfo patientInfo)
{
var mapper = Mappers.Xy<MeasureModel>()
.X(model => model.DateTime.Ticks)
.Y(model => model.Value);
Charting.For<MeasureModel>(mapper);
ChartValues = new ChartValues<MeasureModel>();
DateTimeFormatter = value => new DateTime((long)value).ToString("mm:ss");
AxisStep = TimeSpan.FromSeconds(1).Ticks;
AxisUnit = TimeSpan.TicksPerSecond;
SetAxisLimits(DateTime.Now);
IsReading = true;
ChartValues.Add(new MeasureModel
{
DateTime = DateTime.Now,
Value = 8
});
}
private void SetAxisLimits(DateTime now)
{
AxisMax = now.Ticks + TimeSpan.FromSeconds(1).Ticks; // lets force the axis to be 1 second ahead
AxisMin = now.Ticks - TimeSpan.FromSeconds(8).Ticks; // and 8 seconds behind
}
protected virtual void OnPropertyChanged(string propertyName = null)
{
if (PropertyChanged != null)
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public void NewValue(double value)
{
var now = DateTime.Now;
_trend = value;
ChartValues.Add(new MeasureModel
{
DateTime = now,
Value = _trend
});
SetAxisLimits(now);
if (ChartValues.Count > 150) ChartValues.RemoveAt(0);
}
public void Clear()
{
Debug.WriteLine("clear");
ChartValues.Clear();
}
}
public class MeasureModel
{
public DateTime DateTime { get; set; }
public double Value { get; set; }
}
}

View File

@@ -10,11 +10,11 @@ namespace DoctorApp.Models
public string Status { get; set; } public string Status { get; set; }
public int Speed { get; set; } public double Speed { get; set; }
public int BPM { get; set; } public int BPM { get; set; }
public int Resistance { get; set; } public float Resistance { get; set; }
public int Acc_Power { get; set; } public int Acc_Power { get; set; }
public int Curr_Power { get; set; } public int Curr_Power { get; set; }

View File

@@ -90,7 +90,7 @@ namespace DoctorApp.Utils
string responseStatus = DataParser.getResponseStatus(payloadbytes); string responseStatus = DataParser.getResponseStatus(payloadbytes);
if (responseStatus == "OK") if (responseStatus == "OK")
{ {
Debug.WriteLine("Username and password correct!"); Debug.WriteLine("Doctor Username and password correct!");
this.LoginViewModel.setLoginStatus(true); this.LoginViewModel.setLoginStatus(true);
this.connected = true; this.connected = true;
@@ -121,9 +121,13 @@ namespace DoctorApp.Utils
break; break;
} }
} }
else if (DataParser.isRawData(messageBytes)) else if (DataParser.isRawDataBikeDoctor(messageBytes))
{ {
Console.WriteLine($"Received data: {BitConverter.ToString(payloadbytes)}"); MainViewModel.TransferDataToClientBike(payloadbytes);
}
else if (DataParser.isRawDataBPMDoctor(messageBytes))
{
MainViewModel.TransferDataToClientBPM(payloadbytes);
} }
totalBufferReceived -= expectedMessageLength; totalBufferReceived -= expectedMessageLength;

View File

@@ -5,6 +5,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics;
using System.Text; using System.Text;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Input; using System.Windows.Input;
@@ -17,6 +18,17 @@ namespace DoctorApp.ViewModels
{ {
public PatientInfo PatientInfo { get; set; } public PatientInfo PatientInfo { get; set; }
private string _mySelectedItem;
public string MySelectedItem
{
get { return _mySelectedItem; }
set
{
Chart.Clear();
_mySelectedItem = value;
}
}
public ICommand StartSession { get; set; } public ICommand StartSession { get; set; }
public ICommand StopSession { get; set; } public ICommand StopSession { get; set; }
@@ -32,10 +44,13 @@ namespace DoctorApp.ViewModels
public MainWindowViewModel MainWindowViewModel { get; set; } public MainWindowViewModel MainWindowViewModel { get; set; }
private Client client; private Client client;
public Chart Chart { get; set; }
public ClientInfoViewModel(MainWindowViewModel mainWindowViewModel, string username) public ClientInfoViewModel(MainWindowViewModel mainWindowViewModel, string username)
{ {
MainWindowViewModel = mainWindowViewModel; MainWindowViewModel = mainWindowViewModel;
this.PatientInfo = new PatientInfo() { Username = username, Status = "Waiting to start" }; this.PatientInfo = new PatientInfo() { Username = username, Status = "Waiting to start" };
this.Chart = new Chart(this.PatientInfo);
PatientInfo.ChatLog = new ObservableCollection<string>(); PatientInfo.ChatLog = new ObservableCollection<string>();
client = mainWindowViewModel.client; client = mainWindowViewModel.client;
@@ -61,11 +76,52 @@ namespace DoctorApp.ViewModels
SetResistance = new RelayCommand<object>((parameter) => SetResistance = new RelayCommand<object>((parameter) =>
{ {
client.sendMessage(DataParser.getSetResistanceJson(PatientInfo.Username, float.Parse(((TextBox)parameter).Text))); Debug.WriteLine("resistance");
//client.sendMessage(DataParser.getSetResistanceJson(PatientInfo.Username, float.Parse(((TextBox)parameter).Text)));
PatientInfo.Resistance = float.Parse(((TextBox)parameter).Text);
}); });
} }
public void BPMData(byte [] bytes)
{
//TODO
//Parsen van de data you fuck
PatientInfo.BPM = bytes[1];
if (MySelectedItem == "BPM")
{
Chart.NewValue(PatientInfo.BPM);
}
}
public void BikeData(byte[] bytes)
{
//TODO
//Parsen van de data you fuck
switch (bytes[0])
{
case 0x10:
if (bytes[1] != 25)
{
throw new Exception();
}
PatientInfo.Distance = bytes[3];
PatientInfo.Speed = (bytes[4] | (bytes[5] << 8)) * 0.01;
break;
case 0x19:
PatientInfo.Acc_Power = bytes[3] | (bytes[4] << 8);
PatientInfo.Curr_Power = (bytes[5]) | (bytes[6] & 0b00001111) << 8;
break;
default:
throw new Exception();
}
if (MySelectedItem == "Speed")
{
Chart.NewValue(PatientInfo.Speed);
}
}
} }
} }

View File

@@ -48,6 +48,30 @@ namespace DoctorApp.ViewModels
} }
}); });
} }
public void TransferDataToClientBike(byte[] bytes)
{
string username = DataParser.getNameFromBytesBike(bytes);
foreach(ClientInfoViewModel item in Tabs)
{
if(item.PatientInfo.Username == username)
{
item.BikeData(bytes);
}
}
}
public void TransferDataToClientBPM(byte[] bytes)
{
string username = DataParser.getNameFromBytesBPM(bytes);
foreach (ClientInfoViewModel item in Tabs)
{
if (item.PatientInfo.Username == username)
{
item.BikeData(bytes);
}
}
}
} }

View File

@@ -5,9 +5,8 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:DoctorApp.Views" xmlns:local="clr-namespace:DoctorApp.Views"
mc:Ignorable="d" mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800" xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
d:DesignHeight="450" d:DesignWidth="800">
>
<Grid Margin="15,5,15,15"> <Grid Margin="15,5,15,15">
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition Width="5*"/> <ColumnDefinition Width="5*"/>
@@ -56,13 +55,35 @@
</DockPanel> </DockPanel>
</StackPanel> </StackPanel>
<ListBox Name="ChatBox" ItemsSource="{Binding PatientInfo.ChatLog}" Grid.Column="1" Margin="59,41,0,0" Grid.RowSpan="3"/> <ListBox Name="ChatBox" ItemsSource="{Binding PatientInfo.ChatLog}" Grid.Column="1" Margin="59,41,0,0" Grid.RowSpan="3"/>
<TextBox Name="textBox_Chat" Grid.Column="1" HorizontalAlignment="Left" Margin="59,10,0,0" Grid.Row="3" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Width="235"/> <TextBox Name="textBox_Chat" Grid.Column="1" HorizontalAlignment="Left" Margin="59,10,0,0" Grid.Row="3" Text="" TextWrapping="Wrap" VerticalAlignment="Top" Width="235"/>
<Button x:Name="Send" Content="Send" Grid.Column="1" HorizontalAlignment="Left" Margin="59,33,0,0" Grid.Row="3" VerticalAlignment="Top" Command="{Binding Chat}" CommandParameter="{Binding ElementName=textBox_Chat}"/> <Button x:Name="Send" Content="Send" Grid.Column="1" HorizontalAlignment="Left" Margin="59,33,0,0" Grid.Row="3" VerticalAlignment="Top" Command="{Binding Chat}" CommandParameter="{Binding ElementName=textBox_Chat}"/>
<Button Content="Start Session" Grid.Column="1" HorizontalAlignment="Left" Margin="69,86,0,0" Grid.Row="3" VerticalAlignment="Top" Width="97" Command="{Binding StartSession}" CommandParameter=""/> <Button Content="Start Session" Grid.Column="1" HorizontalAlignment="Left" Margin="69,86,0,0" Grid.Row="3" VerticalAlignment="Top" Width="97" Command="{Binding StartSession}" CommandParameter=""/>
<Button Content="Stop Session" Grid.Column="1" HorizontalAlignment="Left" Margin="187,86,0,0" Grid.Row="3" VerticalAlignment="Top" Width="97" Command="{Binding StopSession}" CommandParameter=""/> <Button Content="Stop Session" Grid.Column="1" HorizontalAlignment="Left" Margin="187,86,0,0" Grid.Row="3" VerticalAlignment="Top" Width="97" Command="{Binding StopSession}" CommandParameter=""/>
<TextBox x:Name="textBox_SetResistance" Grid.Column="1" HorizontalAlignment="Left" Margin="69,128,0,0" Grid.Row="3" TextWrapping="Wrap" VerticalAlignment="Top" Width="97"/> <TextBox x:Name="textBox_SetResistance" Grid.Column="1" HorizontalAlignment="Left" Margin="69,128,0,0" Grid.Row="3" TextWrapping="Wrap" VerticalAlignment="Top" Width="97"/>
<Button Content="Set Resistance" Grid.Column="1" HorizontalAlignment="Left" Margin="187,128,0,0" Grid.Row="3" VerticalAlignment="Top" Width="97" Height="18" Command="{Binding SetResistance}" CommandParameter="{Binding ElementName=textBox_SetResistance}"/> <Button Content="Set Resistance" Grid.Column="1" HorizontalAlignment="Left" Margin="187,128,0,0" Grid.Row="3" VerticalAlignment="Top" Width="97" Height="18" Command="{Binding SetResistance}" CommandParameter="{Binding ElementName=textBox_SetResistance}"/>
<Canvas Grid.Row="3" Background="White" Margin="0,33,0,0"/> <lvc:CartesianChart Grid.Row="3" AnimationsSpeed="0:0:0.5" Hoverable="False" DataTooltip="{x:Null}" Margin="0,33,0,-5">
<ComboBox Name="DropBox" HorizontalAlignment="Left" Margin="0,6,0,0" Grid.Row="3" VerticalAlignment="Top" Width="190"/> <lvc:CartesianChart.Series>
<lvc:LineSeries Values="{Binding Chart.ChartValues}"
PointGeometry="{x:Null}"
LineSmoothness="1"
StrokeThickness="6"
Stroke="#F34336"
Fill="Transparent"/>
</lvc:CartesianChart.Series>
<lvc:CartesianChart.AxisX>
<lvc:Axis LabelFormatter="{Binding Chart.DateTimeFormatter}"
MaxValue="{Binding Chart.AxisMax}"
MinValue="{Binding Chart.AxisMin}"
Unit="{Binding Chart.AxisUnit}">
<lvc:Axis.Separator>
<lvc:Separator Step="{Binding Chart.AxisStep}" />
</lvc:Axis.Separator>
</lvc:Axis>
</lvc:CartesianChart.AxisX>
</lvc:CartesianChart>
<ComboBox Name="DropBox" HorizontalAlignment="Left" Margin="0,6,0,0" Grid.Row="3" VerticalAlignment="Top" Width="190" SelectedItem="{Binding Path=MySelectedItem}">
<ComboBoxItem Content="Speed" IsSelected="True"></ComboBoxItem>
<ComboBoxItem Content="BPM"></ComboBoxItem>
</ComboBox>
</Grid> </Grid>
</UserControl> </UserControl>

View File

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

View File

@@ -5,6 +5,7 @@ using System.Diagnostics;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime; using System.Runtime.InteropServices.WindowsRuntime;
using System.Security.Cryptography;
using System.Text; using System.Text;
namespace Util namespace Util
@@ -185,7 +186,7 @@ namespace Util
/// </summary> /// </summary>
/// <param name="bytes">message</param> /// <param name="bytes">message</param>
/// <returns>if message contains raw data</returns> /// <returns>if message contains raw data</returns>
public static bool isRawData(byte[] bytes) public static bool isRawDataBikeServer(byte[] bytes)
{ {
if (bytes.Length <= 5) if (bytes.Length <= 5)
{ {
@@ -194,6 +195,34 @@ namespace Util
return bytes[4] == 0x02; return bytes[4] == 0x02;
} }
public static bool isRawDataBPMServer(byte[] bytes)
{
if (bytes.Length <= 5)
{
throw new ArgumentException("bytes to short");
}
return bytes[4] == 0x03;
}
public static bool isRawDataBikeDoctor(byte[] bytes)
{
if (bytes.Length <= 5)
{
throw new ArgumentException("bytes to short");
}
return bytes[4] == 0x04;
}
public static bool isRawDataBPMDoctor(byte[] bytes)
{
if (bytes.Length <= 5)
{
throw new ArgumentException("bytes to short");
}
return bytes[4] == 0x05;
}
/// <summary> /// <summary>
/// constructs a message with the payload, messageId and clientId /// constructs a message with the payload, messageId and clientId
/// </summary> /// </summary>
@@ -218,11 +247,34 @@ namespace Util
/// <param name="payload"></param> /// <param name="payload"></param>
/// <param name="clientId"></param> /// <param name="clientId"></param>
/// <returns>the message ready for sending</returns> /// <returns>the message ready for sending</returns>
public static byte[] GetRawDataMessage(byte[] payload) public static byte[] GetRawBikeDataMessageServer(byte[] payload)
{ {
return getMessage(payload, 0x02); return getMessage(payload, 0x02);
} }
public static byte[] GetRawBPMDataMessageServer(byte[] payload)
{
return getMessage(payload, 0x03);
}
public static byte[] GetRawBikeDataDoctor(byte[] payload, string username)
{
return GetRawDataDoctor(payload, username, 0x04);
}
public static byte[] GetRawBPMDataDoctor(byte[] payload, string username)
{
return GetRawDataDoctor(payload,username,0x05);
}
private static byte[] GetRawDataDoctor(byte[] payload, string username, byte messageID)
{
return getMessage(payload.Concat(Encoding.ASCII.GetBytes(username)).ToArray(), messageID);
}
/// <summary> /// <summary>
/// constructs a message with the payload and clientId and assumes the payload is json /// constructs a message with the payload and clientId and assumes the payload is json
/// </summary> /// </summary>

View File

@@ -140,26 +140,16 @@ namespace Server
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payloadbytes)); dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payloadbytes));
} }
else if (DataParser.isRawData(message)) else if (DataParser.isRawDataBikeServer(message))
{ {
// print the raw data saveData?.WriteDataRAWBike(payloadbytes);
//Console.WriteLine(BitConverter.ToString(payloadbytes));
// TODO change, checking for length is not that safe
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));
}
} }
else if (DataParser.isRawDataBPMServer(message))
{
saveData?.WriteDataRAWBPM(payloadbytes);
}
} }
private bool handleLogin(byte[] payloadbytes) private bool handleLogin(byte[] payloadbytes)