From 60d5790ea50f4dbccddb6fcb0b3c0d0d750c6547 Mon Sep 17 00:00:00 2001 From: Z Date: Thu, 18 Apr 2024 17:51:18 +0800 Subject: [PATCH] fixed model and add example --- Data/DataServerUrl.cs | 5 ++ Framework/EGFramework/EGFramework.cs | 12 ++-- .../ProtocolTools/EGProtocolSchedule.cs | 69 +++++++++++++++++++ Model/ModelNumericalValue.cs | 15 ++++ Program.cs | 49 +++++++------ Protocol/ResponseNumericalValue.cs | 34 +++++++++ 6 files changed, 155 insertions(+), 29 deletions(-) create mode 100644 Data/DataServerUrl.cs create mode 100644 Framework/EGFramework/Module/ProtocolTools/EGProtocolSchedule.cs create mode 100644 Model/ModelNumericalValue.cs create mode 100644 Protocol/ResponseNumericalValue.cs diff --git a/Data/DataServerUrl.cs b/Data/DataServerUrl.cs new file mode 100644 index 0000000..9fc6c00 --- /dev/null +++ b/Data/DataServerUrl.cs @@ -0,0 +1,5 @@ +namespace Emergency_platform{ + public class DataServerUrl{ + public const string Url = ""; + } +} diff --git a/Framework/EGFramework/EGFramework.cs b/Framework/EGFramework/EGFramework.cs index 2c3dede..e17ae90 100644 --- a/Framework/EGFramework/EGFramework.cs +++ b/Framework/EGFramework/EGFramework.cs @@ -88,14 +88,6 @@ namespace EGFramework IArchitecture GetArchitecture(); } - public static class CanGetModuleExtension - { - public static T GetModule(this IBelongToArchitecture self) where T : class, IModule,new() - { - return self.GetArchitecture().GetModule(); - } - } - #endregion #region IOC @@ -211,6 +203,10 @@ namespace EGFramework { return EGArchitectureImplement.Interface.GetModule(); } + public static void RegisterModule(this IEGFramework self,T model) where T : class, IModule,new() + { + EGArchitectureImplement.Interface.RegisterModule(model); + } } #endregion } diff --git a/Framework/EGFramework/Module/ProtocolTools/EGProtocolSchedule.cs b/Framework/EGFramework/Module/ProtocolTools/EGProtocolSchedule.cs new file mode 100644 index 0000000..946c26d --- /dev/null +++ b/Framework/EGFramework/Module/ProtocolTools/EGProtocolSchedule.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Threading; + +namespace EGFramework{ + /// + /// In Godot engine, the async method return cannot operate the godot main thread such as Screen Trees Node. + /// The protocol schedule provide a main thread to check the message in protocol tools 's response message. + /// If you have more idea ,please issue to me, thanks. + /// + public partial class EGProtocolSchedule : IModule,IEGFramework + { + public Dictionary ProtocolTools = new Dictionary(); + public void Init() + { + int frameRate = 30; // 帧率 + int interval = 1000 / frameRate; // 计算每帧间隔时间 + + Timer timer = new Timer(_Process, null, 0, interval); + } + + public void _Process(object? state) + { + foreach(IProtocolReceived tool in ProtocolTools.Values){ + if(tool.GetReceivedMsg().Count>0){ + this.GetModule().OnDataReceived.Invoke(tool.GetReceivedMsg().Dequeue()); + } + } + } + public void EnabledAllTools(){ + ProtocolTools.Add(typeof(EGTCPClient),this.GetModule()); + ProtocolTools.Add(typeof(EGTCPServer),this.GetModule()); + ProtocolTools.Add(typeof(EGUDP),this.GetModule()); + ProtocolTools.Add(typeof(EGSerialPort),this.GetModule()); + } + public void EnabledTool() where TProtocolReceived : class, IModule,IProtocolReceived,new(){ + ProtocolTools.Add(typeof(TProtocolReceived),this.GetModule()); + } + + public void DisabledAllTools(){ + ProtocolTools.Clear(); + } + + public void DisabledTool() where TProtocolReceived : class, IModule,IProtocolReceived,new(){ + if(ProtocolTools.ContainsKey(typeof(TProtocolReceived))){ + ProtocolTools.Remove(typeof(TProtocolReceived)); + } + } + + public IArchitecture GetArchitecture() + { + return EGArchitectureImplement.Interface; + } + + } + public static class CanGetEGProtocolExtension{ + public static EGProtocolSchedule EGProtocolSchedule(this IEGFramework self){ + return self.GetModule(); + } + public static void EGEnabledProtocolTools(this IEGFramework self){ + self.GetModule().EnabledAllTools(); + } + public static void EGEnabledProtocolTool(this IEGFramework self) where TProtocolReceived : class, IModule,IProtocolReceived,new(){ + self.GetModule().EnabledTool(); + } + + } +} + diff --git a/Model/ModelNumericalValue.cs b/Model/ModelNumericalValue.cs new file mode 100644 index 0000000..6e2dba8 --- /dev/null +++ b/Model/ModelNumericalValue.cs @@ -0,0 +1,15 @@ +using EGFramework; +namespace Emergency_platform{ + public class ModelNumericalValue : EGModule,IEGFramework + { + public override void Init() + { + this.EGRegisterMessageEvent((e,sender,protocol)=>{ + Console.WriteLine("Get Command " + e.action); + Console.WriteLine("Command params is" + e.halmet + "|" + e.targetname + "|" + e.taskid); + }); + this.EGOnMessage(); + Console.WriteLine("ModelNumericalValue has been Loaded!"); + } + } +} diff --git a/Program.cs b/Program.cs index d6cfb61..1440a72 100644 --- a/Program.cs +++ b/Program.cs @@ -15,31 +15,38 @@ using System.IO; using System.Runtime.Serialization.Formatters.Binary; using Guidaoji.Camera; using Guidaoji.Common; +using EGFramework; +using Emergency_platform; -namespace Emergency_platform +Program program = new Program(); +program.Start(); +public partial class Program :IEGFramework { - public class Program + public void Start() { - - static void Main(string[] args) - { - - TCP tcp = new TCP(); - Patrol patrol = new Patrol(); - //IP - string sIP = "192.168.10.102"; - //端口号 - string sPassword = "tschkj88"; - //连接轨道机服务器 - //TcpClientWrapper.Socket("192.168.10.104", 20000); - //tcp.Socket(); - //patrol.PatrolD(); - Console.WriteLine("InitSuccess"); - //登录摄像头 - //CHNetHelp.CameraInit(sIP, sPassword); - } + TCP tcp = new TCP(); + Patrol patrol = new Patrol(); + //IP + string sIP = "192.168.10.102"; + //端口号 + string sPassword = "tschkj88"; + //连接轨道机服务器 + //TcpClientWrapper.Socket("192.168.10.104", 20000); + TcpClientWrapper.Socket("192.168.1.170", 20000); + //tcp.Socket(); + //patrol.PatrolD(); + Console.WriteLine("InitSuccess"); + StartServer(); + //登录摄像头 + //CHNetHelp.CameraInit(sIP, sPassword); + while (!Console.ReadLine().ToUpper().Contains("CLOSE")) continue; + Environment.Exit(0); + } - + public void StartServer(){ + this.EGTCPServerListen(20001); + this.EGEnabledProtocolTool(); + this.RegisterModule(new ModelNumericalValue()); } } diff --git a/Protocol/ResponseNumericalValue.cs b/Protocol/ResponseNumericalValue.cs new file mode 100644 index 0000000..1baac47 --- /dev/null +++ b/Protocol/ResponseNumericalValue.cs @@ -0,0 +1,34 @@ +using EGFramework; +using Newtonsoft.Json; +namespace Emergency_platform{ + public class ResponseNumericalValue : IResponse + { + public string action { set; get; } + public string taskid { set; get; } + public string halmet { set; get; } + public string cabinetid { set; get; } + public string targetname { set; get; } + + public bool TrySetData(string protocolData, byte[] protocolBytes) + { + try + { + ResponseNumericalValue data = JsonConvert.DeserializeObject(protocolData); + if(data.action == "numericalValue"){ + this.action = data.action; + this.taskid = data.taskid; + this.halmet = data.halmet; + this.targetname = data.targetname; + this.cabinetid = data.cabinetid; + return true; + } + return false; + } + catch (System.Exception) + { + return false; + throw; + } + } + } +} \ No newline at end of file