Prototype for event and action framework.
authorStephaneLenclud
Fri, 22 Jul 2016 18:19:49 +0200
changeset 209fef4ca058087
parent 208 6cd9ffdd6386
child 210 83dd86e73448
Prototype for event and action framework.
Server/MainForm.cs
Server/SharpDisplayManager.csproj
Server/Slions/Ear/Action.cs
Server/Slions/Ear/ActionCallback.cs
Server/Slions/Ear/ActionSleep.cs
Server/Slions/Ear/ActionType.cs
Server/Slions/Ear/Event.cs
Server/Slions/Ear/EventMonitorPowerOff.cs
Server/Slions/Ear/EventMonitorPowerOn.cs
Server/Slions/Ear/Manager.cs
     1.1 --- a/Server/MainForm.cs	Fri Jul 22 13:47:44 2016 +0200
     1.2 +++ b/Server/MainForm.cs	Fri Jul 22 18:19:49 2016 +0200
     1.3 @@ -46,6 +46,7 @@
     1.4  using SharpDisplay;
     1.5  using MiniDisplayInterop;
     1.6  using SharpLib.Display;
     1.7 +using Slions.Ear;
     1.8  
     1.9  namespace SharpDisplayManager
    1.10  {
    1.11 @@ -69,6 +70,7 @@
    1.12  	[System.ComponentModel.DesignerCategory("Form")]
    1.13  	public partial class MainForm : MainFormHid, IMMNotificationClient
    1.14      {
    1.15 +        private Manager iManager = new Manager();
    1.16          DateTime LastTickTime;
    1.17          Display iDisplay;
    1.18          System.Drawing.Bitmap iBmp;
     2.1 --- a/Server/SharpDisplayManager.csproj	Fri Jul 22 13:47:44 2016 +0200
     2.2 +++ b/Server/SharpDisplayManager.csproj	Fri Jul 22 18:19:49 2016 +0200
     2.3 @@ -188,6 +188,14 @@
     2.4      <Compile Include="RichTextBoxTextWriter.cs" />
     2.5      <Compile Include="Session.cs" />
     2.6      <Compile Include="Settings.cs" />
     2.7 +    <Compile Include="Slions\Ear\Action.cs" />
     2.8 +    <Compile Include="Slions\Ear\ActionCallback.cs" />
     2.9 +    <Compile Include="Slions\Ear\ActionSleep.cs" />
    2.10 +    <Compile Include="Slions\Ear\ActionType.cs" />
    2.11 +    <Compile Include="Slions\Ear\Event.cs" />
    2.12 +    <Compile Include="Slions\Ear\EventMonitorPowerOff.cs" />
    2.13 +    <Compile Include="Slions\Ear\EventMonitorPowerOn.cs" />
    2.14 +    <Compile Include="Slions\Ear\Manager.cs" />
    2.15      <Compile Include="StartupManager.cs" />
    2.16      <Compile Include="TaskScheduler.cs" />
    2.17      <Compile Include="Win32API.cs" />
    2.18 @@ -300,6 +308,7 @@
    2.19      </Content>
    2.20      <EmbeddedResource Include="Resources\record.ico" />
    2.21    </ItemGroup>
    2.22 +  <ItemGroup />
    2.23    <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    2.24    <Import Project="..\packages\LibMiniDisplay.1.1.8\build\LibMiniDisplay.targets" Condition="Exists('..\packages\LibMiniDisplay.1.1.8\build\LibMiniDisplay.targets')" />
    2.25    <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/Server/Slions/Ear/Action.cs	Fri Jul 22 18:19:49 2016 +0200
     3.3 @@ -0,0 +1,16 @@
     3.4 +//
     3.5 +
     3.6 +
     3.7 +using System.Runtime.Serialization;
     3.8 +using System.Threading;
     3.9 +
    3.10 +namespace Slions.Ear
    3.11 +{
    3.12 +    [DataContract]
    3.13 +    abstract class Action
    3.14 +    {
    3.15 +        public abstract void Execute();
    3.16 +    }
    3.17 +
    3.18 +
    3.19 +}
    3.20 \ No newline at end of file
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/Server/Slions/Ear/ActionCallback.cs	Fri Jul 22 18:19:49 2016 +0200
     4.3 @@ -0,0 +1,29 @@
     4.4 +//
     4.5 +
     4.6 +using System.Runtime.Serialization;
     4.7 +
     4.8 +
     4.9 +namespace Slions.Ear
    4.10 +{
    4.11 +    [DataContract]
    4.12 +    abstract class ActionCallback : Action
    4.13 +    {
    4.14 +        public delegate void Delegate();
    4.15 +
    4.16 +        private readonly Delegate iCallback;
    4.17 +
    4.18 +        public ActionCallback(Delegate aCallback = null)
    4.19 +        {
    4.20 +            iCallback = aCallback;
    4.21 +        }
    4.22 +
    4.23 +        public override void Execute()
    4.24 +        {
    4.25 +            if (iCallback != null)
    4.26 +            {
    4.27 +                iCallback.Invoke();
    4.28 +            }            
    4.29 +        }
    4.30 +    }
    4.31 +
    4.32 +}
    4.33 \ No newline at end of file
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/Server/Slions/Ear/ActionSleep.cs	Fri Jul 22 18:19:49 2016 +0200
     5.3 @@ -0,0 +1,38 @@
     5.4 +//
     5.5 +
     5.6 +
     5.7 +using System.Runtime.Serialization;
     5.8 +using System.Threading;
     5.9 +
    5.10 +namespace Slions.Ear
    5.11 +{
    5.12 +    
    5.13 +
    5.14 +    [DataContract]
    5.15 +    class ActionSleep : Action
    5.16 +    {
    5.17 +        static readonly string Name = "Sleep";
    5.18 +        static readonly string Description = "Have the current thread sleep for the specified amount of milliseconds.";
    5.19 +
    5.20 +        private readonly int iMillisecondsTimeout;
    5.21 +
    5.22 +        public ActionSleep()
    5.23 +        {
    5.24 +            iMillisecondsTimeout = 1000;
    5.25 +        }
    5.26 +
    5.27 +
    5.28 +        public ActionSleep(int aMillisecondsTimeout)
    5.29 +        {
    5.30 +            iMillisecondsTimeout = aMillisecondsTimeout;
    5.31 +        }
    5.32 +
    5.33 +        public override void Execute()
    5.34 +        {
    5.35 +            Thread.Sleep(iMillisecondsTimeout);
    5.36 +        }
    5.37 +    }
    5.38 +
    5.39 +
    5.40 +
    5.41 +}
    5.42 \ No newline at end of file
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/Server/Slions/Ear/ActionType.cs	Fri Jul 22 18:19:49 2016 +0200
     6.3 @@ -0,0 +1,19 @@
     6.4 +//
     6.5 +
     6.6 +
     6.7 +using System.Runtime.Serialization;
     6.8 +using System.Threading;
     6.9 +
    6.10 +namespace Slions.Ear
    6.11 +{
    6.12 +    [DataContract]
    6.13 +    abstract class ActionType
    6.14 +    {
    6.15 +        [DataMember]
    6.16 +        public string Name { get; protected set;  }
    6.17 +        [DataMember]
    6.18 +        public string Description { get; protected set; }
    6.19 +    }
    6.20 +
    6.21 +
    6.22 +}
    6.23 \ No newline at end of file
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/Server/Slions/Ear/Event.cs	Fri Jul 22 18:19:49 2016 +0200
     7.3 @@ -0,0 +1,42 @@
     7.4 +//
     7.5 +
     7.6 +
     7.7 +using System;
     7.8 +using System.Collections.Generic;
     7.9 +using System.Runtime.Serialization;
    7.10 +
    7.11 +namespace Slions.Ear
    7.12 +{
    7.13 +    [DataContract]
    7.14 +    abstract class MEvent
    7.15 +    {
    7.16 +        [DataMember]
    7.17 +        public string Name { get; protected set; }
    7.18 +
    7.19 +        [DataMember]
    7.20 +        public string Description { get; protected set; }
    7.21 +
    7.22 +        public abstract void Trigger();
    7.23 +    };
    7.24 +
    7.25 +    [DataContract]
    7.26 +    abstract class Event : MEvent
    7.27 +    {
    7.28 +        List<Action> iActions;
    7.29 +
    7.30 +        protected Event()
    7.31 +        {
    7.32 +            iActions = new List<Action>();
    7.33 +        }
    7.34 +
    7.35 +        public override void Trigger()
    7.36 +        {
    7.37 +            Console.WriteLine("Event '" + Name + "' triggered.");
    7.38 +            foreach (Action action in iActions)
    7.39 +            {
    7.40 +                action.Execute();
    7.41 +            }
    7.42 +        }
    7.43 +    }
    7.44 +
    7.45 +}
    7.46 \ No newline at end of file
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/Server/Slions/Ear/EventMonitorPowerOff.cs	Fri Jul 22 18:19:49 2016 +0200
     8.3 @@ -0,0 +1,18 @@
     8.4 +//
     8.5 +
     8.6 +
     8.7 +using System.Runtime.Serialization;
     8.8 +
     8.9 +namespace Slions.Ear
    8.10 +{
    8.11 +    [DataContract]
    8.12 +    class EventMonitorPowerOff : Event
    8.13 +    {
    8.14 +        public EventMonitorPowerOff()
    8.15 +        {
    8.16 +            Name = "Monitor Power Off";
    8.17 +            Description = "";
    8.18 +        }
    8.19 +    }
    8.20 +
    8.21 +}
    8.22 \ No newline at end of file
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/Server/Slions/Ear/EventMonitorPowerOn.cs	Fri Jul 22 18:19:49 2016 +0200
     9.3 @@ -0,0 +1,19 @@
     9.4 +//
     9.5 +
     9.6 +
     9.7 +using System.Runtime.Serialization;
     9.8 +
     9.9 +namespace Slions.Ear
    9.10 +{
    9.11 +    [DataContract]
    9.12 +    class EventMonitorPowerOn : Event
    9.13 +    {
    9.14 +        public EventMonitorPowerOn()
    9.15 +        {
    9.16 +            Name = "Monitor Power On";
    9.17 +            Description = "";
    9.18 +        }
    9.19 +
    9.20 +    }
    9.21 +
    9.22 +}
    9.23 \ No newline at end of file
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/Server/Slions/Ear/Manager.cs	Fri Jul 22 18:19:49 2016 +0200
    10.3 @@ -0,0 +1,49 @@
    10.4 +//
    10.5 +
    10.6 +
    10.7 +using System;
    10.8 +using System.Collections.Generic;
    10.9 +using System.Linq;
   10.10 +using System.Reflection;
   10.11 +using System.Runtime.Serialization;
   10.12 +
   10.13 +namespace Slions.Ear
   10.14 +{
   10.15 +    public static class ReflectiveEnumerator
   10.16 +    {
   10.17 +        static ReflectiveEnumerator() { }
   10.18 +
   10.19 +        public static IEnumerable<T> GetEnumerableOfType<T>() where T : class
   10.20 +        {
   10.21 +            List<T> objects = new List<T>();
   10.22 +            foreach (Type type in
   10.23 +                Assembly.GetAssembly(typeof(T)).GetTypes()
   10.24 +                .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
   10.25 +            {
   10.26 +                objects.Add((T)Activator.CreateInstance(type));
   10.27 +            }
   10.28 +            //objects.Sort();
   10.29 +            return objects;
   10.30 +        }
   10.31 +    }
   10.32 +
   10.33 +
   10.34 +    [DataContract]
   10.35 +    class Manager
   10.36 +    {
   10.37 +        private IEnumerable<Action> Actions;
   10.38 +        private IEnumerable<Event> Events;
   10.39 +
   10.40 +        public Manager()
   10.41 +        {
   10.42 +            Actions = ReflectiveEnumerator.GetEnumerableOfType<Action>();
   10.43 +            Events = ReflectiveEnumerator.GetEnumerableOfType<Event>();
   10.44 +
   10.45 +            //CollectActions();
   10.46 +            //CollectEvents();
   10.47 +        }
   10.48 +
   10.49 +
   10.50 +
   10.51 +    }
   10.52 +}
   10.53 \ No newline at end of file