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 result = new List(); + if (_eventHandls.ContainsKey(@event.Id)) + { + if (_eventHandls[@event.Id].ContainsKey(typeof(TEvent))) + { + //_eventHandls[typeof(TEvent)].ForEach(p => + //{ + // p(@event, delegate (object[] args) + // { + // result.AddRange(args); + // }); + //}); + + for (int i = _eventHandls[@event.Id][typeof(TEvent)].Count - 1; i >= 0; i--) + { + _eventHandls[@event.Id][typeof(TEvent)][i](@event, delegate (object[] args) + { + result.AddRange(args); + }); + } + + } + } + eventCallBack.Invoke(result.ToArray()); + } + + /// + /// 事件总线释放 + /// + public void Dispose() + { + _eventHandls.Clear(); + } + } + +} diff --git a/BPASmartClient.EventBus/IEvent.cs b/BPASmartClient.EventBus/IEvent.cs new file mode 100644 index 00000000..219ae85d --- /dev/null +++ b/BPASmartClient.EventBus/IEvent.cs @@ -0,0 +1,10 @@ +namespace BPASmartClient.EventBus +{ + /// + /// 事件接口 + /// + public interface IEvent + { + string Id { get; set; } + } +} diff --git a/BPASmartClient.EventBus/IEventExtends.cs b/BPASmartClient.EventBus/IEventExtends.cs new file mode 100644 index 00000000..e6ef4a43 --- /dev/null +++ b/BPASmartClient.EventBus/IEventExtends.cs @@ -0,0 +1,38 @@ +/* ============================================================================== +* 功能描述: +* 创 建 者:张原川 +* 创建日期:2016/10/10 16:50:12 +* ==============================================================================*/ + +namespace BPASmartClient.EventBus +{ + /// + /// + /// + public static class IEventExtends + { + + #region Methods - Public + /// + /// 发布 + /// + /// + /// + public static void Publish(this TEvent message) where TEvent : class, IEvent + { + EventBus.GetInstance().Publish(message); + } + /// + /// + /// + /// + /// + /// + public static void Publish(this TEvent message, EventBus.EventCallBackHandle eventCallBack) where TEvent : class, IEvent + { + EventBus.GetInstance().Publish(message, eventCallBack); + } + + #endregion + } +} diff --git a/BPASmartClient.Helper/Json.cs b/BPASmartClient.Helper/Json.cs index 79b49031..e1cfca21 100644 --- a/BPASmartClient.Helper/Json.cs +++ b/BPASmartClient.Helper/Json.cs @@ -27,8 +27,8 @@ namespace BPASmartClient.Helper { get { - Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{LocaPath.GetInstance.FilePath}\\JSON")); - return $"{AppDomain.CurrentDomain.BaseDirectory}{LocaPath.GetInstance.FilePath}JSON\\{typeof(T).Name}.json"; + Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{LocaPath.GetInstance().FilePath}\\JSON")); + return $"{AppDomain.CurrentDomain.BaseDirectory}{LocaPath.GetInstance().FilePath}JSON\\{typeof(T).Name}.json"; } } diff --git a/BPASmartClient.Helper/LocaPath.cs b/BPASmartClient.Helper/LocaPath.cs index d9edef46..afc71e39 100644 --- a/BPASmartClient.Helper/LocaPath.cs +++ b/BPASmartClient.Helper/LocaPath.cs @@ -7,12 +7,9 @@ using System.Threading.Tasks; namespace BPASmartClient.Helper { - public class LocaPath + public class LocaPath:Singleton { - private volatile static LocaPath _Instance; - public static LocaPath GetInstance => _Instance ?? (_Instance = new LocaPath()); - private LocaPath() { } public string FilePath { get; set; } = string.Empty; diff --git a/BPASmartClient.Helper/TextHelper.cs b/BPASmartClient.Helper/TextHelper.cs index 3f4407f2..cf817cb4 100644 --- a/BPASmartClient.Helper/TextHelper.cs +++ b/BPASmartClient.Helper/TextHelper.cs @@ -22,8 +22,8 @@ namespace BPASmartClient.Helper { if (info?.Length > 0) { - Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{LocaPath.GetInstance.FilePath}\\{DicrectoryName}")); - string path = $"{AppDomain.CurrentDomain.BaseDirectory}{LocaPath.GetInstance.FilePath}\\{DicrectoryName}\\{DateTime.Now.ToString("yyyy-MM-dd") + " " + name}.txt"; + Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{LocaPath.GetInstance().FilePath}\\{DicrectoryName}")); + string path = $"{AppDomain.CurrentDomain.BaseDirectory}{LocaPath.GetInstance().FilePath}\\{DicrectoryName}\\{DateTime.Now.ToString("yyyy-MM-dd") + " " + name}.txt"; StringBuilder sb = new StringBuilder(); sb.Append($"****************************************** {DateTime.Now} ******************************************" + "\n"); sb.Append(info); diff --git a/BPASmartClient.Helper/XmlUtil.cs b/BPASmartClient.Helper/XmlUtil.cs new file mode 100644 index 00000000..e2e5cdf3 --- /dev/null +++ b/BPASmartClient.Helper/XmlUtil.cs @@ -0,0 +1,85 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.IO; +using System.Data; +using System.Xml; +using System.Xml.Serialization; + +namespace BPASmartClient.Helper +{ + /// + /// Xml序列化与反序列化 + /// + public class XmlUtil + { + #region 反序列化 + /// + /// 反序列化 + /// + /// 类型 + /// XML字符串 + /// + public static object Deserialize(Type type, string xml) + { + try + { + using (StringReader sr = new StringReader(xml)) + { + XmlSerializer xmldes = new XmlSerializer(type); + return xmldes.Deserialize(sr); + } + } + catch (Exception e) + { + + return null; + } + } + /// + /// 反序列化 + /// + /// + /// + /// + public static object Deserialize(Type type, Stream stream) + { + XmlSerializer xmldes = new XmlSerializer(type); + return xmldes.Deserialize(stream); + } + #endregion + + #region 序列化 + /// + /// 序列化 + /// + /// 类型 + /// 对象 + /// + public static string Serializer(Type type, object obj) + { + MemoryStream Stream = new MemoryStream(); + XmlSerializer xml = new XmlSerializer(type); + try + { + //序列化对象 + xml.Serialize(Stream, obj); + } + catch (InvalidOperationException) + { + throw; + } + Stream.Position = 0; + StreamReader sr = new StreamReader(Stream); + string str = sr.ReadToEnd(); + + sr.Dispose(); + Stream.Dispose(); + + return str; + } + + #endregion + } +} diff --git a/BPASmartClient.Lebai/BPASmartClient.Lebai.csproj b/BPASmartClient.Lebai/BPASmartClient.Lebai.csproj index a335ae33..aaa4bd7e 100644 --- a/BPASmartClient.Lebai/BPASmartClient.Lebai.csproj +++ b/BPASmartClient.Lebai/BPASmartClient.Lebai.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -7,8 +7,10 @@ + + diff --git a/BPASmartClient.Lebai/LebaiRobot.cs b/BPASmartClient.Lebai/LebaiRobot.cs index be887054..abf1b598 100644 --- a/BPASmartClient.Lebai/LebaiRobot.cs +++ b/BPASmartClient.Lebai/LebaiRobot.cs @@ -11,6 +11,9 @@ using BPASmartClient.Message; using BPASmartClient.Helper; using TaskStatus = Lebai.SDK.Dtos.TaskStatus; using BPASmartClient.Peripheral; +using BPASmartClient.EventBus; +using static BPASmartClient.EventBus.EventBus; +using BPASmartClient.Model; namespace BPASmartClient.Lebai { @@ -240,5 +243,13 @@ namespace BPASmartClient.Lebai public override void Stop() { } + + public override void Init() + { + EventBus.EventBus.GetInstance().Subscribe(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack ) + { + + }); + } } } diff --git a/BPASmartClient.Model/BPASmartClient.Model.csproj b/BPASmartClient.Model/BPASmartClient.Model.csproj new file mode 100644 index 00000000..9ff0b6e0 --- /dev/null +++ b/BPASmartClient.Model/BPASmartClient.Model.csproj @@ -0,0 +1,13 @@ + + + + net6.0 + enable + enable + + + + + + + diff --git a/BPASmartClient.Model/BaseEvent.cs b/BPASmartClient.Model/BaseEvent.cs new file mode 100644 index 00000000..9955a6f9 --- /dev/null +++ b/BPASmartClient.Model/BaseEvent.cs @@ -0,0 +1,14 @@ +using BPASmartClient.EventBus; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BPASmartClient.Model +{ + public class BaseEvent : IEvent + { + public string Id { get; set; } + } +} diff --git a/BPASmartClient.Model/Demo_MakeCoffeeEvent.cs b/BPASmartClient.Model/Demo_MakeCoffeeEvent.cs new file mode 100644 index 00000000..4aa56820 --- /dev/null +++ b/BPASmartClient.Model/Demo_MakeCoffeeEvent.cs @@ -0,0 +1,14 @@ +using BPASmartClient.EventBus; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BPASmartClient.Model +{ + public class Demo_MakeCoffeeEvent : BaseEvent + { + + } +} diff --git a/BPASmartClient.Model/DeviceConfigModel.cs b/BPASmartClient.Model/DeviceConfigModel.cs new file mode 100644 index 00000000..eefd5bc3 --- /dev/null +++ b/BPASmartClient.Model/DeviceConfigModel.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BPASmartClient.Model +{ + public class DeviceConfigModel + { + public List Devices { get; set; } = new List(); + } + + public class DeviceConfig + { + public string Name { get; set; } + public string Module { get; set; } + public string DeviceId { get; set; } + public List Peripherals { get; set; } = new List(); + } + + public class Peripheral + { + public string Module { get; set; } + public Dictionary Parameters { get; set; } = new Dictionary(); + } +} diff --git a/BPASmartClient.MorkT/BPASmartClient.MorkT.csproj b/BPASmartClient.MorkT/BPASmartClient.MorkT.csproj index dbc15171..9f8f9534 100644 --- a/BPASmartClient.MorkT/BPASmartClient.MorkT.csproj +++ b/BPASmartClient.MorkT/BPASmartClient.MorkT.csproj @@ -4,4 +4,8 @@ net6.0 + + + + diff --git a/BPASmartClient.MorkT/Device_MorkT.cs b/BPASmartClient.MorkT/Device_MorkT.cs index d45f47d1..db88f643 100644 --- a/BPASmartClient.MorkT/Device_MorkT.cs +++ b/BPASmartClient.MorkT/Device_MorkT.cs @@ -1,4 +1,6 @@ -using System; +using BPASmartClient.Device; +using BPASmartClient.Peripheral; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,9 +8,14 @@ using System.Threading.Tasks; namespace BPASmartClient.MorkT { - public class Device_MorkT + public class Device_MorkT : BaseDevice { - } + public override void StartMain() + { + } - + public override void Stop() + { + } + } } diff --git a/BPASmartClient.Peripheral/BasePeripheral.cs b/BPASmartClient.Peripheral/BasePeripheral.cs index b3dabd27..c282e95d 100644 --- a/BPASmartClient.Peripheral/BasePeripheral.cs +++ b/BPASmartClient.Peripheral/BasePeripheral.cs @@ -9,7 +9,7 @@ namespace BPASmartClient.Peripheral /// /// 外设基类 /// - public abstract class BasePeripheral : IBasePeripheral + public abstract class BasePeripheral : IPeripheral { /// /// 是否已连接 @@ -19,6 +19,11 @@ namespace BPASmartClient.Peripheral /// 是否工作正常 /// public bool IsWork { get; protected set; } + /// + /// 归属设备Id + /// + public string DeviceId { get; set; } + /// /// 外设状态集合 /// @@ -41,6 +46,6 @@ namespace BPASmartClient.Peripheral public abstract void Stop(); - + public abstract void Init(); } } diff --git a/BPASmartClient.Peripheral/IBasePeripheral.cs b/BPASmartClient.Peripheral/IPeripheral.cs similarity index 73% rename from BPASmartClient.Peripheral/IBasePeripheral.cs rename to BPASmartClient.Peripheral/IPeripheral.cs index ca5b2a7b..79655400 100644 --- a/BPASmartClient.Peripheral/IBasePeripheral.cs +++ b/BPASmartClient.Peripheral/IPeripheral.cs @@ -9,8 +9,12 @@ namespace BPASmartClient.Peripheral /// /// 外设接口 /// - public interface IBasePeripheral + public interface IPeripheral { + /// + /// 设备ID + /// + string DeviceId { get; set; } /// /// 获取指定状态值 /// @@ -18,6 +22,10 @@ namespace BPASmartClient.Peripheral /// 状态值 object? GetStatus(string statusName); /// + /// 初始化 + /// + void Init(); + /// /// 驱动开启 /// void Start(); diff --git a/BPASmartClient.SCChip/BPASmartClient.SCChip.csproj b/BPASmartClient.SCChip/BPASmartClient.SCChip.csproj index e645d6a1..4bbee89a 100644 --- a/BPASmartClient.SCChip/BPASmartClient.SCChip.csproj +++ b/BPASmartClient.SCChip/BPASmartClient.SCChip.csproj @@ -6,6 +6,7 @@ + diff --git a/BPASmartClient.SCChip/ICChipMachine.cs b/BPASmartClient.SCChip/ICChipMachine.cs index 4b0eeb9e..a8433e3c 100644 --- a/BPASmartClient.SCChip/ICChipMachine.cs +++ b/BPASmartClient.SCChip/ICChipMachine.cs @@ -1,4 +1,5 @@ using BPASmartClient.Helper; +using BPASmartClient.Peripheral; using BPASmartClient.SerialPort; using System; using System.Collections.Generic; @@ -7,7 +8,7 @@ using System.Threading; namespace BPASmartClient.SCChip { - public class ICChipMachine + public class ICChipMachine: BasePeripheral { //指令组装 private CommandHandler commandHandler = new CommandHandler(); @@ -23,23 +24,21 @@ namespace BPASmartClient.SCChip - public ICChipMachine(string portName, BaudRates baud) + public ICChipMachine() { - commProxy = new SerialPortClient(portName, baud); - commProxy.SetDataStorage(dataStorage); - commandHandler.Init(commProxy); + //commProxy = new SerialPortClient(portName, baud); + //commProxy.SetDataStorage(dataStorage); + //commandHandler.Init(commProxy); } - public void Start() + public override void Start() { commProxy.Start(); running = true; MainLoop(); } - public void Stop() - { - } + private void MainLoop() { @@ -96,6 +95,18 @@ namespace BPASmartClient.SCChip } return structure; } + + protected override void InitStatus() + { + } + + public override void Stop() + { + } + + public override void Init() + { + } } } diff --git a/BPASmartClient/BPASmartClient.csproj b/BPASmartClient/BPASmartClient.csproj index 4106cb0f..c7e47637 100644 --- a/BPASmartClient/BPASmartClient.csproj +++ b/BPASmartClient/BPASmartClient.csproj @@ -7,4 +7,22 @@ true + + + + + + + + + + + + + + + PreserveNewest + + + diff --git a/BPASmartClient/DeviceInfo.xml b/BPASmartClient/DeviceInfo.xml index 63e4369b..c0ff5449 100644 --- a/BPASmartClient/DeviceInfo.xml +++ b/BPASmartClient/DeviceInfo.xml @@ -1,6 +1,6 @@  - + @@ -9,16 +9,12 @@ 0 - + - COM5 - 9600 - + - COM6 - 115200 diff --git a/BPASmartClient/MainWindow.xaml b/BPASmartClient/MainWindow.xaml index c6423776..0cdf7660 100644 --- a/BPASmartClient/MainWindow.xaml +++ b/BPASmartClient/MainWindow.xaml @@ -7,6 +7,7 @@ mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> +