Browse Source

fixed model and add example

master
Z 8 months ago
parent
commit
60d5790ea5
  1. 5
      Data/DataServerUrl.cs
  2. 12
      Framework/EGFramework/EGFramework.cs
  3. 69
      Framework/EGFramework/Module/ProtocolTools/EGProtocolSchedule.cs
  4. 15
      Model/ModelNumericalValue.cs
  5. 49
      Program.cs
  6. 34
      Protocol/ResponseNumericalValue.cs

5
Data/DataServerUrl.cs

@ -0,0 +1,5 @@ @@ -0,0 +1,5 @@
namespace Emergency_platform{
public class DataServerUrl{
public const string Url = "";
}
}

12
Framework/EGFramework/EGFramework.cs

@ -88,14 +88,6 @@ namespace EGFramework @@ -88,14 +88,6 @@ namespace EGFramework
IArchitecture GetArchitecture();
}
public static class CanGetModuleExtension
{
public static T GetModule<T>(this IBelongToArchitecture self) where T : class, IModule,new()
{
return self.GetArchitecture().GetModule<T>();
}
}
#endregion
#region IOC
@ -211,6 +203,10 @@ namespace EGFramework @@ -211,6 +203,10 @@ namespace EGFramework
{
return EGArchitectureImplement.Interface.GetModule<T>();
}
public static void RegisterModule<T>(this IEGFramework self,T model) where T : class, IModule,new()
{
EGArchitectureImplement.Interface.RegisterModule(model);
}
}
#endregion
}

69
Framework/EGFramework/Module/ProtocolTools/EGProtocolSchedule.cs

@ -0,0 +1,69 @@ @@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Threading;
namespace EGFramework{
/// <summary>
/// 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.
/// </summary>
public partial class EGProtocolSchedule : IModule,IEGFramework
{
public Dictionary<Type,IProtocolReceived> ProtocolTools = new Dictionary<Type, IProtocolReceived>();
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<EGMessage>().OnDataReceived.Invoke(tool.GetReceivedMsg().Dequeue());
}
}
}
public void EnabledAllTools(){
ProtocolTools.Add(typeof(EGTCPClient),this.GetModule<EGTCPClient>());
ProtocolTools.Add(typeof(EGTCPServer),this.GetModule<EGTCPServer>());
ProtocolTools.Add(typeof(EGUDP),this.GetModule<EGUDP>());
ProtocolTools.Add(typeof(EGSerialPort),this.GetModule<EGSerialPort>());
}
public void EnabledTool<TProtocolReceived>() where TProtocolReceived : class, IModule,IProtocolReceived,new(){
ProtocolTools.Add(typeof(TProtocolReceived),this.GetModule<TProtocolReceived>());
}
public void DisabledAllTools(){
ProtocolTools.Clear();
}
public void DisabledTool<TProtocolReceived>() 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<EGProtocolSchedule>();
}
public static void EGEnabledProtocolTools(this IEGFramework self){
self.GetModule<EGProtocolSchedule>().EnabledAllTools();
}
public static void EGEnabledProtocolTool<TProtocolReceived>(this IEGFramework self) where TProtocolReceived : class, IModule,IProtocolReceived,new(){
self.GetModule<EGProtocolSchedule>().EnabledTool<TProtocolReceived>();
}
}
}

15
Model/ModelNumericalValue.cs

@ -0,0 +1,15 @@ @@ -0,0 +1,15 @@
using EGFramework;
namespace Emergency_platform{
public class ModelNumericalValue : EGModule,IEGFramework
{
public override void Init()
{
this.EGRegisterMessageEvent<ResponseNumericalValue>((e,sender,protocol)=>{
Console.WriteLine("Get Command " + e.action);
Console.WriteLine("Command params is" + e.halmet + "|" + e.targetname + "|" + e.taskid);
});
this.EGOnMessage<ResponseNumericalValue>();
Console.WriteLine("ModelNumericalValue has been Loaded!");
}
}
}

49
Program.cs

@ -15,31 +15,38 @@ using System.IO; @@ -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<EGTCPServer>();
this.RegisterModule(new ModelNumericalValue());
}
}

34
Protocol/ResponseNumericalValue.cs

@ -0,0 +1,34 @@ @@ -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<ResponseNumericalValue>(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;
}
}
}
}
Loading…
Cancel
Save