From 3322b2867c5740970e274f5587e0f2980e98d7eb Mon Sep 17 00:00:00 2001 From: lxw Date: Wed, 29 May 2024 21:00:22 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=B5=E6=B1=A0=E8=87=AA=E5=8A=A8=E7=A7=BB?= =?UTF-8?q?=E4=BB=93=E4=BB=BB=E5=8A=A1=EF=BC=8Ctbox=E5=A4=9A=E6=AC=A1?= =?UTF-8?q?=E7=9A=84=E8=AF=BB=EF=BC=8Cplcreset?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ModbusTcpMaster/ModbusTcpMaster.cs | 8 +- .../BusinessTask/MyTask/BatteryMoveTask.cs | 75 +++++++++++++++++++ Service/Execute/Api/PlcApi.cs | 14 ++-- Service/Execute/Api/TboxApi.cs | 31 ++++++++ Service/Execute/Mgr/CommonMgr.cs | 5 ++ Service/Execute/StationSoftMgr.cs | 3 + Service/Execute/Step/CarCtrlState.cs | 2 +- Service/Execute/Step/DoSwappingState.cs | 4 +- Service/Execute/Step/SwapDoneState.cs | 2 +- Service/Plc/Client/PlcClient.cs | 2 +- Service/Plc/Client/PlcMgr.cs | 8 ++ Service/Station/MonitorService.cs | 27 +++++-- 12 files changed, 161 insertions(+), 20 deletions(-) create mode 100644 Service/BusinessTask/MyTask/BatteryMoveTask.cs diff --git a/HybirdFrameworkDriver/ModbusTcpMaster/ModbusTcpMaster.cs b/HybirdFrameworkDriver/ModbusTcpMaster/ModbusTcpMaster.cs index 6983728..252468b 100644 --- a/HybirdFrameworkDriver/ModbusTcpMaster/ModbusTcpMaster.cs +++ b/HybirdFrameworkDriver/ModbusTcpMaster/ModbusTcpMaster.cs @@ -121,6 +121,11 @@ public class ModbusTcpMaster GetLog().Info("stop listen"); } + public void Reset() + { + ReadAction(this); + } + public byte[]? ReadRegister(int registerNo, int start, int length) { start = start % 16 == 0 ? start / 16 : start / 16 + 1; @@ -207,7 +212,6 @@ public class ModbusTcpMaster { return ModbusTcpNet.Write(address, value); } - public bool WriteValue(ModbusProperty property) @@ -255,7 +259,7 @@ public class ModbusTcpMaster else { var preWriteCont = BitUtls.ProcessEnding(setValue, ByteSeq, WordSeq); - + operateResult = ModbusTcpNet.Write("x=16;" + registerNo, preWriteCont); result = operateResult.IsSuccess; } diff --git a/Service/BusinessTask/MyTask/BatteryMoveTask.cs b/Service/BusinessTask/MyTask/BatteryMoveTask.cs new file mode 100644 index 0000000..d39d5ba --- /dev/null +++ b/Service/BusinessTask/MyTask/BatteryMoveTask.cs @@ -0,0 +1,75 @@ +using Autofac; +using Entity.Constant; +using Entity.DbModel.Station; +using HybirdFrameworkCore.Autofac; +using HybirdFrameworkCore.Entity; +using log4net; +using Repository.Station; +using Service.Execute; +using Service.Execute.Api; +using Service.Execute.Model; +using Service.Plc.Client; +using Service.Station; + +namespace Service.BusinessTask.MyTask; + +/// +/// 电池移仓任务 +/// +public class BatteryMoveTask : AbstractTaskHandler +{ + private static readonly ILog Log = LogManager.GetLogger(typeof(BatteryMoveTask)); + + + + private readonly BinInfoRepository _binInfoRepository = + AppInfo.Container.Resolve(); + + private readonly MonitorService _monitorService = + AppInfo.Container.Resolve(); + + protected override int Interval() + { + return 20 * 1000; + } + + /// + /// 自动移仓 当缓存仓没有电池的时候 如果有电池充满了 就移仓 + /// + protected override void Handle() + { + var cacheBin = _binInfoRepository.QueryByClause(i => i.CacheBinFlag == 1); + if (cacheBin.Exists == 1) + { + return; + } + + BinInfo? binInfo = _binInfoRepository.QueryListByClause(i => i.ChargeStatus == 4, "battery_enter_seq asc") + .First(); + + if (binInfo == null) + { + return; + } + + Result success = _monitorService.BatteryRelocation(ushort.Parse(binInfo.No), ushort.Parse(cacheBin.No)); + + //查询人物状态 + if (success.IsSuccess) + { + bool readPlcTaskStatus9000 = false; + while (!readPlcTaskStatus9000) + { + Thread.Sleep(1000); + readPlcTaskStatus9000 = PlcMgr.ReadPlcTaskStatus() == 9000; + } + + Log.Info($"execute BatteryMoveTask success from binNo ={binInfo.No}"); + } + } + + protected override string Name() + { + return "SwapOrderReportCloudTask"; + } +} \ No newline at end of file diff --git a/Service/Execute/Api/PlcApi.cs b/Service/Execute/Api/PlcApi.cs index 809846a..b8276a2 100644 --- a/Service/Execute/Api/PlcApi.cs +++ b/Service/Execute/Api/PlcApi.cs @@ -17,11 +17,12 @@ public class PlcApi /// PLC是否连接 /// public static bool con; - + /// /// 数据有效时间。单位秒 /// static int DataTimeSeconds = 2; + /// /// 数据刷新时间 /// @@ -41,6 +42,7 @@ public class PlcApi { return PlcMgr.IsAuto(); } + /// /// 入口雷达检测 /// @@ -101,10 +103,10 @@ public class PlcApi /// public static bool StartSwapping(string inBinNo, string outBinNo) { - return PlcMgr.DistributeTask(ushort.Parse(inBinNo),ushort.Parse(outBinNo),1); + return PlcMgr.DistributeTask(ushort.Parse(inBinNo), ushort.Parse(outBinNo), 1); } - - + + /// /// 读取任务状态 /// @@ -129,9 +131,10 @@ public class PlcApi /// public static ushort ChannelStatus() { + PlcMgr.ResetPlc(); var channelStatus = PlcMgr.ChannelStatus(); Log.Info($"plc ChannelStatus = {channelStatus}"); - return channelStatus; + return channelStatus; } @@ -143,5 +146,4 @@ public class PlcApi { return PlcMgr.ReadPlcTaskStatus(); } - } \ No newline at end of file diff --git a/Service/Execute/Api/TboxApi.cs b/Service/Execute/Api/TboxApi.cs index e46e524..9019585 100644 --- a/Service/Execute/Api/TboxApi.cs +++ b/Service/Execute/Api/TboxApi.cs @@ -12,6 +12,8 @@ public class TBoxApi //TODO::TBox 服务地址 private static readonly string BASE_URL = "http://localhost:5036"; + private static int _times = 5; + private static int _successTimes = 3; private static readonly HttpClient _httpClient = new HttpClient() { Timeout = TimeSpan.FromSeconds(60) @@ -81,6 +83,35 @@ public class TBoxApi } } + + public static async Task UnLockCarManyTimes(string carNo) + { + List bools = new List(); + for (int i = 0; i < _times; i++) + { + Thread.Sleep(100); + var unLockCar = await UnLockCar(carNo); + bools.Add(unLockCar); + } + + return bools.Select(i => i).Count() > _successTimes; + + } + + public static async Task LockCarManyTimes(string carNo) + { + List bools = new List(); + for (int i = 0; i < _times; i++) + { + Thread.Sleep(100); + var unLockCar = await LockCar(carNo); + bools.Add(unLockCar); + } + + return bools.Select(i => i).Count() > _successTimes; + + } + /// /// 车辆解锁 /// diff --git a/Service/Execute/Mgr/CommonMgr.cs b/Service/Execute/Mgr/CommonMgr.cs index 1af1346..4f8de43 100644 --- a/Service/Execute/Mgr/CommonMgr.cs +++ b/Service/Execute/Mgr/CommonMgr.cs @@ -225,6 +225,9 @@ public class CommonMgr inBinInfo.Soc = -1; inBinInfo.Soe = -1; inBinInfo.BatteryNo = "-1"; + //修改入仓顺序 + BinInfo binInfo = _binInfoRepository.QueryListByClause(i=>i.BatteryEnterSeq!=null,"battery_enter_seq desc").First(); + inBinInfo.BatteryEnterSeq = binInfo.BatteryEnterSeq + 1; _binInfoRepository.Update(inBinInfo); var upBinInfo = machineSwapOrderBatteryInfo.UpBinInfo; @@ -233,6 +236,8 @@ public class CommonMgr upBinInfo.BatteryNo = "-1"; _binInfoRepository.Update(upBinInfo); } + + } diff --git a/Service/Execute/StationSoftMgr.cs b/Service/Execute/StationSoftMgr.cs index ba5186d..892bc34 100644 --- a/Service/Execute/StationSoftMgr.cs +++ b/Service/Execute/StationSoftMgr.cs @@ -13,6 +13,8 @@ namespace Service.Execute private static readonly AbstractTaskHandler SwapOrderReportCloudTask = new SwapOrderReportCloudTask(); + private static readonly AbstractTaskHandler BatteryMoveTask = new BatteryMoveTask(); + #region Task @@ -22,6 +24,7 @@ namespace Service.Execute public static void StartTasks() { SwapOrderReportCloudTask.Start(); + BatteryMoveTask.Start(); } /// diff --git a/Service/Execute/Step/CarCtrlState.cs b/Service/Execute/Step/CarCtrlState.cs index 118dc9e..012607c 100644 --- a/Service/Execute/Step/CarCtrlState.cs +++ b/Service/Execute/Step/CarCtrlState.cs @@ -50,7 +50,7 @@ public class CarCtrlState : IState return Invoker.Invoke("UnLockCar", 500, 100, machine.IsCanceled, () => machine.VelUnlockFlag, () => { - Task result = TBoxApi.UnLockCar(machine.RfidReadModel.VelNo); + Task result = TBoxApi.UnLockCarManyTimes(machine.RfidReadModel.VelNo); bool unLock = result.Result; if (unLock) { diff --git a/Service/Execute/Step/DoSwappingState.cs b/Service/Execute/Step/DoSwappingState.cs index 098e1d5..cd5eea0 100644 --- a/Service/Execute/Step/DoSwappingState.cs +++ b/Service/Execute/Step/DoSwappingState.cs @@ -115,7 +115,7 @@ public class DoSwappingState : IState return Invoker.Invoke("UnLockCar", 1000, 5, machine.IsCanceled, () => machine.StartSwappingFlag, () => { - Task result = TBoxApi.UnLockCar(machine.RfidReadModel.VelNo); + Task result = TBoxApi.UnLockCarManyTimes(machine.RfidReadModel.VelNo); bool unLock = result.Result; if (unLock) { @@ -357,7 +357,7 @@ public class DoSwappingState : IState else if (channelStatus == 1020) { //需要移车 先解锁 提示移动车辆,等待3分钟 - var lockCar = TBoxApi.LockCar(machine.RfidReadModel.VelNo); + var lockCar = TBoxApi.LockCarManyTimes(machine.RfidReadModel.VelNo); if (lockCar.Result) { // machine.LedTool?.WriteProgramContent(InfoEnum.SwapInfo.ErrChannelStatus.GetLed()); diff --git a/Service/Execute/Step/SwapDoneState.cs b/Service/Execute/Step/SwapDoneState.cs index 0fe2cd8..b1ccd54 100644 --- a/Service/Execute/Step/SwapDoneState.cs +++ b/Service/Execute/Step/SwapDoneState.cs @@ -76,7 +76,7 @@ public class SwapDoneState : IState return Invoker.Invoke("LockCar", 500, 100, machine.IsCanceled, () => machine.VelLockFlag, () => { - Task result = TBoxApi.LockCar(machine.RfidReadModel.VelNo); + Task result = TBoxApi.LockCarManyTimes(machine.RfidReadModel.VelNo); bool unLock = result.Result; if (unLock) { diff --git a/Service/Plc/Client/PlcClient.cs b/Service/Plc/Client/PlcClient.cs index 2b18b06..9e4257c 100644 --- a/Service/Plc/Client/PlcClient.cs +++ b/Service/Plc/Client/PlcClient.cs @@ -20,7 +20,7 @@ public class PlcClient : ModbusTcpMaster ReadAction = BatchRead; Ip = "172.0.20.66"; Port = 502; - Duration = 2000; + Duration = 1000; AutoReConnect = true; _binInfoRepository = binInfoRepository; //DataFormat = HslCommunication.Core.DataFormat.DCBA; diff --git a/Service/Plc/Client/PlcMgr.cs b/Service/Plc/Client/PlcMgr.cs index 93a3315..bb84015 100644 --- a/Service/Plc/Client/PlcMgr.cs +++ b/Service/Plc/Client/PlcMgr.cs @@ -280,6 +280,14 @@ public class PlcMgr return 0; } + /// + /// 重新读 + /// + public static void ResetPlc() + { + PlcClient.Reset(); + } + /// /// 读plc任务状态 diff --git a/Service/Station/MonitorService.cs b/Service/Station/MonitorService.cs index 2685dbe..0e630a5 100644 --- a/Service/Station/MonitorService.cs +++ b/Service/Station/MonitorService.cs @@ -115,10 +115,13 @@ public class MonitorService { SwapAndChargingCountResp chargingCountResp = new() { - ChargeTodayCount = ChargeOrderRepository.GetCount(i => DateTime.Today<=i.EndTime && i.EndTime<= DateUtils.GetTomorrowFirst()), + ChargeTodayCount = ChargeOrderRepository.GetCount(i => + DateTime.Today <= i.EndTime && i.EndTime <= DateUtils.GetTomorrowFirst()), ChargeTotalCount = ChargeOrderRepository.GetCount(i => i.EndTime != null), SwapTodayCount = - SwapOrderRepository.GetCount(i => i.SwapResult != 0 && DateTime.Today<=i.SwapEndTime && i.SwapEndTime<= DateUtils.GetTomorrowFirst()), + SwapOrderRepository.GetCount(i => + i.SwapResult != 0 && DateTime.Today <= i.SwapEndTime && + i.SwapEndTime <= DateUtils.GetTomorrowFirst()), SwapTotalCount = SwapOrderRepository.GetCount(i => i.SwapResult != 0), }; @@ -135,7 +138,8 @@ public class MonitorService { //校验:出仓位 BinInfo? removeBin = BinInfoRepository.QueryByClause(i => - i.No.Equals(removeBinNo) && (i.ChargeStatus == 2 || i.ChargeStatus==4) && i.Exists == 1 && i.AmtLock == 0); + i.No.Equals(removeBinNo) && (i.ChargeStatus == 2 || i.ChargeStatus == 4) && i.Exists == 1 && + i.AmtLock == 0); if (removeBin == null) { @@ -143,18 +147,27 @@ public class MonitorService } BinInfo? putBin = BinInfoRepository.QueryByClause(i => - i.No.Equals(putBinNo) && (i.ChargeStatus == 2 || i.ChargeStatus==4) && i.Exists == 0 && i.AmtLock == 0 && i.Status == 1); + i.No.Equals(putBinNo) && (i.ChargeStatus == 2 || i.ChargeStatus == 4) && i.Exists == 0 && i.AmtLock == 0 && + i.Status == 1); if (putBin == null) { return Result.Fail("入仓位状态有误"); } - var result = PlcMgr.DistributeTask(putBinNo, removeBinNo, 2) ? Result.Success() : Result.Fail(); - - if (!result.IsSuccess) + var result = PlcMgr.DistributeTask(putBinNo, removeBinNo, 2); + + if (!result) { return Result.Fail(); } + + bool taskSuccess = false; + if (!taskSuccess) + { + taskSuccess=PlcMgr.ReadTaskStatus(2); + PlcMgr.DistributeTask(putBinNo, removeBinNo, 2); + } + var succ = PlcMgr.HoldOn(); if (!succ)