SharpLibEar/Event.cs
author StephaneLenclud
Wed, 31 Aug 2016 16:24:33 +0200
changeset 263 3ab73a2a72d8
parent 260 d44943088c67
child 265 82e87f4956ea
permissions -rw-r--r--
Adding Delay action to replace our Sleep action.
     1 //
     2 
     3 
     4 using System;
     5 using System.Collections.Generic;
     6 using System.Diagnostics;
     7 using System.Runtime.Serialization;
     8 using System.Threading.Tasks;
     9 
    10 namespace SharpLib.Ear
    11 {
    12     [DataContract]
    13     [AttributeObject(Id = "Event", Name = "User Event", Description = "An event that can be triggered by users.")]
    14     public class Event : Object
    15     {
    16         [DataMember]
    17         [AttributeObjectProperty
    18             (
    19                 Id = "Event.Enabled",
    20                 Name = "Enabled",
    21                 Description = "When enabled an event instance can be triggered."
    22             )
    23         ]
    24         public bool Enabled { get; set; } = true;
    25 
    26 
    27         /// <summary>
    28         /// TODO: Should the name property be moved to our EAR Object?
    29         /// </summary>
    30         [DataMember]
    31         [AttributeObjectProperty
    32             (
    33                 Id = "Event.Name",
    34                 Name = "Name",
    35                 Description = "Given event name. Can be used to trigger it."
    36             )
    37         ]
    38         public string Name { get; set; } = "";
    39 
    40 
    41         [DataMember]
    42         public List<Action> Actions = new List<Action>();
    43 
    44 
    45         protected override void DoConstruct()
    46         {
    47             base.DoConstruct();
    48 
    49             //Make sure our name is not null
    50             if (Name == null)
    51             {
    52                 Name = "";
    53             }
    54 
    55             // TODO: Construct properties too
    56             foreach (Action a in Actions)
    57             {
    58                 a.Construct();
    59             }
    60 
    61         }
    62 
    63 
    64         /// <summary>
    65         /// Allows testing from generic edit dialog.
    66         /// </summary>
    67         public async void Test()
    68         {
    69             Trace.WriteLine("Event test");
    70             await Trigger();
    71         }
    72 
    73 
    74         public async Task Trigger()
    75         {
    76             Trace.WriteLine("Event triggered: " + AttributeName);
    77             foreach (Action action in Actions)
    78             {
    79                 await action.Execute();
    80             }
    81         }
    82 
    83         //
    84         public override bool Equals(object obj)
    85         {
    86             //Default implementation assumes event are the same if types are the same
    87             bool res=  obj.GetType() == GetType();
    88             return res;
    89         }
    90     };
    91 
    92 }