From cf5fac91337ceaae005a3bfa66255c7743bd024a Mon Sep 17 00:00:00 2001
From: applelon <380149513@qq.com>
Date: Tue, 19 Apr 2022 11:27:44 +0800
Subject: [PATCH] =?UTF-8?q?1=E3=80=81IOC=E5=AE=8C=E6=88=90=EF=BC=9B=202?=
=?UTF-8?q?=E3=80=81=E5=8A=A8=E6=80=81=E5=8A=A0=E8=BD=BD=E5=AE=8C=E6=88=90?=
=?UTF-8?q?=EF=BC=9B=203=E3=80=81=E4=BA=8B=E4=BB=B6=E6=80=BB=E7=BA=BF?=
=?UTF-8?q?=E5=AE=8C=E6=88=90?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.editorconfig | 16 +++
.../BPASmartClient.Business.csproj | 8 +-
BPASmartClient.Business/Class1.cs | 7 -
BPASmartClient.Business/ConfigMgr.cs | 54 ++++++++
BPASmartClient.Business/DeviceMgr.cs | 67 +++++++++
BPASmartClient.Business/IPlugin.cs | 16 +++
BPASmartClient.Business/Plugin.cs | 37 +++++
.../BPASmartClient.DRCoffee.csproj | 5 +
BPASmartClient.DRCoffee/CoffeeMachine.cs | 35 +++--
BPASmartClient.DRCoffee/CommandHandler.cs | 2 +-
BPASmartClient.DRCoffee/MorkCStatus.cs | 2 +-
.../BPASmartClient.Device.csproj | 4 +
BPASmartClient.Device/BaseDevice.cs | 41 ++++++
BPASmartClient.Device/IDevice.cs | 13 +-
.../BPASmartClient.EventBus.csproj | 13 ++
BPASmartClient.EventBus/EventBus.cs | 128 ++++++++++++++++++
BPASmartClient.EventBus/IEvent.cs | 10 ++
BPASmartClient.EventBus/IEventExtends.cs | 38 ++++++
BPASmartClient.Helper/Json.cs | 4 +-
BPASmartClient.Helper/LocaPath.cs | 5 +-
BPASmartClient.Helper/TextHelper.cs | 4 +-
BPASmartClient.Helper/XmlUtil.cs | 85 ++++++++++++
.../BPASmartClient.Lebai.csproj | 4 +-
BPASmartClient.Lebai/LebaiRobot.cs | 11 ++
.../BPASmartClient.Model.csproj | 13 ++
BPASmartClient.Model/BaseEvent.cs | 14 ++
BPASmartClient.Model/Demo_MakeCoffeeEvent.cs | 14 ++
BPASmartClient.Model/DeviceConfigModel.cs | 27 ++++
.../BPASmartClient.MorkT.csproj | 4 +
BPASmartClient.MorkT/Device_MorkT.cs | 15 +-
BPASmartClient.Peripheral/BasePeripheral.cs | 9 +-
.../{IBasePeripheral.cs => IPeripheral.cs} | 10 +-
.../BPASmartClient.SCChip.csproj | 1 +
BPASmartClient.SCChip/ICChipMachine.cs | 29 ++--
BPASmartClient/BPASmartClient.csproj | 18 +++
BPASmartClient/DeviceInfo.xml | 10 +-
BPASmartClient/MainWindow.xaml | 1 +
BPASmartClient/MainWindow.xaml.cs | 11 +-
SmartClient.sln | 26 ++++
39 files changed, 754 insertions(+), 57 deletions(-)
create mode 100644 .editorconfig
delete mode 100644 BPASmartClient.Business/Class1.cs
create mode 100644 BPASmartClient.Business/ConfigMgr.cs
create mode 100644 BPASmartClient.Business/DeviceMgr.cs
create mode 100644 BPASmartClient.Business/IPlugin.cs
create mode 100644 BPASmartClient.Business/Plugin.cs
create mode 100644 BPASmartClient.Device/BaseDevice.cs
create mode 100644 BPASmartClient.EventBus/BPASmartClient.EventBus.csproj
create mode 100644 BPASmartClient.EventBus/EventBus.cs
create mode 100644 BPASmartClient.EventBus/IEvent.cs
create mode 100644 BPASmartClient.EventBus/IEventExtends.cs
create mode 100644 BPASmartClient.Helper/XmlUtil.cs
create mode 100644 BPASmartClient.Model/BPASmartClient.Model.csproj
create mode 100644 BPASmartClient.Model/BaseEvent.cs
create mode 100644 BPASmartClient.Model/Demo_MakeCoffeeEvent.cs
create mode 100644 BPASmartClient.Model/DeviceConfigModel.cs
rename BPASmartClient.Peripheral/{IBasePeripheral.cs => IPeripheral.cs} (73%)
diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 00000000..12dd7909
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,16 @@
+[*.cs]
+
+# CS8600: 将 null 字面量或可能为 null 的值转换为非 null 类型。
+dotnet_diagnostic.CS8600.severity = none
+
+# CS8604: 引用类型参数可能为 null。
+dotnet_diagnostic.CS8604.severity = none
+
+# CS8603: 可能返回 null 引用。
+dotnet_diagnostic.CS8603.severity = none
+
+# Default severity for all analyzer diagnostics
+dotnet_analyzer_diagnostic.severity = none
+
+# CS8602: 解引用可能出现空引用。
+dotnet_diagnostic.CS8602.severity = none
diff --git a/BPASmartClient.Business/BPASmartClient.Business.csproj b/BPASmartClient.Business/BPASmartClient.Business.csproj
index 132c02c5..7d75a6b6 100644
--- a/BPASmartClient.Business/BPASmartClient.Business.csproj
+++ b/BPASmartClient.Business/BPASmartClient.Business.csproj
@@ -1,4 +1,4 @@
-
+
net6.0
@@ -6,4 +6,10 @@
enable
+
+
+
+
+
+
diff --git a/BPASmartClient.Business/Class1.cs b/BPASmartClient.Business/Class1.cs
deleted file mode 100644
index 4c571328..00000000
--- a/BPASmartClient.Business/Class1.cs
+++ /dev/null
@@ -1,7 +0,0 @@
-namespace BPASmartClient.Business
-{
- public class Class1
- {
-
- }
-}
\ No newline at end of file
diff --git a/BPASmartClient.Business/ConfigMgr.cs b/BPASmartClient.Business/ConfigMgr.cs
new file mode 100644
index 00000000..9030a538
--- /dev/null
+++ b/BPASmartClient.Business/ConfigMgr.cs
@@ -0,0 +1,54 @@
+using BPASmartClient.Model;
+using System.Xml.Linq;
+using System.Xml.XPath;
+
+namespace BPASmartClient.Business
+{
+ public class ConfigMgr : IPlugin
+ {
+ private List deviceConfigs;
+ public void Dispose()
+ {
+ }
+
+ public void Initialize()
+ {
+ InitDeviceModel();
+ }
+
+ public List GetDeviceConfigs()
+ {
+ if (null == deviceConfigs)
+ InitDeviceModel();
+ return deviceConfigs;
+ }
+
+ private void InitDeviceModel()
+ {
+ deviceConfigs = new List();
+ var devicePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DeviceInfo.xml");
+ var xdoc = XDocument.Load(devicePath);
+ var devices = xdoc.XPathSelectElements("//Device");
+ foreach (var device in devices)
+ {
+ DeviceConfig deviceConfig = new DeviceConfig();
+ deviceConfig.Name = device.Attribute("Name").Value;
+ deviceConfig.Module = device.Attribute("Module").Value;
+ deviceConfig.DeviceId = device.Attribute("DeviceId").Value;
+
+ foreach (var peripheralEl in device.XPathSelectElements("//Peripheral"))
+ {
+ BPASmartClient.Model.Peripheral peripheral = new BPASmartClient.Model.Peripheral();
+ peripheral.Module = peripheralEl.Attribute("Module").Value;
+ foreach (XElement parameter in peripheralEl.Element("Parameters").Elements())
+ {
+ peripheral.Parameters.Add(parameter.Name.LocalName, parameter.Value);
+ }
+ deviceConfig.Peripherals.Add(peripheral);
+ }
+ deviceConfigs.Add(deviceConfig);
+ }
+ }
+
+ }
+}
\ No newline at end of file
diff --git a/BPASmartClient.Business/DeviceMgr.cs b/BPASmartClient.Business/DeviceMgr.cs
new file mode 100644
index 00000000..83fba32a
--- /dev/null
+++ b/BPASmartClient.Business/DeviceMgr.cs
@@ -0,0 +1,67 @@
+using BPASmartClient.Device;
+using BPASmartClient.Helper;
+using BPASmartClient.Peripheral;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace BPASmartClient.Business
+{
+ ///
+ /// 设备管理器,统一管理所有设备资源
+ ///
+ public class DeviceMgr : IPlugin
+ {
+ //设备集合
+ private List devices = new List();
+
+ public void Dispose()
+ {
+ }
+
+ public void Initialize()
+ {
+ LoadDevice();
+ }
+
+ ///
+ /// 设备加载
+ ///
+ private void LoadDevice()
+ {
+ var devices = Plugin.GetInstance().GetPlugin().GetDeviceConfigs();
+ foreach (var device in devices)
+ {
+ var deviceTemp = Assembly.Load(device.Module.Substring(0, device.Module.LastIndexOf('.'))).CreateInstance(device.Module) as IDevice;
+ deviceTemp.Name = device.Name;
+ deviceTemp.DeviceId = device.DeviceId;
+
+ List peripherals = new List();
+ foreach (var peripheral in device.Peripherals)
+ {
+ var peripheralTemp = Assembly.Load(peripheral.Module.Substring(0, peripheral.Module.LastIndexOf('.'))).CreateInstance(peripheral.Module) as IPeripheral;
+ foreach (var pars in peripheral.Parameters)
+ {
+ peripheralTemp.GetType().GetProperty(pars.Key).SetValue(peripheralTemp, Convert.ChangeType(pars.Value, peripheralTemp.GetType().GetProperty(pars.Key).PropertyType));
+ }
+ peripherals.Add(peripheralTemp);
+ }
+ deviceTemp.Initliaze(peripherals);
+ this.devices.Add(deviceTemp);
+ }
+ }
+
+ public void StartService()
+ {
+ this.devices.ForEach(device => device.StartMain());
+ }
+
+ public void StopService()
+ {
+ this.devices.ForEach(device => device.Stop());
+ }
+ }
+}
diff --git a/BPASmartClient.Business/IPlugin.cs b/BPASmartClient.Business/IPlugin.cs
new file mode 100644
index 00000000..f3204909
--- /dev/null
+++ b/BPASmartClient.Business/IPlugin.cs
@@ -0,0 +1,16 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace BPASmartClient.Business
+{
+ ///
+ /// 业务插件
+ ///
+ public interface IPlugin:IDisposable
+ {
+ void Initialize();
+ }
+}
diff --git a/BPASmartClient.Business/Plugin.cs b/BPASmartClient.Business/Plugin.cs
new file mode 100644
index 00000000..c8865f65
--- /dev/null
+++ b/BPASmartClient.Business/Plugin.cs
@@ -0,0 +1,37 @@
+using BPASmartClient.Helper;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace BPASmartClient.Business
+{
+ public class Plugin : Singleton, IDisposable
+ {
+ private List plugins = new List();
+
+ public void Dispose()
+ {
+ plugins.ForEach(p => p.Dispose());
+ }
+
+ public void Init()
+ {
+ this.GetType().Assembly.GetTypes().ToList().ForEach(plugin =>
+ {
+ if (plugin.GetInterfaces().Contains(typeof(IPlugin)))
+ {
+ plugins.Add((IPlugin)Activator.CreateInstance(plugin));
+ }
+ });
+
+ plugins.ForEach(p => p.Initialize());
+ }
+
+ public T GetPlugin() where T : IPlugin
+ {
+ return (T)plugins.FirstOrDefault(p => p.GetType().Equals(typeof(T)));
+ }
+ }
+}
diff --git a/BPASmartClient.DRCoffee/BPASmartClient.DRCoffee.csproj b/BPASmartClient.DRCoffee/BPASmartClient.DRCoffee.csproj
index e645d6a1..1d904b63 100644
--- a/BPASmartClient.DRCoffee/BPASmartClient.DRCoffee.csproj
+++ b/BPASmartClient.DRCoffee/BPASmartClient.DRCoffee.csproj
@@ -4,8 +4,13 @@
net6.0
+
+
+
+
+
diff --git a/BPASmartClient.DRCoffee/CoffeeMachine.cs b/BPASmartClient.DRCoffee/CoffeeMachine.cs
index e7e9850e..95609276 100644
--- a/BPASmartClient.DRCoffee/CoffeeMachine.cs
+++ b/BPASmartClient.DRCoffee/CoffeeMachine.cs
@@ -1,16 +1,17 @@
using BPASmartClient.DRCoffee;
using BPASmartClient.Helper;
+using BPASmartClient.Peripheral;
using BPASmartClient.SerialPort;
using System;
using System.Collections.Generic;
using System.Threading;
-namespace HBLDevice.Coffee
+namespace BPASmartClient.DRCoffee
{
///
/// 咖啡机
///
- public class CoffeeMachine
+ public class CoffeeMachine: BasePeripheral
{
//通讯代理
@@ -69,21 +70,21 @@ namespace HBLDevice.Coffee
///
public Action CoffeeAppStatusChanged;
- public CoffeeMachine(string portName, BaudRates baud)
+ public CoffeeMachine()
{
- commProxy = new SerialPortClient(portName, baud);
- commProxy.SetDataStorage(dataStorage);
- commandHandler.Init(commProxy);
- commandHandler.PauseAsk = delegate (bool pause)
- {
- free = !pause;
- };
+ //commProxy = new SerialPortClient(portName, baud);
+ //commProxy.SetDataStorage(dataStorage);
+ //commandHandler.Init(commProxy);
+ //commandHandler.PauseAsk = delegate (bool pause)
+ //{
+ // free = !pause;
+ //};
}
///
/// 主线程开始运行
///
- public void Start()
+ public override void Start()
{
commProxy.Start();
running = true;
@@ -93,7 +94,7 @@ namespace HBLDevice.Coffee
///
/// 停止运行
///
- public void Stop()
+ public override void Stop()
{
commProxy.Stop();
running = false;
@@ -151,5 +152,15 @@ namespace HBLDevice.Coffee
Thread.Sleep(5);
}), "咖啡机解析线程");
}
+
+ protected override void InitStatus()
+ {
+ throw new NotImplementedException();
+ }
+
+
+ public override void Init()
+ {
+ }
}
}
diff --git a/BPASmartClient.DRCoffee/CommandHandler.cs b/BPASmartClient.DRCoffee/CommandHandler.cs
index a9fd0510..f42e3a34 100644
--- a/BPASmartClient.DRCoffee/CommandHandler.cs
+++ b/BPASmartClient.DRCoffee/CommandHandler.cs
@@ -11,7 +11,7 @@ using System.Text;
using System.Threading;
using System.Threading.Tasks;
-namespace HBLDevice.Coffee
+namespace BPASmartClient.DRCoffee
{
///
/// 指令封装
diff --git a/BPASmartClient.DRCoffee/MorkCStatus.cs b/BPASmartClient.DRCoffee/MorkCStatus.cs
index 72221603..226aa9e7 100644
--- a/BPASmartClient.DRCoffee/MorkCStatus.cs
+++ b/BPASmartClient.DRCoffee/MorkCStatus.cs
@@ -6,7 +6,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
-namespace HBLDevice.Coffee
+namespace BPASmartClient.DRCoffee
{
public class MorkCStatus
{
diff --git a/BPASmartClient.Device/BPASmartClient.Device.csproj b/BPASmartClient.Device/BPASmartClient.Device.csproj
index dbc15171..bd08bd1d 100644
--- a/BPASmartClient.Device/BPASmartClient.Device.csproj
+++ b/BPASmartClient.Device/BPASmartClient.Device.csproj
@@ -4,4 +4,8 @@
net6.0
+
+
+
+
diff --git a/BPASmartClient.Device/BaseDevice.cs b/BPASmartClient.Device/BaseDevice.cs
new file mode 100644
index 00000000..ba833aee
--- /dev/null
+++ b/BPASmartClient.Device/BaseDevice.cs
@@ -0,0 +1,41 @@
+using BPASmartClient.Peripheral;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace BPASmartClient.Device
+{
+ ///
+ /// 设备基类
+ ///
+ public abstract class BaseDevice : IDevice
+ {
+ ///
+ /// 设备ID
+ ///
+ public string DeviceId { get; set; }
+ ///
+ /// 设备所有状态
+ ///
+ public DeviceStatus Status { get; set; }
+ public string Name { get; set; }
+
+ public void Initliaze()
+ {
+ }
+
+ public void Initliaze(List peripherals)
+ {
+ peripherals.ForEach(p => {
+ p.DeviceId = this.DeviceId;
+ p.Init();
+ });
+ }
+
+ public abstract void StartMain();
+
+ public abstract void Stop();
+ }
+}
diff --git a/BPASmartClient.Device/IDevice.cs b/BPASmartClient.Device/IDevice.cs
index dbb851b7..8ffb679f 100644
--- a/BPASmartClient.Device/IDevice.cs
+++ b/BPASmartClient.Device/IDevice.cs
@@ -1,4 +1,5 @@
-using System;
+using BPASmartClient.Peripheral;
+using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -11,11 +12,19 @@ namespace BPASmartClient.Device
///
public interface IDevice
{
+ ///
+ /// 设备ID
+ ///
+ string DeviceId { get; set; }
+ string Name { get; set; }
+ ///
+ /// 设备所有状态
+ ///
DeviceStatus Status { get; set; }
///
/// 初始化设备加载
///
- void Initliaze();
+ void Initliaze(List peripherals);
///
/// 开启设备主任务
///
diff --git a/BPASmartClient.EventBus/BPASmartClient.EventBus.csproj b/BPASmartClient.EventBus/BPASmartClient.EventBus.csproj
new file mode 100644
index 00000000..cb005768
--- /dev/null
+++ b/BPASmartClient.EventBus/BPASmartClient.EventBus.csproj
@@ -0,0 +1,13 @@
+
+
+
+ net6.0
+ enable
+ enable
+
+
+
+
+
+
+
diff --git a/BPASmartClient.EventBus/EventBus.cs b/BPASmartClient.EventBus/EventBus.cs
new file mode 100644
index 00000000..c0a1453f
--- /dev/null
+++ b/BPASmartClient.EventBus/EventBus.cs
@@ -0,0 +1,128 @@
+using BPASmartClient.Helper;
+using System;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+
+/* ***********************************************
+ * subject 事件总线,总线入口,后续按类型分发
+ * author 张原川
+ * date 2019/6/3 15:49:03
+ * ***********************************************/
+
+namespace BPASmartClient.EventBus
+{
+ ///
+ /// 事件总线
+ ///
+ public class EventBus : Singleton
+ {
+ ///
+ /// 事件处理委托
+ ///
+ ///
+ public delegate void EventCallBackHandle(params object[] args);
+ ///
+ /// 事件处理委托
+ ///
+ ///
+ ///
+ public delegate void EventHandle(IEvent @event, EventCallBackHandle callBack = null);
+ ///
+ /// 事件订阅者集合
+ ///
+ private ConcurrentDictionary>> _eventHandls = new ConcurrentDictionary>>();
+
+
+ ///
+ /// 事件订阅
+ ///
+ public void Subscribe(string id,EventHandle handle)
+ {
+ if (!_eventHandls.ContainsKey(id))
+ _eventHandls.TryAdd(id, new ConcurrentDictionary>());
+ if (!_eventHandls[id].ContainsKey(typeof(TEvent)))
+ _eventHandls[id].TryAdd(typeof(TEvent), new List());
+ lock (_eventHandls)
+ _eventHandls[id][typeof(TEvent)].Add(handle);
+ }
+
+ ///
+ /// 事件退订
+ ///
+ public void UnSubscribe(string id,EventHandle handle)
+ {
+ if (_eventHandls.ContainsKey(id))
+ {
+ if (_eventHandls[id].ContainsKey(typeof(TEvent)))
+ {
+ if (_eventHandls[id][typeof(TEvent)].Contains(handle))
+ {
+ lock (_eventHandls)
+ _eventHandls[id][typeof(TEvent)].Remove(handle);
+ }
+ }
+ }
+ }
+
+
+ ///
+ /// 事件发布,不带返回
+ ///
+ public void Publish(TEvent @event) where TEvent : IEvent
+ {
+ if (_eventHandls.ContainsKey(@event.Id))
+ {
+ if (_eventHandls[@event.Id].ContainsKey(typeof(TEvent)))
+ {
+ for (int i = _eventHandls[@event.Id][typeof(TEvent)].Count - 1; i >= 0; i--)
+ _eventHandls[@event.Id][typeof(TEvent)][i](@event);
+
+ //_eventHandls[typeof(TEvent)].ForEach(p =>
+ //{
+ // p(@event);
+ //});
+ }
+ }
+ }
+
+ ///
+ /// 事件发布,带返回
+ ///
+ public void Publish(TEvent @event, EventCallBackHandle eventCallBack) where TEvent : IEvent
+ {
+ List