Files
Heimdall/src/Heimdall.Web/Components/Pages/CamerasPage.razor
T

208 lines
7.5 KiB
Plaintext

@page "/sites/{SiteId:guid}/cameras"
@attribute [Authorize]
@layout MainLayout
@inject HeimdallApiClient Api
@inject ISnackbar Snackbar
@inject NavigationManager Nav
<MudText Typo="Typo.h4" Class="mb-6">
<MudIcon Icon="@Icons.Material.Filled.Videocam" Class="mr-2" />
Caméras du site
</MudText>
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="@(() => _showCreate = true)" Class="mb-4">
<MudIcon Icon="@Icons.Material.Filled.Add" Class="mr-1" /> Ajouter une caméra
</MudButton>
<MudButton Variant="Variant.Outlined" Color="Color.Default" OnClick="@(() => Nav.NavigateTo("/sites"))" Class="mb-4 ml-2">
← Retour aux sites
</MudButton>
@if (_loading)
{
<MudProgressCircular Indeterminate="true" />
}
else if (_error)
{
<MudAlert Severity="Severity.Error" Class="mb-4" Variant="Variant.Filled">
<MudText>Erreur lors du chargement des caméras.</MudText>
<MudButton Variant="Variant.Text" Color="Color.Inherit" OnClick="@LoadCameras" Class="mt-2">
<MudIcon Icon="@Icons.Material.Filled.Refresh" Class="mr-1" Size="Size.Small" /> Réessayer
</MudButton>
</MudAlert>
}
else
{
<MudTable Items="@_cameras" Hover="true" Striped="true" Elevation="2">
<HeaderContent>
<MudTh>Nom</MudTh>
<MudTh>Fabricant</MudTh>
<MudTh>Modèle</MudTh>
<MudTh>RTSP</MudTh>
<MudTh>Statut</MudTh>
<MudTh>Actions</MudTh>
</HeaderContent>
<RowTemplate>
<MudTd DataLabel="Nom">
<MudLink Href="@($"/cameras/{context.Id}")">@context.Name</MudLink>
</MudTd>
<MudTd DataLabel="Fabricant">@context.Manufacturer</MudTd>
<MudTd DataLabel="Modèle">@context.Model</MudTd>
<MudTd DataLabel="RTSP" Style="max-width:200px;overflow:hidden;text-overflow:ellipsis;">
@context.RtspUrl
</MudTd>
<MudTd DataLabel="Statut">
<MudText Color="@GetStatusColor(context.Status)" Typo="Typo.body2">
@context.Status
</MudText>
</MudTd>
<MudTd DataLabel="Actions">
<MudIconButton Icon="@Icons.Material.Filled.Delete" Size="Size.Small" Color="Color.Error"
OnClick="@(() => ConfirmDeleteCamera(context.Id, context.Name))" />
</MudTd>
</RowTemplate>
<NoRecordsContent>
<MudText Align="Align.Center" Class="py-4">Aucune caméra. Cliquez sur "Ajouter une caméra".</MudText>
</NoRecordsContent>
</MudTable>
}
@* ─── Confirm delete dialog ───────────────────────────────── *@
@if (_confirmDeleteShow)
{
<MudOverlay Visible="@_confirmDeleteShow" OnClick="@(() => _confirmDeleteShow = false)" />
<MudDialog @bind-Visible="_confirmDeleteShow">
<TitleContent><MudText Typo="Typo.h6">Confirmer la suppression</MudText></TitleContent>
<DialogContent>
<MudText>Supprimer la caméra &quot;@_confirmDeleteName&quot; ? Cette action est irréversible.</MudText>
</DialogContent>
<DialogActions>
<MudButton Variant="Variant.Text" OnClick="@(() => _confirmDeleteShow = false)">Annuler</MudButton>
<MudButton Variant="Variant.Filled" Color="Color.Error" OnClick="@ExecuteDeleteCamera">Supprimer</MudButton>
</DialogActions>
</MudDialog>
}
@if (_showCreate)
{
<MudOverlay Visible="@_showCreate" OnClick="@(() => _showCreate = false)" />
<MudDialog @bind-Visible="_showCreate">
<TitleContent><MudText Typo="Typo.h6">Ajouter une caméra</MudText></TitleContent>
<DialogContent>
<MudTextField @bind-Value="_newName" Label="Nom *" Variant="Variant.Outlined"
Class="mb-4" Required="true" Immediate="true" />
<MudTextField @bind-Value="_newManufacturer" Label="Fabricant" Variant="Variant.Outlined" Class="mb-4" />
<MudTextField @bind-Value="_newModel" Label="Modèle" Variant="Variant.Outlined" Class="mb-4" />
<MudTextField @bind-Value="_newRtspUrl" Label="URL RTSP" Variant="Variant.Outlined" Class="mb-4"
HelperText="Ex: rtsp://192.168.1.100:554/stream1" />
@if (!string.IsNullOrEmpty(_dialogError))
{
<MudAlert Severity="Severity.Error" Class="mb-4">@_dialogError</MudAlert>
}
</DialogContent>
<DialogActions>
<MudButton Variant="Variant.Text" OnClick="@(() => _showCreate = false)">Annuler</MudButton>
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="@CreateCamera" Disabled="@_saving">
@if (_saving) { <MudProgressCircular Size="Size.Small" Class="mr-1" /> }
Ajouter
</MudButton>
</DialogActions>
</MudDialog>
}
@code {
[Parameter] public Guid SiteId { get; set; }
private IReadOnlyList<CameraItem>? _cameras;
private bool _loading = true;
private bool _error;
private bool _showCreate;
private bool _saving;
private string _dialogError = "";
private string _newName = "";
private string? _newManufacturer;
private string? _newModel;
private string? _newRtspUrl;
protected override async Task OnInitializedAsync() => await LoadCameras();
private async Task LoadCameras()
{
_loading = true;
_error = false;
try
{
var result = await Api.GetCamerasBySiteAsync(SiteId);
_cameras = result?.Items;
}
catch (Exception ex)
{
_error = true;
Snackbar.Add($"Erreur : {ex.Message}", Severity.Error);
}
finally { _loading = false; }
}
private bool _confirmDeleteShow;
private Guid _confirmDeleteId;
private string _confirmDeleteName = "";
private void ConfirmDeleteCamera(Guid id, string name)
{
_confirmDeleteId = id;
_confirmDeleteName = name;
_confirmDeleteShow = true;
}
private async Task ExecuteDeleteCamera()
{
_confirmDeleteShow = false;
await DeleteCamera(_confirmDeleteId, _confirmDeleteName);
}
private async Task CreateCamera()
{
if (string.IsNullOrWhiteSpace(_newName)) { _dialogError = "Le nom est requis"; return; }
_saving = true;
_dialogError = "";
try
{
var result = await Api.CreateCameraAsync(SiteId, _newName, _newRtspUrl, _newManufacturer, _newModel);
if (result is null) { _dialogError = "Erreur lors de la création"; return; }
_showCreate = false;
ResetForm();
Snackbar.Add("Caméra ajoutée", Severity.Success);
await LoadCameras();
}
catch (Exception ex) { _dialogError = $"Erreur : {ex.Message}"; }
finally { _saving = false; }
}
private async Task DeleteCamera(Guid id, string name)
{
if (await Api.DeleteCameraAsync(id))
{
Snackbar.Add($"Caméra \"{name}\" supprimée", Severity.Success);
await LoadCameras();
}
else Snackbar.Add("Erreur", Severity.Error);
}
private void ResetForm()
{
_newName = "";
_newManufacturer = null;
_newModel = null;
_newRtspUrl = null;
}
private static Color GetStatusColor(string status) => status switch
{
"Online" => Color.Success,
"Offline" => Color.Error,
"Disabled" => Color.Default,
_ => Color.Warning
};
}