diff --git a/Layout/NavMenu.razor b/Layout/NavMenu.razor index 8bf5a5b..2a7c52d 100644 --- a/Layout/NavMenu.razor +++ b/Layout/NavMenu.razor @@ -24,6 +24,11 @@ Saved Points + diff --git a/Pages/PointsPage.razor b/Pages/PointsPage.razor index 8ec450c..4187809 100644 --- a/Pages/PointsPage.razor +++ b/Pages/PointsPage.razor @@ -6,6 +6,7 @@ @inject PointsService _PointsService @inject RTD8TMService _RTD8TMService +@inject SyncService _SyncService @inject NavigationManager Navigator @@ -28,7 +29,7 @@ protected override void OnInitialized() { //_RTD8TMService.SetAddress("http://10.250.251.131/"); - _RTD8TMService.SetAddress(Navigator.BaseUri); + _SyncService.SetAddress(Navigator.BaseUri); } diff --git a/Pages/RTD8TMPage.razor b/Pages/RTD8TMPage.razor index 652a0a5..af19a48 100644 --- a/Pages/RTD8TMPage.razor +++ b/Pages/RTD8TMPage.razor @@ -8,6 +8,7 @@ @inject NavigationManager Navigator @inject RTD8TMService _RTD8TMService @inject PointsService _PointsService +@inject SyncService _SyncService Temperature @@ -104,7 +105,7 @@ protected override void OnInitialized() { //_RTD8TMService.SetAddress("http://10.250.251.131/"); - _RTD8TMService.SetAddress(Navigator.BaseUri); + _SyncService.SetAddress(Navigator.BaseUri); _RTD8TMService.EventUpdateValues += UpdateView; } diff --git a/Pages/Start.razor b/Pages/Start.razor index 9b60730..5e46ae6 100644 --- a/Pages/Start.razor +++ b/Pages/Start.razor @@ -2,17 +2,21 @@ @using UI_SequentMicrosystems.Services @inject NavigationManager Navigator -@inject RTD8TMService _RTD8TMService +@inject SyncService _SyncService Welcome
-
TesDevice App V1.1
+
TesDevice App V1.2
+
Added Clients Synchronization
+
+
+
TesDevice App V1.1
Added Automatic points Saving, Calibration and Calculating RTDCard values change in last 3O minutes in saved points
TesDevice App V1.O
-
Firt Fully function Build --> Reading RTD cards, RTD Graph, Points Saving and exporting as csv
+
First Fully function Build --> Reading RTD cards, RTD Graph, Points Saving and exporting as csv
@@ -24,7 +28,7 @@ protected override void OnInitialized() { //_RTD8TMService.SetAddress("http://10.250.251.131/"); - _RTD8TMService.SetAddress(Navigator.BaseUri); + _SyncService.SetAddress(Navigator.BaseUri); diff --git a/Pages/SyncPage.razor b/Pages/SyncPage.razor new file mode 100644 index 0000000..2e8ccc9 --- /dev/null +++ b/Pages/SyncPage.razor @@ -0,0 +1,97 @@ +@page "/Sync" + +@using UI_SequentMicrosystems.Services +@using UI_SequentMicrosystems.Models + +@inject SyncService _SyncService +@inject NavigationManager Navigator + + +Clients Sync + +
+ This ClientID: @_SyncService.GetClientID() +
+ +@if (SelectedSyncClientID != null) +{ +
+
+ +
+ + @if (SelectedSyncClientID != null && SelectedSyncClientID != "") + { +
+ +
+ } +
+} + + + + +@code { + private string SelectedSyncClientID { get; set; } + + protected override async void OnInitialized() + { + //_RTD8TMService.SetAddress("http://10.250.251.131/"); + _SyncService.SetAddress(Navigator.BaseUri); + SelectedSyncClientID = await _SyncService.GetSelectedSyncClient(); + _SyncService.EventUpdateClientsIDs += UpdateView; + } + + + private async void ClientSelected(string select) + { + Console.WriteLine($"SyncPage:ClientSelected Selected ID: {select}"); + if (select == "-1") + { + SelectedSyncClientID = ""; + await _SyncService.SendClientSyncRequest(""); + } + else + { + SelectedSyncClientID = select; + await _SyncService.SendClientSyncRequest(select); + } + } + + + + + + + + + + + + public async Task UpdateView(object? o, bool b) + { + await InvokeAsync(() => + { + StateHasChanged(); + }); + } +} diff --git a/Program.cs b/Program.cs index 0b4a6c2..4dac534 100644 --- a/Program.cs +++ b/Program.cs @@ -18,6 +18,7 @@ namespace UI_SequentMicrosystems builder.Services.AddSingleton(); builder.Services.AddSingleton(); + builder.Services.AddSingleton(); await builder.Build().RunAsync(); } diff --git a/Services/PointsService.cs b/Services/PointsService.cs index 5629664..bba366f 100644 --- a/Services/PointsService.cs +++ b/Services/PointsService.cs @@ -72,6 +72,22 @@ namespace UI_SequentMicrosystems.Services return _PointsModel; } + /// + /// Set Points (used by sync) + /// + /// + public async void SetPoints(PointsModel points) + { + _PointsModel = points; + + PointsCount = _PointsModel.RTD8TM.Keys.Count; + + if (EventUpdateTopBar != null) + { + await EventUpdateTopBar.Invoke(this, true); + } + } + /// /// Get points Count /// diff --git a/Services/RTD8TMService.cs b/Services/RTD8TMService.cs index d95575a..0a131a6 100644 --- a/Services/RTD8TMService.cs +++ b/Services/RTD8TMService.cs @@ -46,6 +46,9 @@ namespace UI_SequentMicrosystems.Services GetChanelsNames(); GetValueTypes(); GetCalibrations(); + + + } /// @@ -435,6 +438,24 @@ namespace UI_SequentMicrosystems.Services return 0; } + /// + /// Get all Chart Data + /// + /// + public SortedList>> GetAllChartData() + { + return GraphData; + } + + /// + /// Set Chart Data (used by syncservice) + /// + /// + public void SetChartData(SortedList>> data) + { + GraphData = data; + } + //Values Types /// diff --git a/Services/SyncService.cs b/Services/SyncService.cs new file mode 100644 index 0000000..228affa --- /dev/null +++ b/Services/SyncService.cs @@ -0,0 +1,201 @@ +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 _SyncClients = new(); + + public delegate Task AsyncEventHandler(object? sender, TEventArgs? e); + public event AsyncEventHandler? EventUpdateClientsIDs; + + public SyncService(PointsService pointsService, RTD8TMService rtD8TMService) + { + _pointsService = pointsService; + _RTD8TMService = rtD8TMService; + ClientID = CreateClientID(); + Console.WriteLine($"SyncService:* Local Client ID: {ClientID}"); + } + + /// + /// Set api address + /// + /// + public void SetAddress(string address) + { + if (Address == address) { return; } + Address = address; + _RTD8TMService.SetAddress(Address); + + + + StartConnection(); + } + + /// + /// Create Random ClientID + /// + private string CreateClientID() + { + string id = ""; + List chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".ToCharArray().ToList(); + for (int i = 0; i < 4; i++) + { + id += chars[Random.Shared.Next(0, chars.Count - 1)]; + } + return id; + } + + /// + /// Get this ClientID + /// + /// + public string GetClientID() + { + return ClientID; + } + + /// + /// Start connection to Server + /// + 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>>>(data)); + } + + private async Task ClientPointsyncData(object? o, string? id, string data) + { + if (data == null || id == null || id != SyncRequestedClientID) { return; } + + _pointsService.SetPoints(JsonConvert.DeserializeObject(data)); + } + + /// + /// Request Other Clients IDs + /// + public async void RequestClientsIds() + { + await SyncClient.SendClientsRequest(); + } + + /// + /// Get Clients IDs + /// + /// + public List GetClientsIDs() + { + Console.WriteLine($"SyncService:GetClientsIDs {JsonConvert.SerializeObject(_SyncClients.ToList())}"); + return _SyncClients.ToList(); + } + + /// + /// Request Clients Sync + /// + /// ID of client from sync requested + /// + 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); + } + } + + /// + /// Get selected Sync Client + /// + /// + public async Task GetSelectedSyncClient() + { + if (SyncRequestedClientID == null || SyncRequestedClientID == "") + { + return ""; + } + else + { + return SyncRequestedClientID; + } + } + + + + + + + + + + + + + + + + + } +}