namespace Salmon.Core; public class Event { public enum ValenceType { Normal, Error, Emergency } public const string TYPE_STATE_CHANGED = "state_changed"; public const string TYPE_LOG = "log"; public string UniqueId { get; set; } public string ThrowerId { get; set; } public string Type { get; set; } public DateTime When { get; set; } = DateTime.Now; public string Description { get; set; } = ""; public ValenceType Valence { get; set; } = default; public Dictionary Properties { get; protected set; } = new Dictionary(); public object? this[string key] { get { if(Properties.ContainsKey(key)) return Properties[key]; return null; } set { if (!Properties.TryAdd(key, value)) Properties[key] = value; } } protected Event() { } public Event(string uniqueId, string throwerId, string type, string description = "", DateTime? when = null) { UniqueId = uniqueId; ThrowerId = throwerId; Type = type; Description = description; if(when != null) When = when.Value; } public static Event FromStateChange(string key, object? value, DateTime when) { string id = Guid.NewGuid().ToString(); var ret = new Event(id, key, TYPE_STATE_CHANGED, when:when); ret[nameof(Triplet.value)] = value; return ret; } public static Event FromLog( string thrower, string content, DateTime? when = null, int severity = 6, Exception? exception = null ) { if (when == null) when = DateTime.Now; string id = Guid.NewGuid().ToString(); var ret = new Event(id, thrower, TYPE_LOG, description: content, when: when); ret["severity"] = severity; if (exception is not null && exception.StackTrace is not null) ret["trace"] = exception.StackTrace; return ret; } }