Salmon/Salmon.Core/Event.cs
2024-04-11 21:30:36 +02:00

52 lines
1.2 KiB
C#

namespace Salmon.Core;
public class Event
{
public const string TYPE_STATE_CHANGED = "state_changed";
public string UniqueId { get; set; }
public string ThrowerId { get; set; }
public string Type { get; set; }
public DateTime When { get; set; } = DateTime.Now;
public Dictionary<string, object> Properties { get; protected set; } = new Dictionary<string, object>();
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, DateTime? when = null)
{
UniqueId = uniqueId;
ThrowerId = throwerId;
Type = type;
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);
ret[nameof(Triplet.value)] = value;
return ret;
}
}