Develop #10

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

View File

@@ -301,6 +301,7 @@ namespace ClientApp.Utils
this.stream.Dispose(); this.stream.Dispose();
this.client.Dispose(); this.client.Dispose();
this.handler.stop(); this.handler.stop();
this.engineConnection.Stop();
} }
} }
} }

View File

@@ -178,7 +178,7 @@ namespace ClientApp.Utils
Console.WriteLine("set tunnel id to " + tunnelId); Console.WriteLine("set tunnel id to " + tunnelId);
if (tunnelId == null) if (tunnelId == null)
{ {
Write("could not find a valid tunnel id!"); //Write("could not find a valid tunnel id!");
OnNoTunnelId?.Invoke(); OnNoTunnelId?.Invoke();
Connected = false; Connected = false;
FollowingRoute = false; FollowingRoute = false;
@@ -256,9 +256,9 @@ namespace ClientApp.Utils
WriteTextMessage(mainCommand.ShowRoute("showRouteFalse", false)); WriteTextMessage(mainCommand.ShowRoute("showRouteFalse", false));
}); });
}); });
setEnvironment(); setEnvironment();
} }
private void setEnvironment() private void setEnvironment()
@@ -348,7 +348,7 @@ namespace ClientApp.Utils
ImprovedPerlin improvedPerlin = new ImprovedPerlin(0, LibNoise.NoiseQuality.Best); ImprovedPerlin improvedPerlin = new ImprovedPerlin(0, LibNoise.NoiseQuality.Best);
for (int i = 0; i < 256 * 256; i++) for (int i = 0; i < 256 * 256; i++)
{ {
height[i] = improvedPerlin.GetValue(x /10, x / 10, x * 100) / 3.5f + 1; height[i] = improvedPerlin.GetValue(x / 10, x / 10, x * 100) / 3.5f + 1;
//if (height[i] > 1.1f) //if (height[i] > 1.1f)
//{ //{

View File

@@ -10,7 +10,7 @@ using Util;
namespace DoctorApp.Utils namespace DoctorApp.Utils
{ {
public delegate void EngineCallback(); public delegate void EngineCallback();
public class Client : IDataReceiver public class Client
{ {
private TcpClient client; private TcpClient client;
private NetworkStream stream; private NetworkStream stream;
@@ -18,8 +18,6 @@ namespace DoctorApp.Utils
private bool connected; private bool connected;
private byte[] totalBuffer = new byte[1024]; private byte[] totalBuffer = new byte[1024];
private int totalBufferReceived = 0; private int totalBufferReceived = 0;
private bool sessionRunning = false;
private IHandler handler = null;
private LoginViewModel LoginViewModel; private LoginViewModel LoginViewModel;
private MainViewModel MainViewModel; private MainViewModel MainViewModel;
private ClientInfoViewModel ClientInfoViewModel; private ClientInfoViewModel ClientInfoViewModel;
@@ -83,6 +81,8 @@ namespace DoctorApp.Utils
string identifier; string identifier;
bool isJson = DataParser.getJsonIdentifier(messageBytes, out identifier); bool isJson = DataParser.getJsonIdentifier(messageBytes, out identifier);
Debug.WriteLine("doctor " + Encoding.ASCII.GetString(payloadbytes));
if (isJson) if (isJson)
{ {
switch (identifier) switch (identifier)
@@ -104,28 +104,15 @@ namespace DoctorApp.Utils
break; break;
/*case DataParser.START_SESSION: /*case DataParser.START_SESSION:
Console.WriteLine("Session started!"); Console.WriteLine("Session started!");
this.sessionRunning = true;
sendMessage(DataParser.getStartSessionJson());
break; break;
case DataParser.STOP_SESSION: case DataParser.STOP_SESSION:
Console.WriteLine("Stop session identifier"); Console.WriteLine("Stop session identifier");
this.sessionRunning = false; break;
sendMessage(DataParser.getStopSessionJson());
break;*/
case DataParser.SET_RESISTANCE: case DataParser.SET_RESISTANCE:
Console.WriteLine("Set resistance identifier"); Console.WriteLine("Set resistance identifier");
if (this.handler == null)
{
Console.WriteLine("handler is null");
sendMessage(DataParser.getSetResistanceResponseJson(false));
}
else
{
this.handler.setResistance(DataParser.getResistanceFromJson(payloadbytes));
sendMessage(DataParser.getSetResistanceResponseJson(true));
}
break; break;
case DataParser.NEW_CONNECTION: case DataParser.NEW_CONNECTION:
Debug.WriteLine("doctor client new connection");
this.MainViewModel.NewConnectedUser(DataParser.getUsernameFromResponseJson(payloadbytes)); this.MainViewModel.NewConnectedUser(DataParser.getUsernameFromResponseJson(payloadbytes));
break; break;
case DataParser.DISCONNECT: case DataParser.DISCONNECT:
@@ -167,63 +154,6 @@ namespace DoctorApp.Utils
this.stream.EndWrite(ar); this.stream.EndWrite(ar);
} }
#region interface
//maybe move this to other place
/// <summary>
/// bpm method for receiving the BPM value from the bluetooth bike or the simulation
/// </summary>
/// <param name="bytes">the message</param>
public void BPM(byte[] bytes)
{
if (!sessionRunning)
{
return;
}
if (bytes == null)
{
throw new ArgumentNullException("no bytes");
}
byte[] message = DataParser.GetRawDataMessage(bytes);
this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
}
/// <summary>
/// method for receiving the bike message from the bluetooth bike or the simulation
/// </summary>
/// <param name="bytes">the message</param>
public void Bike(byte[] bytes)
{
if (!sessionRunning)
{
return;
}
if (bytes == null)
{
throw new ArgumentNullException("no bytes");
}
byte[] message = DataParser.GetRawDataMessage(bytes);
/* switch (bytes[0])
{
case 0x10:
if (canSendToEngine) engineConnection.BikeSpeed = (bytes[4] | (bytes[5] << 8)) * 0.01f;
break;
case 0x19:
if (canSendToEngine) engineConnection.BikePower = (bytes[5]) | (bytes[6] & 0b00001111) << 8;
break;
}*/
this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
}
#endregion
/// <summary> /// <summary>
/// wether or not the client stream is connected /// wether or not the client stream is connected
/// </summary> /// </summary>
@@ -246,15 +176,6 @@ namespace DoctorApp.Utils
this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
} }
/// <summary>
/// sets the handler for the client, so either the bike simulator or the bluetooth bike handler
/// </summary>
/// <param name="handler"></param>
public void SetHandler(IHandler handler)
{
this.handler = handler;
}
internal void SetLoginViewModel(LoginViewModel loginViewModel) internal void SetLoginViewModel(LoginViewModel loginViewModel)
{ {
this.LoginViewModel = loginViewModel; this.LoginViewModel = loginViewModel;
@@ -275,7 +196,6 @@ namespace DoctorApp.Utils
Debug.WriteLine("client dispose called"); Debug.WriteLine("client dispose called");
this.stream.Dispose(); this.stream.Dispose();
this.client.Dispose(); this.client.Dispose();
this.handler?.stop();
} }
} }
} }

View File

@@ -26,6 +26,7 @@ namespace DoctorApp.ViewModels
public void NewConnectedUser(string username) public void NewConnectedUser(string username)
{ {
System.Diagnostics.Debug.WriteLine("CREATING TAB FOR " + username);
App.Current.Dispatcher.Invoke((Action)delegate App.Current.Dispatcher.Invoke((Action)delegate
{ {
Tabs.Add(new ClientInfoViewModel(MainWindowViewModel) Tabs.Add(new ClientInfoViewModel(MainWindowViewModel)
@@ -52,5 +53,5 @@ namespace DoctorApp.ViewModels
} }
} }
} }

View File

@@ -234,6 +234,8 @@ namespace Util
public static byte[] getNewConnectionJson(string user) public static byte[] getNewConnectionJson(string user)
{ {
if (user == null)
throw new ArgumentNullException("user null");
dynamic data = new dynamic data = new
{ {
username = user username = user

View File

@@ -5,6 +5,7 @@ using System.Text;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Diagnostics; using System.Diagnostics;
using Util; using Util;
using System.Linq;
namespace Server namespace Server
{ {
@@ -96,16 +97,17 @@ namespace Server
switch (identifier) switch (identifier)
{ {
case DataParser.LOGIN: case DataParser.LOGIN:
handleLogin(payloadbytes); if (handleLogin(payloadbytes))
communication.NewLogin(this);
break; break;
case DataParser.LOGIN_DOCTOR: case DataParser.LOGIN_DOCTOR:
if (communication.doctor != null) if (communication.Doctor != null)
return; return;
if (handleLogin(payloadbytes)) if (handleLogin(payloadbytes))
{ {
communication.doctor = this; communication.Doctor = this;
Console.WriteLine("Set doctor to " + communication.doctor + " , this is " + this); Console.WriteLine("Set doctor to " + communication.Doctor + " , this is " + this);
} }
break; break;
case DataParser.START_SESSION: case DataParser.START_SESSION:
@@ -135,7 +137,7 @@ namespace Server
else if (DataParser.isRawData(message)) else if (DataParser.isRawData(message))
{ {
// print the raw data // print the raw data
Console.WriteLine(BitConverter.ToString(payloadbytes)); //Console.WriteLine(BitConverter.ToString(payloadbytes));
// TODO change, checking for length is not that safe // TODO change, checking for length is not that safe
if (payloadbytes.Length == 8) if (payloadbytes.Length == 8)
{ {
@@ -167,7 +169,6 @@ namespace Server
this.username = username; this.username = username;
sendMessage(DataParser.getLoginResponse("OK")); sendMessage(DataParser.getLoginResponse("OK"));
//sendMessage(DataParser.getStartSessionJson()); //sendMessage(DataParser.getStartSessionJson());
communication.NewLogin(this);
return true; return true;
} }
else else
@@ -184,6 +185,7 @@ namespace Server
public void sendMessage(byte[] message) public void sendMessage(byte[] message)
{ {
Debug.WriteLine("serverclient " + Encoding.ASCII.GetString(message.Skip(5).ToArray()));
stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
} }

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Pipes; using System.IO.Pipes;
using System.Linq; using System.Linq;
using System.Net.Sockets; using System.Net.Sockets;
@@ -12,7 +13,25 @@ namespace Server
{ {
private TcpListener listener; private TcpListener listener;
private List<Client> clients; private List<Client> clients;
public Client doctor; private Client mDoctor;
public Client Doctor
{
get
{
return this.mDoctor;
}
set
{
this.mDoctor = value;
this.clients.ForEach((client) =>
{
Debug.WriteLine("foreach called for " + client.username);
byte[] dinges = DataParser.getNewConnectionJson(client.username);
Debug.WriteLine("foreach " + Encoding.ASCII.GetString(dinges.Skip(5).ToArray()));
this.mDoctor.sendMessage(dinges);
});
}
}
public Communication(TcpListener listener) public Communication(TcpListener listener)
{ {
this.listener = listener; this.listener = listener;
@@ -33,7 +52,7 @@ namespace Server
var tcpClient = listener.EndAcceptTcpClient(ar); var tcpClient = listener.EndAcceptTcpClient(ar);
Console.WriteLine($"Client connected from {tcpClient.Client.RemoteEndPoint}"); Console.WriteLine($"Client connected from {tcpClient.Client.RemoteEndPoint}");
clients.Add(new Client(this, tcpClient)); new Client(this, tcpClient);
listener.BeginAcceptTcpClient(new AsyncCallback(OnConnect), null); listener.BeginAcceptTcpClient(new AsyncCallback(OnConnect), null);
} }
@@ -45,15 +64,20 @@ namespace Server
public void NewLogin(Client client) public void NewLogin(Client client)
{ {
if (doctor == null) this.clients.Add(client);
Debug.WriteLine("amount of clients is now " + this.clients.Count);
var dinges = DataParser.getNewConnectionJson(client.username);
Debug.WriteLine("new login" + Encoding.ASCII.GetString(dinges));
Doctor?.sendMessage(dinges);
}
public void LogOff(Client client)
{
if (this.Doctor == client)
{ {
doctor = client; this.Doctor = null;
} }
else this.clients.Remove(client);
{
doctor.sendMessage(DataParser.getNewConnectionJson(client.username));
}
} }
public void StartSessionUser(string user) public void StartSessionUser(string user)