From e7ec40a522cce4546bae293f12fe277f838714fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8C=83?= <15095123350@163.com> Date: Sat, 27 Jul 2024 09:28:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=B0=8F=E8=BD=A6=E6=9A=82?= =?UTF-8?q?=E5=81=9C=E5=92=8C=E5=90=AF=E5=8A=A8=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/RecieveRcsController.cs | 12 ++ src/Khd.Core.Application/HttpHelper.cs | 170 ++++++++++++++++++ .../Interface/IWcsTaskApplication.cs | 1 + .../WcsTaskApplication.cs | 43 ++++- .../Dto/webapi/CallMaterial.cs | 6 + src/Khd.Core.Wcs/appsettings.json | 2 +- 6 files changed, 232 insertions(+), 2 deletions(-) create mode 100644 src/Khd.Core.Application/HttpHelper.cs diff --git a/src/Khd.Core.Api/Controllers/RecieveRcsController.cs b/src/Khd.Core.Api/Controllers/RecieveRcsController.cs index 80cd444..4b50591 100644 --- a/src/Khd.Core.Api/Controllers/RecieveRcsController.cs +++ b/src/Khd.Core.Api/Controllers/RecieveRcsController.cs @@ -88,5 +88,17 @@ namespace Khd.Core.Api.Controllers LogManager.Info($"AgvTaskComplete 接口收到消息"); return _application.AGVTaskComplete(); } + + /// + /// 通知agv停止或启动 + /// + /// + /// + [HttpPost("AgvStopOrStart")] + public ReponseMessage AGVStopOrStart(CallAgvStopOrStart agvStopOrStart) + { + LogManager.Info($"AGVStopOrStart 接口收到消息: {agvStopOrStart.ToJsonString()}"); + return _application.CallAgvStopOrStart(agvStopOrStart); + } } } \ No newline at end of file diff --git a/src/Khd.Core.Application/HttpHelper.cs b/src/Khd.Core.Application/HttpHelper.cs new file mode 100644 index 0000000..f79cf3e --- /dev/null +++ b/src/Khd.Core.Application/HttpHelper.cs @@ -0,0 +1,170 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; + +namespace Khd.Core.Application +{ + public class HttpHelper + { + + public static string SendPostMessage(string ip, int port, string url, string message, string contentType = "application/Text") + { + string retsult = HttpPost("http://" + ip + ":" + port + "/" + url, message, contentType, 30, null); + return retsult; + } + + + public static string SendGetMessage(string ip, int port, string url) + { + string retsult = HttpGet("http://" + ip + ":" + port + "/" + url); + return retsult; + } + + /// + /// 发起POST同步请求 + /// + /// + /// + /// + /// application/xml、application/json、application/text、application/x-www-form-urlencoded + /// 填充消息头 + /// + public static string HttpPost(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary headers = null) + { + postData = postData ?? ""; + using (HttpClient client = new HttpClient()) + { + if (headers != null) + { + foreach (var header in headers) + client.DefaultRequestHeaders.Add(header.Key, header.Value); + } + using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8)) + { + if (contentType != null) + httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); + + HttpResponseMessage response = client.PostAsync(url, httpContent).Result; + return response.Content.ReadAsStringAsync().Result; + } + } + } + + + /// + /// 发起POST异步请求 + /// + /// + /// + /// application/xml、application/json、application/text、application/x-www-form-urlencoded + /// 填充消息头 + /// + public static async Task HttpPostAsync(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary headers = null) + { + postData = postData ?? ""; + using (HttpClient client = new HttpClient()) + { + client.Timeout = new TimeSpan(0, 0, timeOut); + if (headers != null) + { + foreach (var header in headers) + client.DefaultRequestHeaders.Add(header.Key, header.Value); + } + using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8)) + { + if (contentType != null) + httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); + + HttpResponseMessage response = await client.PostAsync(url, httpContent); + return await response.Content.ReadAsStringAsync(); + } + } + } + + /// + /// 发起GET同步请求 + /// + /// + /// + /// + /// + public static string HttpGet(string url, Dictionary headers = null) + { + using (HttpClient client = new HttpClient()) + { + if (headers != null) + { + foreach (var header in headers) + client.DefaultRequestHeaders.Add(header.Key, header.Value); + } + HttpResponseMessage response = client.GetAsync(url).Result; + return response.Content.ReadAsStringAsync().Result; + } + } + + /// + /// 发起GET异步请求 + /// + /// + /// + /// + /// + public static async Task HttpGetAsync(string url, Dictionary headers = null) + { + using (HttpClient client = new HttpClient()) + { + if (headers != null) + { + foreach (var header in headers) + client.DefaultRequestHeaders.Add(header.Key, header.Value); + } + HttpResponseMessage response = await client.GetAsync(url); + return await response.Content.ReadAsStringAsync(); + } + } + + /// + /// 发起GET同步请求 + /// + /// + /// + /// + /// + public static string HttpDelete(string url, Dictionary headers = null) + { + using (HttpClient client = new HttpClient()) + { + if (headers != null) + { + foreach (var header in headers) + client.DefaultRequestHeaders.Add(header.Key, header.Value); + } + HttpResponseMessage response = client.DeleteAsync(url).Result; + return response.Content.ReadAsStringAsync().Result; + } + } + + /// + /// 发起GET异步请求 + /// + /// + /// + /// + /// + public static async Task HttpDeleteAsync(string url, Dictionary headers = null) + { + using (HttpClient client = new HttpClient()) + { + if (headers != null) + { + foreach (var header in headers) + client.DefaultRequestHeaders.Add(header.Key, header.Value); + } + HttpResponseMessage response = await client.DeleteAsync(url); + return await response.Content.ReadAsStringAsync(); + } + } + } +} diff --git a/src/Khd.Core.Application/Interface/IWcsTaskApplication.cs b/src/Khd.Core.Application/Interface/IWcsTaskApplication.cs index 1071720..285e52c 100644 --- a/src/Khd.Core.Application/Interface/IWcsTaskApplication.cs +++ b/src/Khd.Core.Application/Interface/IWcsTaskApplication.cs @@ -32,5 +32,6 @@ namespace Khd.Core.Application.Interface ReponseagvCallbackDto AgvCallback2(agvCallbackDto agvCallbackDto); ReponseMessage CallMaterial(CallMaterial callMaterial); ReponseMessage AGVTaskComplete(); + ReponseMessage CallAgvStopOrStart(CallAgvStopOrStart agvStopOrStart); } } \ No newline at end of file diff --git a/src/Khd.Core.Application/WcsTaskApplication.cs b/src/Khd.Core.Application/WcsTaskApplication.cs index e5c7408..a91d715 100644 --- a/src/Khd.Core.Application/WcsTaskApplication.cs +++ b/src/Khd.Core.Application/WcsTaskApplication.cs @@ -3,8 +3,10 @@ using Khd.Core.Application.Interface; using Khd.Core.Domain.Dto.webapi; using Khd.Core.Domain.Models; using Khd.Core.EntityFramework; +using Masuit.Tools; using Masuit.Tools.Logging; using Microsoft.Extensions.DependencyInjection; +using Newtonsoft.Json; using System; using System.Linq; using Z.EntityFramework.Plus; @@ -163,7 +165,7 @@ namespace Khd.Core.Application if (baseEquip != null) { var wcsTask = _dbContext.WcsTask - .Where(t => (t.taskType == 32 || t.taskType == 48)&&(t.endPointId == baseEquip.objid || t.currPointId == baseEquip.objid)) + .Where(t => (t.taskType == 32 || t.taskType == 48) && (t.endPointId == baseEquip.objid || t.currPointId == baseEquip.objid)) .FirstOrDefault(); if (wcsTask != null) { @@ -432,5 +434,44 @@ namespace Khd.Core.Application } return reponseMessage; } + + public ReponseMessage CallAgvStopOrStart(CallAgvStopOrStart agvStopOrStart) + { + try + { + string Ip = "172.16.12.24"; + int Port = 8182; + if (agvStopOrStart.Meth.ToLower() == "stop") + { + var AgvMessage = new + { + reqCode= _snowId.NextId().ToString(), + robots=new string[] {agvStopOrStart.AgvCode} + }; + string result = HttpHelper.SendPostMessage(Ip, Port, "rcms/services/rest/hikRpcService/stopRobot", AgvMessage.ToJsonString()); + ReponseMessage reponseMessage = JsonConvert.DeserializeObject(result); + return reponseMessage; + } + else if (agvStopOrStart.Meth.ToLower() == "start") + { + var AgvMessage = new + { + reqCode = _snowId.NextId().ToString(), + robots = new string[] { agvStopOrStart.AgvCode } + }; + string result = HttpHelper.SendPostMessage(Ip, Port, "rcms/services/rest/hikRpcService/resumeRobot", AgvMessage.ToJsonString()); + ReponseMessage reponseMessage = JsonConvert.DeserializeObject(result); + return reponseMessage; + } + else + { + return new ReponseMessage() { code = "1", message = "传入Meth不正确" }; + } + } + catch + { + return new ReponseMessage() { code = "1", message = "调用失败" }; + } + } } } \ No newline at end of file diff --git a/src/Khd.Core.Domain/Dto/webapi/CallMaterial.cs b/src/Khd.Core.Domain/Dto/webapi/CallMaterial.cs index 71d34d0..a366da8 100644 --- a/src/Khd.Core.Domain/Dto/webapi/CallMaterial.cs +++ b/src/Khd.Core.Domain/Dto/webapi/CallMaterial.cs @@ -5,4 +5,10 @@ public string rawOutstockId { get; set; } public string method { get; set; } } + + public class CallAgvStopOrStart + { + public string Meth { get; set; } + public string AgvCode { get; set; } + } } diff --git a/src/Khd.Core.Wcs/appsettings.json b/src/Khd.Core.Wcs/appsettings.json index d70ecf1..0679e37 100644 --- a/src/Khd.Core.Wcs/appsettings.json +++ b/src/Khd.Core.Wcs/appsettings.json @@ -42,7 +42,7 @@ "Logging": { "LogLevel": { "Default": "Information", - "Microsoft": "Warning", + "Microsoft": "Error", "Microsoft.Hosting.Lifetime": "Information" } }