StephaneLenclud@210: //
StephaneLenclud@210:
StephaneLenclud@210:
StephaneLenclud@210: using System;
StephaneLenclud@210: using System.Collections.Generic;
StephaneLenclud@253: using System.Diagnostics;
StephaneLenclud@210: using System.Runtime.Serialization;
StephaneLenclud@258: using System.Threading.Tasks;
StephaneLenclud@210:
StephaneLenclud@210: namespace SharpLib.Ear
StephaneLenclud@210: {
StephaneLenclud@210: [DataContract]
StephaneLenclud@260: [AttributeObject(Id = "Event", Name = "User Event", Description = "An event that can be triggered by users.")]
StephaneLenclud@260: public class Event : Object
StephaneLenclud@210: {
StephaneLenclud@231: [DataMember]
StephaneLenclud@231: [AttributeObjectProperty
StephaneLenclud@231: (
StephaneLenclud@231: Id = "Event.Enabled",
StephaneLenclud@231: Name = "Enabled",
StephaneLenclud@231: Description = "When enabled an event instance can be triggered."
StephaneLenclud@231: )
StephaneLenclud@231: ]
Stephane@243: public bool Enabled { get; set; } = true;
StephaneLenclud@210:
StephaneLenclud@262:
StephaneLenclud@262: ///
StephaneLenclud@262: /// TODO: Should the name property be moved to our EAR Object?
StephaneLenclud@262: ///
Stephane@212: [DataMember]
StephaneLenclud@260: [AttributeObjectProperty
StephaneLenclud@260: (
StephaneLenclud@260: Id = "Event.Name",
StephaneLenclud@260: Name = "Name",
StephaneLenclud@260: Description = "Given event name. Can be used to trigger it."
StephaneLenclud@260: )
StephaneLenclud@260: ]
StephaneLenclud@260: public string Name { get; set; } = "";
StephaneLenclud@260:
StephaneLenclud@262:
StephaneLenclud@260: [DataMember]
Stephane@212: public List Actions = new List();
StephaneLenclud@210:
StephaneLenclud@231:
Stephane@243: protected override void DoConstruct()
Stephane@243: {
Stephane@243: base.DoConstruct();
StephaneLenclud@231:
StephaneLenclud@262: //Make sure our name is not null
StephaneLenclud@262: if (Name == null)
StephaneLenclud@262: {
StephaneLenclud@262: Name = "";
StephaneLenclud@262: }
StephaneLenclud@262:
Stephane@243: // TODO: Construct properties too
Stephane@243: foreach (Action a in Actions)
Stephane@243: {
Stephane@243: a.Construct();
Stephane@243: }
Stephane@243:
StephaneLenclud@210: }
StephaneLenclud@210:
StephaneLenclud@231:
StephaneLenclud@231: ///
StephaneLenclud@231: /// Allows testing from generic edit dialog.
StephaneLenclud@231: ///
StephaneLenclud@260: public async void Test()
StephaneLenclud@231: {
StephaneLenclud@253: Trace.WriteLine("Event test");
StephaneLenclud@260: await Trigger();
StephaneLenclud@231: }
StephaneLenclud@231:
StephaneLenclud@231:
StephaneLenclud@258: public async Task Trigger()
StephaneLenclud@210: {
StephaneLenclud@260: Trace.WriteLine("Event triggered: " + AttributeName);
StephaneLenclud@210: foreach (Action action in Actions)
StephaneLenclud@210: {
StephaneLenclud@258: await action.Execute();
StephaneLenclud@210: }
StephaneLenclud@210: }
StephaneLenclud@231:
StephaneLenclud@237: //
StephaneLenclud@237: public override bool Equals(object obj)
StephaneLenclud@237: {
StephaneLenclud@237: //Default implementation assumes event are the same if types are the same
StephaneLenclud@237: bool res= obj.GetType() == GetType();
StephaneLenclud@237: return res;
StephaneLenclud@237: }
StephaneLenclud@231: };
StephaneLenclud@210:
StephaneLenclud@210: }