# HG changeset patch # User Stephane Lenclud # Date 1469359808 -7200 # Node ID 1a0791daa24310e659172b023c0175f68a63627e # Parent 96f8b4dc430063bddf52886b5fcba3f99bf39a55 Actions persistence working. diff -r 96f8b4dc4300 -r 1a0791daa243 Server/ConsumerElectronicControl.cs --- a/Server/ConsumerElectronicControl.cs Sat Jul 23 19:22:56 2016 +0200 +++ b/Server/ConsumerElectronicControl.cs Sun Jul 24 13:30:08 2016 +0200 @@ -93,7 +93,7 @@ private void OnMonitorPowerOn() { - EventActionManager.Current.GetEvent().Trigger(); + ManagerEventAction.Current.GetEvent().Trigger(); Console.WriteLine("OnMonitorPowerOn"); @@ -112,7 +112,7 @@ private void OnMonitorPowerOff() { - EventActionManager.Current.GetEvent().Trigger(); + ManagerEventAction.Current.GetEvent().Trigger(); Console.WriteLine("OnMonitorPowerOff"); diff -r 96f8b4dc4300 -r 1a0791daa243 Server/DisplaySettings.cs --- a/Server/DisplaySettings.cs Sat Jul 23 19:22:56 2016 +0200 +++ b/Server/DisplaySettings.cs Sun Jul 24 13:30:08 2016 +0200 @@ -28,6 +28,7 @@ using System.Runtime.Serialization.Json; using System.IO; using System.Drawing; +using SharpLib.Utils; namespace SharpDisplayManager { @@ -114,10 +115,12 @@ /// /// Contain settings for each of our display type. /// - [TypeConverter(typeof(DisplaySettingsConverter))] + [TypeConverter(typeof(TypeConverterJson))] [DataContract] public class DisplaysSettings { + private List iDisplays; + public DisplaysSettings() { Init(); @@ -125,9 +128,9 @@ public void Init() { - if (Displays == null) + if (iDisplays == null) { - Displays = new List(); + iDisplays = new List(); } } @@ -135,50 +138,9 @@ //public int CurrentSettingsIndex { get; set; } [DataMember] - public List Displays { get; set; } + public List Displays { get { Init(); return iDisplays; } private set { iDisplays = value; } } - public override string ToString() - { - //Save settings into JSON string - MemoryStream stream = new MemoryStream(); - DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DisplaysSettings)); - ser.WriteObject(stream, this); - // convert stream to string - stream.Position = 0; - StreamReader reader = new StreamReader(stream); - string text = reader.ReadToEnd(); - return text; - } + } - - public class DisplaySettingsConverter : TypeConverter - { - public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) - { - if (sourceType == typeof(string)) - return true; - else - return base.CanConvertFrom(context, sourceType); - } - - public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) - { - string stringValue = value as string; - if (stringValue != null) - { - //Load settings form JSON string - byte[] byteArray = Encoding.UTF8.GetBytes(stringValue); - MemoryStream stream = new MemoryStream(byteArray); - DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DisplaysSettings)); - DisplaysSettings settings = (DisplaysSettings)ser.ReadObject(stream); - settings.Init(); - return settings; - } - else - return base.ConvertFrom(context, culture, value); - } - }; - - } diff -r 96f8b4dc4300 -r 1a0791daa243 Server/FormEditAction.cs --- a/Server/FormEditAction.cs Sat Jul 23 19:22:56 2016 +0200 +++ b/Server/FormEditAction.cs Sun Jul 24 13:30:08 2016 +0200 @@ -23,9 +23,9 @@ private void FormEditAction_Load(object sender, EventArgs e) { //Populate registered actions - foreach (string key in EventActionManager.Current.ActionTypes.Keys) + foreach (string key in ManagerEventAction.Current.ActionTypes.Keys) { - Type t = EventActionManager.Current.ActionTypes[key]; + Type t = ManagerEventAction.Current.ActionTypes[key]; comboBoxActionType.Items.Add(t); } diff -r 96f8b4dc4300 -r 1a0791daa243 Server/MainForm.cs --- a/Server/MainForm.cs Sat Jul 23 19:22:56 2016 +0200 +++ b/Server/MainForm.cs Sun Jul 24 13:30:08 2016 +0200 @@ -70,7 +70,7 @@ [System.ComponentModel.DesignerCategory("Form")] public partial class MainForm : MainFormHid, IMMNotificationClient { - public EventActionManager iManager = new EventActionManager(); + //public ManagerEventAction iManager = new ManagerEventAction(); DateTime LastTickTime; Display iDisplay; System.Drawing.Bitmap iBmp; @@ -131,7 +131,19 @@ public MainForm() { - EventActionManager.Current = iManager; + ManagerEventAction.Current = Properties.Settings.Default.Actions; + if (ManagerEventAction.Current == null) + { + //No actions in our settings yet + ManagerEventAction.Current = new ManagerEventAction(); + Properties.Settings.Default.Actions = ManagerEventAction.Current; + } + else + { + //We loaded actions from our settings + //We need to hook them with corresponding events + ManagerEventAction.Current.Init(); + } iSkipFrameRendering = false; iClosing = false; iCurrentClientSessionId = ""; @@ -295,9 +307,9 @@ //Reset our tree iTreeViewEvents.Nodes.Clear(); //Populate registered events - foreach (string key in EventActionManager.Current.Events.Keys) + foreach (string key in ManagerEventAction.Current.Events.Keys) { - Event e = EventActionManager.Current.Events[key]; + Event e = ManagerEventAction.Current.Events[key]; TreeNode eventNode = iTreeViewEvents.Nodes.Add(key,e.Name); eventNode.Tag = e; eventNode.Nodes.Add(key + ".Description", e.Description); @@ -2654,8 +2666,13 @@ private void buttonAddAction_Click(object sender, EventArgs e) { - Event ear = (Event)iTreeViewEvents.SelectedNode.Tag; - if (ear == null) + if (iTreeViewEvents.SelectedNode==null) + { + return; + } + + Event earEvent = (Event)iTreeViewEvents.SelectedNode.Tag; + if (earEvent == null) { //Must select event node return; @@ -2665,7 +2682,9 @@ DialogResult res = CodeProject.Dialog.DlgBox.ShowDialog(ea); if (res == DialogResult.OK) { - ear.Actions.Add(ea.Action); + earEvent.Actions.Add(ea.Action); + Properties.Settings.Default.Actions = ManagerEventAction.Current; + Properties.Settings.Default.Save(); SetupEvents(); } } diff -r 96f8b4dc4300 -r 1a0791daa243 Server/Properties/Settings.Designer.cs --- a/Server/Properties/Settings.Designer.cs Sat Jul 23 19:22:56 2016 +0200 +++ b/Server/Properties/Settings.Designer.cs Sun Jul 24 13:30:08 2016 +0200 @@ -189,5 +189,16 @@ this["CecReconnectToPowerTv"] = value; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + public global::SharpLib.Ear.ManagerEventAction Actions { + get { + return ((global::SharpLib.Ear.ManagerEventAction)(this["Actions"])); + } + set { + this["Actions"] = value; + } + } } } diff -r 96f8b4dc4300 -r 1a0791daa243 Server/Properties/Settings.settings --- a/Server/Properties/Settings.settings Sat Jul 23 19:22:56 2016 +0200 +++ b/Server/Properties/Settings.settings Sun Jul 24 13:30:08 2016 +0200 @@ -44,5 +44,8 @@ False + + + \ No newline at end of file diff -r 96f8b4dc4300 -r 1a0791daa243 Server/SharpDisplayManager.csproj --- a/Server/SharpDisplayManager.csproj Sat Jul 23 19:22:56 2016 +0200 +++ b/Server/SharpDisplayManager.csproj Sun Jul 24 13:30:08 2016 +0200 @@ -34,7 +34,7 @@ index.htm false 0 - 0.9.3.0 + 0.9.4.0 false true true @@ -221,7 +221,9 @@ Resources.resx True - + + Designer + diff -r 96f8b4dc4300 -r 1a0791daa243 SharpLibEar/Action.cs --- a/SharpLibEar/Action.cs Sat Jul 23 19:22:56 2016 +0200 +++ b/SharpLibEar/Action.cs Sun Jul 24 13:30:08 2016 +0200 @@ -2,12 +2,14 @@ using System; +using System.Collections.Generic; using System.Runtime.Serialization; using System.Threading; namespace SharpLib.Ear { [DataContract] + [KnownType("DerivedTypes")] public abstract class Action: IComparable { public abstract void Execute(); @@ -23,6 +25,12 @@ //Sort by action name return Utils.Reflection.GetAttribute(GetType()).Name.CompareTo(obj.GetType()); } + + private static IEnumerable DerivedTypes() + { + return SharpLib.Utils.Reflection.GetDerivedTypes(); + } + } diff -r 96f8b4dc4300 -r 1a0791daa243 SharpLibEar/ActionSleep.cs --- a/SharpLibEar/ActionSleep.cs Sat Jul 23 19:22:56 2016 +0200 +++ b/SharpLibEar/ActionSleep.cs Sun Jul 24 13:30:08 2016 +0200 @@ -13,6 +13,7 @@ [AttributeAction(Id = "Thread.Sleep", Name = "Sleep", Description = "Have the current thread sleep for the specified amount of milliseconds.")] public class ActionSleep : Action { + [DataMember] private readonly int iMillisecondsTimeout; public ActionSleep() diff -r 96f8b4dc4300 -r 1a0791daa243 SharpLibEar/Event.cs --- a/SharpLibEar/Event.cs Sat Jul 23 19:22:56 2016 +0200 +++ b/SharpLibEar/Event.cs Sun Jul 24 13:30:08 2016 +0200 @@ -10,10 +10,7 @@ [DataContract] public abstract class MEvent { - [DataMember] public string Name { get; protected set; } - - [DataMember] public string Description { get; protected set; } public abstract void Trigger(); @@ -22,11 +19,12 @@ [DataContract] public abstract class Event : MEvent { - public List Actions; + [DataMember] + public List Actions = new List(); protected Event() { - Actions = new List(); + } public override void Trigger() diff -r 96f8b4dc4300 -r 1a0791daa243 SharpLibEar/EventActionManager.cs --- a/SharpLibEar/EventActionManager.cs Sat Jul 23 19:22:56 2016 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,31 +0,0 @@ -// - - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Runtime.Serialization; - -namespace SharpLib.Ear -{ - [DataContract] - public class EventActionManager - { - public static EventActionManager Current = null; - public IDictionary ActionTypes; - public readonly IDictionary Events; - - public EventActionManager() - { - ActionTypes = Utils.Reflection.GetConcreteClassesDerivedFromByName(); - Events = Utils.Reflection.GetConcreteClassesInstanceDerivedFromByName(); - } - - public Event GetEvent() where T : class - { - return Events[typeof(T).Name]; - } - - } -} \ No newline at end of file diff -r 96f8b4dc4300 -r 1a0791daa243 SharpLibEar/ManagerEventAction.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SharpLibEar/ManagerEventAction.cs Sun Jul 24 13:30:08 2016 +0200 @@ -0,0 +1,70 @@ +// + + +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Reflection; +using System.Runtime.Serialization; +using SharpLib.Utils; + +namespace SharpLib.Ear +{ + [TypeConverter(typeof(TypeConverterJson))] + [DataContract] + public class ManagerEventAction + { + public static ManagerEventAction Current = null; + public IDictionary ActionTypes; + public IDictionary Events; + [DataMember] + public Dictionary> ActionsByEvents = new Dictionary>(); + + + public ManagerEventAction() + { + Init(); + } + + /// + /// + /// + public void Init() + { + //Create our list of supported actions + ActionTypes = Utils.Reflection.GetConcreteClassesDerivedFromByName(); + //Create our list or support events + Events = Utils.Reflection.GetConcreteClassesInstanceDerivedFromByName(); + + if (ActionsByEvents == null) + { + + ActionsByEvents = new Dictionary>(); + } + + //Hook in loaded actions with corresponding events + foreach (string key in Events.Keys) + { + Event e = Events[key]; + if (ActionsByEvents.ContainsKey(key)) + { + //We have actions for that event, hook them in then + e.Actions = ActionsByEvents[key]; + } + else + { + //We do not have actions for that event yet, create empty action list + e.Actions = new List(); + ActionsByEvents[key] = e.Actions; + } + } + } + + public Event GetEvent() where T : class + { + return Events[typeof(T).Name]; + } + + } +} \ No newline at end of file diff -r 96f8b4dc4300 -r 1a0791daa243 SharpLibEar/SharpLibEar.csproj --- a/SharpLibEar/SharpLibEar.csproj Sat Jul 23 19:22:56 2016 +0200 +++ b/SharpLibEar/SharpLibEar.csproj Sun Jul 24 13:30:08 2016 +0200 @@ -7,7 +7,7 @@ {84A9ED37-E6EA-4CBD-B995-B713F46EAAB0} Library Properties - SharpLibEar + SharpLib.Ear SharpLibEar v4.6 512 @@ -49,7 +49,7 @@ - + diff -r 96f8b4dc4300 -r 1a0791daa243 SharpLibUtils/Reflection.cs --- a/SharpLibUtils/Reflection.cs Sat Jul 23 19:22:56 2016 +0200 +++ b/SharpLibUtils/Reflection.cs Sun Jul 24 13:30:08 2016 +0200 @@ -113,6 +113,21 @@ return null; } + /// + /// + /// + /// + /// + /// + public static IEnumerable GetDerivedTypes() where T: class + { + var types = from t in Assembly.GetAssembly(typeof(T)).GetTypes() + where t.IsSubclassOf(typeof(T)) + select t; + + return types; + } + } } diff -r 96f8b4dc4300 -r 1a0791daa243 SharpLibUtils/SharpLibUtils.csproj --- a/SharpLibUtils/SharpLibUtils.csproj Sat Jul 23 19:22:56 2016 +0200 +++ b/SharpLibUtils/SharpLibUtils.csproj Sun Jul 24 13:30:08 2016 +0200 @@ -7,7 +7,7 @@ {AE897704-461D-4018-8336-2517988BF7AD} Library Properties - SharpLibUtils + SharpLib.Utils SharpLibUtils v4.6 512 @@ -32,6 +32,7 @@ + @@ -42,6 +43,7 @@ +