Skip to content

Commit

Permalink
State part rework, error handling on blazor exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
LucHeart committed Apr 26, 2024
1 parent 82cf4b5 commit a3154d5
Show file tree
Hide file tree
Showing 16 changed files with 222 additions and 68 deletions.
2 changes: 1 addition & 1 deletion ShockOsc/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public static Microsoft.Maui.Hosting.MauiApp CreateMauiApp()

builder.Services.AddMudServices();
builder.Services.AddMauiBlazorWebView();

// <---- App ---->

builder
Expand Down
2 changes: 1 addition & 1 deletion ShockOsc/Services/LiveControlManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private async Task RefreshInternal()
_configManager.Config.OpenShock.Token, _liveControlLogger);
LiveControlClients.Add(device.Id, client);
client.OnStateUpdate += async (state) =>
client.State.OnValueChanged += async state =>
{
_logger.LogTrace("Live control client for device [{DeviceId}] status updated {Status}",
device.Id, state);
Expand Down
4 changes: 2 additions & 2 deletions ShockOsc/Services/ShockOsc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,8 @@ private async Task InstantShock(ProgramGroup programGroup, uint duration, byte i
programGroup.TriggerMethod = TriggerMethod.None;
var inSeconds = MathF.Round(duration / 1000f, 1).ToString(CultureInfo.InvariantCulture);
_logger.LogInformation(
"Sending shock to {GroupName} Intensity: {Intensity} IntensityPercentage: {IntensityPercentage}% Length:{Length}s",
programGroup.Name, intensity, intensityPercentage, inSeconds);
"Sending shock to {GroupName} Intensity: {Intensity} IntensityPercentage: {IntensityPercentage}% Length:{Length}s Exclusive: {Exclusive}",
programGroup.Name, intensity, intensityPercentage, inSeconds, exclusive);

await _backendHubManager.ControlGroup(programGroup.Id, duration, intensity, ControlType.Shock, exclusive);

Expand Down
6 changes: 3 additions & 3 deletions ShockOsc/ShockOsc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@
<PackageReference Include="EmbedIO" Version="3.5.2" />
<PackageReference Include="LucHeart.CoreOSC" Version="1.2.1" />
<PackageReference Include="MeaMod.DNS" Version="1.0.70" />
<PackageReference Include="OpenShock.SDK.CSharp" Version="0.0.15" />
<PackageReference Include="OpenShock.SDK.CSharp.Hub" Version="0.0.16" />
<PackageReference Include="OpenShock.SDK.CSharp.Live" Version="0.0.17" />
<PackageReference Include="OpenShock.SDK.CSharp" Version="0.0.19" />
<PackageReference Include="OpenShock.SDK.CSharp.Hub" Version="0.0.19" />
<PackageReference Include="OpenShock.SDK.CSharp.Live" Version="0.0.20" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.4" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" />
Expand Down
4 changes: 0 additions & 4 deletions ShockOsc/Ui/Components/MainLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
</style>

<MudThemeProvider Theme="ThemeDefinition.ShockOscTheme"/>
<MudDialogProvider/>
<MudSnackbarProvider/>

<MudLayout>
<div class="d-flex" style="width: 100vw; height: 100vh">

Expand Down
11 changes: 9 additions & 2 deletions ShockOsc/Ui/Components/Parts/StatePart.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
<div style="display: flex; flex-direction: column" class="child-div-align-center flex-grow-1">
<MudText Style="font-size: 10pt">@Text</MudText>
<MudTooltip Text="@Tooltip">
<MudIcon Icon="@Icon" Color="@IconColor"/>
<MudTooltip Arrow="true" Placement="Placement.Top">
<ChildContent>
<MudIcon Icon="@Icons.Material.Filled.Wifi" Color="GetConnectionStateColor()"/>
</ChildContent>
<TooltipContent>
<MudText Typo="Typo.body2">@Client.State.Value.ToString()</MudText>
<MudText Typo="Typo.caption">@Client.Gateway</MudText><br/>
<MudText Typo="Typo.caption">Latency: @(Client.Latency.Value)ms</MudText>
</TooltipContent>
</MudTooltip>
</div>

Expand Down
50 changes: 43 additions & 7 deletions ShockOsc/Ui/Components/Parts/StatePart.razor.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,55 @@
using Microsoft.AspNetCore.Components;
using OpenShock.SDK.CSharp.Live;
using OpenShock.SDK.CSharp.Live.LiveControlModels;
using Color = MudBlazor.Color;

namespace OpenShock.ShockOsc.Ui.Components.Parts;

public partial class StatePart : ComponentBase
public partial class StatePart : ComponentBase, IDisposable
{
[Parameter]
public required Color IconColor { get; set; }
public required IOpenShockLiveControlClient Client { get; set; }

[Parameter]
public required string Icon { get; set; }
public required string Text { get; set; }


[Parameter]
public required string Tooltip { get; set; }
private Task StateOnValueChanged(WebsocketConnectionState state)
{
return InvokeAsync(StateHasChanged);
}

[Parameter]
public required string Text { get; set; }
private Color GetConnectionStateColor() =>
Client.State.Value switch
{
WebsocketConnectionState.Connected => Color.Success,
WebsocketConnectionState.Reconnecting => Color.Warning,
WebsocketConnectionState.Connecting => Color.Warning,
WebsocketConnectionState.Disconnected => Color.Error,
_ => Color.Error
};

protected override void OnInitialized()
{
Client.State.OnValueChanged += StateOnValueChanged;
Client.Latency.OnValueChanged += LatencyOnValueChanged;
}

private Task LatencyOnValueChanged(ulong arg)
{
return InvokeAsync(StateHasChanged);
}

private bool _disposed = false;

public void Dispose()
{
if (_disposed) return;
_disposed = true;

Client.State.OnValueChanged -= StateOnValueChanged;
Client.Latency.OnValueChanged -= LatencyOnValueChanged;

GC.SuppressFinalize(this);
}
}
38 changes: 16 additions & 22 deletions ShockOsc/Ui/Components/SideBar.razor
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,28 @@

<MudText Style="font-size: 10pt">ShockOSC v@(Version)</MudText>
<div class="d-flex gap-5" style="flex-wrap: wrap; margin: 10px 0;">
<StatePart Text="Hub" Icon="@Icons.Material.Filled.Wifi"
IconColor="@GetConnectionStateColor(ApiHubClient.State)"
Tooltip="@ApiHubClient.State.ToString()">
</StatePart>

<div style="display: flex; flex-direction: column" class="child-div-align-center flex-grow-1">
<MudText Style="font-size: 10pt">Hub</MudText>
<MudTooltip Arrow="true" Placement="Placement.Top" Text="@ApiHubClient.State.ToString()">
<MudIcon Icon="@Icons.Material.Filled.Wifi" Color="@GetConnectionStateColor(ApiHubClient.State)"/>
</MudTooltip>
</div>

@foreach (var device in Api.Devices)
{
if (LiveControlManager.LiveControlClients.TryGetValue(device.Id, out var client))
{
<StatePart Text="@device.Name.Truncate(13)"
Icon="@Icons.Material.Filled.Wifi"
IconColor="@GetConnectionStateColor(client.State)"
Tooltip="@client.State.ToString()"/>
<StatePart Text="@device.Name.Truncate(13)" Client="client"/>
}
else
{
<StatePart Text="@device.Name.Truncate(13)" Icon="@Icons.Material.Filled.Wifi" IconColor="@Color.Dark" Tooltip="Offline"/>
<div style="display: flex; flex-direction: column" class="child-div-align-center flex-grow-1">
<MudText Style="font-size: 10pt">@device.Name.Truncate(13)</MudText>
<MudTooltip Arrow="true" Placement="Placement.Top" Text="Offline">
<MudIcon Icon="@Icons.Material.Filled.Wifi" Color="@Color.Dark"/>
</MudTooltip>
</div>
}
}

Expand All @@ -92,7 +97,7 @@
get => _currentTab;
set
{
if(_currentTab == value) return;
if (_currentTab == value) return;
CurrentTabChanged.InvokeAsync(value);
_currentTab = value;
}
Expand All @@ -118,16 +123,5 @@
_ => Color.Error
};

private static Color GetConnectionStateColor(WebsocketConnectionState state) =>
state switch
{
WebsocketConnectionState.Connected => Color.Success,
WebsocketConnectionState.Reconnecting => Color.Warning,
WebsocketConnectionState.Connecting => Color.Warning,
WebsocketConnectionState.Disconnected => Color.Error,
_ => Color.Error
};


}

}
2 changes: 2 additions & 0 deletions ShockOsc/Ui/Components/Tabs/DashboardTab.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

<h3>DashboardTab</h3>

<MudButton OnClick="() => throw new Exception()">Yes</MudButton>

@code {

}
16 changes: 10 additions & 6 deletions ShockOsc/Ui/Components/Tabs/LogsTab.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@using OpenShock.ShockOsc.Logging
@using OpenShock.ShockOsc.Utils
@using Serilog.Events
@using OpenShock.ShockOsc.Ui.ErrorHandling
@implements IDisposable

<MudTable T="LogStore.LogEntry" RowClass="cursor-pointer" RowClassFunc="RowClassFunc" Items="LogStore.Logs.OrderByDescending(x => x.Time)" OnRowClick="LogRowClick" Breakpoint="Breakpoint.Sm" Hover="true" Dense="true" Height="100%">
Expand All @@ -17,8 +18,8 @@
<ChildRowContent>
@if (context.IsExpanded)
{
<MudTr Style="pointer-events: none;">
<td colspan="12" style="padding: 0 10px">
<MudTr>
<td colspan="3" style="padding: 0 10px">

<br/>
<MudTextField ReadOnly Class="option-width" Variant="Variant.Filled" Value="@context.Time.ToString("HH:mm:ss")" Label="Time"></MudTextField>
Expand All @@ -27,9 +28,10 @@

<br/>
<br/>
<MudPaper Class="rounded-lg" Style="padding: 10px 15px; margin: 0 50px;">
<MudText>@context.Message</MudText>
</MudPaper>
@* HACK, thank you html tables *@
<div style="width: calc(100vw - 335px)">
<CodeBlock Value="@context.Message"/>
</div>
<br/>
<br/>
</td>
Expand All @@ -44,10 +46,12 @@

private void LogRowClick(TableRowClickEventArgs<LogStore.LogEntry> rowClickEventArgs)
{
if(rowClickEventArgs.Item == null) return;
rowClickEventArgs.Item.IsExpanded = !rowClickEventArgs.Item.IsExpanded;
}

private string RowClassFunc(LogStore.LogEntry? log, int arg2) => log == null ? string.Empty : GetLogClass(log.Level) + (log.IsExpanded ? " expanded" : "");
private string RowClassFunc(LogStore.LogEntry? log, int arg2) =>
log == null ? string.Empty : GetLogClass(log.Level) + (log.IsExpanded ? " expanded" : "");

protected override void OnInitialized()
{
Expand Down
45 changes: 45 additions & 0 deletions ShockOsc/Ui/ErrorHandling/CodeBlock.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
@inject ISnackbar Snackbar

<MudPaper Class="rounded-lg mud-paper-padding ex-code-block" Outlined="true">
<pre class="code">
@Value
</pre>
<MudIconButton Icon="@Icons.Material.Outlined.FileCopy" Size="Size.Small" Class="copy-code-button"
@onclick="CopyTextToClipboard"/>
</MudPaper>

<style>
.ex-code-block {
position: relative;
background-color: #2a2a2a;
width: 100%;
}
.ex-code-block .code {
outline: 0;
background-color: #2a2a2a;
color: var(--mud-palette-text-primary);
border-radius: 5px;
text-align: left;
overflow: auto;
}
.ex-code-block .copy-code-button {
position: absolute;
right: 10px;
bottom: 10px;
background: var(--mud-palette-surface);
}
</style>

@code {
[Parameter] public string Value { get; set; }

private async Task CopyTextToClipboard()
{
await Clipboard.SetTextAsync(Value);
Snackbar.Add("Copied to clipboard", Severity.Success);
}

}
20 changes: 20 additions & 0 deletions ShockOsc/Ui/ErrorHandling/ContainerWithExceptionHandling.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<div @attributes="Attributes">
<ErrorBoundary @ref="_boundary">
<ChildContent>
@ChildContent
</ChildContent>
<ErrorContent Context="ex">
<ExceptionView Exception="ex" OnResume="() => _boundary.Recover()"></ExceptionView>
</ErrorContent>
</ErrorBoundary>
</div>

@code {

[Parameter(CaptureUnmatchedValues = true)]
public Dictionary<string, object> Attributes { get; set; }

[Parameter] public RenderFragment ChildContent { get; set; }

private ErrorBoundary _boundary = null!;
}
41 changes: 41 additions & 0 deletions ShockOsc/Ui/ErrorHandling/ExceptionView.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@using System.Diagnostics

<MudContainer Class="align-content-center" MaxWidth="MaxWidth.Medium" Style="padding-top: 30px">
<MudPaper Class="rounded-lg" Elevation="0" Style="padding: 40px 20px; text-align: center;" Outlined="true">
<MudText Typo="Typo.h3">An unknown error occurred</MudText>
<br/>
<MudText Typo="Typo.h6">Please report this to us on GitHub</MudText>

<CodeBlock Value="@Exception.ToString()"></CodeBlock>
<br/>

<MudButton IconClass="@Icons.Material.Filled.Link" OnClick="OpenGithub" Variant="Variant.Filled"
Color="Color.Primary">
Open Github
</MudButton>
<MudButton OnClick="() => OnResume.InvokeAsync()" Variant="Variant.Filled" Color="Color.Primary">
Resume
</MudButton>

</MudPaper>
</MudContainer>

@code {

/// <summary>
/// Resume button click event
/// </summary>
[Parameter]
public EventCallback<MouseEventArgs> OnResume { get; set; }

/// <summary>
/// Exception to display
/// </summary>
[Parameter]
public Exception Exception { get; set; }

private void OpenGithub()
{
Process.Start(new ProcessStartInfo("https://github.com/OpenShock/ShockOsc/issues/new/choose") { UseShellExecute = true });
}
}
Loading

0 comments on commit a3154d5

Please sign in to comment.