Kaynağa Gözat

添加注释

ntminer 6 yıl önce
ebeveyn
işleme
c811de198e

+ 5 - 5
src/AppModels/AppContext.cs

@@ -10,7 +10,7 @@ namespace NTMiner {
         public static ExtendedNotifyIcon NotifyIcon;
         public static Action<RemoteDesktopInput> RemoteDesktop;
 
-        private static readonly List<IMessageHandler> _contextHandlers = new List<IMessageHandler>();
+        private static readonly List<IHandlerId> _contextHandlers = new List<IHandlerId>();
 
         private AppContext() {
         }
@@ -19,7 +19,7 @@ namespace NTMiner {
         /// <summary>
         /// 命令窗口。使用该方法的代码行应将前两个参数放在第一行以方便vs查找引用时展示出参数信息
         /// </summary>
-        public static IMessageHandler CmdPath<TCmd>(string description, LogEnum logType, Action<TCmd> action)
+        public static IHandlerId CmdPath<TCmd>(string description, LogEnum logType, Action<TCmd> action)
             where TCmd : ICmd {
             return VirtualRoot.Path(description, logType, action).AddToCollection(_contextHandlers);
         }
@@ -27,20 +27,20 @@ namespace NTMiner {
         /// <summary>
         /// 事件响应
         /// </summary>
-        public static IMessageHandler EventPath<TEvent>(string description, LogEnum logType, Action<TEvent> action)
+        public static IHandlerId EventPath<TEvent>(string description, LogEnum logType, Action<TEvent> action)
             where TEvent : IEvent {
             return VirtualRoot.Path(description, logType, action).AddToCollection(_contextHandlers);
         }
 
         public static void Enable() {
             foreach (var handler in _contextHandlers) {
-                handler.HandlerId.IsEnabled = true;
+                handler.IsEnabled = true;
             }
         }
 
         public static void Disable() {
             foreach (var handler in _contextHandlers) {
-                handler.HandlerId.IsEnabled = false;
+                handler.IsEnabled = false;
             }
         }
         #endregion

+ 3 - 2
src/AppModels/Vms/StartStopMineButtonViewModel.cs

@@ -1,4 +1,5 @@
-using System.Windows.Input;
+using NTMiner.Bus;
+using System.Windows.Input;
 
 namespace NTMiner.Vms {
     public class StartStopMineButtonViewModel : ViewModelBase {
@@ -40,7 +41,7 @@ namespace NTMiner.Vms {
             if (IsAutoStart && !this.MinerProfile.IsMining) {
                 this.MinerProfile.IsMining = true;
                 int n = MinerProfile.AutoStartDelaySeconds;
-                Bus.IMessageHandler handler = null;
+                IHandlerId handler = null;
                 handler = AppContext.EventPath<Per1SecondEvent>("挖矿倒计时", LogEnum.None,
                 action: message => {
                     if (NTMinerRoot.IsAutoStartCanceled) {

+ 2 - 2
src/NTMiner/INTMinerRoot.cs

@@ -11,8 +11,8 @@ using System.Collections.Generic;
 
 namespace NTMiner {
     public interface INTMinerRoot {
-        IMessageHandler ServerContextCmdPath<TCmd>(string description, LogEnum logType, Action<TCmd> action) where TCmd : ICmd;
-        IMessageHandler ServerContextEventPath<TEvent>(string description, LogEnum logType, Action<TEvent> action) where TEvent : IEvent;
+        IHandlerId ServerContextCmdPath<TCmd>(string description, LogEnum logType, Action<TCmd> action) where TCmd : ICmd;
+        IHandlerId ServerContextEventPath<TEvent>(string description, LogEnum logType, Action<TEvent> action) where TEvent : IEvent;
         
         void ReInitMinerProfile();
 

+ 3 - 3
src/NTMiner/NTMinerRoot.cs

@@ -26,12 +26,12 @@ using System.Threading.Tasks;
 
 namespace NTMiner {
     public partial class NTMinerRoot : INTMinerRoot {
-        private readonly List<IMessageHandler> _serverContextHandlers = new List<IMessageHandler>();
+        private readonly List<IHandlerId> _serverContextHandlers = new List<IHandlerId>();
 
         /// <summary>
         /// 命令窗口。使用该方法的代码行应将前两个参数放在第一行以方便vs查找引用时展示出参数信息
         /// </summary>
-        public IMessageHandler ServerContextCmdPath<TCmd>(string description, LogEnum logType, Action<TCmd> action)
+        public IHandlerId ServerContextCmdPath<TCmd>(string description, LogEnum logType, Action<TCmd> action)
             where TCmd : ICmd {
             return VirtualRoot.Path(description, logType, action).AddToCollection(_serverContextHandlers);
         }
@@ -39,7 +39,7 @@ namespace NTMiner {
         /// <summary>
         /// 事件响应
         /// </summary>
-        public IMessageHandler ServerContextEventPath<TEvent>(string description, LogEnum logType, Action<TEvent> action)
+        public IHandlerId ServerContextEventPath<TEvent>(string description, LogEnum logType, Action<TEvent> action)
             where TEvent : IEvent {
             return VirtualRoot.Path(description, logType, action).AddToCollection(_serverContextHandlers);
         }

+ 26 - 5
src/NTMinerBus/Bus/DelegateHandler`1.cs

@@ -1,26 +1,47 @@
 using System;
+using System.ComponentModel;
 
 namespace NTMiner.Bus {
     /// <summary>
     /// 委托处理器,该处理器将处理逻辑委托给构造时传入的委托。
     /// </summary>
     /// <typeparam name="TMessage"></typeparam>
-    public class DelegateHandler<TMessage> : IMessageHandler {
+    public class DelegateHandler<TMessage> : IHandlerId, INotifyPropertyChanged {
         private readonly Action<TMessage> _action;
+        private bool _isEnabled;
 
-        public IHandlerId HandlerId { get; private set; }
+        public event PropertyChangedEventHandler PropertyChanged;
 
-        public DelegateHandler(IHandlerId handlerId, Action<TMessage> action) {
-            this.HandlerId = handlerId;
+        public DelegateHandler(Type messageType, Type location, string description, LogEnum logType, Action<TMessage> action) {
+            this.IsEnabled = true;
+            string path = $"{location.FullName}[{messageType.FullName}]";
+            MessageType = messageType;
+            Location = location;
+            HandlerPath = path;
+            Description = description;
+            LogType = logType;
             _action = action;
         }
 
+        public Type MessageType { get; set; }
+        public Type Location { get; set; }
+        public string HandlerPath { get; set; }
+        public LogEnum LogType { get; set; }
+        public string Description { get; set; }
+        public bool IsEnabled {
+            get => _isEnabled;
+            set {
+                _isEnabled = value;
+                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsEnabled)));
+            }
+        }
+
         public void Handle(TMessage message) {
             try {
                 _action?.Invoke(message);
             }
             catch (Exception e) {
-                Logger.ErrorDebugLine(HandlerId.HandlerPath + ":" + e.Message, e);
+                Logger.ErrorDebugLine(HandlerPath + ":" + e.Message, e);
                 throw;
             }
         }

+ 3 - 0
src/NTMinerBus/Bus/IHandlerId.cs

@@ -1,6 +1,9 @@
 using System;
 
 namespace NTMiner.Bus {
+    /// <summary>
+    /// 处理器标识
+    /// </summary>
     public interface IHandlerId {
         Type MessageType { get; }
         bool IsEnabled { get; set; }

+ 1 - 1
src/NTMinerBus/Bus/IMessageDispatcher.cs

@@ -8,7 +8,7 @@ namespace NTMiner.Bus {
 
         void Register<TMessage>(DelegateHandler<TMessage> handler);
 
-        void UnRegister(IMessageHandler handler);
+        void UnRegister(IHandlerId handlerId);
 
         IEnumerable<IHandlerId> HandlerIds { get; }
         event Action<IHandlerId> HandlerIdAdded;

+ 0 - 8
src/NTMinerBus/Bus/IMessageHandler.cs

@@ -1,8 +0,0 @@
-namespace NTMiner.Bus {
-    /// <summary>
-    /// 处理器
-    /// </summary>
-    public interface IMessageHandler {
-        IHandlerId HandlerId { get; }
-    }
-}

+ 12 - 13
src/NTMinerBus/Bus/MessageDispatcher.cs

@@ -29,18 +29,18 @@ namespace NTMiner.Bus {
                 var messageHandlers = _handlers[messageType].ToArray();
                 foreach (var messageHandler in messageHandlers) {
                     var tMessageHandler = (DelegateHandler<TMessage>)messageHandler;
-                    if (!tMessageHandler.HandlerId.IsEnabled) {
+                    if (!tMessageHandler.IsEnabled) {
                         continue;
                     }
                     var evtArgs = new MessageDispatchEventArgs(message, messageHandler.GetType(), messageHandler);
-                    switch (tMessageHandler.HandlerId.LogType) {
+                    switch (tMessageHandler.LogType) {
                         case LogEnum.DevConsole:
                             if (DevMode.IsDevMode) {
-                                Write.DevDebug($"({messageType.Name})->({tMessageHandler.HandlerId.Location.Name}){tMessageHandler.HandlerId.Description}");
+                                Write.DevDebug($"({messageType.Name})->({tMessageHandler.Location.Name}){tMessageHandler.Description}");
                             }
                             break;
                         case LogEnum.Log:
-                            Logger.InfoDebugLine($"({messageType.Name})->({tMessageHandler.HandlerId.Location.Name}){tMessageHandler.HandlerId.Description}");
+                            Logger.InfoDebugLine($"({messageType.Name})->({tMessageHandler.Location.Name}){tMessageHandler.Description}");
                             break;
                         case LogEnum.None:
                         default:
@@ -61,7 +61,7 @@ namespace NTMiner.Bus {
             lock (_locker) {
                 var keyType = typeof(TMessage);
 
-                var handlerId = handler.HandlerId;
+                var handlerId = handler;
                 if (!_paths.ContainsKey(handlerId.HandlerPath)) {
                     _paths.Add(handlerId.HandlerPath, new List<IHandlerId> { handlerId });
                 }
@@ -86,27 +86,26 @@ namespace NTMiner.Bus {
                     var registeredHandlers = new List<dynamic> { handler };
                     _handlers.Add(keyType, registeredHandlers);
                 }
-                _handlerIds.Add(handler.HandlerId);
-                HandlerIdAdded?.Invoke(handler.HandlerId);
+                _handlerIds.Add(handlerId);
+                HandlerIdAdded?.Invoke(handlerId);
             }
         }
 
-        public void UnRegister(IMessageHandler handler) {
-            if (handler == null) {
+        public void UnRegister(IHandlerId handlerId) {
+            if (handlerId == null) {
                 return;
             }
             lock (_locker) {
-                var handlerId = handler.HandlerId;
                 _paths.Remove(handlerId.HandlerPath);
                 var keyType = handlerId.MessageType;
                 if (_handlers.ContainsKey(keyType) &&
                     _handlers[keyType] != null &&
                     _handlers[keyType].Count > 0 &&
-                    _handlers[keyType].Contains(handler)) {
-                    _handlers[keyType].Remove(handler);
+                    _handlers[keyType].Contains(handlerId)) {
+                    _handlers[keyType].Remove(handlerId);
                     _handlerIds.Remove(handlerId);
                     HandlerIdRemoved?.Invoke(handlerId);
-                    Write.DevDebug("拆除路径" + handler.HandlerId.HandlerPath);
+                    Write.DevDebug("拆除路径" + handlerId.HandlerPath);
                 }
             }
         }

+ 2 - 2
src/NTMinerBus/Bus/MessageDispatcherExtensions.cs

@@ -2,11 +2,11 @@
 
 namespace NTMiner.Bus {
     public static class MessageDispatcherExtensions {
-        public static DelegateHandler<TMessage> Register<TMessage>(this IMessageDispatcher dispatcher, IHandlerId handlerId, Action<TMessage> action) {
+        public static DelegateHandler<TMessage> Register<TMessage>(this IMessageDispatcher dispatcher, Type messageType, Type location, string description, LogEnum logType, Action<TMessage> action) {
             if (action == null) {
                 throw new ArgumentNullException(nameof(action));
             }
-            DelegateHandler<TMessage> handler = new DelegateHandler<TMessage>(handlerId, action);
+            DelegateHandler<TMessage> handler = new DelegateHandler<TMessage>(messageType, location, description, logType, action);
             dispatcher.Register(handler);
             return handler;
         }

+ 1 - 1
src/NTMinerBus/Bus/MessageHandlerExtensions.cs

@@ -3,7 +3,7 @@
 namespace NTMiner.Bus {
     public static class MessageHandlerExtensions {
 
-        public static IMessageHandler AddToCollection(this IMessageHandler handler, List<IMessageHandler> handlers) {
+        public static IHandlerId AddToCollection(this IHandlerId handler, List<IHandlerId> handlers) {
             if (!handlers.Contains(handler)) {
                 handlers.Add(handler);
             }

+ 0 - 1
src/NTMinerBus/NTMinerBus.csproj

@@ -44,7 +44,6 @@
     <Compile Include="Bus\IBus.cs" />
     <Compile Include="Bus\ICmd.cs" />
     <Compile Include="Bus\ICmdBus.cs" />
-    <Compile Include="Bus\IMessageHandler.cs" />
     <Compile Include="Bus\IEvent.cs" />
     <Compile Include="Bus\IEventBus.cs" />
     <Compile Include="Bus\IHandlerId.cs" />

+ 0 - 3
src/NTMinerDaemon/NTMinerDaemon.csproj

@@ -89,9 +89,6 @@
     <Compile Include="..\MinerClient\RemoteDesktopEnabler\Rdp.cs">
       <Link>RemoteDesktopEnabler\Rdp.cs</Link>
     </Compile>
-    <Compile Include="..\NTMinerlib\HandlerId.cs">
-      <Link>HandlerId.cs</Link>
-    </Compile>
     <Compile Include="..\NTMinerlib\Messages.Timing.cs">
       <Link>Messages.Timing.cs</Link>
     </Compile>

+ 5 - 5
src/NTMinerWpf/WindowExtension.cs

@@ -35,9 +35,9 @@ namespace NTMiner {
             if (window.Resources == null) {
                 window.Resources = new ResourceDictionary();
             }
-            List<IMessageHandler> contextHandlers = (List<IMessageHandler>)window.Resources["ntminer_contextHandlers"];
+            List<IHandlerId> contextHandlers = (List<IHandlerId>)window.Resources["ntminer_contextHandlers"];
             if (contextHandlers == null) {
-                contextHandlers = new List<IMessageHandler>();
+                contextHandlers = new List<IHandlerId>();
                 window.Resources.Add("ntminer_contextHandlers", contextHandlers);
                 window.Closed += UiElement_Closed;
             }
@@ -55,9 +55,9 @@ namespace NTMiner {
             if (window.Resources == null) {
                 window.Resources = new ResourceDictionary();
             }
-            List<IMessageHandler> contextHandlers = (List<IMessageHandler>)window.Resources["ntminer_contextHandlers"];
+            List<IHandlerId> contextHandlers = (List<IHandlerId>)window.Resources["ntminer_contextHandlers"];
             if (contextHandlers == null) {
-                contextHandlers = new List<IMessageHandler>();
+                contextHandlers = new List<IHandlerId>();
                 window.Resources.Add("ntminer_contextHandlers", contextHandlers);
                 window.Closed += UiElement_Closed; ;
             }
@@ -66,7 +66,7 @@ namespace NTMiner {
 
         private static void UiElement_Closed(object sender, EventArgs e) {
             Window uiElement = (Window)sender;
-            List<IMessageHandler> contextHandlers = (List<IMessageHandler>)uiElement.Resources["ntminer_contextHandlers"];
+            List<IHandlerId> contextHandlers = (List<IHandlerId>)uiElement.Resources["ntminer_contextHandlers"];
             foreach (var handler in contextHandlers) {
                 VirtualRoot.UnPath(handler);
             }

+ 0 - 41
src/NTMinerlib/HandlerId.cs

@@ -1,41 +0,0 @@
-using NTMiner.Bus;
-using System;
-using System.ComponentModel;
-
-namespace NTMiner {
-    public partial class HandlerId : IHandlerId, INotifyPropertyChanged {
-        private bool _isEnabled;
-
-        public event PropertyChangedEventHandler PropertyChanged;
-
-        public static IHandlerId Create(Type messageType, Type location, string description, LogEnum logType) {
-            string path = $"{location.FullName}[{messageType.FullName}]";
-            var item = new HandlerId {
-                MessageType = messageType,
-                Location = location,
-                HandlerPath = path,
-                Description = description,
-                LogType = logType
-            };
-            return item;
-        }
-
-        private HandlerId() {
-            this.IsEnabled = true;
-        }
-
-        public Type MessageType { get; set; }
-        [LiteDB.BsonIgnore]
-        public Type Location { get; set; }
-        public string HandlerPath { get; set; }
-        public LogEnum LogType { get; set; }
-        public string Description { get; set; }
-        public bool IsEnabled {
-            get => _isEnabled;
-            set {
-                _isEnabled = value;
-                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsEnabled)));
-            }
-        }
-    }
-}

+ 0 - 1
src/NTMinerlib/NTMinerlib.csproj

@@ -60,7 +60,6 @@
     <Compile Include="NTMinerRegistry.cs" />
     <Compile Include="Repositories\IJsonReadOnlyRepository.cs" />
     <Compile Include="SortNumberComparer.cs" />
-    <Compile Include="HandlerId.cs" />
     <Compile Include="EntityExtensions.cs" />
     <Compile Include="EnumExtension.cs" />
     <Compile Include="NTMinerException.cs" />

+ 2 - 3
src/NTMinerlib/VirtualRoot.partials.cs

@@ -29,8 +29,7 @@ namespace NTMiner {
             StackTrace ss = new StackTrace(false);
             // 0是Path,1是Window或On,2是当地
             Type location = ss.GetFrame(2).GetMethod().DeclaringType;
-            IHandlerId handlerId = HandlerId.Create(typeof(TMessage), location, description, logType);
-            return SMessageDispatcher.Register(handlerId, action);
+            return SMessageDispatcher.Register(typeof(TMessage), location, description, logType, action);
         }
 
         /// <summary>
@@ -70,7 +69,7 @@ namespace NTMiner {
         }
 
         // 拆除消息(命令或事件)的运动路径
-        public static void UnPath(IMessageHandler handler) {
+        public static void UnPath(IHandlerId handler) {
             if (handler == null) {
                 return;
             }