From b5f2cd0a75729313a22adf7f93216214edbd6683 Mon Sep 17 00:00:00 2001 From: lxw Date: Fri, 31 May 2024 11:24:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B6=88=E9=98=B2=E7=A7=BB=E4=BB=93=20=20?= =?UTF-8?q?=E7=94=B5=E6=B1=A0=E7=A7=BB=E4=BB=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Service/Execute/Api/ChargeApi.cs | 46 +++++++++ Service/Execute/Invoker.cs | 81 +++++++++++++++- Service/Execute/Model/RfidReadModel.cs | 3 + Service/Mgr/PlcTaskMgr.cs | 49 ++++++++++ Service/Plc/Client/PlcMgr.cs | 49 +--------- Service/Station/BinInfoService.cs | 97 +++++++++++++++++-- Service/Station/MonitorService.cs | 27 ++++-- .../Controllers/SwapMonitorController.cs | 16 +-- 8 files changed, 296 insertions(+), 72 deletions(-) create mode 100644 Service/Execute/Api/ChargeApi.cs create mode 100644 Service/Mgr/PlcTaskMgr.cs diff --git a/Service/Execute/Api/ChargeApi.cs b/Service/Execute/Api/ChargeApi.cs new file mode 100644 index 0000000..c3276a8 --- /dev/null +++ b/Service/Execute/Api/ChargeApi.cs @@ -0,0 +1,46 @@ +using HybirdFrameworkCore.Entity; +using log4net; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Service.Execute.Model; + +namespace Service.Execute.Api; + +public class ChargeApi +{ + private static readonly ILog Log = LogManager.GetLogger(typeof(ChargeApi)); + + private static readonly string BASE_URL = "http://localhost:5035"; + + private static readonly HttpClient _httpClient = new HttpClient() + { + Timeout = TimeSpan.FromSeconds(60) + }; + + public static async Task StopCharge(string binNo) + { + Log.Info("StopCharge"); + string url = BASE_URL + "/api/Charge/StopChargeByBinNo/" + binNo; + try + { + string s = await _httpClient.GetStringAsync(url); + if (!String.IsNullOrWhiteSpace(s)) + { + Result? succ = JsonConvert.DeserializeObject>(s); + if (succ.IsSuccess) + { + return true; + } + + return false; + } + + return false; + } + catch (Exception e) + { + Console.WriteLine(e); + return false; + } + } +} \ No newline at end of file diff --git a/Service/Execute/Invoker.cs b/Service/Execute/Invoker.cs index 9fbdf3b..7aefe68 100644 --- a/Service/Execute/Invoker.cs +++ b/Service/Execute/Invoker.cs @@ -45,7 +45,7 @@ public class Invoker return InvokeStatus.Done; } - + public static InvokeStatus Invoke(string name, int duration, int times, Func cancel, Func done, Action doAction, Action exceptionAction) @@ -70,7 +70,6 @@ public class Invoker Log.Error($"{name}", e); exceptionAction(); } - } Log.Info($" {name} done"); @@ -79,7 +78,8 @@ public class Invoker public static InvokeStatus Invoke(string name, int duration, int times, Func cancel, Func done, Action doAction, - Action timeoutAction, bool isTimeOutExit, Action exceptionAction, int timeOutActionTime,InvokeStatus timeOutException) + Action timeoutAction, bool isTimeOutExit, Action exceptionAction, int timeOutActionTime, + InvokeStatus timeOutException) { int count = 0; while (!done()) @@ -120,6 +120,81 @@ public class Invoker return InvokeStatus.Done; } + + public static InvokeStatus Invoke(string name, int duration, int times, Func done, + Action doAction, + Action timeoutAction, bool isExcepOutExit, Action exceptionAction, int timeOutActionTime, + InvokeStatus excepOutException) + { + int count = 0; + while (!done()) + { + Log.Info($"begin {name}"); + Thread.Sleep(duration); + try + { + doAction(); + } + catch (Exception e) + { + Log.Error($"{name}", e); + exceptionAction(); + if (isExcepOutExit) + { + return excepOutException; + } + } + + if (count++ > times) + { + if (count % timeOutActionTime == 0) + { + timeoutAction(); + Log.Info($" {name} timeout"); + } + } + } + + Log.Info($" {name} done"); + return InvokeStatus.Done; + } + + + public static InvokeStatus Invoke(string name, int duration, int times, Func done, + Action doAction, + Action timeoutAction, bool isTimeOut, Action exceptionAction, + InvokeStatus timeOutActionTime) + { + int count = 0; + while (!done()) + { + Log.Info($"begin {name}"); + Thread.Sleep(duration); + try + { + doAction(); + } + catch (Exception e) + { + Log.Error($"{name}", e); + exceptionAction(); + } + + if (count++ > times) + { + Log.Info($" {name} timeout"); + timeoutAction(); + if (isTimeOut) + { + return timeOutActionTime; + } + } + } + + Log.Info($" {name} done"); + return InvokeStatus.Done; + } + public static InvokeStatus Invoke(string name, int duration, int times, Func cancel, Func done, Action doAction) { diff --git a/Service/Execute/Model/RfidReadModel.cs b/Service/Execute/Model/RfidReadModel.cs index 98fcbc0..ff0dda1 100644 --- a/Service/Execute/Model/RfidReadModel.cs +++ b/Service/Execute/Model/RfidReadModel.cs @@ -2,6 +2,9 @@ public class RfidReadModel { + /// + /// 1:成功 0:失败 + /// public string Result { get; set; } /// diff --git a/Service/Mgr/PlcTaskMgr.cs b/Service/Mgr/PlcTaskMgr.cs new file mode 100644 index 0000000..d913fdd --- /dev/null +++ b/Service/Mgr/PlcTaskMgr.cs @@ -0,0 +1,49 @@ +using HybirdFrameworkCore.Autofac.Attribute; +using log4net; +using Service.Execute; +using Service.Plc.Client; + +namespace Service.Mgr; + +[Scope("SingleInstance")] +public class PlcTaskMgr +{ + private static readonly ILog Log = LogManager.GetLogger(typeof(PlcTaskMgr)); + + + + public bool QueryPlcTask(int taskNo, int ingStatus, int exceptionStatus) + { + Log.Info($"PlcTaskMgr.QueryPlcTask taskNo={taskNo}"); + bool isSuccess = false; + + InvokeStatus invokeStatus = Invoker.Invoke("QueryPlcTask", 1000, 5, + () => isSuccess, () => + { + var readPlcTaskStatus = PlcMgr.ReadPlcTaskStatus(); + if (ingStatus == readPlcTaskStatus) + { + } + else if (exceptionStatus == readPlcTaskStatus) + { + Log.Info( + $"PlcTaskMgr.QueryPlcTask taskNo={taskNo} execute fail readPlcTaskStatus={readPlcTaskStatus}"); + throw new Exception("PlcTaskMgr.QueryPlcTask readPlcTaskStatus=" + readPlcTaskStatus); + } + else if (9000 == readPlcTaskStatus) + { + isSuccess = true; + } + }, () => { Log.Info($"PlcTaskMgr.QueryPlcTask taskNo={taskNo} execute ing"); }, true, () => { } + , 5, InvokeStatus.Exception); + + if (InvokeStatus.Exception == invokeStatus) + { + return false; + } + + return true; + } + + +} \ No newline at end of file diff --git a/Service/Plc/Client/PlcMgr.cs b/Service/Plc/Client/PlcMgr.cs index 19434d0..06b00b0 100644 --- a/Service/Plc/Client/PlcMgr.cs +++ b/Service/Plc/Client/PlcMgr.cs @@ -347,53 +347,6 @@ public class PlcMgr } return bResult; } - private static readonly ILog Log = LogManager.GetLogger(typeof(FireProtectionRelocation)); - /// - /// 消防移仓 - /// - /// 入仓位选择 - /// - public static bool FireProtectionRelocation(ushort entrySelection) - { - //TODO:: - bool bResult = false; - if (PlcClient != null) - { - FireProtectionRelocation distributeTask = new() - { - TaskType = 5, - EntrySelection = entrySelection, - ExitSelection = 5, - BatteryPackType = 0 - }; - byte[] encode = ModelConvert.Encode(distributeTask); - //TODO::現在手動處理encode - for (int i = 0; i < encode.Length; i++) - { - if (i % 2 == 0) - { - var temp = encode[i + 1]; - encode[i + 1] = encode[i]; - encode[i] = temp; - } - } - //需要将数据转换成byte数组 - OperateResult operateResult = PlcClient.Write("x=16;111",encode); - - //查询人物状态 - if (operateResult.IsSuccess) - { - bool readPlcTaskStatus9000 = false; - while (!readPlcTaskStatus9000) - { - Thread.Sleep(1000); - readPlcTaskStatus9000 = PlcMgr.ReadPlcTaskStatus() == 9000; - } - - Log.Info($"execute FireProtectionRelocation success from EntrySelection ={distributeTask.EntrySelection}"); - } - } - return bResult; - } + } \ No newline at end of file diff --git a/Service/Station/BinInfoService.cs b/Service/Station/BinInfoService.cs index 392bc33..8afe4dc 100644 --- a/Service/Station/BinInfoService.cs +++ b/Service/Station/BinInfoService.cs @@ -15,13 +15,21 @@ using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; +using HybirdFrameworkCore.Entity; +using log4net; +using Service.Execute; +using Service.Execute.Api; +using Service.Plc.Client; namespace Service.Station { [Scope("SingleInstance")] public class BinInfoService : BaseServices { - BinInfoRepository _binInfoRepository; + private static readonly ILog Log = LogManager.GetLogger(typeof(BinInfoService)); + private BinInfoRepository _binInfoRepository; + public PlcTaskMgr PlcTaskMgr { get; set; } + public BinInfoService(BinInfoRepository dal) { _binInfoRepository = dal; @@ -37,13 +45,90 @@ namespace Service.Station { RefAsync total = 0; var items = await _binInfoRepository.QueryPageAsync( - entity => true, false, entity => true, + entity => true, false, entity => true, !string.IsNullOrEmpty(input.No), u => u.No == input.No, - !string.IsNullOrEmpty(input.Name), u => u.Name == input.Name, - u=>u.Id,input.Page, input.PageSize, total - ); + !string.IsNullOrEmpty(input.Name), u => u.Name == input.Name, + u => u.Id, input.Page, input.PageSize, total + ); return SqlSugarPagedExtensions.CreateSqlSugarPagedList(items, total, input.Page, input.PageSize); } + + /// + /// 消防移仓 + /// + /// + /// + public Result FireProtectionRelocation(string entrySelection) + { + BinInfo? queryByClause = + _binInfoRepository.QueryByClause(i => i.No.Equals(entrySelection) && i.Exists == 1); + if (queryByClause == null) + { + return Result.Fail("无电池"); + } + + + if (queryByClause.ChargeStatus == 1) + { + if (!StopCharge(entrySelection)) + { + return Result.Fail("停止充电失败"); + } + } + + var result = PlcMgr.DistributeTask(0, ushort.Parse(entrySelection), 5); + + if (!result) + { + return Result.Fail(); + } + + result = PlcMgr.ReadTaskStatus(5); + if (!result) + { + return Result.Fail(); + } + + result = PlcMgr.HoldOn(); + if (!result) + { + return Result.Fail(); + } + + Log.Info($"FireProtectionRelocation task Manual removeBinNo={entrySelection} "); + + + return PlcTaskMgr.QueryPlcTask(5, 1100, 1101) ? Result.Success() : Result.Fail(); + } + + + private bool StopCharge(string binNo) + { + + bool success = false; + int count = 0; + while (!success) + { + ChargeApi.StopCharge(binNo); + BinInfo? bin = _binInfoRepository.QueryByClause(i => i.No.Equals(binNo)); + + if (bin.ChargeStatus == 4) + { + success = true; + } + + Thread.Sleep(1000); + count++; + + if (count > 10) + { + Log.Info($"StopCharge timeout"); + return false; + } + } + + return true; + } } -} +} \ No newline at end of file diff --git a/Service/Station/MonitorService.cs b/Service/Station/MonitorService.cs index 0e630a5..50750dc 100644 --- a/Service/Station/MonitorService.cs +++ b/Service/Station/MonitorService.cs @@ -5,6 +5,7 @@ using Entity.Constant; using Entity.DbModel.Station; using HybirdFrameworkCore.Autofac.Attribute; using HybirdFrameworkCore.Entity; +using log4net; using OfficeOpenXml.FormulaParsing.Excel.Functions.Math; using Repository.Station; using Service.Cloud.Client; @@ -12,6 +13,7 @@ using Service.Execute; using Service.Execute.Api; using Service.Execute.Model; using Service.Init; +using Service.Mgr; using Service.Plc.Client; namespace Service.Station; @@ -19,10 +21,12 @@ namespace Service.Station; [Scope("SingleInstance")] public class MonitorService { + + private static readonly ILog Log = LogManager.GetLogger(typeof(MonitorService)); public BinInfoRepository BinInfoRepository { get; set; } public SwapOrderRepository SwapOrderRepository { get; set; } public ChargeOrderRepository ChargeOrderRepository { get; set; } - + public PlcTaskMgr PlcTaskMgr { get; set; } public Result GetSwapMonitorData() { var configBinInfo = @@ -161,16 +165,22 @@ public class MonitorService return Result.Fail(); } - bool taskSuccess = false; - if (!taskSuccess) + result = PlcMgr.ReadTaskStatus(2); + if (!result) { - taskSuccess=PlcMgr.ReadTaskStatus(2); - PlcMgr.DistributeTask(putBinNo, removeBinNo, 2); + return Result.Fail(); } - var succ = PlcMgr.HoldOn(); + result = PlcMgr.HoldOn(); + if (!result) + { + + return Result.Fail(); + } + Log.Info($"battery move task Manual removeBinNo={removeBinNo}, putBinNo={putBinNo} "); - if (!succ) + var queryPlcTask = PlcTaskMgr.QueryPlcTask(2, 1200, 1201); + if (!queryPlcTask) { return Result.Fail(); } @@ -195,6 +205,9 @@ public class MonitorService removeBin.Soh = -1; BinInfoRepository.Update(removeBin); } + return Result.Success(); + + return Result.Success(); } diff --git a/WebStarter/Controllers/SwapMonitorController.cs b/WebStarter/Controllers/SwapMonitorController.cs index ce86e3f..60d7598 100644 --- a/WebStarter/Controllers/SwapMonitorController.cs +++ b/WebStarter/Controllers/SwapMonitorController.cs @@ -18,10 +18,12 @@ namespace WebStarter.Controllers; public class SwapMonitorController : ControllerBase { private readonly MonitorService _swapMonitorService; + private readonly BinInfoService _binInfoService; - public SwapMonitorController(MonitorService swapMonitorService) + public SwapMonitorController(MonitorService swapMonitorService, BinInfoService binInfoService) { _swapMonitorService = swapMonitorService; + _binInfoService = binInfoService; } @@ -72,7 +74,7 @@ public class SwapMonitorController : ControllerBase var success = await TBoxApi.LockCarManyTimes(StationSoftMgr.SwappingStateMachine.RfidReadModel.VelNo); return success ? Result.Success() : Result.Fail(); } - + /// /// 车辆手动解锁 /// @@ -110,8 +112,6 @@ public class SwapMonitorController : ControllerBase } - - /// /// 换电复位 /// @@ -175,12 +175,12 @@ public class SwapMonitorController : ControllerBase /// /// 消防移仓 /// - /// 入仓位选择:0-无操作、1-缓存位舱位号、2-20:电池存储仓位 + /// 入仓位选择:移仓仓位号 /// - [HttpPost("FireProtectionRelocation/{entrySelet}")] - public async Task> FireProtectionRelocation(ushort entrySelet) + [HttpPost("FireProtectionRelocation/{entrySelect}")] + public Result FireProtectionRelocation(string entrySelect) { - return Result.Success(PlcMgr.FireProtectionRelocation(entrySelet)); + return _binInfoService.FireProtectionRelocation(entrySelect); }