Develop #10

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

View File

@@ -139,12 +139,12 @@ namespace ClientApp.Utils
Console.WriteLine("Session started!");
this.sessionRunning = true;
if (engineConnection.Connected && !engineConnection.FollowingRoute) engineConnection.StartRouteFollow();
sendMessage(DataParser.getStartSessionJson());
Debug.WriteLine("start");
break;
case DataParser.STOP_SESSION:
Console.WriteLine("Stop session identifier");
this.sessionRunning = false;
sendMessage(DataParser.getStopSessionJson());
Debug.WriteLine("stop");
break;
case DataParser.SET_RESISTANCE:
Console.WriteLine("Set resistance identifier");
@@ -181,7 +181,7 @@ namespace ClientApp.Utils
/// starts sending a message to the server
/// </summary>
/// <param name="message">the message to send</param>
private void sendMessage(byte[] message)
public void sendMessage(byte[] message)
{
stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
}
@@ -297,6 +297,7 @@ namespace ClientApp.Utils
public void Dispose()
{
Debug.WriteLine("client dispose called");
sendMessage(DataParser.getDisconnectJson(LoginViewModel.Username));
this.stream.Dispose();
this.client.Dispose();
this.handler.stop();

View File

@@ -116,7 +116,7 @@ namespace DoctorApp.Utils
this.MainViewModel.NewConnectedUser(DataParser.getUsernameFromResponseJson(payloadbytes));
break;
case DataParser.DISCONNECT:
this.MainViewModel.NewConnectedUser(DataParser.getUsernameFromResponseJson(payloadbytes));
this.MainViewModel.DisconnectedUser(DataParser.getUsernameFromResponseJson(payloadbytes));
break;
default:
Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}");
@@ -140,7 +140,7 @@ namespace DoctorApp.Utils
/// starts sending a message to the server
/// </summary>
/// <param name="message">the message to send</param>
private void sendMessage(byte[] message)
public void sendMessage(byte[] message)
{
stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
}
@@ -170,7 +170,7 @@ namespace DoctorApp.Utils
string hashPassword = Util.Hasher.HashString(password);
byte[] message = DataParser.getJsonMessage(DataParser.LoginAsDoctor(username, hashPassword));
byte[] message = DataParser.getJsonMessage(DataParser.GetLoginJson(username, hashPassword));
this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);

View File

@@ -1,13 +1,81 @@
using System;
using DoctorApp.Utils;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Text;
using System.Windows.Controls;
using System.Windows.Input;
using Util;
namespace DoctorApp.ViewModels
{
class ClientInfoViewModel
class ClientInfoViewModel : ObservableObject
{
public ObservableCollection<string> ChatLog { get; set; }
public string Username { get; set; }
public string TabName { get; set; }
public string Status { get; set; }
public int Speed { get; set; }
public int BPM { get; set; }
public int Resistance { get; set; }
public int Acc_Power { get; set; }
public int Curr_Power { get; set; }
public int Distance { get; set; }
public ICommand StartSession { get; set; }
public ICommand StopSession { get; set; }
public ICommand Chat { get; set; }
public ICommand ChatToAll { get; set; }
public ICommand ClientInfo { get; set; }
public ICommand SetResistance { get; set; }
public MainWindowViewModel MainWindowViewModel { get; set; }
private Client client;
public ClientInfoViewModel(MainWindowViewModel mainWindowViewModel)
{
MainWindowViewModel = mainWindowViewModel;
ChatLog = new ObservableCollection<string>();
client = mainWindowViewModel.client;
StartSession = new RelayCommand(()=>{
client.sendMessage(DataParser.getStartSessionJson(Username));
Status = "Session started";
});
StopSession = new RelayCommand(() => {
client.sendMessage(DataParser.getStopSessionJson(Username));
Status = "Session stopped, waiting to start again.";
});
Chat = new RelayCommand<object>((parameter) =>
{
client.sendMessage(DataParser.getChatJson(Username, ((TextBox)parameter).Text));
ChatLog.Add(DateTime.Now+": "+ ((TextBox)parameter).Text);
});
//TODO RelayCommand ChatToAll
SetResistance = new RelayCommand<object>((parameter) =>
{
client.sendMessage(DataParser.getSetResistanceJson(Username, float.Parse(((TextBox)parameter).Text)));
});
}
}
}

View File

@@ -11,7 +11,7 @@ namespace DoctorApp.ViewModels
{
class MainViewModel : ObservableObject
{
public ObservableCollection<object> Tabs { get; set; }
public ObservableCollection<ClientInfoViewModel> Tabs { get; set; }
public int Selected { get; set; }
public MainWindowViewModel MainWindowViewModel { get; set; }
@@ -21,7 +21,7 @@ namespace DoctorApp.ViewModels
{
this.MainWindowViewModel = mainWindowViewModel;
client = this.MainWindowViewModel.client;
Tabs = new ObservableCollection<object>();
Tabs= new ObservableCollection<ClientInfoViewModel>();
}
public void NewConnectedUser(string username)
@@ -29,17 +29,27 @@ namespace DoctorApp.ViewModels
System.Diagnostics.Debug.WriteLine("CREATING TAB FOR " + username);
App.Current.Dispatcher.Invoke((Action)delegate
{
Tabs.Add(new ClientInfoViewModel
Tabs.Add(new ClientInfoViewModel(MainWindowViewModel)
{
Username = username,
TabName = username
Status = "Waiting to start"
});
});
}
public void DisconnectedUser(string username)
{
App.Current.Dispatcher.Invoke((Action)delegate
{
foreach (ClientInfoViewModel item in Tabs)
{
if (item.Username == username)
{
Tabs.Remove(item);
break;
}
}
});
}
}

View File

@@ -24,8 +24,8 @@
<Setter Property="Margin" Value="0,0,20,0"/>
</Style>
</StackPanel.Resources>
<Label Content="UserName" Name="Username_Label"/>
<Label Content="Status: " Name="Status_Label"/>
<Label Content="{Binding Path=Username}"/>
<Label Content="{Binding Path=Status}"/>
</StackPanel>
<StackPanel Margin="0,10,0,0" Grid.RowSpan="2" Grid.Row="1">
<StackPanel.Resources>
@@ -39,9 +39,9 @@
<Label Content="Current BPM" Width="110" DockPanel.Dock="Top"/>
</DockPanel>
<DockPanel Height="26" LastChildFill="False" HorizontalAlignment="Stretch">
<TextBox Name="textBox_Resistance" Text="" TextWrapping="Wrap" Width="110" DockPanel.Dock="Right" IsReadOnly="true"/>
<TextBox Name="textBox_CurrentSpeed" Text="" TextWrapping="Wrap" Width="110" DockPanel.Dock="Left" IsReadOnly="true"/>
<TextBox Name="textBox_CurrentBPM" Text="" TextWrapping="Wrap" Width="110" DockPanel.Dock="Top" Height="26" IsReadOnly="true"/>
<TextBox Name="textBox_Resistance" Text="{Binding Path=Resistance}" TextWrapping="Wrap" Width="110" DockPanel.Dock="Right" IsReadOnly="true"/>
<TextBox Name="textBox_CurrentSpeed" Text="{Binding Path=Speed}" TextWrapping="Wrap" Width="110" DockPanel.Dock="Left" IsReadOnly="true"/>
<TextBox Name="textBox_CurrentBPM" Text="{Binding Path=BPM}" TextWrapping="Wrap" Width="110" DockPanel.Dock="Top" Height="26" IsReadOnly="true"/>
</DockPanel>
<DockPanel Height="26" LastChildFill="False">
<Label Content="Distance Covered" Width="110" DockPanel.Dock="Right"/>
@@ -49,20 +49,19 @@
<Label Content="Acc. Power" Width="110" DockPanel.Dock="Top"/>
</DockPanel>
<DockPanel Height="26" LastChildFill="False">
<TextBox Name="textBox_DistanceCovered" Text="" TextWrapping="Wrap" Width="110" DockPanel.Dock="Right" IsReadOnly="true"/>
<TextBox Name="textBox_CurrentPower" Text="" TextWrapping="Wrap" Width="110" DockPanel.Dock="Left" IsReadOnly="true"/>
<TextBox Name="textBox_AccPower" Text="" TextWrapping="Wrap" Width="110" DockPanel.Dock="Top" Height="26" IsReadOnly="true"/>
<TextBox Name="textBox_DistanceCovered" Text="{Binding Path=Distance}" TextWrapping="Wrap" Width="110" DockPanel.Dock="Right" IsReadOnly="true"/>
<TextBox Name="textBox_CurrentPower" Text="{Binding Path=Curr_Power}" TextWrapping="Wrap" Width="110" DockPanel.Dock="Left" IsReadOnly="true"/>
<TextBox Name="textBox_AccPower" Text="{Binding Path=Acc_Power}" TextWrapping="Wrap" Width="110" DockPanel.Dock="Top" Height="26" IsReadOnly="true"/>
</DockPanel>
</StackPanel>
<ListBox Name="ChatBox" Grid.Column="1" Margin="59,41,0,0" Grid.RowSpan="3"/>
<ListBox Name="ChatBox" ItemsSource="{Binding 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"/>
<Button Content="Button" Grid.Column="1" HorizontalAlignment="Left" Margin="59,33,0,0" Grid.Row="3" VerticalAlignment="Top" />
<Button Content="Start Session" Grid.Column="1" HorizontalAlignment="Left" Margin="69,86,0,0" Grid.Row="3" VerticalAlignment="Top" Width="97"/>
<Button Content="Stop Session" Grid.Column="1" HorizontalAlignment="Left" Margin="187,86,0,0" Grid.Row="3" VerticalAlignment="Top" Width="97" />
<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="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"/>
<Button Content="Set Resistance" Grid.Column="1" HorizontalAlignment="Left" Margin="187,128,0,0" Grid.Row="3" VerticalAlignment="Top" Width="97" Height="18"/>
<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"/>
<ComboBox Name="DropBox" HorizontalAlignment="Left" Margin="0,6,0,0" Grid.Row="3" VerticalAlignment="Top" Width="190"/>
<Button Content="Client Info" Grid.Column="1" HorizontalAlignment="Left" Margin="207,6,0,0" VerticalAlignment="Top" Height="26" Width="82"/>
</Grid>
</UserControl>

View File

@@ -8,10 +8,10 @@
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<TabControl ItemsSource="{Binding Tabs}" SelectedItem="{Binding Selected}">
<TabControl TabStripPlacement="Left" ItemsSource="{Binding Tabs}" SelectedItem="{Binding Selected}">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding TabName}"/>
<TextBlock Text="{Binding Username}"/>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>

View File

@@ -5,6 +5,9 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DoctorApp"
mc:Ignorable="d"
Background="Transparent"
WindowStyle="None"
AllowsTransparency="True"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<Style TargetType="{x:Type local:MainWindow}">

View File

@@ -195,20 +195,29 @@ namespace Util
return getMessage(payload, 0x01);
}
public static byte[] getStartSessionJson()
{
return getJsonMessage(START_SESSION);
}
public static byte[] getStopSessionJson()
{
return getJsonMessage(STOP_SESSION);
}
public static byte[] getSetResistanceJson(float mResistance)
public static byte[] getStartSessionJson(string user)
{
dynamic data = new
{
username = user
};
return getJsonMessage(START_SESSION, data);
}
public static byte[] getStopSessionJson(string user)
{
dynamic data = new
{
username = user
};
return getJsonMessage(STOP_SESSION, data);
}
public static byte[] getSetResistanceJson(string user,float mResistance)
{
dynamic data = new
{
username = user,
resistance = mResistance
};
return getJsonMessage(SET_RESISTANCE, data);
@@ -258,6 +267,26 @@ namespace Util
return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.username;
}
public static string getChatMessageFromJson(byte[] json)
{
return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.chat;
}
public static string getUsernameFromJson(byte[] json)
{
return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.username;
}
public static byte[] getChatJson(string user, string message)
{
dynamic data = new
{
username = user,
chat = message
};
return getJsonMessage(MESSAGE, data);
}
}
}

View File

@@ -3,7 +3,6 @@ using System.IO;
using System.Net.Sockets;
using System.Text;
using Newtonsoft.Json;
using ClientApp.Utils;
using System.Diagnostics;
using Util;
using System.Linq;
@@ -112,18 +111,18 @@ namespace Server
}
break;
case DataParser.START_SESSION:
this.saveData = new SaveData(Directory.GetCurrentDirectory() + "/" + this.username + "/" + sessionStart.ToString("yyyy-MM-dd HH-mm-ss"));
this.communication.StartSessionUser(DataParser.getUsernameFromJson(payloadbytes));
break;
case DataParser.STOP_SESSION:
this.saveData = null;
this.communication.StopSessionUser(DataParser.getUsernameFromJson(payloadbytes));
break;
case DataParser.SET_RESISTANCE:
bool worked = DataParser.getResistanceFromResponseJson(payloadbytes);
Console.WriteLine($"set resistance worked is " + worked);
//set resistance on doctor GUI
break;
case DataParser.MESSAGE:
//TODO send message to clients
case DataParser.DISCONNECT:
communication.Disconnect(this);
break;
default:
Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}");
@@ -244,5 +243,15 @@ namespace Server
hex.AppendFormat("{0:x2}", b);
return hex.ToString();
}
public void StartSession()
{
this.saveData = new SaveData(Directory.GetCurrentDirectory() + "/" + this.username + "/" + sessionStart.ToString("yyyy-MM-dd HH-mm-ss"));
}
public void StopSession()
{
this.saveData = null;
}
}
}

View File

@@ -5,7 +5,6 @@ using System.IO.Pipes;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using DoctorApp.Utils;
using Util;
namespace Server
@@ -60,6 +59,7 @@ namespace Server
internal void Disconnect(Client client)
{
clients.Remove(client);
Doctor.sendMessage(DataParser.getDisconnectJson(client.username));
}
public void NewLogin(Client client)
@@ -79,5 +79,30 @@ namespace Server
}
this.clients.Remove(client);
}
public void StartSessionUser(string user)
{
foreach(Client client in clients)
{
if(client.username == user)
{
client.sendMessage(DataParser.getStartSessionJson(user));
client.StartSession();
}
}
}
public void StopSessionUser(string user)
{
foreach (Client client in clients)
{
if (client.username == user)
{
client.sendMessage(DataParser.getStopSessionJson(user));
client.StopSession();
}
}
}
}
}