202 lines
5.9 KiB
C#
202 lines
5.9 KiB
C#
|
using Newtonsoft.Json;
|
|||
|
using UI_SequentMicrosystems.Models;
|
|||
|
using Wrapper_Api_SequentMicrosystems.SignalR;
|
|||
|
|
|||
|
namespace UI_SequentMicrosystems.Services
|
|||
|
{
|
|||
|
public class SyncService
|
|||
|
{
|
|||
|
private PointsService _pointsService { get; set; }
|
|||
|
private RTD8TMService _RTD8TMService { get; set; }
|
|||
|
private string? Address { get; set; }
|
|||
|
private string ClientID { get; set; }
|
|||
|
private string? SyncRequestedClientID { get; set; }
|
|||
|
private Sync? SyncClient { get; set; }
|
|||
|
private List<string> _SyncClients = new();
|
|||
|
|
|||
|
public delegate Task AsyncEventHandler<TEventArgs>(object? sender, TEventArgs? e);
|
|||
|
public event AsyncEventHandler<bool>? EventUpdateClientsIDs;
|
|||
|
|
|||
|
public SyncService(PointsService pointsService, RTD8TMService rtD8TMService)
|
|||
|
{
|
|||
|
_pointsService = pointsService;
|
|||
|
_RTD8TMService = rtD8TMService;
|
|||
|
ClientID = CreateClientID();
|
|||
|
Console.WriteLine($"SyncService:* Local Client ID: {ClientID}");
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Set api address
|
|||
|
/// </summary>
|
|||
|
/// <param name="address"></param>
|
|||
|
public void SetAddress(string address)
|
|||
|
{
|
|||
|
if (Address == address) { return; }
|
|||
|
Address = address;
|
|||
|
_RTD8TMService.SetAddress(Address);
|
|||
|
|
|||
|
|
|||
|
|
|||
|
StartConnection();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Create Random ClientID
|
|||
|
/// </summary>
|
|||
|
private string CreateClientID()
|
|||
|
{
|
|||
|
string id = "";
|
|||
|
List<char> chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".ToCharArray().ToList();
|
|||
|
for (int i = 0; i < 4; i++)
|
|||
|
{
|
|||
|
id += chars[Random.Shared.Next(0, chars.Count - 1)];
|
|||
|
}
|
|||
|
return id;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Get this ClientID
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
public string GetClientID()
|
|||
|
{
|
|||
|
return ClientID;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Start connection to Server
|
|||
|
/// </summary>
|
|||
|
private async void StartConnection()
|
|||
|
{
|
|||
|
if (Address == null) { return; }
|
|||
|
|
|||
|
SyncClient = new(Address);
|
|||
|
SyncClient.EventClientIDRequest += ClientIDRequestRequested;
|
|||
|
SyncClient.EventClientsIDResponse += ClientIDRequestResponse;
|
|||
|
SyncClient.EventClientSyncRequest += ClientSyncRequested;
|
|||
|
SyncClient.EventClientRTD8TMChartSync += ClientRTD8TMSyncData;
|
|||
|
SyncClient.EventClientPointsSync += ClientPointsyncData;
|
|||
|
|
|||
|
RequestClientsIds();
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
private async Task ClientIDRequestRequested(object? o, bool b)
|
|||
|
{
|
|||
|
if (SyncClient == null) { return; }
|
|||
|
|
|||
|
_SyncClients.Clear();
|
|||
|
|
|||
|
await SyncClient.SendClientsResponse(ClientID);
|
|||
|
}
|
|||
|
|
|||
|
#pragma warning disable CS1998 // V této asynchronní metodě chybí operátory await a spustí se synchronně.
|
|||
|
private async Task ClientIDRequestResponse(object? o, string? id)
|
|||
|
{
|
|||
|
if (id == null || id == ClientID) { return; }
|
|||
|
Console.WriteLine($"SyncService:ClientIDRequestResponse clientID: {id}");
|
|||
|
_SyncClients.Add(id);
|
|||
|
|
|||
|
if (EventUpdateClientsIDs != null)
|
|||
|
{
|
|||
|
await EventUpdateClientsIDs.Invoke(this, true);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private async Task ClientSyncRequested(object? o, string? id)
|
|||
|
{
|
|||
|
if (SyncClient == null || id == null || id != ClientID) { return; }
|
|||
|
|
|||
|
await SyncClient.SendClientsPoints(ClientID, JsonConvert.SerializeObject(_pointsService.GetPoints()));
|
|||
|
await SyncClient.SendClientsRTD8TMChart(ClientID, JsonConvert.SerializeObject(_RTD8TMService.GetAllChartData()));
|
|||
|
}
|
|||
|
|
|||
|
private async Task ClientRTD8TMSyncData(object? o, string? id, string data)
|
|||
|
{
|
|||
|
if (data == null || id == null || id != SyncRequestedClientID) { return; }
|
|||
|
|
|||
|
_RTD8TMService.SetChartData(JsonConvert.DeserializeObject<SortedList<byte, List<List<RTD8TMPointModel>>>>(data));
|
|||
|
}
|
|||
|
|
|||
|
private async Task ClientPointsyncData(object? o, string? id, string data)
|
|||
|
{
|
|||
|
if (data == null || id == null || id != SyncRequestedClientID) { return; }
|
|||
|
|
|||
|
_pointsService.SetPoints(JsonConvert.DeserializeObject<PointsModel>(data));
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Request Other Clients IDs
|
|||
|
/// </summary>
|
|||
|
public async void RequestClientsIds()
|
|||
|
{
|
|||
|
await SyncClient.SendClientsRequest();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Get Clients IDs
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
public List<string> GetClientsIDs()
|
|||
|
{
|
|||
|
Console.WriteLine($"SyncService:GetClientsIDs {JsonConvert.SerializeObject(_SyncClients.ToList())}");
|
|||
|
return _SyncClients.ToList();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Request Clients Sync
|
|||
|
/// </summary>
|
|||
|
/// <param name="ClientID">ID of client from sync requested</param>
|
|||
|
/// <returns></returns>
|
|||
|
public async Task SendClientSyncRequest(string ClientID)
|
|||
|
{
|
|||
|
if (SyncClient == null) { return; }
|
|||
|
SyncRequestedClientID = ClientID;
|
|||
|
|
|||
|
if (SyncRequestedClientID == "")
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
await SyncClient.SendClientSyncRequest(ClientID);
|
|||
|
|
|||
|
if (EventUpdateClientsIDs != null)
|
|||
|
{
|
|||
|
await EventUpdateClientsIDs.Invoke(this, true);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Get selected Sync Client
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
public async Task<string> GetSelectedSyncClient()
|
|||
|
{
|
|||
|
if (SyncRequestedClientID == null || SyncRequestedClientID == "")
|
|||
|
{
|
|||
|
return "";
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
return SyncRequestedClientID;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
}
|