From 33ea1c52bd868ed91e66d02364b9fe9f9305176b Mon Sep 17 00:00:00 2001 From: Z Date: Fri, 19 Apr 2024 15:03:36 +0800 Subject: [PATCH] fixed Code --- Common/TcpClientWrapper.cs | 4 +- Data/DataServerUrl.cs | 10 +- .../Module/ProtocolTools/EGHttpClient.cs | 4 +- Model/ModelPatrol.cs | 98 +++++++++++++++++++ Program.cs | 1 + Protocol/ResponsePatrol.cs | 64 ++++++++++++ 6 files changed, 176 insertions(+), 5 deletions(-) create mode 100644 Model/ModelPatrol.cs create mode 100644 Protocol/ResponsePatrol.cs diff --git a/Common/TcpClientWrapper.cs b/Common/TcpClientWrapper.cs index f40e1a8..87b089c 100644 --- a/Common/TcpClientWrapper.cs +++ b/Common/TcpClientWrapper.cs @@ -99,7 +99,7 @@ namespace Guidaoji.Common //客户端IP和端口号 string clientIP = client.RemoteEndPoint == null ? "" : client.RemoteEndPoint.ToString(); clientIP = clientIP.Substring(0, clientIP.LastIndexOf(':')); - int clientPort = ((IPEndPoint)client.RemoteEndPoint).Port; + int clientPort = ((IPEndPoint)client.LocalEndPoint).Port; // 将与客户端连接的 套接字 对象添加到集合中; dict.Add(clientPort, client); PrintInfo(string.Format("客户端连接成功!IP:{0},端口号:{1}", clientIP, clientPort)); @@ -133,7 +133,7 @@ namespace Guidaoji.Common { bConnect = true; iGuidaojiClientPort = clientPort; - Console.WriteLine("连接成功"); + Console.WriteLine("轨道机连接成功"); PrintInfo("客户端连接成功连接成功......"); PrintInfo("Socket is end......"); Thread t = new Thread(GuidaojiExecute); diff --git a/Data/DataServerUrl.cs b/Data/DataServerUrl.cs index 9fc6c00..8d20aa7 100644 --- a/Data/DataServerUrl.cs +++ b/Data/DataServerUrl.cs @@ -1,5 +1,13 @@ namespace Emergency_platform{ public class DataServerUrl{ - public const string Url = ""; + + //Default is http://192.168.10.104:8080 + public string ServerAddress { set; get; } = ""; + public const string FrpAddress = "http://175.178.105.185:13000"; + public const string LocalAddress = "http://192.168.10.104:8080"; + /// + /// /api/CabinetInstance/id + /// + public const string UrlCabinetInstanceId = "/api/CabinetInstance/id"; } } diff --git a/Framework/EGFramework/Module/ProtocolTools/EGHttpClient.cs b/Framework/EGFramework/Module/ProtocolTools/EGHttpClient.cs index e705c56..8ab9c11 100644 --- a/Framework/EGFramework/Module/ProtocolTools/EGHttpClient.cs +++ b/Framework/EGFramework/Module/ProtocolTools/EGHttpClient.cs @@ -50,10 +50,10 @@ namespace EGFramework{ httpResponse = await HTTPClient.PostAsync(hostUrl,httpContent); break; case ProtocolType.HttpPut: - httpResponse = await HTTPClient.PostAsync(hostUrl,httpContent); + httpResponse = await HTTPClient.PutAsync(hostUrl,httpContent); break; case ProtocolType.HttpPatch: - httpResponse = await HTTPClient.PostAsync(hostUrl,httpContent); + httpResponse = await HTTPClient.PatchAsync(hostUrl,httpContent); break; case ProtocolType.HttpDelete: httpResponse = await HTTPClient.DeleteAsync(hostUrl); diff --git a/Model/ModelPatrol.cs b/Model/ModelPatrol.cs new file mode 100644 index 0000000..6d0a86d --- /dev/null +++ b/Model/ModelPatrol.cs @@ -0,0 +1,98 @@ +using System.Net.Http.Json; +using System.Text; +using System.Text.Json; +using EGFramework; +using Guidaoji; +using Guidaoji.Camera; +using Guidaoji.Common; +using Newtonsoft.Json; + +namespace Emergency_platform{ + public class ModelPatrol : EGModule,IEGFramework + { + public override void Init() + { + this.EGRegisterMessageEvent((e,sender,protocol)=>{ + Console.WriteLine("Get Command " + e.action); + Console.WriteLine("Command params is" + e.action + "|" + e.taskid + "|" + e.cabinetid); + ExecutePatrol(e,sender,protocol); + }); + this.EGOnMessage(); + } + + public async void ExecutePatrol(ResponsePatrol responsePatrol,string sender,ProtocolType protocolType){ + //var obj = JsonConvert.DeserializeObject(task); + //obj.cabinetid[] 可以得到要识别的柜子id + //把要识别的柜子id发送给硬件抓图,算法返回数据 + //把数据返回给应急平台,将柜子识别完之后返回数据 和算法交流返回的数据 + //var result = AlgorithmTCP.Connect(picPath, targetName); + foreach (int cabinetId in responsePatrol.cabinetid) + { + string picPath = $"D:/Picture/{DateTime.Now:yyyyMMddHHmmss}.jpeg"; + string videoPath = $"D:/Video/{DateTime.Now:yyyyMMddHHmmss}.mp4"; + //调用轨道机方法 + Console.WriteLine(cabinetId); + using (var httpClient = new HttpClient()) + { + //发送post请求 + //origin url = http://192.168.10.104:8080/api/CabinetInstance/id + var response = await httpClient.PostAsJsonAsync(DataServerUrl.LocalAddress + DataServerUrl.UrlCabinetInstanceId, cabinetId.ToString()); + if (response.IsSuccessStatusCode) + { + var responseBody = await response.Content.ReadAsStringAsync(); + Console.WriteLine(responseBody); + JsonDocument jsonDocument = JsonDocument.Parse(responseBody); + JsonElement dataElement = jsonDocument.RootElement.GetProperty("data"); + string cabinetPosX = dataElement.GetProperty("cabinetPosX").GetString(); + string cabinetPosY = dataElement.GetProperty("cabinetPosY").GetString(); + float poseP = float.Parse(dataElement.GetProperty("poseP").GetString()); + float poseT = float.Parse(dataElement.GetProperty("poseT").GetString()); + float poseZ = float.Parse(dataElement.GetProperty("poseZ").GetString()); + var posX = double.Parse(cabinetPosX); + var posY = double.Parse(cabinetPosY); + //Console.WriteLine(cabinetPosX); + //Console.WriteLine(cabinetPosY); + //Console.WriteLine(poseX); + //Console.WriteLine(poseY); + //Console.WriteLine(poseP); + //Console.WriteLine(poseT); + //Console.WriteLine(poseZ); + //CHNetHelp.SaveViedo(true, 1, videoPath); + TcpClientWrapper.GuiDaoJiMove(posX, posY); + GuiDaoJiRet gui = TcpClientWrapper.SendDataRead(); + bool guidaoji = true; + while (guidaoji) + { + if (gui != null && gui.data.type == "0") + { + guidaoji = false; + CHNetHelp.SetCameraProcess(OperationType.PTZ, poseP, poseT, poseZ, 1, videoPath); + CHNetHelp.JPEG(1, picPath); + } + } + Console.WriteLine("轨道机运动成功"); + } + else + { + //处理错误响应 + Console.WriteLine("请求失败"); + } + } + //发给算法 + + //模拟的返回数据 + RequestPatrol requestPatrol = new RequestPatrol(){ + taskid = "1", + cabinetid = cabinetId, + }; + requestPatrol.target.Add(new PatrolTarget("1号电流表","100.1")); + requestPatrol.target.Add(new PatrolTarget("3号指示灯","亮")); + requestPatrol.target.Add(new PatrolTarget("停电牌","无")); + //发送数据 + this.EGSendMessage(requestPatrol,sender,protocolType); + //socket_commu.Send(dataBytes); + Console.WriteLine("发送成功"); + } + } + } +} \ No newline at end of file diff --git a/Program.cs b/Program.cs index 1440a72..2519ba3 100644 --- a/Program.cs +++ b/Program.cs @@ -48,5 +48,6 @@ public partial class Program :IEGFramework this.EGTCPServerListen(20001); this.EGEnabledProtocolTool(); this.RegisterModule(new ModelNumericalValue()); + this.RegisterModule(new ModelPatrol()); } } diff --git a/Protocol/ResponsePatrol.cs b/Protocol/ResponsePatrol.cs new file mode 100644 index 0000000..5867cee --- /dev/null +++ b/Protocol/ResponsePatrol.cs @@ -0,0 +1,64 @@ +using EGFramework; +using Newtonsoft.Json; +namespace Emergency_platform{ + public class ResponsePatrol : IResponse + { + public string action { set; get; } + public string taskid { set; get; } + public List cabinetid { set; get; } + + public bool TrySetData(string protocolData, byte[] protocolBytes) + { + try + { + ResponsePatrol data = JsonConvert.DeserializeObject(protocolData); + if(data.action == "patrol"){ + this.action = data.action; + this.taskid = data.taskid; + this.cabinetid = data.cabinetid; + return true; + } + return false; + } + catch (System.Exception) + { + return false; + throw; + } + } + } + + public class RequestPatrol : IRequest + { + public string action = "identify"; + public string date { set; get; } + public string taskid { set; get; } + public int cabinetid = 1; + public List target { set; get; } + public RequestPatrol(){ + date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); + target = new List(); + } + public RequestPatrol(List target){ + this.target = target; + date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); + } + public byte[] ToProtocolByteData() + { + return null; + } + + public string ToProtocolData() + { + return JsonConvert.SerializeObject(this); + } + } + public struct PatrolTarget{ + public string name { set; get; } + public string result { set; get; } + public PatrolTarget(string name_,string result_){ + name = name_; + result = result_; + } + } +} \ No newline at end of file