Skip to content

Commit

Permalink
Settings ui and service
Browse files Browse the repository at this point in the history
  • Loading branch information
DTTerastar committed Sep 9, 2024
1 parent 923ea14 commit 162b6c2
Show file tree
Hide file tree
Showing 17 changed files with 566 additions and 122 deletions.
75 changes: 75 additions & 0 deletions Controllers/SettingsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Microsoft.AspNetCore.Mvc;
using System.IO;
using System.Text.Json;

[ApiController]
[Route("api/settings")]
public class SettingsController : ControllerBase
{
private const string SettingsFilePath = "settings.json";

[HttpGet]
public ActionResult<Settings> GetSettings()
{
if (!System.IO.File.Exists(SettingsFilePath))
{
return new Settings();
}

var json = System.IO.File.ReadAllText(SettingsFilePath);
return JsonSerializer.Deserialize<Settings>(json);
}

[HttpPost]
public IActionResult SaveSettings([FromBody] Settings settings)
{
var json = JsonSerializer.Serialize(settings);
System.IO.File.WriteAllText(SettingsFilePath, json);
return Ok();
}
}

public class Settings
{
public Updating Updating { get; set; }
public Scanning Scanning { get; set; }
public Counting Counting { get; set; }
public Filtering Filtering { get; set; }
public Calibration Calibration { get; set; }
}

public class Updating
{
public bool AutoUpdate { get; set; }
public bool PreRelease { get; set; }
}

public class Scanning
{
public int? ForgetAfterMs { get; set; }
}

public class Counting
{
public string IdPrefixes { get; set; }
public double? StartCountingDistance { get; set; }
public double? StopCountingDistance { get; set; }
public int? IncludeDevicesAge { get; set; }
}

public class Filtering
{
public string IncludeIds { get; set; }
public string ExcludeIds { get; set; }
public double? MaxReportDistance { get; set; }
public double? EarlyReportDistance { get; set; }
public int? SkipReportAge { get; set; }
}

public class Calibration
{
public int? RssiAt1m { get; set; }
public int? RssiAdjustment { get; set; }
public double? AbsorptionFactor { get; set; }
public int? IBeaconRssiAt1m { get; set; }
}
75 changes: 75 additions & 0 deletions src/Controllers/SettingsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Microsoft.AspNetCore.Mvc;
using System.IO;
using System.Text.Json;

[ApiController]
[Route("api/settings")]
public class SettingsController : ControllerBase
{
private const string SettingsFilePath = "settings.json";

[HttpGet]
public ActionResult<Settings> GetSettings()
{
if (!System.IO.File.Exists(SettingsFilePath))
{
return new Settings();
}

var json = System.IO.File.ReadAllText(SettingsFilePath);
return JsonSerializer.Deserialize<Settings>(json);

Check warning on line 20 in src/Controllers/SettingsController.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'value' in 'ActionResult<Settings>.implicit operator ActionResult<Settings>(Settings value)'.
}

[HttpPost]
public IActionResult SaveSettings([FromBody] Settings settings)
{
var json = JsonSerializer.Serialize(settings);
System.IO.File.WriteAllText(SettingsFilePath, json);
return Ok();
}
}

public class Settings
{
public UpdatingSettings Updating { get; set; } = new UpdatingSettings();
public ScanningSettings Scanning { get; set; } = new ScanningSettings();
public CountingSettings Counting { get; set; } = new CountingSettings();
public FilteringSettings Filtering { get; set; } = new FilteringSettings();
public CalibrationSettings Calibration { get; set; } = new CalibrationSettings();
}

public class UpdatingSettings
{
public bool? AutoUpdate { get; set; }
public bool? PreRelease { get; set; }
}

public class ScanningSettings
{
public int? ForgetAfterMs { get; set; }
}

public class CountingSettings
{
public string? IdPrefixes { get; set; }
public double? StartCountingDistance { get; set; }
public double? StopCountingDistance { get; set; }
public int? IncludeDevicesAge { get; set; }
}

public class FilteringSettings
{
public string? IncludeIds { get; set; }
public string? ExcludeIds { get; set; }
public double? MaxReportDistance { get; set; }
public double? EarlyReportDistance { get; set; }
public int? SkipReportAge { get; set; }
}

public class CalibrationSettings
{
public int? RssiAt1m { get; set; }
public int? RssiAdjustment { get; set; }
public double? AbsorptionFactor { get; set; }
public int? IBeaconRssiAt1m { get; set; }
}
2 changes: 1 addition & 1 deletion src/Pages/Error.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
ViewData["Title"] = "Error";
}

<h1 class="text-danger">Error.</h1>
<h1 class="text-danger font-bold mb-2 px-2">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>

@if (Model.ShowRequestId)
Expand Down
109 changes: 109 additions & 0 deletions src/ui/src/lib/CalibrationMatrix.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<script lang="ts">
import { calibration } from '$lib/stores';
import { RadioGroup, RadioItem } from '@skeletonlabs/skeleton';
import { popup } from '@skeletonlabs/skeleton';
function coloring(percent: number) {
if (percent == null) {
return '';
}
return 'background-color: hsl(' + (240 - Math.min(Math.max(percent * 120, 0), 240)) + ', 50%, 50%)';
}
function value(n1: any, data_point: number) {
if (data_point === 0) {
return n1 ? Number(Math.round(n1.percent * 100)) + '%' : null;
} else if (data_point >= 1 && data_point <= 5) {
let num;
switch (data_point) {
case 1:
num = n1?.err;
break;
case 2:
num = n1?.absorption;
break;
case 3:
num = n1?.rx_adj_rssi;
break;
case 4:
num = n1?.tx_ref_rssi;
break;
case 5:
num = n1?.var;
break;
}
return num !== null && num !== undefined ? Number(num.toPrecision(3)) : 'n/a';
}
}
let rxColumns: Array<string> = [];
$: {
let rx = new Set(Object.keys($calibration?.matrix ?? {}));
Object.entries($calibration?.matrix ?? {})
.flatMap(([key, value]) => Object.keys(value))
.forEach((key) => rx.add(key));
rxColumns = new Array(...rx);
}
let data_point: number = 0;
</script>

{#if $calibration?.matrix}
{#each Object.entries($calibration?.matrix) as [id1, n1] (id1)}
{#each rxColumns as id2 (id2)}
<div class="card variant-filled-secondary p-4" data-popup={'popup-' + id1 + '-' + id2}>
{#if n1[id2]}
Expected {@html Number(n1[id2].expected?.toPrecision(3))} - Actual {@html Number(n1[id2]?.actual?.toPrecision(3))} = Error {@html Number(n1[id2]?.err?.toPrecision(3))}
{:else}
No beacon Received in last 30 seconds
{/if}
<div class="arrow variant-filled-secondary" />
</div>
{/each}
{/each}
{/if}

<div class="card p-2">
{#if $calibration?.matrix}
<header>
<div class="flex justify-center p-2">
<RadioGroup active="variant-filled-primary" hover="hover:variant-soft-primary">
<RadioItem bind:group={data_point} name="justify" value={0}>Error %</RadioItem>
<RadioItem bind:group={data_point} name="justify" value={1}>Error (m)</RadioItem>
<RadioItem bind:group={data_point} name="justify" value={2}>Absorption</RadioItem>
<RadioItem bind:group={data_point} name="justify" value={3}>Rx Rssi Adj</RadioItem>
<RadioItem bind:group={data_point} name="justify" value={4}>Tx Rssi Ref</RadioItem>
<RadioItem bind:group={data_point} name="justify" value={5}>Variance (m)</RadioItem>
</RadioGroup>
</div>
</header>
<section class="p-4 pt-0">
<table class="table table-hover">
<thead>
<tr>
<th>Name</th>
{#each rxColumns as id}
<th>Rx: {@html id}</th>
{/each}
</tr>
</thead>
<tbody>
{#each Object.entries($calibration.matrix) as [id1, n1] (id1)}
<tr>
<td>Tx: {@html id1}</td>
{#each rxColumns as id2 (id2)}
{#if n1[id2]}
<td use:popup={{ event: 'hover', target: 'popup-' + id1 + '-' + id2, placement: 'top' }} style={coloring(n1[id2]?.percent)}>{@html value(n1[id2], data_point)}</td>
{:else}
<td />
{/if}
{/each}
</tr>
{/each}
</tbody>
</table>
</section>
{:else}
<p>Loading...</p>
{/if}
</div>
2 changes: 1 addition & 1 deletion src/ui/src/lib/DevicesTable.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
}
</script>

<div class="table-container p-2">
<div class="p-2">
{#if $devices}
<SvelteTable {columns} classNameTable="table table-hover table-compact" rows={$devices} on:clickRow={onRowClick} sortBy="id" />
{/if}
Expand Down
Loading

0 comments on commit 162b6c2

Please sign in to comment.