Connections shit

This commit is contained in:
fabjuuuh
2020-10-14 13:05:56 +02:00
parent 301f6b447f
commit 25176dbaeb
13 changed files with 186 additions and 34 deletions

View File

@@ -20,6 +20,8 @@ namespace DoctorApp.Utils
private bool sessionRunning = false;
private IHandler handler = null;
private LoginViewModel LoginViewModel;
private MainViewModel MainViewModel;
private ClientInfoViewModel ClientInfoViewModel;
public Client() : this("localhost", 5555)
@@ -119,6 +121,12 @@ namespace DoctorApp.Utils
sendMessage(DataParser.getSetResistanceResponseJson(true));
}
break;
case DataParser.NEW_CONNECTION:
this.MainViewModel.NewConnectedUser(DataParser.getUsernameFromResponseJson(payloadbytes));
break;
case DataParser.DISCONNECT:
this.MainViewModel.NewConnectedUser(DataParser.getUsernameFromResponseJson(payloadbytes));
break;
default:
Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}");
break;
@@ -247,5 +255,15 @@ namespace DoctorApp.Utils
{
this.LoginViewModel = loginViewModel;
}
internal void SetMainViewModel(MainViewModel mainViewModel)
{
this.MainViewModel = mainViewModel;
}
internal void SetClientInfoViewModel(ClientInfoViewModel clientInfoViewModel)
{
this.ClientInfoViewModel = clientInfoViewModel;
}
}
}

View File

@@ -15,6 +15,8 @@ namespace DoctorApp.Utils
public const string START_SESSION = "START SESSION";
public const string STOP_SESSION = "STOP SESSION";
public const string SET_RESISTANCE = "SET RESISTANCE";
public const string NEW_CONNECTION = "NEW CONNECTION";
public const string DISCONNECT = "DISCONNECT";
/// <summary>
/// makes the json object with LOGIN identifier and username and password
/// </summary>
@@ -191,6 +193,24 @@ namespace DoctorApp.Utils
return getJsonMessage(SET_RESISTANCE, data);
}
public static byte[] getNewConnectionJson(string user)
{
dynamic data = new
{
username = user
};
return getJsonMessage(NEW_CONNECTION, data);
}
public static byte[] getDisconnectJson(string user)
{
dynamic data = new
{
username = user
};
return getJsonMessage(DISCONNECT, data);
}
public static float getResistanceFromJson(byte[] json)
{
return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.resistance;
@@ -201,6 +221,11 @@ namespace DoctorApp.Utils
return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.worked;
}
public static string getUsernameFromResponseJson(byte[] json)
{
return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.username;
}
}
}