diff --git a/CentralHub.WebUI/Pages/Index.razor b/CentralHub.WebUI/Pages/Index.razor index a31ef7b..5a2c7fd 100644 --- a/CentralHub.WebUI/Pages/Index.razor +++ b/CentralHub.WebUI/Pages/Index.razor @@ -1,9 +1,597 @@ -@page "/" +@page "/" +@using CentralHub.Api.Model.Responses.Tracker +@using CentralHub.WebUI.Data +@using CentralHub.Api.Model.Responses.Room +@implements IDisposable +@inject RoomService RoomService +@inject TrackerService TrackerService +@inject IJSRuntime JSRuntime +@inject NavigationManager NavigationManager; -Index +Rooms -

HVAC Tracking System

+

Rooms

-This is a webfront for the HVAC Tracking system.
+
+
+
+ +
+
+
+ + + @foreach (var wRoom in _wRooms) + { + + + + + + + + } + +
+ + @wRoom.Room.Name + +
+ + + + +
+
+ + + @foreach (var tracker in wRoom.Children) + { + + + + + } + +
+ + @tracker.Name + +
+ +
+
+
+
+
-On this site it is possible to see the configured trackers and the statistics of when the rooms are in use. + + + + +@if(_unregisteredTrackers.Length == 0) +{ + +} +else +{ + +} + + + + +@code { + private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource(); + private List _rooms = new List(); + private List _wRooms = new List(); + private UnregisteredTracker[] _unregisteredTrackers = Array.Empty(); + private RoomWrapper? _selectedWRoom; + private Tracker? _selectedTracker; + private int _selectedRoomId = -1; + private string _roomName = "Your room name"; + private string _roomDescription = "Your room description"; + private string _trackerName = "Your tracker name"; + private string _trackerDescription = "Your tracker description"; + private string? _selectedTrackerMacAddress = null; + + private async Task AddRoomModal() + { + await ShowModal("addRoomModal"); + StateHasChanged(); + } + + private async Task ConfirmAddRoom(string name, string description) + { + if(string.IsNullOrWhiteSpace(name)) + { + await JSRuntime.InvokeVoidAsync("alert", "Please enter a name for the room"); + return; + } + + await RoomService.AddRoomAsync(name, description, _cancellationTokenSource.Token); + await HideModal("addRoomModal"); + NavigationManager.NavigateTo(NavigationManager.Uri, forceLoad: true); + } + + private async Task EditRoomModal(RoomWrapper wRoom) + { + _selectedWRoom = wRoom; + _roomName = _selectedWRoom.Room.Name; + _roomDescription = _selectedWRoom.Room.Description; + wRoom.TrackerCount = await GetTrackerCountAsync(wRoom.Room); + await ShowModal("editRoomModal"); + StateHasChanged(); + } + + private async Task DeleteRoomModal(RoomWrapper? wRoom) + { + if(wRoom == null) + { + await JSRuntime.InvokeVoidAsync("alert", "Room is null"); + return; + } + + await DeleteRoom(wRoom); + await HideModal("editRoomModal"); + StateHasChanged(); + } + + private async Task ConfirmEditRoom(RoomWrapper? wRoom, string name, string description) + { + if(string.IsNullOrWhiteSpace(name)) + { + await JSRuntime.InvokeVoidAsync("alert", "Please enter a name for the room"); + return; + } + if(wRoom == null) + { + await JSRuntime.InvokeVoidAsync("alert", "Room is null"); + return; + } + + + await RoomService.UpdateRoomAsync(wRoom.Room, name, description, _cancellationTokenSource.Token); + await HideModal("editRoomModal"); + NavigationManager.NavigateTo(NavigationManager.Uri, forceLoad: true); + } + + private async Task AddTrackerModal(RoomWrapper wRoom) + { + _unregisteredTrackers = await TrackerService.GetUnregisteredTrackersAsync(_cancellationTokenSource.Token); + + if(_unregisteredTrackers.Length != 0) + { + _selectedTrackerMacAddress = _unregisteredTrackers[0].WifiMacAddress; + } + + _selectedWRoom = wRoom; + _selectedRoomId = wRoom.Room.RoomId; + await ShowModal("addTrackerModal"); + StateHasChanged(); + } + + private async Task ConfirmAddTracker(int roomId, string? trackerMacAddress, string name, string description) + { + if(string.IsNullOrWhiteSpace(name)) + { + await JSRuntime.InvokeVoidAsync("alert", "Please enter a name for the tracker"); + return; + } + if(roomId == -1) + { + await JSRuntime.InvokeVoidAsync("alert", "Room's ID is invalid"); + return; + } + if(trackerMacAddress == null) + { + await JSRuntime.InvokeVoidAsync("alert", "Tracker's MAC-Address is null"); + return; + } + + var selectedRoom = _rooms.Single(r => r.RoomId == roomId); + + var selectedTracker = _unregisteredTrackers.Single(t => t.WifiMacAddress == trackerMacAddress); + + await TrackerService.AddTrackerAsync(selectedRoom, selectedTracker, name, description, _cancellationTokenSource.Token); + await HideModal("addTrackerModal"); + NavigationManager.NavigateTo(NavigationManager.Uri, forceLoad: true); + } + + private async Task EditTrackerModal(RoomWrapper wRoom, Tracker tracker) + { + _selectedWRoom = wRoom; + _selectedRoomId = wRoom.Room.RoomId; + _selectedTracker = tracker; + _trackerName = tracker.Name; + _trackerDescription = tracker.Description; + await ShowModal("editTrackerModal"); + StateHasChanged(); + } + + private async Task ConfirmEditTracker(Tracker? tracker, int roomId, string name, string description) + { + if(string.IsNullOrWhiteSpace(name)) + { + await JSRuntime.InvokeVoidAsync("alert", "Please enter a name for the tracker"); + return; + } + if(tracker == null) + { + await JSRuntime.InvokeVoidAsync("alert", "Tracker is null"); + return; + } + if(roomId == -1) + { + await JSRuntime.InvokeVoidAsync("alert", "Room's ID is invalid"); + return; + } + + var selectedRoom = _rooms.Single(r => r.RoomId == roomId); + + await TrackerService.UpdateTrackerAsync(selectedRoom, tracker, name, description, _cancellationTokenSource.Token); + await HideModal("editTrackerModal"); + NavigationManager.NavigateTo(NavigationManager.Uri, forceLoad: true); + } + + private async Task DeleteTrackerModal(RoomWrapper? wRoom, Tracker? tracker) + { + if(wRoom == null) + { + await JSRuntime.InvokeVoidAsync("alert", "Room is null"); + return; + } + if(tracker == null) + { + await JSRuntime.InvokeVoidAsync("alert", "Tracker is null"); + return; + } + + await DeleteTracker(wRoom, tracker); + await HideModal("editTrackerModal"); + NavigationManager.NavigateTo(NavigationManager.Uri, forceLoad: true); + + } + + private sealed class RoomWrapper + { + public RoomWrapper(Room room, Tracker[] children) + { + Room = room; + Children = children; + TrackerCount = children.Length; + } + public Room Room { get; set; } + public Tracker[] Children { get; set; } + public int TrackerCount { get; set; } + public bool IsExpanded { get; set; } + } + + protected override async Task OnInitializedAsync() + { + _rooms = (await RoomService.GetRoomsAsync(_cancellationTokenSource.Token)).ToList(); + _unregisteredTrackers = await TrackerService.GetUnregisteredTrackersAsync(_cancellationTokenSource.Token); + + var roomWrappers = new List(); + foreach (var room in _rooms) + { + roomWrappers.Add(new RoomWrapper( + room, + await TrackerService.GetTrackersAsync(room, _cancellationTokenSource.Token) + )); + } + _wRooms = roomWrappers; + AssignStandardValues(); + } + private async Task GetTrackerCountAsync(Room room) + { + var trackers = await TrackerService.GetTrackersAsync(room, _cancellationTokenSource.Token); + + return trackers.Length; + } + + private async Task DeleteRoom(RoomWrapper wRoom) + { + // Remove the room on the server + await RoomService.RemoveRoomAsync(wRoom.Room, _cancellationTokenSource.Token); + // Remove the room in memory + _rooms.Remove(wRoom.Room); + _wRooms.Remove(wRoom); + + StateHasChanged(); + } + + private async Task DeleteTracker(RoomWrapper wRoom, Tracker tracker) + { + // Remove the tracker on the server + await TrackerService.RemoveTrackerAsync(tracker, _cancellationTokenSource.Token); + // Remove the tracker in memory + var newTrackers = await TrackerService.GetTrackersAsync(wRoom.Room, _cancellationTokenSource.Token); + wRoom.Children = newTrackers; + + StateHasChanged(); + } + + public void Dispose() + { + _cancellationTokenSource.Cancel(); + _cancellationTokenSource.Dispose(); + } + + private void ToggleNode(RoomWrapper wRoom) + { + wRoom.IsExpanded = !wRoom.IsExpanded; + StateHasChanged(); + } + + private async Task HideModal(string modalId) + { + await JSRuntime.InvokeVoidAsync("hideModal", modalId); + AssignStandardValues(); + StateHasChanged(); + } + + private async Task ShowModal(string modalId) + { + await JSRuntime.InvokeVoidAsync("showModal", modalId); + } + + private void AssignStandardValues() + { + _selectedWRoom = null; + _selectedTracker = null; + + _selectedRoomId = -1; + _roomName = "Your room name"; + _roomDescription = "Your room description"; + _trackerName = "Your tracker name"; + _trackerDescription = "Your tracker description"; + _selectedTrackerMacAddress = null; + } + + private static string ShowOrHide(bool show) + { + return show ? string.Empty : "display: none"; + } +} \ No newline at end of file diff --git a/CentralHub.WebUI/Pages/Rooms.razor b/CentralHub.WebUI/Pages/Rooms.razor deleted file mode 100644 index f7ee900..0000000 --- a/CentralHub.WebUI/Pages/Rooms.razor +++ /dev/null @@ -1,590 +0,0 @@ -@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 - -

Rooms

- -
- -
- - - @foreach (var wRoom in _wRooms) - { - - - - - @if (wRoom.IsExpanded) - { - - - - } - } - -
- - @wRoom.Room.Name - -
- - - - -
-
- - - @foreach (var tracker in wRoom.Children) - { - - - - - } - -
- - @tracker.Name - -
- -
-
-
- - - - - -@if(_unregisteredTrackers.Length == 0) -{ - -} -else -{ - -} - - - - -@code { - private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource(); - private List _rooms = new List(); - private List _wRooms = new List(); - private UnregisteredTracker[] _unregisteredTrackers = new UnregisteredTracker[0]; - private RoomWrapper? _selectedWRoom; - private Tracker? _selectedTracker; - private int _selectedRoomId = -1; - private string _roomName = "Your room name"; - private string _roomDescription = "Your room description"; - private string _trackerName = "Your tracker name"; - private string _trackerDescription = "Your tracker description"; - private string? _selectedTrackerMacAddress = null; - - private async Task AddRoomModal() - { - await ShowModal("addRoomModal"); - StateHasChanged(); - } - - private async Task ConfirmAddRoom(string name, string description) - { - if(string.IsNullOrWhiteSpace(name)) - { - await JSRuntime.InvokeVoidAsync("alert", "Please enter a name for the room"); - return; - } - - await RoomService.AddRoomAsync(name, description, _cancellationTokenSource.Token); - await HideModal("addRoomModal"); - NavigationManager.NavigateTo(NavigationManager.Uri, forceLoad: true); - } - - private async Task EditRoomModal(RoomWrapper wRoom) - { - _selectedWRoom = wRoom; - _roomName = _selectedWRoom.Room.Name; - _roomDescription = _selectedWRoom.Room.Description; - wRoom.TrackerCount = await GetTrackerCountAsync(wRoom.Room); - await ShowModal("editRoomModal"); - StateHasChanged(); - } - - private async Task DeleteRoomModal(RoomWrapper? wRoom) - { - if(wRoom == null) - { - await JSRuntime.InvokeVoidAsync("alert", "Room is null"); - return; - } - - await DeleteRoom(wRoom); - await HideModal("editRoomModal"); - StateHasChanged(); - } - - private async Task ConfirmEditRoom(RoomWrapper? wRoom, string name, string description) - { - if(string.IsNullOrWhiteSpace(name)) - { - await JSRuntime.InvokeVoidAsync("alert", "Please enter a name for the room"); - return; - } - if(wRoom == null) - { - await JSRuntime.InvokeVoidAsync("alert", "Room is null"); - return; - } - - - await RoomService.UpdateRoomAsync(wRoom.Room, name, description, _cancellationTokenSource.Token); - await HideModal("editRoomModal"); - NavigationManager.NavigateTo(NavigationManager.Uri, forceLoad: true); - } - - private async Task AddTrackerModal(RoomWrapper wRoom) - { - _unregisteredTrackers = await TrackerService.GetUnregisteredTrackersAsync(_cancellationTokenSource.Token); - - if(_unregisteredTrackers.Length != 0) - { - _selectedTrackerMacAddress = _unregisteredTrackers[0].WifiMacAddress; - } - - _selectedWRoom = wRoom; - _selectedRoomId = wRoom.Room.RoomId; - await ShowModal("addTrackerModal"); - StateHasChanged(); - } - - private async Task ConfirmAddTracker(int roomId, string? trackerMacAddress, string name, string description) - { - if(string.IsNullOrWhiteSpace(name)) - { - await JSRuntime.InvokeVoidAsync("alert", "Please enter a name for the tracker"); - return; - } - if(roomId == -1) - { - await JSRuntime.InvokeVoidAsync("alert", "Room's ID is invalid"); - return; - } - if(trackerMacAddress == null) - { - await JSRuntime.InvokeVoidAsync("alert", "Tracker's MAC-Address is null"); - return; - } - - var selectedRoom = _rooms.Single(r => r.RoomId == roomId); - - var selectedTracker = _unregisteredTrackers.Single(t => t.WifiMacAddress == trackerMacAddress); - - await TrackerService.AddTrackerAsync(selectedRoom, selectedTracker, name, description, _cancellationTokenSource.Token); - await HideModal("addTrackerModal"); - NavigationManager.NavigateTo(NavigationManager.Uri, forceLoad: true); - } - - private async Task EditTrackerModal(RoomWrapper wRoom, Tracker tracker) - { - _selectedWRoom = wRoom; - _selectedRoomId = wRoom.Room.RoomId; - _selectedTracker = tracker; - _trackerName = tracker.Name; - _trackerDescription = tracker.Description; - await ShowModal("editTrackerModal"); - StateHasChanged(); - } - - private async Task ConfirmEditTracker(Tracker? tracker, int roomId, string name, string description) - { - if(string.IsNullOrWhiteSpace(name)) - { - await JSRuntime.InvokeVoidAsync("alert", "Please enter a name for the tracker"); - return; - } - if(tracker == null) - { - await JSRuntime.InvokeVoidAsync("alert", "Tracker is null"); - return; - } - if(roomId == -1) - { - await JSRuntime.InvokeVoidAsync("alert", "Room's ID is invalid"); - return; - } - - var selectedRoom = _rooms.Single(r => r.RoomId == roomId); - - await TrackerService.UpdateTrackerAsync(selectedRoom, tracker, name, description, _cancellationTokenSource.Token); - await HideModal("editTrackerModal"); - NavigationManager.NavigateTo(NavigationManager.Uri, forceLoad: true); - } - - private async Task DeleteTrackerModal(RoomWrapper? wRoom, Tracker? tracker) - { - if(wRoom == null) - { - await JSRuntime.InvokeVoidAsync("alert", "Room is null"); - return; - } - if(tracker == null) - { - await JSRuntime.InvokeVoidAsync("alert", "Tracker is null"); - return; - } - - await DeleteTracker(wRoom, tracker); - await HideModal("editTrackerModal"); - NavigationManager.NavigateTo(NavigationManager.Uri, forceLoad: true); - - } - - private sealed class RoomWrapper - { - public RoomWrapper(Room room, Tracker[] children) - { - Room = room; - Children = children; - TrackerCount = children.Length; - } - public Room Room { get; set; } - public Tracker[] Children { get; set; } - public int TrackerCount { get; set; } - public bool IsExpanded { get; set; } - } - - protected override async Task OnInitializedAsync() - { - _rooms = (await RoomService.GetRoomsAsync(_cancellationTokenSource.Token)).ToList(); - _unregisteredTrackers = await TrackerService.GetUnregisteredTrackersAsync(_cancellationTokenSource.Token); - - var roomWrappers = new List(); - foreach (var room in _rooms) - { - roomWrappers.Add(new RoomWrapper( - room, - await TrackerService.GetTrackersAsync(room, _cancellationTokenSource.Token) - )); - } - _wRooms = roomWrappers; - AssignStandardValues(); - } - private async Task GetTrackerCountAsync(Room room) - { - var trackers = await TrackerService.GetTrackersAsync(room, _cancellationTokenSource.Token); - - return trackers.Length; - } - - private async Task DeleteRoom(RoomWrapper wRoom) - { - // Remove the room on the server - await RoomService.RemoveRoomAsync(wRoom.Room, _cancellationTokenSource.Token); - // Remove the room in memory - _rooms.Remove(wRoom.Room); - _wRooms.Remove(wRoom); - - StateHasChanged(); - } - - private async Task DeleteTracker(RoomWrapper wRoom, Tracker tracker) - { - // Remove the tracker on the server - await TrackerService.RemoveTrackerAsync(tracker, _cancellationTokenSource.Token); - // Remove the tracker in memory - var newTrackers = await TrackerService.GetTrackersAsync(wRoom.Room, _cancellationTokenSource.Token); - wRoom.Children = newTrackers; - - StateHasChanged(); - } - - public void Dispose() - { - _cancellationTokenSource.Cancel(); - _cancellationTokenSource.Dispose(); - } - - private void ToggleNode(RoomWrapper wRoom) - { - wRoom.IsExpanded = !wRoom.IsExpanded; - StateHasChanged(); - } - - private async Task HideModal(string modalId) - { - await JSRuntime.InvokeVoidAsync("hideModal", modalId); - AssignStandardValues(); - StateHasChanged(); - } - - private async Task ShowModal(string modalId) - { - await JSRuntime.InvokeVoidAsync("showModal", modalId); - } - - private void AssignStandardValues() - { - _selectedWRoom = null; - _selectedTracker = null; - - _selectedRoomId = -1; - _roomName = "Your room name"; - _roomDescription = "Your room description"; - _trackerName = "Your tracker name"; - _trackerDescription = "Your tracker description"; - _selectedTrackerMacAddress = null; - } -} \ No newline at end of file diff --git a/CentralHub.WebUI/Pages/UnregisteredTrackers.razor b/CentralHub.WebUI/Pages/UnregisteredTrackers.razor index 01e74a1..f9dff97 100644 --- a/CentralHub.WebUI/Pages/UnregisteredTrackers.razor +++ b/CentralHub.WebUI/Pages/UnregisteredTrackers.razor @@ -2,6 +2,7 @@ @using CentralHub.WebUI.Data @using CentralHub.Api.Model.Responses.Room @using CentralHub.Api.Model.Responses.Tracker +@using System.Collections.Immutable @implements IDisposable @inject TrackerService TrackerService @@ -9,8 +10,6 @@

Unregistered Trackers

-

This component demonstrates fetching data from a service.

- @if (_unregisteredTrackers == null) {

@@ -19,32 +18,38 @@ } else { - - - - - - - - - @foreach (var unregisteredTracker in _unregisteredTrackers) - { - - - - - } - -
Wifi Mac AddressBluetooth Mac Address
@unregisteredTracker.WifiMacAddress@unregisteredTracker.BluetoothMacAddress
+

+
+
+ + + + + + + + + @foreach (var unregisteredTracker in _unregisteredTrackers) + { + + + + + } + +
Wifi Mac AddressBluetooth Mac Address
@unregisteredTracker.WifiMacAddress@unregisteredTracker.BluetoothMacAddress
+
+
+
} @code { private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource(); - private List? _unregisteredTrackers; + private ImmutableArray? _unregisteredTrackers; protected override async Task OnInitializedAsync() { - _unregisteredTrackers = (await TrackerService.GetUnregisteredTrackersAsync(_cancellationTokenSource.Token)).ToList(); + _unregisteredTrackers = (await TrackerService.GetUnregisteredTrackersAsync(_cancellationTokenSource.Token)).ToImmutableArray(); } public void Dispose() diff --git a/CentralHub.WebUI/Shared/NavMenu.razor b/CentralHub.WebUI/Shared/NavMenu.razor index 5d2d97d..b3444f7 100644 --- a/CentralHub.WebUI/Shared/NavMenu.razor +++ b/CentralHub.WebUI/Shared/NavMenu.razor @@ -14,11 +14,6 @@ Home -