Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of UI #42

Merged
merged 22 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
_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 All @@ -31,7 +111,7 @@
throw new InvalidOperationException("Shit brokey");
}

return trackers.Trackers.ToArray();

Check warning on line 114 in CentralHub.WebUI/Data/TrackerService.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Possible null reference argument for parameter 'source' in 'Tracker[] Enumerable.ToArray<Tracker>(IEnumerable<Tracker> source)'.

Check warning on line 114 in CentralHub.WebUI/Data/TrackerService.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Possible null reference argument for parameter 'source' in 'Tracker[] Enumerable.ToArray<Tracker>(IEnumerable<Tracker> source)'.

Check warning on line 114 in CentralHub.WebUI/Data/TrackerService.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Possible null reference argument for parameter 'source' in 'Tracker[] Enumerable.ToArray<Tracker>(IEnumerable<Tracker> source)'.

Check warning on line 114 in CentralHub.WebUI/Data/TrackerService.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Possible null reference argument for parameter 'source' in 'Tracker[] Enumerable.ToArray<Tracker>(IEnumerable<Tracker> source)'.

Check warning on line 114 in CentralHub.WebUI/Data/TrackerService.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Possible null reference argument for parameter 'source' in 'Tracker[] Enumerable.ToArray<Tracker>(IEnumerable<Tracker> source)'.

Check warning on line 114 in CentralHub.WebUI/Data/TrackerService.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Possible null reference argument for parameter 'source' in 'Tracker[] Enumerable.ToArray<Tracker>(IEnumerable<Tracker> source)'.
}

public async Task<UnregisteredTracker[]> GetUnregisteredTrackersAsync(CancellationToken cancellationToken)
Expand Down
Loading
Loading