Occupancy script update

This commit is contained in:
Justinas K. 2021-12-06 17:52:05 +02:00
parent 479bb921db
commit 7b8b022ae0
2 changed files with 23 additions and 20 deletions

View File

@ -8,7 +8,8 @@ using MDP.SID.Scripting.Shared.Http;
using MDP.SID.Shared.Public.Events;
using MDP.SID.Shared.Public.Readers;
using Event = MDP.SID.Scripting.Grpc.Event;
public class Tenant
{
public string Name { get; set; }
@ -38,7 +39,7 @@ public class AccessLevel
public bool IsEntry { get; set; }
}
private readonly SemaphoreSlim _syncLock = new SemaphoreSlim(1, 1);
private readonly SemaphoreSlim _syncLock = new(1, 1);
Context.OnEventReceived += OnEventReceived;
@ -95,7 +96,7 @@ private void SetupHttpEndpoints()
_syncLock.Release();
}
});
Http.MapGet("/ping", async _ => "pong");
Http.MapPost("/reset", async context =>
@ -120,10 +121,10 @@ private void SetupHttpEndpoints()
{
var accessLevels = await Context.GetAccessLevelsAsync();
return accessLevels.Select(e => new AccessLevel()
return accessLevels.Where(e => !e.BuiltIn).Select(e => new AccessLevel()
{
Id = e.Id,
Name = e.Name
Name = $"{e.Name} ({e.LocationName})"
});
});
@ -220,12 +221,13 @@ private async Task EvaluateOccupancyAsync(Tenant tenant, int newOccupancyValue,
bool forceAccessLevelUpdate = false)
{
newOccupancyValue = Math.Clamp(newOccupancyValue, 0, int.MaxValue);
if (newOccupancyValue >= newMaxOccupancyValue)
{
await AddRemoveOccupancyAccessLevelAsync(tenant.Id, true);
}
else if (newOccupancyValue < newMaxOccupancyValue && (tenant.CurrentOccupancy >= newMaxOccupancyValue || forceAccessLevelUpdate))
else if (newOccupancyValue < newMaxOccupancyValue &&
(tenant.CurrentOccupancy >= newMaxOccupancyValue || forceAccessLevelUpdate))
{
await AddRemoveOccupancyAccessLevelAsync(tenant.Id, false);
}
@ -233,7 +235,7 @@ private async Task EvaluateOccupancyAsync(Tenant tenant, int newOccupancyValue,
// normalize
tenant.CurrentOccupancy = Math.Min(tenant.MaxOccupancy, newOccupancyValue);
tenant.MaxOccupancy = newMaxOccupancyValue;
await Database.UpdateAsync(tenant.Id, tenant);
@ -246,14 +248,15 @@ private async Task AddRemoveOccupancyAccessLevelAsync(int tenantId, bool remove)
try
{
IEnumerable<MDP.SID.Scripting.Grpc.AccessLevel> als = await Context.GetAccessLevelsAsync();
List<EntityUpdate> toUpdate = als.Where(e => e.Id != Configuration.AccessLevelId).Select(e =>
new EntityUpdate()
{
Id = e.Id,
Remove = false,
Update = false
}).ToList();
List<EntityUpdate> toUpdate = als.Where(e => e.Id != Configuration.AccessLevelId && !e.BuiltIn).Select(
e =>
new EntityUpdate()
{
Id = e.Id,
Remove = false,
Update = false
}).ToList();
toUpdate.Add(new EntityUpdate
{
@ -282,7 +285,7 @@ private async Task SyncTenantsAsync()
{
// this request can't happen twice at the same time as there might state where database
await _syncLock.WaitAsync();
try
{
var existing = (await Database.GetAllAsync<Tenant>()).ToDictionary(e => e.Id);
@ -293,7 +296,7 @@ private async Task SyncTenantsAsync()
{
if (!existing.TryGetValue(company.Id, out var existingCompany))
{
var tenant = new Tenant()
var tenant = new Tenant
{
Id = company.Id,
Name = company.Name
@ -326,4 +329,4 @@ private async Task SyncTenantsAsync()
{
_syncLock.Release();
}
}
}

View File

@ -1,2 +1,2 @@
//export const BASE_URL = 'http://localhost:58111'
export const BASE_URL = 'http://localhost:8090/scripting/europeum'
export const BASE_URL = 'http://localhost:58111'