Browse Source

接口 开关程序

dev_DY_CC
郑勃旭 9 months ago
parent
commit
26f1397c8c
  1. 2
      InterFaceContorl/Dy_Exchange/src/Dy_Exchange.Application.Contracts/Z_Business/IDataExchangeInterfaceConfigAppService.cs
  2. 142
      InterFaceContorl/Dy_Exchange/src/Dy_Exchange.Application/Z_Business/DataExchangeInterfaceConfigAppService.cs

2
InterFaceContorl/Dy_Exchange/src/Dy_Exchange.Application.Contracts/Z_Business/IDataExchangeInterfaceConfigAppService.cs

@ -19,5 +19,5 @@ public interface IDataExchangeInterfaceConfigAppService :
CreateUpdateDataExchangeInterfaceConfigDto>
{
Task<bool> CheckWmsStatusAsync(int wmsPort);
Task<bool> CloseWmsAsync(string wmsCode,string cmdPath);
Task ReSetAsync();
}

142
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<DataExchangeInterfa
protected override string UpdatePolicyName { get; set; } = Dy_ExchangePermissions.DataExchangeInterfaceConfig.Update;
protected override string DeletePolicyName { get; set; } = Dy_ExchangePermissions.DataExchangeInterfaceConfig.Delete;
private readonly IDataExchangeInterfaceConfigRepository _repository;
public string wmsCode = "WMS";
public string cmdStartPath = @"D:\Proc_DyNevWms\publish\start.cmd";
public string cmdClosePath1 = @"D:\Proc_DyNevWms\publish\stop.cmd";
public string cmdClosePath2 = @"D:\Proc_DyNevWms\publish\TaskKill.exe";
public string wmsUrl = "http://10.164.233.5:60085/swagger/AbpTenant/swagger.json";
private readonly IDataExchangeInterfaceConfigRepository _repository;
public DataExchangeInterfaceConfigAppService(IDataExchangeInterfaceConfigRepository repository) : base(repository)
{
@ -62,27 +72,74 @@ public class DataExchangeInterfaceConfigAppService : ZbxBase<DataExchangeInterfa
return inUse;
}
static string ExecuteBatFile(string filePath)
{
try
{
Process process = new Process();
process.StartInfo.FileName = filePath;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
StringBuilder output = new StringBuilder();
process.OutputDataReceived += (sender, e) =>
{
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<bool> CloseWmsAsync(string wmsCode,string cmdPath)
public async Task<bool> 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<DataExchangeInterfa
[HttpPost("StartWmsAsync")]
[Authorize]
public async Task<bool> StartWmsAsync(string wmsCode, string cmdPath)
public async Task<bool> StartWmsAsync()
{
ExecuteBatFile(cmdStartPath);
return false;
}
[HttpPut("Base/Update")]
public override async Task<DataExchangeInterfaceConfigDto> 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<bool> 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;
}
}
}

Loading…
Cancel
Save