From 71c5d519ae09a9a2050c61bf1b5763305e0b6ef5 Mon Sep 17 00:00:00 2001 From: wyy <645583145@qq.com> Date: Tue, 24 Sep 2024 11:03:33 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=89=E5=8C=85=E9=80=89=E4=B8=AD=E5=9C=A8?= =?UTF-8?q?=E5=85=85=E7=94=B5=E7=9A=84=E5=8C=85=EF=BC=8C=E9=9C=80=E8=A6=81?= =?UTF-8?q?=E5=81=9C=E6=AD=A2=E5=85=85=E7=94=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Repository/Station/BinInfoRepository.cs | 46 +++++++++------ Service/Execute/Api/ChargeApi.cs | 10 +++- Service/Execute/Step/CarPrepareState.cs | 59 ++++++++++++++++--- .../Execute/SwapException/ExceptionReason.cs | 3 +- Service/Execute/SwappingStateMachine.cs | 4 ++ Service/RealTime/HubHolder.cs | 15 +++++ Service/RealTime/MyHub.cs | 4 ++ .../Test/WeatherForecastController.cs | 35 ++++++++++- WebStarter/Program.cs | 16 ++--- WebStarter/appsettings.dev.json | 3 + 10 files changed, 157 insertions(+), 38 deletions(-) create mode 100644 Service/RealTime/HubHolder.cs diff --git a/Repository/Station/BinInfoRepository.cs b/Repository/Station/BinInfoRepository.cs index 6169854..7d3d683 100644 --- a/Repository/Station/BinInfoRepository.cs +++ b/Repository/Station/BinInfoRepository.cs @@ -16,6 +16,21 @@ public class BinInfoRepository : BaseRepository { } + + /// + /// + /// + /// + /// + public BinInfo? SelectByBinNo(string? binNo) + { + if (string.IsNullOrWhiteSpace(binNo)) + { + return null; + } + return this.QueryByClause(info => info.No == binNo); + } + /// /// 选包 /// @@ -53,31 +68,28 @@ public class BinInfoRepository : BaseRepository list = list.Where(i => !upMoveNo.Equals(i.No) && i.No != "" && i.No != "-1").ToList(); _log.Info($"BinInfoRepository SelectPack list3={JsonConvert.SerializeObject(list)},upMoveNo={upMoveNo}"); } - - list = list.Where(i => i.ChargeStatus == 2 || i.ChargeStatus == 4).ToList(); + + list = list.Where(i => i.Soc != null &&i.Soc >= swapSoc).OrderByDescending(i => i.Soc).ToList(); if (list.Count <= 0) { - selectPackDto.Info = InfoEnum.SelectBinStatusInfo.LessOfFinishCharging; + selectPackDto.Info = InfoEnum.SelectBinStatusInfo.LessOfSoc; return selectPackDto; } - - list = list.Where(i => i.Soc != null &&i.Soc >= swapSoc).ToList(); - if (list.Count <= 0) + // 符合soc,不在充电 + var socList = list.Where(i => i.ChargeStatus == 2 || i.ChargeStatus == 4).ToList(); + if (socList.Count <= 0) { - selectPackDto.Info = InfoEnum.SelectBinStatusInfo.LessOfSoc; - return selectPackDto; + // 符合soc,在充电 + socList= list.Where(i => i.ChargeStatus == 1).ToList(); + if (socList.Count <= 0) + { + selectPackDto.Info = InfoEnum.SelectBinStatusInfo.LessOfFinishCharging; + return selectPackDto; + } } - - /*list = list.Where(i => i.LastChargeFinishTime != null || new TimeSpan(DateTime.Now.Ticks - - i.LastChargeFinishTime.ToDateTime().Ticks) - .TotalMinutes > swapFinishChargeTime).ToList(); - if (list.Count <= 0) - { selectPackDto.Info = InfoEnum.SelectBinStatusInfo.LessOf3Minute; - return selectPackDto; - }*/ - + list = socList; selectPackDto.BinInfo = list[0]; diff --git a/Service/Execute/Api/ChargeApi.cs b/Service/Execute/Api/ChargeApi.cs index 49c6834..3dc6515 100644 --- a/Service/Execute/Api/ChargeApi.cs +++ b/Service/Execute/Api/ChargeApi.cs @@ -1,10 +1,9 @@ using HybirdFrameworkCore.Entity; using log4net; using Newtonsoft.Json; -using Newtonsoft.Json.Linq; -using Service.Execute.Model; namespace Service.Execute.Api; + /// /// chargeApi /// @@ -19,9 +18,14 @@ public class ChargeApi Timeout = TimeSpan.FromSeconds(60) }; - public static async Task StopCharge(string binNo) + public static async Task StopCharge(string? binNo) { Log.Info($" ChargeApi StopCharge binNo={binNo}"); + if (string.IsNullOrWhiteSpace(binNo)) + { + return false; + } + string url = BASE_URL + "/api/Charge/StopChargeByBinNo/" + binNo; try { diff --git a/Service/Execute/Step/CarPrepareState.cs b/Service/Execute/Step/CarPrepareState.cs index 49e6928..2f3e8ac 100644 --- a/Service/Execute/Step/CarPrepareState.cs +++ b/Service/Execute/Step/CarPrepareState.cs @@ -1,26 +1,21 @@ using Autofac; -using AutoMapper; using Entity.Attr; using Entity.Constant; using Entity.DbModel.Station; -using Entity.Dto; using HybirdFrameworkCore.Autofac; -using HybirdFrameworkCore.Configuration; using log4net; -using OfficeOpenXml.FormulaParsing.Excel.Functions.Text; +using Newtonsoft.Json; using Repository.Station; using Service.Execute.Api; using Service.Execute.Model; using Service.Execute.Model.Tbox; -using Service.Execute.StaticTools; using Service.Execute.SwapException; using Service.Execute.Utils; using Service.Init; using Service.Led; using Service.Padar.Client; +using Service.RealTime; using Service.Sound.SoundClient; -using Service.Station; -using System.Runtime.CompilerServices; namespace Service.Execute.Step; @@ -74,8 +69,15 @@ public class CarPrepareState : IState return SwappingStateMachine.ReturnWithInvokeErr(selectPack, ExceptionReason.None); } + //在充电的包停止充电 + InvokeStatus stopChargingForUpBin = StopChargingForUpBin(machine); + if (stopChargingForUpBin != InvokeStatus.Done) + { + return SwappingStateMachine.ReturnWithInvokeErr(selectPack, ExceptionReason.StopChargingError); + } + - //车辆到位 + //车辆到位 InvokeStatus carInPosition = CarInPosition(machine); if (carInPosition != InvokeStatus.Done) { @@ -412,5 +414,46 @@ public class CarPrepareState : IState , false, () => { }, 20, InvokeStatus.None); } + public InvokeStatus StopChargingForUpBin(SwappingStateMachine machine) + { + + return Invoker.Invoke("select pack - stop charging", 1000, 60, machine.IsCanceled, + () => machine.SelectPackStopChargingFlag, + () => + { + if (machine is { SelectPackFlag: true, SwapOrderBatteryInfo.UpBinInfo.ChargeStatus: 1 }) + { + var binNo = machine.SwapOrderBatteryInfo?.UpBinInfo.No; + BinInfo? binInfo = _CommonMgr._binInfoRepository.SelectByBinNo(binNo); + if (binInfo is { ChargeStatus: 4 }) + { + machine.SwapOrderBatteryInfo.UpBinInfo = binInfo; + machine.SelectPackStopChargingFlag = true; + HubHolder.S2CMsg(JsonConvert.SerializeObject(new RtMsg + { + Cmd = "BeginStopChargingDone", + Msg = $"{binNo}" + })).Wait(); + return; + } + //停止充电 + HubHolder.S2CMsg(JsonConvert.SerializeObject(new RtMsg + { + Cmd = "BeginStopCharging", + Msg = $"{binNo}" + })).Wait(); + Task stopCharge = ChargeApi.StopCharge(binNo); + stopCharge.Wait(TimeSpan.FromSeconds(10)); + + } + else + { + machine.SelectPackStopChargingFlag = true; + } + }); + + + } + } diff --git a/Service/Execute/SwapException/ExceptionReason.cs b/Service/Execute/SwapException/ExceptionReason.cs index 2da6f1d..0f93148 100644 --- a/Service/Execute/SwapException/ExceptionReason.cs +++ b/Service/Execute/SwapException/ExceptionReason.cs @@ -21,5 +21,6 @@ namespace Service.Execute.SwapException; LockCarError, LvPwrOffError, SelfCheckError, - TimeOutError + TimeOutError, + StopChargingError, } diff --git a/Service/Execute/SwappingStateMachine.cs b/Service/Execute/SwappingStateMachine.cs index efcdfb3..95eaff3 100644 --- a/Service/Execute/SwappingStateMachine.cs +++ b/Service/Execute/SwappingStateMachine.cs @@ -96,6 +96,9 @@ public class SwappingStateMachine : IDisposable //选包 public bool SelectPackFlag = false; + + //选包 + public bool SelectPackStopChargingFlag = false; //车辆到位 public bool VehiclesInPlaceFlag = false; @@ -380,6 +383,7 @@ public class SwappingStateMachine : IDisposable SelectPackFlag = false; + SelectPackStopChargingFlag = false; VehiclesInPlaceFlag = false; diff --git a/Service/RealTime/HubHolder.cs b/Service/RealTime/HubHolder.cs new file mode 100644 index 0000000..33b2f29 --- /dev/null +++ b/Service/RealTime/HubHolder.cs @@ -0,0 +1,15 @@ +using Autofac; +using HybirdFrameworkCore.Autofac; +using Microsoft.AspNetCore.SignalR; + +namespace Service.RealTime; + +public class HubHolder +{ + public static IHubContext HubContext { get; set; } = AppInfo.Container.Resolve>(); + + public static async Task S2CMsg(string msg) + { + await HubContext.Clients.All.SendAsync("s2cMsg", msg); + } +} \ No newline at end of file diff --git a/Service/RealTime/MyHub.cs b/Service/RealTime/MyHub.cs index 47f7dfd..d6fdbfc 100644 --- a/Service/RealTime/MyHub.cs +++ b/Service/RealTime/MyHub.cs @@ -23,6 +23,10 @@ public class MyHub : Hub #region rpc + public async Task S2CMsg(string message) + { + await Clients.All.SendAsync("s2cMsg", message); + } public async Task SendMsg(string toUser, string msg) { Log.Info($"receive {msg}"); diff --git a/WebStarter/Controllers/Test/WeatherForecastController.cs b/WebStarter/Controllers/Test/WeatherForecastController.cs index b1d9d3a..1b80e3a 100644 --- a/WebStarter/Controllers/Test/WeatherForecastController.cs +++ b/WebStarter/Controllers/Test/WeatherForecastController.cs @@ -1,4 +1,6 @@ using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json; +using Service.RealTime; using Service.System; namespace WebStarter.Controllers.Test; @@ -22,7 +24,7 @@ public class WeatherForecastController : ControllerBase _sysUserServices = sysUserServices; } - [HttpGet(Name = "GetWeatherForecast")] + [HttpGet("GetWeatherForecast")] public IEnumerable Get() { @@ -37,4 +39,35 @@ public class WeatherForecastController : ControllerBase }) .ToArray(); } + + + /// + /// 停止充电 + /// + /// + [HttpGet("BeginStopCharging")] + public string BeginStopCharging() + { + HubHolder.S2CMsg(JsonConvert.SerializeObject(new RtMsg() + { + Cmd = "BeginStopCharging", + Msg = "1" + })).Wait(); + return "ok"; + } + + /// + /// 停止充电成功 + /// + /// + [HttpGet("EndStopCharging")] + public string EndStopCharging() + { + HubHolder.S2CMsg(JsonConvert.SerializeObject(new RtMsg() + { + Cmd = "BeginStopChargingDone", + Msg = "1" + })).Wait(); + return "ok"; + } } \ No newline at end of file diff --git a/WebStarter/Program.cs b/WebStarter/Program.cs index 865ecb2..fce7439 100644 --- a/WebStarter/Program.cs +++ b/WebStarter/Program.cs @@ -64,14 +64,14 @@ builder.Services.AddHttpContextAccessor(); //跨域 builder.Services.AddCors(options => { - options.AddPolicy - (name: "myCors", - builde => + options.AddDefaultPolicy + (builde => { - builde.WithOrigins("*", "*", "*") - .AllowAnyOrigin() + builde + .SetIsOriginAllowed(_ => true) .AllowAnyHeader() - .AllowAnyMethod(); + .AllowAnyMethod() + .AllowCredentials(); } ); }); @@ -103,7 +103,7 @@ builder.Services.AddSwaggerGen(c => c.IncludeXmlComments(Path.Combine(basePath, "HybirdFrameworkCore.xml"), true); }); -bool.TryParse(AppSettingsHelper.GetContent("SignalR", "Enabled"), out var signalrEnabled); +bool.TryParse(AppSettingsHelper.GetContent("SignalR", "enable"), out var signalrEnabled); if (signalrEnabled) { builder.Services.AddSignalR(options => @@ -151,7 +151,7 @@ app.UseSwaggerUI(); app.UseStaticFiles(); app.UseAuthorization(); -app.UseCors("myCors"); +app.UseCors(); app.MapControllers(); if (signalrEnabled) { diff --git a/WebStarter/appsettings.dev.json b/WebStarter/appsettings.dev.json index f04ac4b..b10f434 100644 --- a/WebStarter/appsettings.dev.json +++ b/WebStarter/appsettings.dev.json @@ -142,6 +142,9 @@ } } }, + "SignalR": { + "enable": true + }, "cloud": { "enable": false },