diff --git a/SignalR/Sync.cs b/SignalR/Sync.cs new file mode 100644 index 0000000..f5159be --- /dev/null +++ b/SignalR/Sync.cs @@ -0,0 +1,200 @@ +using Microsoft.AspNetCore.SignalR.Client; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Wrapper_Api_SequentMicrosystems.SignalR +{ + public class Sync + { + + private string BaseAddress { get; set; } + HubConnection connection { get; set; } + + public delegate Task AsyncEventHandler(object? sender, TEventArgs? e); + public delegate Task AsyncEventHandler(object? sender, TEventArgs1 clientID, TEventArgs? e); + public event AsyncEventHandler? EventClientIDRequest; + public event AsyncEventHandler? EventClientsIDResponse; + public event AsyncEventHandler? EventClientSyncRequest; + public event AsyncEventHandler? EventClientPointsSync; + public event AsyncEventHandler? EventClientRTD8TMChartSync; + + + public Sync(string BaseAddress) + { + this.BaseAddress = BaseAddress; + + List recconcents = new List(); + for (int i = 1; i < 50; i++) + { + recconcents.Add(TimeSpan.FromSeconds(i)); + } + + Console.WriteLine($"SignalR Connection address: {this.BaseAddress}signalr"); + + connection = new HubConnectionBuilder().WithUrl($"{this.BaseAddress}signalr").WithAutomaticReconnect(recconcents.ToArray()).Build(); + + connection.On("ClientsRequest", () => ClientRequest()); + connection.On("ClientsResponse", (string x) => ClientIDResponce(x)); + connection.On("SyncRequested", (string x) => ClientSyncRequest(x)); + connection.On("SyncPointsData", (string x,string d) => ClientSyncPoints(x, d)); + connection.On("SyncRTD8TMChartData", (string x, string d) => ClientSyncRTD8TMChart(x, d)); + + Connect(); + } + + + public async void Connect() + { + await connection.StartAsync(); + } + + //Retrieve events generator + + /// + /// Request for clients IDs + /// + private async void ClientRequest() + { + if (EventClientIDRequest != null) + { + Console.WriteLine($"Sync:ClientRequest Raised"); + await EventClientIDRequest.Invoke(this, true); + } + } + + /// + /// Responses on ClientsRequest + /// + /// + private async void ClientIDResponce(string clientID) + { + if (EventClientsIDResponse != null) + { + Console.WriteLine($"Sync:ClientIDResponse Raised ID:{clientID}"); + await EventClientsIDResponse.Invoke(this, clientID); + } + } + + /// + /// Request for client Sync + /// + /// + private async void ClientSyncRequest(string clientID) + { + if (EventClientSyncRequest != null) + { + Console.WriteLine($"Sync:ClientSyncRequest Raised ClientID: {clientID}"); + await EventClientSyncRequest.Invoke(this, clientID); + } + } + + /// + /// Sync point Data + /// + /// + /// + private async void ClientSyncPoints(string clientID, string data) + { + if (EventClientPointsSync != null) + { + Console.WriteLine($"Sync:ClientSyncPoints Raised ClientID: {clientID} - {data}"); + await EventClientPointsSync.Invoke(this, clientID, data); + } + } + + /// + /// Sync RTD8TM chart data + /// + /// + /// + private async void ClientSyncRTD8TMChart(string clientID, string data) + { + if (EventClientRTD8TMChartSync != null) + { + Console.WriteLine($"Sync:ClientRTD8TMChart Raised ClientID: {clientID} - {data}"); + await EventClientRTD8TMChartSync.Invoke(this, clientID, data); + } + } + + + //Sending methods + + /// + /// Send request for clients IDs + /// + public async Task SendClientsRequest() + { + Console.WriteLine($"Sync:SendClientsRequest"); + await connection.SendAsync("SyncClients"); + } + + /// + /// Send Response for Clients ID requests + /// + /// client ID of this instance + public async Task SendClientsResponse(string clientid) + { + Console.WriteLine($"Sync:SendClientsResponse Sending ClientID: {clientid}"); + await connection.SendAsync("SyncClientResponse", clientid); + } + + /// + /// Send Request for sync + /// + /// Client ID of remote client + public async Task SendClientSyncRequest(string clientid) + { + Console.WriteLine($"Sync:SendClientSyncRequest Sending to ClientID: {clientid}"); + await connection.SendAsync("SyncRequest", clientid); + } + + /// + /// Send sync Points Data by ClientSync request + /// + /// client id of this instance + /// Points Data + public async Task SendClientsPoints(string clientid, string data) + { + Console.WriteLine($"Sync:SendClientsPoints Sending to ClientID: {clientid} - {data}"); + await connection.SendAsync("SyncPoints", clientid, data); + } + + /// + /// Send sync RTD8TMChart data by clientSync Request + /// + /// client id of this instance + /// RTD8TM data for sync + public async Task SendClientsRTD8TMChart(string clientid, string data) + { + Console.WriteLine($"Sync:SendClientsRTD8TMChart Sending to ClientID: {clientid} - {data}"); + await connection.SendAsync("SyncRTD8TMChart", clientid, data); + } + + + + + + + + + + + + + + + + + + + + + + + + } +} diff --git a/Wrapper_Api_SequentMicrosystems.csproj b/Wrapper_Api_SequentMicrosystems.csproj index 3b5b170..2f96739 100644 --- a/Wrapper_Api_SequentMicrosystems.csproj +++ b/Wrapper_Api_SequentMicrosystems.csproj @@ -7,6 +7,7 @@ +