save
This commit is contained in:
127
BlazorApp/Components/Pages/Can.razor
Normal file
127
BlazorApp/Components/Pages/Can.razor
Normal file
@@ -0,0 +1,127 @@
|
||||
@page "/can"
|
||||
@inject ICanService CanService
|
||||
@implements IDisposable
|
||||
|
||||
<div class="container mt-4">
|
||||
<div class="card shadow-sm">
|
||||
|
||||
<!-- HEADER -->
|
||||
<div class="card-header d-flex justify-content-between align-items-center">
|
||||
<h5 class="mb-0">CAN Monitor (Live View)</h5>
|
||||
|
||||
<div>
|
||||
<button class="btn btn-success btn-sm me-2"
|
||||
@onclick="Start"
|
||||
disabled="@isRunning">
|
||||
▶ Start
|
||||
</button>
|
||||
|
||||
<button class="btn btn-danger btn-sm"
|
||||
@onclick="Stop"
|
||||
disabled="@(!isRunning)">
|
||||
■ Stop
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- TABLE -->
|
||||
<div class="card-body p-0" style="max-height: 420px; overflow-y: auto;">
|
||||
<table class="table table-striped table-hover table-sm mb-0">
|
||||
<thead class="table-dark sticky-top">
|
||||
<tr>
|
||||
<th style="width:140px">Time</th>
|
||||
<th style="width:80px">CAN-ID</th>
|
||||
<th style="width:120px">Meaning</th>
|
||||
<th style="width:60px">Length</th>
|
||||
<th>Raw Data</th>
|
||||
<th style="width:200px">Result</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var msg in messages.Values.OrderBy(m => m.Id))
|
||||
{
|
||||
<tr>
|
||||
<td>@msg.Timestamp.ToString("HH:mm:ss.fff")</td>
|
||||
<td class="fw-bold text-primary">
|
||||
@msg.Id.ToString("X3")
|
||||
</td>
|
||||
<td>@msg.Meaning</td>
|
||||
<td>@msg.Length</td>
|
||||
<td class="font-monospace">
|
||||
@BitConverter.ToString(
|
||||
msg.Data.Take(msg.Length).ToArray())
|
||||
</td>
|
||||
<td class="fw-semibold">
|
||||
@msg.ValueText
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<!-- FOOTER -->
|
||||
<div class="card-footer text-muted small d-flex justify-content-between">
|
||||
<span>Total CAN IDs: <b>@messages.Count</b></span>
|
||||
<span>Status: <b>@status</b></span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@code {
|
||||
// DATA STORAGE (1 ID = 1 ROW)
|
||||
private Dictionary<uint, CanMessage> messages = new();
|
||||
|
||||
private bool isRunning = false;
|
||||
private string status = "STOPPED";
|
||||
|
||||
// INIT
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
CanService.OnMessageReceived += OnCanMessage;
|
||||
}
|
||||
|
||||
// RECEIVE CAN FRAME
|
||||
private void OnCanMessage(CanMessage msg)
|
||||
{
|
||||
InvokeAsync(() =>
|
||||
{
|
||||
// 🔴 Update data on the SAME row by CAN-ID
|
||||
messages[msg.Id] = msg;
|
||||
|
||||
StateHasChanged();
|
||||
});
|
||||
}
|
||||
|
||||
// START
|
||||
private async Task Start()
|
||||
{
|
||||
if (isRunning) return;
|
||||
|
||||
status = "INITIALIZING...";
|
||||
StateHasChanged();
|
||||
|
||||
await CanService.InitAsync(); // BẮT BUỘC
|
||||
CanService.StartStream();
|
||||
|
||||
isRunning = true;
|
||||
status = "RUNNING";
|
||||
}
|
||||
|
||||
// STOP
|
||||
private void Stop()
|
||||
{
|
||||
if (!isRunning) return;
|
||||
|
||||
CanService.StopStream();
|
||||
isRunning = false;
|
||||
status = "STOPPED";
|
||||
}
|
||||
|
||||
// CLEANUP
|
||||
public void Dispose()
|
||||
{
|
||||
CanService.OnMessageReceived -= OnCanMessage;
|
||||
CanService.StopStream();
|
||||
}
|
||||
}
|
||||
18
BlazorApp/Components/Pages/Counter.razor
Normal file
18
BlazorApp/Components/Pages/Counter.razor
Normal file
@@ -0,0 +1,18 @@
|
||||
@page "/counter"
|
||||
|
||||
<PageTitle>Counter</PageTitle>
|
||||
|
||||
<h1>Counter</h1>
|
||||
|
||||
<p role="status">Current count: @currentCount</p>
|
||||
|
||||
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
|
||||
|
||||
@code {
|
||||
private int currentCount = 0;
|
||||
|
||||
private void IncrementCount()
|
||||
{
|
||||
currentCount++;
|
||||
}
|
||||
}
|
||||
36
BlazorApp/Components/Pages/Error.razor
Normal file
36
BlazorApp/Components/Pages/Error.razor
Normal file
@@ -0,0 +1,36 @@
|
||||
@page "/Error"
|
||||
@using System.Diagnostics
|
||||
|
||||
<PageTitle>Error</PageTitle>
|
||||
|
||||
<h1 class="text-danger">Error.</h1>
|
||||
<h2 class="text-danger">An error occurred while processing your request.</h2>
|
||||
|
||||
@if (ShowRequestId)
|
||||
{
|
||||
<p>
|
||||
<strong>Request ID:</strong> <code>@RequestId</code>
|
||||
</p>
|
||||
}
|
||||
|
||||
<h3>Development Mode</h3>
|
||||
<p>
|
||||
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
|
||||
</p>
|
||||
<p>
|
||||
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
|
||||
It can result in displaying sensitive information from exceptions to end users.
|
||||
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
|
||||
and restarting the app.
|
||||
</p>
|
||||
|
||||
@code{
|
||||
[CascadingParameter]
|
||||
private HttpContext? HttpContext { get; set; }
|
||||
|
||||
private string? RequestId { get; set; }
|
||||
private bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
|
||||
|
||||
protected override void OnInitialized() =>
|
||||
RequestId = Activity.Current?.Id ?? HttpContext?.TraceIdentifier;
|
||||
}
|
||||
7
BlazorApp/Components/Pages/Home.razor
Normal file
7
BlazorApp/Components/Pages/Home.razor
Normal file
@@ -0,0 +1,7 @@
|
||||
@page "/"
|
||||
|
||||
<PageTitle>Home</PageTitle>
|
||||
|
||||
<h1>Hello, world!</h1>
|
||||
|
||||
Welcome to your new app.
|
||||
5
BlazorApp/Components/Pages/NotFound.razor
Normal file
5
BlazorApp/Components/Pages/NotFound.razor
Normal file
@@ -0,0 +1,5 @@
|
||||
@page "/not-found"
|
||||
@layout MainLayout
|
||||
|
||||
<h3>Not Found</h3>
|
||||
<p>Sorry, the content you are looking for does not exist.</p>
|
||||
63
BlazorApp/Components/Pages/Weather.razor
Normal file
63
BlazorApp/Components/Pages/Weather.razor
Normal file
@@ -0,0 +1,63 @@
|
||||
@page "/weather"
|
||||
|
||||
<PageTitle>Weather</PageTitle>
|
||||
|
||||
<h1>Weather</h1>
|
||||
|
||||
<p>This component demonstrates showing data.</p>
|
||||
|
||||
@if (forecasts == null)
|
||||
{
|
||||
<p><em>Loading...</em></p>
|
||||
}
|
||||
else
|
||||
{
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Date</th>
|
||||
<th aria-label="Temperature in Celsius">Temp. (C)</th>
|
||||
<th aria-label="Temperature in Fahrenheit">Temp. (F)</th>
|
||||
<th>Summary</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var forecast in forecasts)
|
||||
{
|
||||
<tr>
|
||||
<td>@forecast.Date.ToShortDateString()</td>
|
||||
<td>@forecast.TemperatureC</td>
|
||||
<td>@forecast.TemperatureF</td>
|
||||
<td>@forecast.Summary</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
}
|
||||
|
||||
@code {
|
||||
private WeatherForecast[]? forecasts;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
// Simulate asynchronous loading to demonstrate a loading indicator
|
||||
await Task.Delay(500);
|
||||
|
||||
var startDate = DateOnly.FromDateTime(DateTime.Now);
|
||||
var summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" };
|
||||
forecasts = Enumerable.Range(1, 5).Select(index => new WeatherForecast
|
||||
{
|
||||
Date = startDate.AddDays(index),
|
||||
TemperatureC = Random.Shared.Next(-20, 55),
|
||||
Summary = summaries[Random.Shared.Next(summaries.Length)]
|
||||
}).ToArray();
|
||||
}
|
||||
|
||||
private class WeatherForecast
|
||||
{
|
||||
public DateOnly Date { get; set; }
|
||||
public int TemperatureC { get; set; }
|
||||
public string? Summary { get; set; }
|
||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user