2026-07-19 12:55:05 +00:00
|
|
|
using Heimdall.Domain.Enums;
|
|
|
|
|
using Heimdall.Domain.Primitives;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.Domain.Entities;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// An event detected on a camera stream — motion, person, vehicle, face, etc.
|
|
|
|
|
/// Stores AI detection results with confidence, bounding boxes, and snapshots.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class CameraEvent : Entity<Guid>, IAuditableEntity
|
|
|
|
|
{
|
|
|
|
|
public Guid CameraId { get; private set; }
|
|
|
|
|
public Guid? AgentId { get; private set; }
|
|
|
|
|
public EventType Type { get; private set; }
|
|
|
|
|
public double Confidence { get; private set; }
|
|
|
|
|
public string? Label { get; private set; } // Human-readable label (e.g. "John Doe", "AB-123-CD")
|
|
|
|
|
public string? SnapshotUrl { get; private set; } // S3 key or URL to snapshot image
|
|
|
|
|
public string? SnapshotThumbUrl { get; private set; } // Thumbnail S3 key or URL
|
|
|
|
|
public string? MetadataJson { get; private set; } // JSON: bounding boxes, track ID, etc.
|
|
|
|
|
public DateTime OccurredAt { get; private set; }
|
|
|
|
|
|
2026-08-11 21:42:46 +02:00
|
|
|
// ── Relations métier (spec 2.1/2.2) ─────────────────────────────
|
|
|
|
|
/// <summary>Vehicle linked to this event (Event → Vehicle N:1).</summary>
|
|
|
|
|
public Guid? VehicleId { get; private set; }
|
|
|
|
|
|
2026-10-22 20:12:25 +02:00
|
|
|
/// <summary>Known person linked by face recognition; kept as an application-level reference because the table is partitioned.</summary>
|
|
|
|
|
public Guid? PersonId { get; private set; }
|
|
|
|
|
|
2026-08-11 21:42:46 +02:00
|
|
|
/// <summary>Recording segment linked to this event (Event → Recording N:1).</summary>
|
|
|
|
|
public Guid? RecordingId { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>Parent event for event hierarchies (Event → Event N:1).</summary>
|
|
|
|
|
public Guid? ParentEventId { get; private set; }
|
|
|
|
|
|
|
|
|
|
// ── Déduplication (spec 2.1) ────────────────────────────────────
|
|
|
|
|
/// <summary>Stable hash of detection content used for deduplication.</summary>
|
|
|
|
|
public string? DetectionHash { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>True when the event was suppressed (duplicate / low value).</summary>
|
|
|
|
|
public bool IsSuppressed { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>Tracking identifier grouping frames of the same object.</summary>
|
|
|
|
|
public string? TrackId { get; private set; }
|
|
|
|
|
|
2026-07-19 12:55:05 +00:00
|
|
|
// Audit
|
|
|
|
|
public DateTime CreatedAt { get; set; }
|
|
|
|
|
public DateTime UpdatedAt { get; set; }
|
|
|
|
|
|
|
|
|
|
// Navigation
|
|
|
|
|
public Camera Camera { get; private set; } = null!;
|
2026-08-27 00:31:12 +02:00
|
|
|
// Note: Vehicle, Recording, ParentEvent navigations removed for partitioned table
|
|
|
|
|
// compatibility. PostgreSQL requires FK columns to include the partition key (OccurredAt),
|
|
|
|
|
// which is not possible with the current schema of those tables.
|
|
|
|
|
// FK IDs (VehicleId, RecordingId, ParentEventId) are retained for application-level queries.
|
2026-07-19 12:55:05 +00:00
|
|
|
|
|
|
|
|
private CameraEvent() { } // EF Core
|
|
|
|
|
|
2026-10-22 19:24:18 +02:00
|
|
|
public CameraEvent(Guid cameraId, EventType type, double confidence, DateTime? occurredAt = null, Guid? id = null)
|
|
|
|
|
: base(id ?? Guid.NewGuid())
|
2026-07-19 12:55:05 +00:00
|
|
|
{
|
|
|
|
|
CameraId = cameraId;
|
|
|
|
|
Type = type;
|
|
|
|
|
Confidence = confidence;
|
2026-10-22 19:24:18 +02:00
|
|
|
OccurredAt = NormalizeOccurredAt(occurredAt ?? DateTime.UtcNow);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Normalizes event timestamps to UTC and PostgreSQL's microsecond precision.</summary>
|
|
|
|
|
public static DateTime NormalizeOccurredAt(DateTime value)
|
|
|
|
|
{
|
|
|
|
|
var utc = value.Kind switch
|
|
|
|
|
{
|
|
|
|
|
DateTimeKind.Local => value.ToUniversalTime(),
|
|
|
|
|
DateTimeKind.Unspecified => DateTime.SpecifyKind(value, DateTimeKind.Utc),
|
|
|
|
|
_ => value
|
|
|
|
|
};
|
|
|
|
|
return new DateTime(utc.Ticks - utc.Ticks % 10, DateTimeKind.Utc);
|
2026-07-19 12:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetSnapshot(string snapshotUrl, string? thumbUrl = null)
|
|
|
|
|
{
|
|
|
|
|
SnapshotUrl = snapshotUrl;
|
|
|
|
|
if (thumbUrl is not null)
|
|
|
|
|
SnapshotThumbUrl = thumbUrl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetMetadata(string? label, string? metadataJson)
|
|
|
|
|
{
|
|
|
|
|
Label = label;
|
|
|
|
|
MetadataJson = metadataJson;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AssignAgent(Guid agentId) => AgentId = agentId;
|
2026-08-11 21:42:46 +02:00
|
|
|
|
|
|
|
|
public void AssignVehicle(Guid vehicleId) => VehicleId = vehicleId;
|
2026-10-22 20:12:25 +02:00
|
|
|
public void AssignPerson(Guid personId) => PersonId = personId;
|
2026-08-11 21:42:46 +02:00
|
|
|
public void AssignRecording(Guid recordingId) => RecordingId = recordingId;
|
|
|
|
|
public void AssignParentEvent(Guid parentEventId) => ParentEventId = parentEventId;
|
|
|
|
|
public void SetTrackId(string trackId) => TrackId = trackId;
|
|
|
|
|
public void SetDetectionHash(string detectionHash) => DetectionHash = detectionHash;
|
|
|
|
|
public void SetSuppressed(bool suppressed) => IsSuppressed = suppressed;
|
2026-07-19 12:55:05 +00:00
|
|
|
}
|