From 41311af5c2af24f34fa06ce4147334aa8b2d8b07 Mon Sep 17 00:00:00 2001 From: thom776g Date: Mon, 27 Nov 2023 15:45:59 +0100 Subject: [PATCH 01/21] Implement UI --- .../Services/SampleRoomRepository.cs | 29 +- .../Services/SampleTrackerRepository.cs | 92 +- CentralHub.WebUI/Data/RoomService.cs | 51 +- CentralHub.WebUI/Data/TrackerService.cs | 78 + CentralHub.WebUI/Pages/Rooms.razor | 571 +- CentralHub.WebUI/Pages/_Host.cshtml | 20 + .../wwwroot/js/bootstrap.bundle.js | 6780 +++++++++++++++++ .../wwwroot/js/bootstrap.bundle.js.map | 1 + .../wwwroot/js/bootstrap.bundle.min.js | 7 + .../wwwroot/js/bootstrap.bundle.min.js.map | 1 + CentralHub.WebUI/wwwroot/js/bootstrap.esm.js | 4977 ++++++++++++ .../wwwroot/js/bootstrap.esm.js.map | 1 + .../wwwroot/js/bootstrap.esm.min.js | 7 + .../wwwroot/js/bootstrap.esm.min.js.map | 1 + CentralHub.WebUI/wwwroot/js/bootstrap.js | 5026 ++++++++++++ CentralHub.WebUI/wwwroot/js/bootstrap.js.map | 1 + CentralHub.WebUI/wwwroot/js/bootstrap.min.js | 7 + .../wwwroot/js/bootstrap.min.js.map | 1 + 18 files changed, 17562 insertions(+), 89 deletions(-) create mode 100644 CentralHub.WebUI/wwwroot/js/bootstrap.bundle.js create mode 100644 CentralHub.WebUI/wwwroot/js/bootstrap.bundle.js.map create mode 100644 CentralHub.WebUI/wwwroot/js/bootstrap.bundle.min.js create mode 100644 CentralHub.WebUI/wwwroot/js/bootstrap.bundle.min.js.map create mode 100644 CentralHub.WebUI/wwwroot/js/bootstrap.esm.js create mode 100644 CentralHub.WebUI/wwwroot/js/bootstrap.esm.js.map create mode 100644 CentralHub.WebUI/wwwroot/js/bootstrap.esm.min.js create mode 100644 CentralHub.WebUI/wwwroot/js/bootstrap.esm.min.js.map create mode 100644 CentralHub.WebUI/wwwroot/js/bootstrap.js create mode 100644 CentralHub.WebUI/wwwroot/js/bootstrap.js.map create mode 100644 CentralHub.WebUI/wwwroot/js/bootstrap.min.js create mode 100644 CentralHub.WebUI/wwwroot/js/bootstrap.min.js.map diff --git a/CentralHub.Api/Services/SampleRoomRepository.cs b/CentralHub.Api/Services/SampleRoomRepository.cs index f00a7e1..3d94d10 100644 --- a/CentralHub.Api/Services/SampleRoomRepository.cs +++ b/CentralHub.Api/Services/SampleRoomRepository.cs @@ -17,24 +17,23 @@ private sealed class LockedStuff static SampleRoomRepository() { var sampleRoomRepository = new SampleRoomRepository(); - var room1 = new RoomDto() - { - Name = "Sample Room 1", - Description = "Sample Room", - RoomDtoId = -1, - Trackers = new List() - }; + List rooms = new List(); - var room2 = new RoomDto() + for (int i = 0; i < 5; i++) { - Name = "Sample Room 2", - Description = "Sample Room", - RoomDtoId = -1, - Trackers = new List() - }; + rooms.Add(new RoomDto() + { + Name = $"Sample Room {i}", + Description = "Sample Room", + RoomDtoId = -1, + Trackers = new List() + }); + } - sampleRoomRepository.AddRoomAsync(room1, default).GetAwaiter().GetResult(); - sampleRoomRepository.AddRoomAsync(room2, default).GetAwaiter().GetResult(); + foreach (var room in rooms) + { + sampleRoomRepository.AddRoomAsync(room, default).GetAwaiter().GetResult(); + } } public async Task AddRoomAsync(RoomDto roomDto, CancellationToken cancellationToken) diff --git a/CentralHub.Api/Services/SampleTrackerRepository.cs b/CentralHub.Api/Services/SampleTrackerRepository.cs index 2c776e5..70dafb5 100644 --- a/CentralHub.Api/Services/SampleTrackerRepository.cs +++ b/CentralHub.Api/Services/SampleTrackerRepository.cs @@ -23,47 +23,65 @@ static SampleTrackerRepository() var room1 = sampleRoomRepository.GetRoomByIdAsync(0, default).GetAwaiter().GetResult()!; var room2 = sampleRoomRepository.GetRoomByIdAsync(1, default).GetAwaiter().GetResult()!; - var tracker1 = new TrackerDto() - { - WifiMacAddress = "AA:BB:CC:DD:EE:FF", - BluetoothMacAddress = "FF:EE:DD:CC:BB:AA", - Name = "Sample Tracker 1", - Description = "Belongs to Sample Room 1", - RoomDtoId = room1.RoomDtoId, - RoomDto = room1, - TrackerDtoId = -1, - }; - - var tracker2 = new TrackerDto() + List rooms = new List(); + + for (int i = 0; i < 5; i++) { - WifiMacAddress = "00:11:22:33:44:55", - BluetoothMacAddress = "55:44:33:22:11:00", - Name = "Sample Tracker 2", - Description = "Belongs to Sample Room 1", - RoomDtoId = room1.RoomDtoId, - RoomDto = room1, - TrackerDtoId = -1, - }; + rooms.Add(sampleRoomRepository.GetRoomByIdAsync(i, default).GetAwaiter().GetResult()!); + } + List trackers = new List(); - var tracker3 = new TrackerDto() + for (int i = 0; i < 5; i++) { - WifiMacAddress = "00:22:44:66:88:00", - BluetoothMacAddress = "00:88:66:44:22:00", - Name = "Sample Tracker 3", - Description = "Belongs to Sample Room 2", - RoomDtoId = room2.RoomDtoId, - RoomDto = room2, - TrackerDtoId = -1, - }; - + String WifiName; + String BluetoothName; + if (i < 10) + { + WifiName = $"00:00:00:00:FE:0{i}"; + BluetoothName = $"00:00:00:00:0{i}:BL"; + } + else + { + WifiName = $"00:00:00:00:FE:{i}"; + BluetoothName = $"00:00:00:00:{i}:BL"; + } - sampleTrackerRepository.AddTrackerAsync(tracker1, default).GetAwaiter().GetResult(); - sampleTrackerRepository.AddTrackerAsync(tracker2, default).GetAwaiter().GetResult(); - sampleTrackerRepository.AddTrackerAsync(tracker3, default).GetAwaiter().GetResult(); + trackers.Add(new TrackerDto() + { + WifiMacAddress = WifiName, + BluetoothMacAddress = BluetoothName, + Name = $"Sample Tracker {i}", + Description = "Sample Tracker", + RoomDtoId = rooms[i % 1].RoomDtoId, + RoomDto = rooms[i % 1], + TrackerDtoId = -1, + }); + } + + + foreach (var tracker in trackers) + { + sampleTrackerRepository.AddTrackerAsync(tracker, default).GetAwaiter().GetResult(); + } + for (int i = 0; i < 5; i++) + { + String WifiName; + String BluetoothName; + if (i < 10) + { + WifiName = $"0{i}:FE:00:00:00:00"; + BluetoothName = $"BL:0{i}:00:00:00:00"; + } + else + { + WifiName = $"{i}:FE:00:00:00:00"; + BluetoothName = $"BL:{i}:00:00:00:00"; + } - sampleTrackerRepository.AddUnregisteredTracker("BB:AA:DD:CC:FF:EE", "EE:FF:CC:DD:AA:BB", default).GetAwaiter().GetResult(); + sampleTrackerRepository.AddUnregisteredTracker(WifiName, BluetoothName, default).GetAwaiter().GetResult(); + } } public async Task AddTrackerAsync(TrackerDto trackerDto, CancellationToken cancellationToken) @@ -98,7 +116,13 @@ public async Task RemoveTrackerAsync(TrackerDto trackerDto, CancellationToken ca await LockedStuffMutex.Lock(stuff => { stuff.Trackers.Remove(trackerDto.TrackerDtoId); + stuff.UnregisteredTrackers.Add(new UnregisteredTrackerDto() + { + WifiMacAddress = trackerDto.WifiMacAddress, + BluetoothMacAddress = trackerDto.BluetoothMacAddress + }); }, cancellationToken); + } public async Task?> GetTrackersInRoomAsync(int roomId, CancellationToken cancellationToken) diff --git a/CentralHub.WebUI/Data/RoomService.cs b/CentralHub.WebUI/Data/RoomService.cs index f11a39b..f764971 100644 --- a/CentralHub.WebUI/Data/RoomService.cs +++ b/CentralHub.WebUI/Data/RoomService.cs @@ -1,7 +1,12 @@ using System.Net; +using System.Net.Http.Headers; +using System.Text; +using System.Text.Json; using CentralHub.Api.Model; +using CentralHub.Api.Model.Requests.Room; using CentralHub.Api.Model.Responses.Room; using CentralHub.Api.Model.Responses.Tracker; +using Microsoft.AspNetCore.Mvc.Formatters; namespace CentralHub.WebUI.Data; @@ -18,7 +23,7 @@ public async Task RemoveRoomAsync(Room room, CancellationToken cancellationToken { var request = new HttpRequestMessage( HttpMethod.Delete, - $"http://localhost:8081/room/remove?id={room.RoomId}"); + $"http://localhost:8081/room/remove?roomId={room.RoomId}"); request.Headers.Add("Accept", "application/json"); request.Headers.Add("User-Agent", "CentralHub.WebUI"); @@ -32,6 +37,50 @@ public async Task RemoveRoomAsync(Room room, CancellationToken cancellationToken } } + public async Task UpdateRoomAsync(Room room, String name, String description, CancellationToken cancellationToken) + { + var updateRoomRequest = new UpdateRoomRequest(room.RoomId, name, description); + var request = new HttpRequestMessage( + HttpMethod.Put, + $"http://localhost:8081/room/update" + ); + request.Headers.Add("Accept", "application/json"); + request.Headers.Add("User-Agent", "CentralHub.WebUI"); + request.Content = new StringContent(JsonSerializer.Serialize(updateRoomRequest), Encoding.UTF8, "application/json"); + + var client = _clientFactory.CreateClient(); + + var response = await client.SendAsync(request, cancellationToken); + + if (response.StatusCode != HttpStatusCode.OK) + { + throw new InvalidOperationException("Somethings fucky"); + } + } + + public async Task AddRoomAsync(String name, String description, CancellationToken cancellationToken) + { + var addRoomRequest = new AddRoomRequest(name, description); + var request = new HttpRequestMessage( + HttpMethod.Post, + $"http://localhost:8081/room/add" + ); + request.Headers.Add("Accept", "application/json"); + request.Headers.Add("User-Agent", "CentralHub.WebUI"); + request.Content = new StringContent(JsonSerializer.Serialize(addRoomRequest), Encoding.UTF8, "application/json"); + + var client = _clientFactory.CreateClient(); + + var response = await client.SendAsync(request, cancellationToken); + + if (response.StatusCode != HttpStatusCode.OK) + { + throw new InvalidOperationException("Somethings fucky"); + } + } + + + public async Task GetRoomsAsync(CancellationToken cancellationToken) { var request = new HttpRequestMessage( diff --git a/CentralHub.WebUI/Data/TrackerService.cs b/CentralHub.WebUI/Data/TrackerService.cs index 07aebe0..f876fa0 100644 --- a/CentralHub.WebUI/Data/TrackerService.cs +++ b/CentralHub.WebUI/Data/TrackerService.cs @@ -1,6 +1,14 @@ +using System.Net; +using System.Text.Json; using System.Collections.Immutable; using CentralHub.Api.Model.Responses.Room; using CentralHub.Api.Model.Responses.Tracker; +using CentralHub.Api.Model.Requests.Tracker; +using System.Net.Http.Headers; +using System.Text; +using CentralHub.Api.Model; +using Microsoft.AspNetCore.Mvc.Formatters; +using CentralHub.Api.Model.Requests; namespace CentralHub.WebUI.Data; @@ -13,6 +21,76 @@ public TrackerService(IHttpClientFactory clientFactory) _clientFactory = clientFactory; } + public async Task RemoveTrackerAsync(Tracker tracker, CancellationToken cancellationToken) + { + var request = new HttpRequestMessage( + HttpMethod.Delete, + $"http://localhost:8081/tracker/remove?trackerId={tracker.TrackerId}"); + request.Headers.Add("Accept", "application/json"); + request.Headers.Add("User-Agent", "CentralHub.WebUI"); + + var client = _clientFactory.CreateClient(); + + var response = await client.SendAsync(request, cancellationToken); + + if (response.StatusCode != HttpStatusCode.OK) + { + throw new InvalidOperationException("Somethings fucky"); + } + } + + public async Task AddTrackerAsync(Room room, UnregisteredTracker tracker, String name, String description, CancellationToken cancellationToken) + { + var addTrackerRequest = new AddTrackerRequest(room.RoomId, name, description, tracker.WifiMacAddress, tracker.BluetoothMacAddress); + var request = new HttpRequestMessage( + HttpMethod.Post, + $"http://localhost:8081/tracker/add" + ); + request.Headers.Add("Accept", "application/json"); + request.Headers.Add("User-Agent", "CentralHub.WebUI"); + request.Content = new StringContent(JsonSerializer.Serialize(addTrackerRequest), Encoding.UTF8, "application/json"); + + var client = _clientFactory.CreateClient(); + + var response = await client.SendAsync(request, cancellationToken); + + if (response.StatusCode != HttpStatusCode.OK) + { + throw new InvalidOperationException("Somethings fucky"); + } + } + + public async Task UpdateTrackerAsync(Room room, Tracker tracker, String name, String description, CancellationToken cancellationToken) + { + if (room.RoomId == tracker.RoomId) + { + var addTrackerRequest = new UpdateTrackerRequest(tracker.TrackerId, name, description); + var request = new HttpRequestMessage( + HttpMethod.Put, + $"http://localhost:8081/tracker/update" + ); + request.Headers.Add("Accept", "application/json"); + request.Headers.Add("User-Agent", "CentralHub.WebUI"); + request.Content = new StringContent(JsonSerializer.Serialize(addTrackerRequest), Encoding.UTF8, "application/json"); + + var client = _clientFactory.CreateClient(); + + var response = await client.SendAsync(request, cancellationToken); + + if (response.StatusCode != HttpStatusCode.OK) + { + throw new InvalidOperationException("Somethings fucky"); + } + } + else + { + await RemoveTrackerAsync(tracker, cancellationToken); + UnregisteredTracker[] uTrackers = await GetUnregisteredTrackersAsync(cancellationToken); + UnregisteredTracker uTracker = uTrackers.Single(t => t.WifiMacAddress == tracker.WifiMacAddress); + await AddTrackerAsync(room, uTracker, name, description, cancellationToken); + } + } + public async Task GetTrackersAsync(Room room, CancellationToken cancellationToken) { var request = new HttpRequestMessage( diff --git a/CentralHub.WebUI/Pages/Rooms.razor b/CentralHub.WebUI/Pages/Rooms.razor index 3b064ae..d6bf5a0 100644 --- a/CentralHub.WebUI/Pages/Rooms.razor +++ b/CentralHub.WebUI/Pages/Rooms.razor @@ -1,10 +1,13 @@ @page "/rooms" +@using CentralHub.Api.Model.Responses.Tracker @using CentralHub.WebUI.Data @using CentralHub.Api.Model @using CentralHub.Api.Model.Responses.Room @implements IDisposable @inject RoomService RoomService @inject TrackerService TrackerService +@inject IJSRuntime JSRuntime +@inject NavigationManager NavigationManager; Rooms @@ -12,7 +15,7 @@

This component demonstrates fetching data from a service.

-@if (_rooms == null) +@if (_wRooms == null) {

Loading... @@ -20,55 +23,504 @@ } else { - - - - - - - - - +
+ +
+
NameLocation# TrackersActions
- @foreach (var room in _rooms) - { - - - - - - - } + @foreach (var wRoom in _wRooms) + { + + + + + @if (wRoom.IsExpanded) + { + + + + } + }
@room.Name@room.Description@_trackerCountDictionary![room] - - - -
+ + @wRoom.Room.Name + +
+ + + + +
+
+ + + @foreach (var tracker in wRoom.Children) + { + + + + + } + +
+ + @tracker.Name + +
+ +
+
+
} +

+ + -@if(_unregisteredTrackers.Length == 0) -{ - -{ - -} -else -} -