Complete MVP implementation (Étape 1) covering: - Domain: primitives (Entity<TId>, ValueObject), 10 entities, 6 enums - EF Core: AppDbContext + Identity, 8 EntityTypeConfigurations, audit/soft-delete interceptors, initial migration - Application: service interfaces, DTOs, FluentValidation, ServiceResult pattern - Security: Identity + JWT + API Key multi-auth, role-based permissions, constant-time key validation - API: Auth/Sites/Agents controllers with explicit auth scheme policies - Workers: Hangfire PostgreSQL setup with schema isolation, HeartbeatMonitorJob - Agent: registration service + 30s heartbeat loop - Docker: multi-stage Dockerfiles for central + agent, docker-compose.yml - CI/CD: GitHub Actions pipeline with build+test and Docker build - Tests: 13 domain tests (Site, Agent, Camera entities) Architecture decisions per deliberation council: - API Key via AuthenticationHandler<T> (not middleware) - Explicit [Authorize(AuthenticationSchemes)] on all endpoints - Event/Recording/Person/Vehicle deferred from initial migration - Hangfire compatible with .NET 10 (v1.8.17)
92 lines
2.6 KiB
C#
92 lines
2.6 KiB
C#
using Heimdall.Domain.Enums;
|
|
using Heimdall.Domain.Primitives;
|
|
|
|
namespace Heimdall.Domain.Entities;
|
|
|
|
public class Agent : Entity<Guid>, IAuditableEntity, ISoftDeletable
|
|
{
|
|
public Guid SiteId { get; private set; }
|
|
public string Name { get; private set; } = string.Empty;
|
|
public string Hostname { get; private set; } = string.Empty;
|
|
public string Version { get; private set; } = string.Empty;
|
|
public AgentStatus Status { get; private set; } = AgentStatus.Offline;
|
|
public DateTime? LastHeartbeat { get; private set; }
|
|
public string? PublicIp { get; private set; }
|
|
|
|
/// <summary>HMACSHA256 hash of the agent's API key.</summary>
|
|
public string? ApiKeyHash { get; private set; }
|
|
|
|
/// <summary>First 8 characters of the API key for identification.</summary>
|
|
public string? ApiKeyPrefix { get; private set; }
|
|
|
|
// Capabilities
|
|
public bool HasGpu { get; private set; }
|
|
public int CpuCores { get; private set; }
|
|
public long MemoryMb { get; private set; }
|
|
public GpuAcceleration GpuType { get; private set; } = GpuAcceleration.None;
|
|
|
|
// Audit
|
|
public DateTime CreatedAt { get; set; }
|
|
public DateTime UpdatedAt { get; set; }
|
|
|
|
// Soft delete
|
|
public bool IsDeleted { get; private set; }
|
|
public DateTime? DeletedAt { get; private set; }
|
|
|
|
// Navigation
|
|
public Site Site { get; private set; } = null!;
|
|
private readonly List<Camera> _cameras = [];
|
|
public IReadOnlyCollection<Camera> Cameras => _cameras.AsReadOnly();
|
|
|
|
private Agent() { } // EF Core
|
|
|
|
public Agent(Guid siteId, string name, string hostname, string version)
|
|
: base(Guid.NewGuid())
|
|
{
|
|
SiteId = siteId;
|
|
Name = name;
|
|
Hostname = hostname;
|
|
Version = version;
|
|
}
|
|
|
|
public void Register(string apiKeyHash, string apiKeyPrefix)
|
|
{
|
|
ApiKeyHash = apiKeyHash;
|
|
ApiKeyPrefix = apiKeyPrefix;
|
|
Status = AgentStatus.Online;
|
|
LastHeartbeat = DateTime.UtcNow;
|
|
}
|
|
|
|
public void UpdateHeartbeat(string? publicIp = null)
|
|
{
|
|
LastHeartbeat = DateTime.UtcNow;
|
|
Status = AgentStatus.Online;
|
|
if (publicIp is not null)
|
|
PublicIp = publicIp;
|
|
}
|
|
|
|
public void MarkOffline()
|
|
{
|
|
Status = AgentStatus.Offline;
|
|
}
|
|
|
|
public void MarkError()
|
|
{
|
|
Status = AgentStatus.Error;
|
|
}
|
|
|
|
public void UpdateCapabilities(bool hasGpu, int cpuCores, long memoryMb, GpuAcceleration gpuType)
|
|
{
|
|
HasGpu = hasGpu;
|
|
CpuCores = cpuCores;
|
|
MemoryMb = memoryMb;
|
|
GpuType = gpuType;
|
|
}
|
|
|
|
public void SoftDelete()
|
|
{
|
|
IsDeleted = true;
|
|
DeletedAt = DateTime.UtcNow;
|
|
}
|
|
}
|