Added Client Synchronization function
parent
95749a5f00
commit
cdc288a8d9
|
@ -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<TEventArgs>(object? sender, TEventArgs? e);
|
||||||
|
public delegate Task AsyncEventHandler<TEventArgs1, TEventArgs>(object? sender, TEventArgs1 clientID, TEventArgs? e);
|
||||||
|
public event AsyncEventHandler<bool>? EventClientIDRequest;
|
||||||
|
public event AsyncEventHandler<string>? EventClientsIDResponse;
|
||||||
|
public event AsyncEventHandler<string>? EventClientSyncRequest;
|
||||||
|
public event AsyncEventHandler<string, string>? EventClientPointsSync;
|
||||||
|
public event AsyncEventHandler<string, string>? EventClientRTD8TMChartSync;
|
||||||
|
|
||||||
|
|
||||||
|
public Sync(string BaseAddress)
|
||||||
|
{
|
||||||
|
this.BaseAddress = BaseAddress;
|
||||||
|
|
||||||
|
List<TimeSpan> recconcents = new List<TimeSpan>();
|
||||||
|
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
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Request for clients IDs
|
||||||
|
/// </summary>
|
||||||
|
private async void ClientRequest()
|
||||||
|
{
|
||||||
|
if (EventClientIDRequest != null)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Sync:ClientRequest Raised");
|
||||||
|
await EventClientIDRequest.Invoke(this, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Responses on ClientsRequest
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="clientID"></param>
|
||||||
|
private async void ClientIDResponce(string clientID)
|
||||||
|
{
|
||||||
|
if (EventClientsIDResponse != null)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Sync:ClientIDResponse Raised ID:{clientID}");
|
||||||
|
await EventClientsIDResponse.Invoke(this, clientID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Request for client Sync
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="clientID"></param>
|
||||||
|
private async void ClientSyncRequest(string clientID)
|
||||||
|
{
|
||||||
|
if (EventClientSyncRequest != null)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Sync:ClientSyncRequest Raised ClientID: {clientID}");
|
||||||
|
await EventClientSyncRequest.Invoke(this, clientID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sync point Data
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="clientID"></param>
|
||||||
|
/// <param name="data"></param>
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sync RTD8TM chart data
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="clientID"></param>
|
||||||
|
/// <param name="data"></param>
|
||||||
|
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
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Send request for clients IDs
|
||||||
|
/// </summary>
|
||||||
|
public async Task SendClientsRequest()
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Sync:SendClientsRequest");
|
||||||
|
await connection.SendAsync("SyncClients");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Send Response for Clients ID requests
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="clientid">client ID of this instance</param>
|
||||||
|
public async Task SendClientsResponse(string clientid)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Sync:SendClientsResponse Sending ClientID: {clientid}");
|
||||||
|
await connection.SendAsync("SyncClientResponse", clientid);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Send Request for sync
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="clientid">Client ID of remote client</param>
|
||||||
|
public async Task SendClientSyncRequest(string clientid)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Sync:SendClientSyncRequest Sending to ClientID: {clientid}");
|
||||||
|
await connection.SendAsync("SyncRequest", clientid);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Send sync Points Data by ClientSync request
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="clientid">client id of this instance</param>
|
||||||
|
/// <param name="data">Points Data</param>
|
||||||
|
public async Task SendClientsPoints(string clientid, string data)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Sync:SendClientsPoints Sending to ClientID: {clientid} - {data}");
|
||||||
|
await connection.SendAsync("SyncPoints", clientid, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Send sync RTD8TMChart data by clientSync Request
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="clientid">client id of this instance</param>
|
||||||
|
/// <param name="data">RTD8TM data for sync</param>
|
||||||
|
public async Task SendClientsRTD8TMChart(string clientid, string data)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Sync:SendClientsRTD8TMChart Sending to ClientID: {clientid} - {data}");
|
||||||
|
await connection.SendAsync("SyncRTD8TMChart", clientid, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,6 +7,7 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.1" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue