Skip to content

Commit

Permalink
Implementation of UI (#42)
Browse files Browse the repository at this point in the history
* Implement UI

* Update CentralHub.WebUI/Data/RoomService.cs

Co-authored-by: Mads Mogensen <[email protected]>

* Update CentralHub.WebUI/Pages/Rooms.razor

Co-authored-by: Mads Mogensen <[email protected]>

* Initial fixes in Rooms.razor

- _selectedTrackerMacAddress has been renamed and is now null as default (checks for this hasn't been changed
- every method and class has been made private
- String -> string
- a list has been changed to var

* Checks in RoomService for if rooms have been successfully been added, romeved and updated

* Checks in TrackerService for if a tracker has benn succesfully been added or removed

* Switched around all checks in Rooms-razor, so the same check isn't made twice

* dotnet format

* The start of fixing the problem with empty lists chrasing the system

(a half commit, so I can continue at home)

* Fixed the problem with addTracker, when no unregistered trackers are available

* list -> var

* list -> var && String -> string && VarName -> varName

* String -> string

* String -> string

* Mainly removal of '!'s from variables

Have removed alsmost all '!' apart from the ones for _selectedTracker and _selectedWRoom at call of ConfirmEditTracker(), DeleteTrackerModal(), DeleteRoomModal(), and ConfirmEditRoom()

* Rettelse continued

* last changes

* last last commit

* last last last commit

* last last last last commit

* reload

---------

Co-authored-by: Mads Mogensen <[email protected]>
  • Loading branch information
thom776g and mads256h authored Dec 8, 2023
1 parent 8d7fc38 commit 65d6489
Show file tree
Hide file tree
Showing 19 changed files with 17,578 additions and 96 deletions.
29 changes: 14 additions & 15 deletions CentralHub.Api/Services/SampleRoomRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TrackerDto>()
};
var rooms = new List<RoomDto>();

var room2 = new RoomDto()
for (int i = 0; i < 5; i++)
{
Name = "Sample Room 2",
Description = "Sample Room",
RoomDtoId = -1,
Trackers = new List<TrackerDto>()
};
rooms.Add(new RoomDto()
{
Name = $"Sample Room {i}",
Description = "Sample Room",
RoomDtoId = -1,
Trackers = new List<TrackerDto>()
});
}

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<int> AddRoomAsync(RoomDto roomDto, CancellationToken cancellationToken)
Expand Down
92 changes: 58 additions & 34 deletions CentralHub.Api/Services/SampleTrackerRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
var rooms = new List<RoomDto>();

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()!);
}

var trackers = new List<TrackerDto>();

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<int> AddTrackerAsync(TrackerDto trackerDto, CancellationToken cancellationToken)
Expand Down Expand Up @@ -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<IEnumerable<TrackerDto>?> GetTrackersInRoomAsync(int roomId, CancellationToken cancellationToken)
Expand Down
1 change: 1 addition & 0 deletions CentralHub.Api/Services/TrackerRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ await UnregisteredTrackersMutex.Lock(unregisteredTrackers =>
var tracker = await _applicationDbContext.Rooms
.Include(r => r.Trackers)
.SelectMany(r => r.Trackers)
.Include(t => t.RoomDto)
.SingleOrDefaultAsync(t => t.TrackerDtoId == id, cancellationToken);

return tracker;
Expand Down
56 changes: 55 additions & 1 deletion CentralHub.WebUI/Data/RoomService.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -18,20 +23,69 @@ 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");

var client = _clientFactory.CreateClient();

var response = await client.SendAsync(request, cancellationToken);
var removeRoomResponse = await response.Content.ReadFromJsonAsync<RemoveRoomResponse>(cancellationToken);

if (response.StatusCode != HttpStatusCode.OK || !removeRoomResponse!.Success)
{
throw new InvalidOperationException("Somethings fucky");
}
}

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);
var updateRoomResponse = await response.Content.ReadFromJsonAsync<UpdateRoomResponse>(cancellationToken);

if (response.StatusCode != HttpStatusCode.OK || !updateRoomResponse!.Success)
{
throw new InvalidOperationException("Somethings fucky");
}
}

public async Task<int> 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");
}

var addRoomResponse = await response.Content.ReadFromJsonAsync<AddRoomResponse>(cancellationToken);
return addRoomResponse!.RoomId;
}



public async Task<Room[]> GetRoomsAsync(CancellationToken cancellationToken)
{
var request = new HttpRequestMessage(
Expand Down
80 changes: 80 additions & 0 deletions CentralHub.WebUI/Data/TrackerService.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -13,6 +21,78 @@ 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);
var removeTrackerResponse = await response.Content.ReadFromJsonAsync<RemoveTrackerResponse>(cancellationToken);

if (response.StatusCode != HttpStatusCode.OK || !removeTrackerResponse!.Success)
{
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);
var addTrackerResponse = await response.Content.ReadFromJsonAsync<AddTrackerResponse>(cancellationToken);

if (response.StatusCode != HttpStatusCode.OK || !addTrackerResponse!.Success)
{
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);
var uTrackers = await GetUnregisteredTrackersAsync(cancellationToken);
var uTracker = uTrackers.Single(t => t.WifiMacAddress == tracker.WifiMacAddress);
await AddTrackerAsync(room, uTracker, name, description, cancellationToken);
}
}

public async Task<Tracker[]> GetTrackersAsync(Room room, CancellationToken cancellationToken)
{
var request = new HttpRequestMessage(
Expand Down
Loading

0 comments on commit 65d6489

Please sign in to comment.