消防移仓 电池移仓

master
lxw 6 months ago
parent bc715a35e4
commit b5f2cd0a75

@ -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<bool> 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<bool>? succ = JsonConvert.DeserializeObject<Result<bool>>(s);
if (succ.IsSuccess)
{
return true;
}
return false;
}
return false;
}
catch (Exception e)
{
Console.WriteLine(e);
return false;
}
}
}

@ -45,7 +45,7 @@ public class Invoker
return InvokeStatus.Done; return InvokeStatus.Done;
} }
public static InvokeStatus Invoke(string name, int duration, int times, Func<bool> cancel, Func<bool> done, public static InvokeStatus Invoke(string name, int duration, int times, Func<bool> cancel, Func<bool> done,
Action doAction, Action doAction,
Action exceptionAction) Action exceptionAction)
@ -70,7 +70,6 @@ public class Invoker
Log.Error($"{name}", e); Log.Error($"{name}", e);
exceptionAction(); exceptionAction();
} }
} }
Log.Info($" {name} done"); Log.Info($" {name} done");
@ -79,7 +78,8 @@ public class Invoker
public static InvokeStatus Invoke(string name, int duration, int times, Func<bool> cancel, Func<bool> done, public static InvokeStatus Invoke(string name, int duration, int times, Func<bool> cancel, Func<bool> done,
Action doAction, 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; int count = 0;
while (!done()) while (!done())
@ -120,6 +120,81 @@ public class Invoker
return InvokeStatus.Done; return InvokeStatus.Done;
} }
public static InvokeStatus Invoke(string name, int duration, int times, Func<bool> 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<bool> 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<bool> cancel, Func<bool> done, public static InvokeStatus Invoke(string name, int duration, int times, Func<bool> cancel, Func<bool> done,
Action doAction) Action doAction)
{ {

@ -2,6 +2,9 @@
public class RfidReadModel public class RfidReadModel
{ {
/// <summary>
/// 1成功 0:失败
/// </summary>
public string Result { get; set; } public string Result { get; set; }
/// <summary> /// <summary>

@ -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;
}
}

@ -347,53 +347,6 @@ public class PlcMgr
} }
return bResult; return bResult;
} }
private static readonly ILog Log = LogManager.GetLogger(typeof(FireProtectionRelocation));
/// <summary>
/// 消防移仓
/// </summary>
/// <param name="entrySelection">入仓位选择</param>
/// <returns></returns>
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;
}
} }

@ -15,13 +15,21 @@ using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using HybirdFrameworkCore.Entity;
using log4net;
using Service.Execute;
using Service.Execute.Api;
using Service.Plc.Client;
namespace Service.Station namespace Service.Station
{ {
[Scope("SingleInstance")] [Scope("SingleInstance")]
public class BinInfoService : BaseServices<BinInfo> public class BinInfoService : BaseServices<BinInfo>
{ {
BinInfoRepository _binInfoRepository; private static readonly ILog Log = LogManager.GetLogger(typeof(BinInfoService));
private BinInfoRepository _binInfoRepository;
public PlcTaskMgr PlcTaskMgr { get; set; }
public BinInfoService(BinInfoRepository dal) public BinInfoService(BinInfoRepository dal)
{ {
_binInfoRepository = dal; _binInfoRepository = dal;
@ -37,13 +45,90 @@ namespace Service.Station
{ {
RefAsync<int> total = 0; RefAsync<int> total = 0;
var items = await _binInfoRepository.QueryPageAsync( 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.No), u => u.No == input.No,
!string.IsNullOrEmpty(input.Name), u => u.Name == input.Name, !string.IsNullOrEmpty(input.Name), u => u.Name == input.Name,
u=>u.Id,input.Page, input.PageSize, total u => u.Id, input.Page, input.PageSize, total
); );
return SqlSugarPagedExtensions.CreateSqlSugarPagedList(items, total, input.Page, input.PageSize); return SqlSugarPagedExtensions.CreateSqlSugarPagedList(items, total, input.Page, input.PageSize);
} }
/// <summary>
/// 消防移仓
/// </summary>
/// <param name="entrySelection"></param>
/// <returns></returns>
public Result<bool> FireProtectionRelocation(string entrySelection)
{
BinInfo? queryByClause =
_binInfoRepository.QueryByClause(i => i.No.Equals(entrySelection) && i.Exists == 1);
if (queryByClause == null)
{
return Result<bool>.Fail("无电池");
}
if (queryByClause.ChargeStatus == 1)
{
if (!StopCharge(entrySelection))
{
return Result<bool>.Fail("停止充电失败");
}
}
var result = PlcMgr.DistributeTask(0, ushort.Parse(entrySelection), 5);
if (!result)
{
return Result<bool>.Fail();
}
result = PlcMgr.ReadTaskStatus(5);
if (!result)
{
return Result<bool>.Fail();
}
result = PlcMgr.HoldOn();
if (!result)
{
return Result<bool>.Fail();
}
Log.Info($"FireProtectionRelocation task Manual removeBinNo={entrySelection} ");
return PlcTaskMgr.QueryPlcTask(5, 1100, 1101) ? Result<bool>.Success() : Result<bool>.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;
}
} }
} }

@ -5,6 +5,7 @@ using Entity.Constant;
using Entity.DbModel.Station; using Entity.DbModel.Station;
using HybirdFrameworkCore.Autofac.Attribute; using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Entity; using HybirdFrameworkCore.Entity;
using log4net;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math; using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using Repository.Station; using Repository.Station;
using Service.Cloud.Client; using Service.Cloud.Client;
@ -12,6 +13,7 @@ using Service.Execute;
using Service.Execute.Api; using Service.Execute.Api;
using Service.Execute.Model; using Service.Execute.Model;
using Service.Init; using Service.Init;
using Service.Mgr;
using Service.Plc.Client; using Service.Plc.Client;
namespace Service.Station; namespace Service.Station;
@ -19,10 +21,12 @@ namespace Service.Station;
[Scope("SingleInstance")] [Scope("SingleInstance")]
public class MonitorService public class MonitorService
{ {
private static readonly ILog Log = LogManager.GetLogger(typeof(MonitorService));
public BinInfoRepository BinInfoRepository { get; set; } public BinInfoRepository BinInfoRepository { get; set; }
public SwapOrderRepository SwapOrderRepository { get; set; } public SwapOrderRepository SwapOrderRepository { get; set; }
public ChargeOrderRepository ChargeOrderRepository { get; set; } public ChargeOrderRepository ChargeOrderRepository { get; set; }
public PlcTaskMgr PlcTaskMgr { get; set; }
public Result<SwapMonitorScreenResp> GetSwapMonitorData() public Result<SwapMonitorScreenResp> GetSwapMonitorData()
{ {
var configBinInfo = var configBinInfo =
@ -161,16 +165,22 @@ public class MonitorService
return Result<bool>.Fail(); return Result<bool>.Fail();
} }
bool taskSuccess = false; result = PlcMgr.ReadTaskStatus(2);
if (!taskSuccess) if (!result)
{ {
taskSuccess=PlcMgr.ReadTaskStatus(2); return Result<bool>.Fail();
PlcMgr.DistributeTask(putBinNo, removeBinNo, 2);
} }
var succ = PlcMgr.HoldOn(); result = PlcMgr.HoldOn();
if (!result)
{
return Result<bool>.Fail();
}
Log.Info($"battery move task Manual removeBinNo={removeBinNo}, putBinNo={putBinNo} ");
if (!succ) var queryPlcTask = PlcTaskMgr.QueryPlcTask(2, 1200, 1201);
if (!queryPlcTask)
{ {
return Result<bool>.Fail(); return Result<bool>.Fail();
} }
@ -195,6 +205,9 @@ public class MonitorService
removeBin.Soh = -1; removeBin.Soh = -1;
BinInfoRepository.Update(removeBin); BinInfoRepository.Update(removeBin);
} }
return Result<bool>.Success();
return Result<bool>.Success(); return Result<bool>.Success();
} }

@ -18,10 +18,12 @@ namespace WebStarter.Controllers;
public class SwapMonitorController : ControllerBase public class SwapMonitorController : ControllerBase
{ {
private readonly MonitorService _swapMonitorService; private readonly MonitorService _swapMonitorService;
private readonly BinInfoService _binInfoService;
public SwapMonitorController(MonitorService swapMonitorService) public SwapMonitorController(MonitorService swapMonitorService, BinInfoService binInfoService)
{ {
_swapMonitorService = swapMonitorService; _swapMonitorService = swapMonitorService;
_binInfoService = binInfoService;
} }
@ -72,7 +74,7 @@ public class SwapMonitorController : ControllerBase
var success = await TBoxApi.LockCarManyTimes(StationSoftMgr.SwappingStateMachine.RfidReadModel.VelNo); var success = await TBoxApi.LockCarManyTimes(StationSoftMgr.SwappingStateMachine.RfidReadModel.VelNo);
return success ? Result<bool>.Success() : Result<bool>.Fail(); return success ? Result<bool>.Success() : Result<bool>.Fail();
} }
/// <summary> /// <summary>
/// 车辆手动解锁 /// 车辆手动解锁
/// </summary> /// </summary>
@ -110,8 +112,6 @@ public class SwapMonitorController : ControllerBase
} }
/// <summary> /// <summary>
/// 换电复位 /// 换电复位
/// </summary> /// </summary>
@ -175,12 +175,12 @@ public class SwapMonitorController : ControllerBase
/// <summary> /// <summary>
/// 消防移仓 /// 消防移仓
/// </summary> /// </summary>
/// <param name="entrySelet">入仓位选择0-无操作、1-缓存位舱位号、2-20电池存储仓位</param> /// <param name="entrySelect">入仓位选择:移仓仓位号</param>
/// <returns></returns> /// <returns></returns>
[HttpPost("FireProtectionRelocation/{entrySelet}")] [HttpPost("FireProtectionRelocation/{entrySelect}")]
public async Task<Result<bool>> FireProtectionRelocation(ushort entrySelet) public Result<bool> FireProtectionRelocation(string entrySelect)
{ {
return Result<bool>.Success(PlcMgr.FireProtectionRelocation(entrySelet)); return _binInfoService.FireProtectionRelocation(entrySelect);
} }

Loading…
Cancel
Save