From 26f1397c8ca9bede4f1a476a1380c2b06499ffa5 Mon Sep 17 00:00:00 2001 From: "boxu.zheng" Date: Tue, 23 Jul 2024 16:40:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=20=E5=BC=80=E5=85=B3?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IDataExchangeInterfaceConfigAppService.cs | 2 +- .../DataExchangeInterfaceConfigAppService.cs | 142 +++++++++++++++++- 2 files changed, 135 insertions(+), 9 deletions(-) diff --git a/InterFaceContorl/Dy_Exchange/src/Dy_Exchange.Application.Contracts/Z_Business/IDataExchangeInterfaceConfigAppService.cs b/InterFaceContorl/Dy_Exchange/src/Dy_Exchange.Application.Contracts/Z_Business/IDataExchangeInterfaceConfigAppService.cs index d612d6647..1de479da0 100644 --- a/InterFaceContorl/Dy_Exchange/src/Dy_Exchange.Application.Contracts/Z_Business/IDataExchangeInterfaceConfigAppService.cs +++ b/InterFaceContorl/Dy_Exchange/src/Dy_Exchange.Application.Contracts/Z_Business/IDataExchangeInterfaceConfigAppService.cs @@ -19,5 +19,5 @@ public interface IDataExchangeInterfaceConfigAppService : CreateUpdateDataExchangeInterfaceConfigDto> { Task CheckWmsStatusAsync(int wmsPort); - Task CloseWmsAsync(string wmsCode,string cmdPath); + Task ReSetAsync(); } \ No newline at end of file diff --git a/InterFaceContorl/Dy_Exchange/src/Dy_Exchange.Application/Z_Business/DataExchangeInterfaceConfigAppService.cs b/InterFaceContorl/Dy_Exchange/src/Dy_Exchange.Application/Z_Business/DataExchangeInterfaceConfigAppService.cs index 03e417664..5f50aad79 100644 --- a/InterFaceContorl/Dy_Exchange/src/Dy_Exchange.Application/Z_Business/DataExchangeInterfaceConfigAppService.cs +++ b/InterFaceContorl/Dy_Exchange/src/Dy_Exchange.Application/Z_Business/DataExchangeInterfaceConfigAppService.cs @@ -2,7 +2,11 @@ using System; using System.Diagnostics; using System.Linq; using System.Net; +using System.Net.Http; using System.Net.NetworkInformation; +using System.Security.Policy; +using System.Text; +using System.Threading; using System.Threading.Tasks; using Dy_Exchange.Enums; using Dy_Exchange.Permissions; @@ -29,7 +33,13 @@ public class DataExchangeInterfaceConfigAppService : ZbxBase + { + if (e.Data != null) + { + output.AppendLine(e.Data); + } + }; + process.ErrorDataReceived += (sender, e) => + { + if (e.Data != null) + { + output.AppendLine("ERROR: " + e.Data); + } + }; + + process.Start(); + process.BeginOutputReadLine(); + process.BeginErrorReadLine(); + process.WaitForExit(); + + return output.ToString(); + } + catch (Exception ex) + { + return $"An error occurred: {ex.Message}"; + } + } + [HttpPost("CloseWmsAsync")] [Authorize] - public async Task CloseWmsAsync(string wmsCode,string cmdPath) + public async Task CloseWmsAsync() { var list=await _repository.GetListAsync().ConfigureAwait(false); var removeWmsCodeList=list.Where(p => p.Code != wmsCode).ToList(); - if (removeWmsCodeList.Any(p => p.Status == EnumStatus.Running)) + if (removeWmsCodeList.Any(p => p.Status == EnumStatus.Running||p.Active==true)) { - throw new UserFriendlyException("有未关闭的接口程序,无法关闭WMS"); + throw new UserFriendlyException($"有未关闭的接口程序{removeWmsCodeList.First().Code}无法关闭WMS"); } else { + var first=removeWmsCodeList.OrderByDescending(p => p.LastModificationTime).First(); + TimeSpan difference = first.LastModificationTime.Value - DateTime.Now; + if (Math.Abs(difference.TotalMinutes) <= 6) + { + throw new UserFriendlyException($"接口刚刚关闭,请5分钟后再关闭WMS。最后一次关闭时间相差{Math.Abs((int)difference.TotalMinutes)}分钟"); + } + var wmsConfig=await _repository.FindAsync(p => p.Code == wmsCode).ConfigureAwait(false); if (wmsConfig == null) { throw new UserFriendlyException("未找到WMS配置项目"); } - wmsConfig.Status = EnumStatus.Closed; - wmsConfig.Active = false; - await _repository.UpdateAsync(wmsConfig).ConfigureAwait(false); + var stop1= ExecuteBatFile(cmdClosePath1); + Thread.Sleep(2000); // 休眠2秒 + var stop2 = ExecuteBatFile(cmdClosePath2); return true; } @@ -90,8 +147,77 @@ public class DataExchangeInterfaceConfigAppService : ZbxBase StartWmsAsync(string wmsCode, string cmdPath) + public async Task StartWmsAsync() { + ExecuteBatFile(cmdStartPath); + return false; } + + [HttpPut("Base/Update")] + public override async Task UpdateAsync(Guid id, CreateUpdateDataExchangeInterfaceConfigDto input) + { + if (input.Code != wmsCode&&input.Active==true) + { + bool isPageLoaded = await IsPageLoadedSuccessfullyAsync(wmsUrl).ConfigureAwait(false); + if (!isPageLoaded) + { + throw new UserFriendlyException("WMS未启动成功,请稍后再启动接口"); + } + } + + //开启wms + if (input.Code == wmsCode&&input.Active==true) + { + input.Status = EnumStatus.Running; + await StartWmsAsync().ConfigureAwait(false); + } + if(input.Code == wmsCode && input.Active == false) + { + input.Status = EnumStatus.Closed; + await CloseWmsAsync().ConfigureAwait(false); + } + + return await base.UpdateAsync(id, input).ConfigureAwait(false); + } + + [HttpPost("ReSetAsync")] + [Authorize] + public async Task ReSetAsync() + { + var list=await _repository.GetListAsync().ConfigureAwait(false); + list.ForEach(p => + { + p.Active = false; + p.Status = EnumStatus.Closed; + }); + await _repository.UpdateManyAsync(list).ConfigureAwait(false); + } + + static async Task IsPageLoadedSuccessfullyAsync(string url) + { + try + { + using HttpClient client = new HttpClient(); + HttpResponseMessage response = await client.GetAsync(url).ConfigureAwait(false); + + // 检查响应状态码是否为200(OK) + if (response.IsSuccessStatusCode) + { + // 你可以进一步检查内容,确保页面加载完毕 + string content = await response.Content.ReadAsStringAsync().ConfigureAwait(false); + // 你可以根据实际需求进一步检查内容 + return !string.IsNullOrEmpty(content); + } + else + { + return false; + } + } + catch (Exception ex) + { + Console.WriteLine($"An error occurred: {ex.Message}"); + return false; + } + } }