From de2d79f2d7d857fc9b7aabf5c0d4931532ea252b Mon Sep 17 00:00:00 2001 From: liuwf Date: Fri, 19 Jul 2024 18:01:23 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HttpTest/HttpHelper.cs | 313 +++++++++++++++++++++++ HttpTest/HttpTest.csproj | 14 + SlnMesnac.Business/PalletStowBusiness.cs | 4 + SlnMesnac.Business/ProdMgmtBusiness.cs | 77 ++++-- SlnMesnac.sln | 2 +- Test111/Program.cs | 14 + Test111/Test111.csproj | 10 + 7 files changed, 405 insertions(+), 29 deletions(-) create mode 100644 HttpTest/HttpHelper.cs create mode 100644 HttpTest/HttpTest.csproj create mode 100644 Test111/Program.cs create mode 100644 Test111/Test111.csproj diff --git a/HttpTest/HttpHelper.cs b/HttpTest/HttpHelper.cs new file mode 100644 index 0000000..8be2325 --- /dev/null +++ b/HttpTest/HttpHelper.cs @@ -0,0 +1,313 @@ +using Newtonsoft.Json; +using System.Net; +using System.Net.Http; +using System.Text; + +namespace HttpTest +{ + public class HttpHelper + { + + + public static string Get(string serviceAddress) + { + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress); + request.Method = "GET"; + request.ContentType = "text/html;charset=UTF-8"; + HttpWebResponse response = (HttpWebResponse)request.GetResponse(); + Stream myResponseStream = response.GetResponseStream(); + StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8); + string retString = myStreamReader.ReadToEnd(); + myStreamReader.Close(); + myResponseStream.Close(); + return retString; + } + public static async Task GetAsync(string serviceAddress) + { + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress); + request.Method = "GET"; + request.ContentType = "text/html;charset=UTF-8"; + HttpWebResponse response = (HttpWebResponse)request.GetResponse(); + Stream myResponseStream = response.GetResponseStream(); + StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8); + string retString = await myStreamReader.ReadToEndAsync(); + myStreamReader.Close(); + myResponseStream.Close(); + return retString; + } + + + public static string Post111(string url, string jsonContent) + { + HttpClient _client = new HttpClient(); + var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); + + HttpResponseMessage response = _client.PostAsync(url, content).Result; + + if (response.IsSuccessStatusCode) + { + return response.Content.ReadAsStringAsync().Result; + } + else + { + throw new HttpRequestException($"POST request to {url} failed with status code {response.StatusCode}"); + } + } + + + 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; + } + + + + /// + /// 发起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; + } + } + + + + public static string HttpPost(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary headers = null) + { + try + { + postData = postData ?? ""; + string result = Post("http://localhost:5001/wcs/RecieveRcs/AgvTaskComplete", ""); + return result; + + //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; + // } + //} + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + return null; + } + } + + public static string Post(string serviceAddress, string strContent = null) + { + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress); + request.Method = "POST"; + request.ContentType = "application/json"; + //判断有无POST内容 + if (!string.IsNullOrWhiteSpace(strContent)) + { + using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream())) + { + dataStream.Write(strContent); + dataStream.Close(); + } + } + HttpWebResponse response = (HttpWebResponse)request.GetResponse(); + string encoding = response.ContentEncoding; + if (encoding.Length < 1) + { + encoding = "UTF-8"; //默认编码 + } + StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)); + string retString = reader.ReadToEnd(); + return retString; + } + + + + + + //public static void TestConnectionAsync(HttpClient client,string url) + //{ + // try + // { + // HttpResponseMessage response = client.GetAsync(url).Result; + + // if (response.IsSuccessStatusCode) + // { + // Console.WriteLine($"Success: Connected to {url}"); + // } + // else + // { + // Console.WriteLine($"Failure: Unable to connect to {url}. Status code: {response.StatusCode}"); + // } + // } + // catch (Exception ex) + // { + // Console.WriteLine($"Exception: {ex.Message}"); + // } + //} + + + + //public static string Post(string serviceAddress, string strContent = null) + //{ + // HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress); + // request.Method = "POST"; + // request.ContentType = "application/Json"; + // //判断有无POST内容 + // if (!string.IsNullOrWhiteSpace(strContent)) + // { + // using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream())) + // { + // dataStream.Write(strContent); + // dataStream.Close(); + // } + // } + // HttpWebResponse response = (HttpWebResponse)request.GetResponse(); + // string encoding = response.ContentEncoding; + // if (encoding.Length < 1) + // { + // encoding = "UTF-8"; //默认编码 + // } + // StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)); + // string retString = reader.ReadToEnd(); + // return retString; + //} + + //public static string Post1(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; + // } + // } + //} + + + public static async Task PostAsync(string serviceAddress, string strContent = null) + { + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress); + request.Method = "POST"; + request.ContentType = "application/Json"; + //判断有无POST内容 + if (!string.IsNullOrWhiteSpace(strContent)) + { + using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream())) + { + dataStream.Write(strContent); + dataStream.Close(); + } + } + HttpWebResponse response = (HttpWebResponse)request.GetResponse(); + string encoding = response.ContentEncoding; + if (encoding.Length < 1) + { + encoding = "UTF-8"; //默认编码 + } + StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)); + string retString = await reader.ReadToEndAsync(); + return retString; + } + + + /// + /// Agv任务是否完成的接口 + /// + /// + /// + public static bool JudgeAgvTaskComplete() + { + + try + { + string url = $"http://192.168.1.103:5001/wcs/RecieveRcs/callMaterial"; + + string v = HttpHelper.SendPostMessage("127.0.0.1", 5001, "wcs/RecieveRcs/AgvTaskComplete", "", "application/Json"); + + return true; + } + catch (Exception ex) + { + + return false; + } + + } + + + /// + /// 通知Agv小车送料 + /// + /// + /// + public static bool SendAgvTask(string orderId) + { + + try + { + // 创建请求对象 + var request = new + { + RawOutstockId = orderId + }; + + // 序列化为 JSON 字符串 + string jsonData = JsonConvert.SerializeObject(request); + + string url = $"http://127.0.0.1:5001/wcs/RecieveRcs/callMaterial"; + + var Result = Post(url, jsonData); + + return true; + } + catch (Exception ex) + { + return false; + } + + } + + + } +} \ No newline at end of file diff --git a/HttpTest/HttpTest.csproj b/HttpTest/HttpTest.csproj new file mode 100644 index 0000000..e14d70a --- /dev/null +++ b/HttpTest/HttpTest.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/SlnMesnac.Business/PalletStowBusiness.cs b/SlnMesnac.Business/PalletStowBusiness.cs index 8b5432c..aca2df1 100644 --- a/SlnMesnac.Business/PalletStowBusiness.cs +++ b/SlnMesnac.Business/PalletStowBusiness.cs @@ -1,5 +1,6 @@ using Microsoft.Extensions.Logging; using SlnMesnac.Business.@base; +using SlnMesnac.Common; using SlnMesnac.Config; using SlnMesnac.Model.domain; using SlnMesnac.Model.dto; @@ -80,6 +81,9 @@ namespace SlnMesnac.Business /// private void EmptyPalletHandle() { + + + //读取PLC空托盘流转信号 while (true) { diff --git a/SlnMesnac.Business/ProdMgmtBusiness.cs b/SlnMesnac.Business/ProdMgmtBusiness.cs index c5955ca..ebdffd3 100644 --- a/SlnMesnac.Business/ProdMgmtBusiness.cs +++ b/SlnMesnac.Business/ProdMgmtBusiness.cs @@ -1,13 +1,17 @@ using HslCommunication.Secs.Types; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using Newtonsoft.Json; using SlnMesnac.Business.@base; +using SlnMesnac.Common; +using SlnMesnac.Common.Model; using SlnMesnac.Config; using SlnMesnac.Model.domain; using SlnMesnac.Model.dto; using SlnMesnac.Model.enums; using SlnMesnac.Plc; using SlnMesnac.Repository.service; +using SlnMesnac.Repository.service.Impl; using SlnMesnac.Rfid; using System; using System.Collections.Generic; @@ -51,17 +55,21 @@ namespace SlnMesnac.Business public readonly IBasePalletInfoService _basePalletInfoService; public readonly IBaseRealTaskService _baseRealTaskService; - + + public readonly IWmsOutStockService _wmsOutStockService; + private List _palletTasks; private DebugConfig debugConfig = DebugConfig.Instance; - public ProdMgmtBusiness(ILogger logger, AppConfig appConfig, List plcFactories, List rfidFactories, IMesProductPlanService mesProductPlanService, IBasePalletInfoService basePalletInfoService, IBaseRealTaskService baseRealTaskService, List palletTasks,IServiceProvider serviceProvider) : base(logger, appConfig, plcFactories, rfidFactories, serviceProvider) + public ProdMgmtBusiness(IWmsOutStockService wmsOutStockService,ILogger logger, AppConfig appConfig, List plcFactories, List rfidFactories, IMesProductPlanService mesProductPlanService, IBasePalletInfoService basePalletInfoService, IBaseRealTaskService baseRealTaskService, List palletTasks,IServiceProvider serviceProvider) : base(logger, appConfig, plcFactories, rfidFactories, serviceProvider) { + _wmsOutStockService = wmsOutStockService; _mesProductPlanService = mesProductPlanService; _basePalletInfoService = basePalletInfoService; _baseRealTaskService = baseRealTaskService; _palletTasks = palletTasks; + } #region 委托事件 @@ -101,6 +109,7 @@ namespace SlnMesnac.Business { try { + // var aaa = _mesProductPlanService.GetMesProductPlans(); var info = _mesProductPlanService.GetPlanDtos(); RefreshProdPlanListEvent?.Invoke(info); } @@ -137,19 +146,22 @@ namespace SlnMesnac.Business continue; } - if (!plc.readBoolByAddress(GetPlcAddressByConfigKey("设备叫料"))) + BaseRealTask task = _baseRealTaskService.GetExeTask(); + if (task != null) { - RefreshMessage("等待设备叫料信号触发......"); + RefreshMessage("已经叫料,请等待AGV送料......"); + Thread.Sleep(5000); continue; } - BaseRealTask task = _baseRealTaskService.GetExeTask(); - if (task!=null) { - RefreshMessage("已经叫料,请等待AGV送料......"); - + + if (!plc.readBoolByAddress(GetPlcAddressByConfigKey("设备叫料"))) + { + RefreshMessage("等待设备叫料信号触发......"); Thread.Sleep(5000); continue; } + RefreshMessage("设备要料信号触发成功"); @@ -160,8 +172,12 @@ namespace SlnMesnac.Business RefreshMessage($"执行计划:{prodPlan.PlanCode};计划数量:{Math.Round(prodPlan.PlanAmount,2)};完成数量:{Math.Round(prodPlan.CompleteAmount,2)};发起叫料申请等待AGV送料......"); + //查询下发agv的主键 + WmsRawOutstock wmsRaw = _wmsOutStockService.GetProdPlanByPlanCode(prodPlan.PlanCode); + + //进行叫料 - if (ApplyDeliveryHandle(productPlanDto.MaterialId.ToString())) + if (ApplyDeliveryHandle(wmsRaw.rawOutstockId.ToString())) { RefreshMessage("根据计划自动申请叫料成功"); #region 本地创建一个叫料任务 @@ -210,19 +226,19 @@ namespace SlnMesnac.Business if (plc == null) { - _logger.LogInformation("读取物料到位信号,PLC连接信息为空......"); + RefreshMessage("读取物料到位信号,PLC连接信息为空......"); Thread.Sleep(3000); continue; } if (!plc.readBoolByAddress(GetPlcAddressByConfigKey("物料到位"))) { - _logger.LogInformation("等待物料到位信号触发......"); + RefreshMessage("等待物料到位信号触发......"); Thread.Sleep(3000); continue; } - // TODO 时间校验 + // TODO 时间校验,修改为计算设备运行20分钟 bool timeCheck = JudgeProductTime(); if (!timeCheck) { @@ -356,38 +372,43 @@ namespace SlnMesnac.Business } /// - /// 申请叫料,TODO等待调度接口 + /// 申请叫料,等待调度接口 /// /// /// /// - public bool ApplyDeliveryHandle(string materialCode) + public bool ApplyDeliveryHandle(String taskId) { bool result = false; try { - var plc = base.GetPlcByKey("plc"); - - if (plc != null) - { - if (string.IsNullOrEmpty(materialCode)) - { - throw new ArgumentException("申请叫料处理异常:物料参数为空"); - } + string url = $"http://localhost:6001/wcs/RecieveRcs/callMaterial"; + //询问小车是否离开接口 0已经离开 + // string httpResult = HttpHelper.SendPostMessage("127.0.0.1", 6001, "wcs/RecieveRcs/AgvTaskComplete", "", "application/Json"); + + var content = new + { + rawOutstockId = taskId, + }; - // TODO -- 修改为调用wcs接口,参数为申请单id,同时本地生成一个叫料任务,任务状态为执行中 - // plc.writeInt16ByAddress(base.GetPlcAddressByConfigKey("WCS叫料"), 1); - // plc.writeStringByAddress(base.GetPlcAddressByConfigKey("物料型号"), materialCode); + string message = JsonConvert.SerializeObject(content); + //询问小车是否离开接口 0已经离开 + string httpResult = HttpHelper.SendPostMessage("127.0.0.1", 6001, "wcs/RecieveRcs/callMaterial", message, "application/Json"); + var reponseMessage = JsonConvert.DeserializeObject(httpResult); + if (reponseMessage != null && reponseMessage.code == "0") + { + RefreshMessage("Agv叫料成功"); result = true; } - + } - catch (Exception e) + catch (Exception ex) { - throw new InvalidOperationException($"申请叫料处理异常:{e.Message}"); + RefreshMessage($"申请叫料处理异常:{ex.Message}"); + result = false; } return result; diff --git a/SlnMesnac.sln b/SlnMesnac.sln index d9e4fe6..6fe744e 100644 --- a/SlnMesnac.sln +++ b/SlnMesnac.sln @@ -31,7 +31,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SlnMesnac.Redis", "SlnMesna EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SlnMesnac.Extensions", "SlnMesnac.Extensions\SlnMesnac.Extensions.csproj", "{C2CEC75D-FA00-426A-9344-9CF3A8C43BE2}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SlnMesnac.Generate", "SlnMesnac.Generate\SlnMesnac.Generate.csproj", "{DD53E273-786B-4947-8CF0-B7C472E10B8D}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SlnMesnac.Generate", "SlnMesnac.Generate\SlnMesnac.Generate.csproj", "{DD53E273-786B-4947-8CF0-B7C472E10B8D}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/Test111/Program.cs b/Test111/Program.cs new file mode 100644 index 0000000..276d99b --- /dev/null +++ b/Test111/Program.cs @@ -0,0 +1,14 @@ +using HttpTest; + +namespace Test111 +{ + internal class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + // string v = HttpHelper.SendPostMessage("127.0.0.1", 5001, "wcs/RecieveRcs/AgvTaskComplete", "", "application/Json"); + HttpHelper.JudgeAgvTaskComplete(); + } + } +} \ No newline at end of file diff --git a/Test111/Test111.csproj b/Test111/Test111.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/Test111/Test111.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + +