Commands + Server first development release ready (USER: TEST, PASS: 1234) (Serveradress: 185.113.120.99, PORT: 3767)

This commit is contained in:
Tim G. | SnapixLP
2025-06-05 22:58:56 +02:00
parent 838b31cb1f
commit 38a9a72740
15 changed files with 148 additions and 138 deletions

View File

@@ -2,5 +2,44 @@
public class Employee
{
private string _id;
private string _code;
private string _surname;
private string _forename;
private string _email;
private string _phone;
private string _street;
private string _city;
private string _postcode;
private string _country;
private string _department;
private string _position;
private EmployeeState _employeeState;
public Employee(string code)
{
_code = code;
}
public string Id
{
get => _id;
}
public string Code
{
get => _code;
}
public string Surname
{
get => _surname;
set => _surname = value;
}
public string Forename
{
get => _forename;
set => _forename = value;
}
}

View File

@@ -12,6 +12,12 @@ public class Server
private StreamReader reader;
private StreamWriter writer;
private Action<string> onMessageReceived;
public Action<string> OnMessageReceived
{
get => onMessageReceived;
set => onMessageReceived = value ?? throw new ArgumentNullException(nameof(value), "onMessageReceived cannot be null.");
}
private ConcurrentDictionary<string, TaskCompletionSource<string>> pendingRequests = new();
@@ -41,11 +47,20 @@ public class Server
string message = await reader.ReadLineAsync();
if (message != null)
{
// Parse the JSON response
var response = JsonSerializer.Deserialize<JsonResponse>(message);
if (response != null && pendingRequests.TryRemove(response.Id, out var tcs))
if (message.StartsWith("{") && message.EndsWith("}"))
{
tcs.SetResult(response.Response); // Complete the task with the response
Console.WriteLine("Received: " + message);
// Parse the JSON response
var response = JsonSerializer.Deserialize<JsonResponse>(message);
Console.WriteLine("Parsed Response: " + (response != null ? $"Id={response.Id}, Response={response.Response}" : "null"));
if (response != null && pendingRequests.TryRemove(response.Id, out var tcs))
{
tcs.SetResult(response.Response); // Complete the task with the response
}
else
{
onMessageReceived?.Invoke(message); // Handle other messages
}
}
else
{
@@ -90,13 +105,13 @@ public class Server
}
}
private class JsonRequest
public class JsonRequest
{
public string cmd { get; set; }
public string cid { get; set; }
}
private class JsonResponse
public class JsonResponse
{
public string Id { get; set; }
public string Response { get; set; }
@@ -147,6 +162,9 @@ public class Server
}
return Task.FromException<string>(new Exception("Failed to login."));
}
public Task<string> HelpCommand() => ExecuteCommandAsync("help");
public Task<string> Logout()
{
@@ -159,6 +177,25 @@ public class Server
return Task.FromException<string>(new Exception("Failed to logout."));
}
public Task<Employee> GetLoggedInEmployee()
{
var commandResult = ExecuteCommandAsync("getSelfUser").Result;
try
{
var employee = JsonSerializer.Deserialize<Employee>(commandResult);
if (employee != null)
{
return Task.FromResult(employee);
}
return Task.FromException<Employee>(new Exception("Failed to deserialize the employee data."));
}
catch (JsonException)
{
Console.WriteLine("Failed to deserialize the command result.");
}
return Task.FromException<Employee>(new Exception("Failed to get logged in employee."));
}
public Task<string> clockIn(Employee employee)
{
var commandResult = ExecuteCommandAsync("clock in").Result;