初始化上传
This commit is contained in:
80
常用工具集/Utility/EventBus/ActionDelegatedEventHandler.cs
Normal file
80
常用工具集/Utility/EventBus/ActionDelegatedEventHandler.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
/****************************************************************
|
||||
** 文件名: ActionDelegatedEventHandler.cs
|
||||
** 主要类: ActionDelegatedEventHandler类
|
||||
** Copyright (c) 章为忠
|
||||
** 创建人:
|
||||
** 日 期: 2017.3.10
|
||||
** 修改人:
|
||||
** 日 期:
|
||||
** 修改内容:
|
||||
** 描 述:
|
||||
** 版 本:
|
||||
** 备 注:
|
||||
****************************************************************/
|
||||
using System;
|
||||
|
||||
namespace Weiz.EventBus.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the event handler which delegates the event handling process to
|
||||
/// a given <see cref="Action{T}"/> delegated method.
|
||||
/// </summary>
|
||||
/// <typeparam name="TEvent">The type of the event to be handled by current handler.</typeparam>
|
||||
public sealed class ActionDelegatedEventHandler<TEvent> : IEventHandler<TEvent>
|
||||
where TEvent : IEvent
|
||||
{
|
||||
#region Private Fields
|
||||
private readonly Action<TEvent> action;
|
||||
#endregion
|
||||
|
||||
#region Ctor
|
||||
/// <summary>
|
||||
|
||||
/// Initializes a new instance of <c>ActionDelegatedEventHandler{TEvent}</c> class.
|
||||
|
||||
/// </summary>
|
||||
|
||||
/// <param name="action">The <see cref="Action{T}"/> instance that delegates the event handling process.</param>
|
||||
|
||||
public ActionDelegatedEventHandler(Action<TEvent> action)
|
||||
{
|
||||
this.action = action;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
/// <summary>
|
||||
/// Returns a <see cref="Boolean"/> value which indicates whether the current
|
||||
/// <c>ActionDelegatedEventHandler{T}</c> equals to the given object.
|
||||
/// </summary>
|
||||
/// <param name="obj">The <see cref="Object"/> which is used to compare to
|
||||
/// the current <c>ActionDelegatedEventHandler{T}</c> instance.</param>
|
||||
/// <returns>If the given object equals to the current <c>ActionDelegatedEventHandler{T}</c>
|
||||
/// instance, returns true, otherwise, false.</returns>
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (ReferenceEquals(this, obj))
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
ActionDelegatedEventHandler<TEvent> other = obj as ActionDelegatedEventHandler<TEvent>;
|
||||
if (other == null)
|
||||
return false;
|
||||
return Delegate.Equals(this.action, other.action);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IHandler<TDomainEvent> Members
|
||||
/// <summary>
|
||||
/// Handles the specified message.
|
||||
/// </summary>
|
||||
/// <param name="message">The message to be handled.</param>
|
||||
public void Handle(TEvent message)
|
||||
{
|
||||
action(message);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
27
常用工具集/Utility/EventBus/ConnectErrorEvent.cs
Normal file
27
常用工具集/Utility/EventBus/ConnectErrorEvent.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Weiz.EventBus.Core;
|
||||
|
||||
namespace MES.Utility.EventBus
|
||||
{
|
||||
public class ConnectErrorEvent : IEvent
|
||||
{
|
||||
public ConnectErrorEvent() { }
|
||||
public ConnectErrorEvent(string message)
|
||||
{
|
||||
Time = DateTime.Now;
|
||||
Message = message;
|
||||
}
|
||||
public ConnectErrorEvent(DateTime time, string message)
|
||||
{
|
||||
Time = time;
|
||||
Message = message;
|
||||
}
|
||||
public DateTime Time { get; set; }
|
||||
|
||||
public string Message { get; set; }
|
||||
}
|
||||
}
|
||||
226
常用工具集/Utility/EventBus/EventBus.cs
Normal file
226
常用工具集/Utility/EventBus/EventBus.cs
Normal file
@@ -0,0 +1,226 @@
|
||||
/****************************************************************
|
||||
** 文件名: EventBus.cs
|
||||
** 主要类: EventBus类
|
||||
** Copyright (c) 章为忠
|
||||
** 创建人:
|
||||
** 日 期: 2017.3.10
|
||||
** 修改人:
|
||||
** 日 期:
|
||||
** 修改内容:
|
||||
** 描 述:
|
||||
** 版 本:
|
||||
** 备 注:
|
||||
****************************************************************/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace Weiz.EventBus.Core
|
||||
{
|
||||
public class EventBus
|
||||
{
|
||||
/// <summary>
|
||||
/// 事件总线对象
|
||||
/// </summary>
|
||||
private static EventBus _eventBus = null;
|
||||
|
||||
/// <summary>
|
||||
/// 领域模型事件句柄字典,用于存储领域模型的句柄
|
||||
/// </summary>
|
||||
private static Dictionary<Type, List<object>> _dicEventHandler = new Dictionary<Type, List<object>>();
|
||||
|
||||
/// <summary>
|
||||
/// 附加领域模型处理句柄时,锁住
|
||||
/// </summary>
|
||||
private readonly object _syncObject = new object();
|
||||
|
||||
/// <summary>
|
||||
/// 单例事件总线
|
||||
/// </summary>
|
||||
public static EventBus Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
return _eventBus ?? (_eventBus = new EventBus());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过XML文件初始化事件总线,订阅信自在XML里配置
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static EventBus InstanceForXml()
|
||||
{
|
||||
if (_eventBus == null)
|
||||
{
|
||||
XElement root = XElement.Load(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "EventBus.xml"));
|
||||
foreach (var evt in root.Elements("Event"))
|
||||
{
|
||||
List<object> handlers = new List<object>();
|
||||
|
||||
Type publishEventType = Type.GetType(evt.Element("PublishEvent").Value);
|
||||
foreach (var subscritedEvt in evt.Elements("SubscribedEvents"))
|
||||
{
|
||||
foreach (var concreteEvt in subscritedEvt.Elements("SubscribedEvent"))
|
||||
{
|
||||
var type = Type.GetType(concreteEvt.Value);
|
||||
handlers.Add(System.Activator.CreateInstance(type));
|
||||
|
||||
}
|
||||
_dicEventHandler[publishEventType] = handlers;
|
||||
}
|
||||
}
|
||||
|
||||
_eventBus = new EventBus();
|
||||
}
|
||||
return _eventBus;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
private readonly Func<object, object, bool> eventHandlerEquals = (o1, o2) =>
|
||||
{
|
||||
var o1Type = o1.GetType();
|
||||
var o2Type = o2.GetType();
|
||||
return o1Type == o2Type;
|
||||
};
|
||||
|
||||
#region 订阅事件
|
||||
|
||||
public void Subscribe<TEvent>(IEventHandler<TEvent> eventHandler) where TEvent : IEvent
|
||||
{
|
||||
//同步锁
|
||||
lock (_syncObject)
|
||||
{
|
||||
//获取领域模型的类型
|
||||
var eventType = typeof(TEvent);
|
||||
//如果此领域类型在事件总线中已注册过
|
||||
if (_dicEventHandler.ContainsKey(eventType))
|
||||
{
|
||||
var handlers = _dicEventHandler[eventType];
|
||||
if (handlers != null)
|
||||
{
|
||||
handlers.Add(eventHandler);
|
||||
}
|
||||
else
|
||||
{
|
||||
handlers = new List<object>
|
||||
{
|
||||
eventHandler
|
||||
};
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_dicEventHandler.Add(eventType, new List<object> { eventHandler });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 订阅事件实体
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="subTypeList"></param>
|
||||
public void Subscribe<TEvent>(Action<TEvent> eventHandlerFunc)
|
||||
where TEvent : IEvent
|
||||
{
|
||||
Subscribe<TEvent>(new ActionDelegatedEventHandler<TEvent>(eventHandlerFunc));
|
||||
}
|
||||
public void Subscribe<TEvent>(IEnumerable<IEventHandler<TEvent>> eventHandlers)
|
||||
where TEvent : IEvent
|
||||
{
|
||||
foreach (var eventHandler in eventHandlers)
|
||||
{
|
||||
Subscribe<TEvent>(eventHandler);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 发布事件
|
||||
|
||||
public void Publish<TEvent>(TEvent tEvent) where TEvent : IEvent
|
||||
{
|
||||
var eventType = typeof(TEvent);
|
||||
if (_dicEventHandler.ContainsKey(eventType) && _dicEventHandler[eventType] != null &&
|
||||
_dicEventHandler[eventType].Count > 0)
|
||||
{
|
||||
var handlers = _dicEventHandler[eventType];
|
||||
try
|
||||
{
|
||||
foreach (var handler in handlers)
|
||||
{
|
||||
var eventHandler = handler as IEventHandler<TEvent>;
|
||||
eventHandler.Handle(tEvent);
|
||||
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public void Publish<TEvent>(TEvent tEvent, Action<TEvent, bool, Exception> callback) where TEvent : IEvent
|
||||
{
|
||||
var eventType = typeof(TEvent);
|
||||
if (_dicEventHandler.ContainsKey(eventType) && _dicEventHandler[eventType] != null &&
|
||||
_dicEventHandler[eventType].Count > 0)
|
||||
{
|
||||
var handlers = _dicEventHandler[eventType];
|
||||
try
|
||||
{
|
||||
foreach (var handler in handlers)
|
||||
{
|
||||
var eventHandler = handler as IEventHandler<TEvent>;
|
||||
eventHandler.Handle(tEvent);
|
||||
callback(tEvent, true, null);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
callback(tEvent, false, ex);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
callback(tEvent, false, null);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 取消订阅
|
||||
/// <summary>
|
||||
/// 取消订阅事件
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="subType"></param>
|
||||
public void Unsubscribe<TEvent>(IEventHandler<TEvent> eventHandler) where TEvent : IEvent
|
||||
{
|
||||
lock (_syncObject)
|
||||
{
|
||||
var eventType = typeof(TEvent);
|
||||
if (_dicEventHandler.ContainsKey(eventType))
|
||||
{
|
||||
var handlers = _dicEventHandler[eventType];
|
||||
if (handlers != null
|
||||
&& handlers.Exists(deh => eventHandlerEquals(deh, eventHandler)))
|
||||
{
|
||||
var handlerToRemove = handlers.First(deh => eventHandlerEquals(deh, eventHandler));
|
||||
handlers.Remove(handlerToRemove);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
17
常用工具集/Utility/EventBus/FileDownloadEvent.cs
Normal file
17
常用工具集/Utility/EventBus/FileDownloadEvent.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Weiz.EventBus.Core;
|
||||
|
||||
namespace MaiFu.Utility.EventBus
|
||||
{
|
||||
public class FileDownloadEvent : IEvent
|
||||
{
|
||||
public int What;
|
||||
public long TotalLength { get; set; }
|
||||
|
||||
public long Current { get; set; }
|
||||
}
|
||||
}
|
||||
20
常用工具集/Utility/EventBus/IEvent.cs
Normal file
20
常用工具集/Utility/EventBus/IEvent.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
/****************************************************************
|
||||
** 文件名: IEvent.cs
|
||||
** 主要类: IEvent
|
||||
** Copyright (c) 章为忠
|
||||
** 创建人:
|
||||
** 日 期: 2017.3.10
|
||||
** 修改人:
|
||||
** 日 期:
|
||||
** 修改内容:
|
||||
** 描 述:
|
||||
** 版 本:
|
||||
** 备 注:
|
||||
****************************************************************/
|
||||
|
||||
namespace Weiz.EventBus.Core
|
||||
{
|
||||
public interface IEvent
|
||||
{
|
||||
}
|
||||
}
|
||||
30
常用工具集/Utility/EventBus/IEventHandler.cs
Normal file
30
常用工具集/Utility/EventBus/IEventHandler.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
/****************************************************************
|
||||
** 文件名: IEventHandler.cs
|
||||
** 主要类: IEventHandler
|
||||
** Copyright (c) 章为忠
|
||||
** 创建人:
|
||||
** 日 期: 2017.3.10
|
||||
** 修改人:
|
||||
** 日 期:
|
||||
** 修改内容:
|
||||
** 描 述:
|
||||
** 版 本:
|
||||
** 备 注:
|
||||
****************************************************************/
|
||||
|
||||
namespace Weiz.EventBus.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// 事件处理接口
|
||||
/// </summary>
|
||||
/// <typeparam name="TEvent">继承IEvent对象的事件源对象</typeparam>
|
||||
public interface IEventHandler<TEvent> where TEvent : IEvent
|
||||
{
|
||||
/// <summary>
|
||||
/// 处理程序
|
||||
/// </summary>
|
||||
/// <param name="evt"></param>
|
||||
void Handle(TEvent evt);
|
||||
|
||||
}
|
||||
}
|
||||
18
常用工具集/Utility/EventBus/KeyBordHookEvent.cs
Normal file
18
常用工具集/Utility/EventBus/KeyBordHookEvent.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using Weiz.EventBus.Core;
|
||||
|
||||
namespace MES.Utility.EventBus
|
||||
{
|
||||
public class KeyBordHookEvent : IEvent
|
||||
{
|
||||
public KeyBordHookEvent(string message)
|
||||
{
|
||||
Time = DateTime.Now;
|
||||
Message = message;
|
||||
|
||||
}
|
||||
public DateTime Time { get; set; }
|
||||
|
||||
public string Message { get; set; }
|
||||
}
|
||||
}
|
||||
27
常用工具集/Utility/EventBus/MessageEvent.cs
Normal file
27
常用工具集/Utility/EventBus/MessageEvent.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Weiz.EventBus.Core;
|
||||
|
||||
namespace MES.Utility.EventBus
|
||||
{
|
||||
public class MessageEvent : IEvent
|
||||
{
|
||||
public MessageEvent() { }
|
||||
public MessageEvent(string message)
|
||||
{
|
||||
Time = DateTime.Now;
|
||||
Message = message;
|
||||
}
|
||||
public MessageEvent(DateTime time, string message)
|
||||
{
|
||||
Time = time;
|
||||
Message = message;
|
||||
}
|
||||
public DateTime Time { get; set; }
|
||||
|
||||
public string Message { get; set; }
|
||||
}
|
||||
}
|
||||
31
常用工具集/Utility/EventBus/ReciveMessageEvent.cs
Normal file
31
常用工具集/Utility/EventBus/ReciveMessageEvent.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using MES.Utility.Core;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Weiz.EventBus.Core;
|
||||
|
||||
namespace MES.Utility.EventBus
|
||||
{
|
||||
public class ReciveMessageEvent : IEvent
|
||||
{
|
||||
public ReciveMessageEvent() { }
|
||||
public ReciveMessageEvent(byte[] data)
|
||||
{
|
||||
Data = data;
|
||||
}
|
||||
|
||||
public byte[] Data { get; set; }
|
||||
|
||||
public string DataHexString
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Data == null)
|
||||
return string.Empty;
|
||||
return Data.Select(it => Convert.ToString(it, 16).PadLeft(2, '0').ToUpper()).ToList().GetStrArray(" ");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
27
常用工具集/Utility/EventBus/ScanEvent.cs
Normal file
27
常用工具集/Utility/EventBus/ScanEvent.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Weiz.EventBus.Core;
|
||||
|
||||
namespace MES.Utility.EventBus
|
||||
{
|
||||
public class ScanEvent : IEvent
|
||||
{
|
||||
public ScanEvent() { }
|
||||
public ScanEvent(string message)
|
||||
{
|
||||
Time = DateTime.Now;
|
||||
Message = message;
|
||||
}
|
||||
public ScanEvent(DateTime time, string message)
|
||||
{
|
||||
Time = time;
|
||||
Message = message;
|
||||
}
|
||||
public DateTime Time { get; set; }
|
||||
|
||||
public string Message { get; set; }
|
||||
}
|
||||
}
|
||||
27
常用工具集/Utility/EventBus/ScanResultEvent.cs
Normal file
27
常用工具集/Utility/EventBus/ScanResultEvent.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Weiz.EventBus.Core;
|
||||
|
||||
namespace MES.Utility.EventBus
|
||||
{
|
||||
public class ScanResultEvent : IEvent
|
||||
{
|
||||
public ScanResultEvent() { }
|
||||
public ScanResultEvent(string message)
|
||||
{
|
||||
Time = DateTime.Now;
|
||||
Message = message;
|
||||
}
|
||||
public ScanResultEvent(DateTime time, string message)
|
||||
{
|
||||
Time = time;
|
||||
Message = message;
|
||||
}
|
||||
public DateTime Time { get; set; }
|
||||
|
||||
public string Message { get; set; }
|
||||
}
|
||||
}
|
||||
31
常用工具集/Utility/EventBus/SendMessageEvent.cs
Normal file
31
常用工具集/Utility/EventBus/SendMessageEvent.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using MES.Utility.Core;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Weiz.EventBus.Core;
|
||||
|
||||
namespace MES.Utility.EventBus
|
||||
{
|
||||
public class SendMessageEvent : IEvent
|
||||
{
|
||||
public SendMessageEvent() { }
|
||||
public SendMessageEvent(byte[] data)
|
||||
{
|
||||
Data = data;
|
||||
}
|
||||
|
||||
public byte[] Data { get; set; }
|
||||
|
||||
public string DataHexString
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Data == null)
|
||||
return string.Empty;
|
||||
return Data.Select(it => Convert.ToString(it, 16).PadLeft(2, '0').ToUpper()).ToList().GetStrArray(" ");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user