From 701daed9fed33e718f9201b660fd67d88e2ef5ba Mon Sep 17 00:00:00 2001 From: smartwyy <645583145@qq.com> Date: Thu, 23 May 2024 15:56:58 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=85=85=E7=94=B5=E7=AD=96?= =?UTF-8?q?=E7=95=A5=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConsoleStarter/ExportDb.cs | 30 +---- ConsoleStarter/Program.cs | 34 +---- .../DbModel/Station/ElecPriceModelVersion.cs | 83 ++++++++++++ .../Station/ElecPriceModelVersionDetail.cs | 119 ++++++++++++++++++ .../ElecPriceModelVersionDetailRepository.cs | 20 +++ .../ElecPriceModelVersionRepository.cs | 20 +++ Service/Charger/Client/ChargerClient.cs | 32 +++-- Service/Charger/Client/ClientMgr.cs | 55 ++++++++ .../QueryBatteryBasicInfo2ResHandler.cs | 2 +- .../Charger/Handler/RecordChargeHandler.cs | 2 +- Service/Init/StaticStationInfo.cs | 25 +++- Service/Station/StationParamConst.cs | 2 + WinFormStarter/Form2.cs | 2 +- 13 files changed, 356 insertions(+), 70 deletions(-) create mode 100644 Entity/DbModel/Station/ElecPriceModelVersion.cs create mode 100644 Entity/DbModel/Station/ElecPriceModelVersionDetail.cs create mode 100644 Repository/Station/ElecPriceModelVersionDetailRepository.cs create mode 100644 Repository/Station/ElecPriceModelVersionRepository.cs diff --git a/ConsoleStarter/ExportDb.cs b/ConsoleStarter/ExportDb.cs index 8769c6d..5a5e30f 100644 --- a/ConsoleStarter/ExportDb.cs +++ b/ConsoleStarter/ExportDb.cs @@ -9,28 +9,8 @@ public class ExportDb private static readonly string[] UsedTable = { - "t_bs_charging_bin_info", - "t_bs_cloud_charge_model_recv_record", - "t_bs_cloud_elec_price_recv_record", - "t_bs_eqm_fault_base_info", - "t_bs_net_cloud_param_info", - "t_bs_net_eqm_param_info", - "t_bs_station_config_info", - "t_bs_station_elec_price_info", - "t_bs_station_info", - "t_cb_amt_order_info", - "t_cb_station_order_batt_log_info", - "t_cb_station_order_sended_log", - "t_cb_station_order_state_log", - "t_fl_repaired_info", - "t_fl_un_repair_info", - "t_rm_charger_record_report", - "t_ss_authority_to_role", - "t_ss_button_info", - "t_ss_menu_info", - "t_ss_role_info", - "t_ss_user_info", - "t_ss_user_to_role" + "elec_price_model_version", + "elec_price_model_version_detail" }; private readonly SqlSugarClient Db = new(new ConnectionConfig @@ -54,9 +34,9 @@ public class ExportDb foreach (var columnInfo in columnInfos) Log.Info($" {columnInfo.DbColumnName}:{columnInfo.ColumnDescription}"); } - + Db.DbFirst - .Where(it => it.Contains("charge_order")) + .Where(it => UsedTable.Contains(it)) .IsCreateAttribute() //创建sqlsugar自带特性 .FormatFileName(ToPascalCase) //格式化文件名(文件名和表名不一样情况) .FormatClassName(ToPascalCase) //格式化类名 (类名和表名不一样的情况) @@ -64,7 +44,7 @@ public class ExportDb .CreateClassFile("D:\\vsproject\\hn_back_charger\\Entity\\DbModel\\Station", "Entity.DbModel.Station"); } - + static string ToPascalCase(string input) { if (string.IsNullOrEmpty(input)) diff --git a/ConsoleStarter/Program.cs b/ConsoleStarter/Program.cs index 8d9621f..4f90d47 100644 --- a/ConsoleStarter/Program.cs +++ b/ConsoleStarter/Program.cs @@ -1,42 +1,14 @@ // See https://aka.ms/new-console-template for more information -using DotNetty.Codecs; -using DotNetty.Common.Internal.Logging; -using DotNetty.Handlers.Logging; -using DotNetty.Handlers.Timeout; -using DotNetty.Transport.Bootstrapping; -using DotNetty.Transport.Channels; -using DotNetty.Transport.Channels.Sockets; +using ConsoleStarter; using log4net.Config; -using Microsoft.Extensions.Logging; -using LogLevel = DotNetty.Handlers.Logging.LogLevel; internal class Program { public static void Main(string[] args) { XmlConfigurator.ConfigureAndWatch(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + @"\log4net.xml")); - InternalLoggerFactory.DefaultFactory.AddProvider(new Log4NetProvider()); - - Bootstrap bootstrap = new Bootstrap(); - bootstrap - .Group(new MultithreadEventLoopGroup()) - .Channel() - .Option(ChannelOption.TcpNodelay, true) - .Handler(new ActionChannelInitializer(channel => - { - - var pipeline = channel.Pipeline; - pipeline.AddLast(new StringDecoder()); - pipeline.AddLast(new StringEncoder()); - // 监听器 - pipeline.AddLast(new LoggingHandler(LogLevel.TRACE)); - pipeline.AddLast("idleStateHandler", new IdleStateHandler(30, 0, 0)); // 触发读取超时 - - })); - - Task task = bootstrap.ConnectAsync("127.0.0.1", 9998); - IChannel channel = task.Result; - channel.WriteAndFlushAsync("1111111111"); + ExportDb exportDb = new ExportDb(); + exportDb.Export(); } } \ No newline at end of file diff --git a/Entity/DbModel/Station/ElecPriceModelVersion.cs b/Entity/DbModel/Station/ElecPriceModelVersion.cs new file mode 100644 index 0000000..6e1ad57 --- /dev/null +++ b/Entity/DbModel/Station/ElecPriceModelVersion.cs @@ -0,0 +1,83 @@ +using System; +using System.Linq; +using System.Text; +using SqlSugar; + +namespace Entity.DbModel.Station +{ + /// + ///电价模型板板;电价模型版本表,生失效时间左开右闭且不能重叠 + /// + [SugarTable("elec_price_model_version")] + public partial class ElecPriceModelVersion + { + public ElecPriceModelVersion(){ + + + } + /// + /// Desc:主键 + /// Default: + /// Nullable:False + /// + [SugarColumn(IsPrimaryKey=true,IsIdentity=true,ColumnName="id")] + public int Id {get;set;} + + /// + /// Desc:版本号;版本号,唯一 + /// Default: + /// Nullable:True + /// + [SugarColumn(ColumnName="version")] + public string Version {get;set;} + + /// + /// Desc:生效时间;生效时间(左开右闭) + /// Default: + /// Nullable:True + /// + [SugarColumn(ColumnName="start_time")] + public DateTime? StartTime {get;set;} + + /// + /// Desc:失效时间;失效时间(左开右闭) + /// Default: + /// Nullable:True + /// + [SugarColumn(ColumnName="end_time")] + public DateTime? EndTime {get;set;} + + /// + /// Desc:创建人 + /// Default: + /// Nullable:True + /// + [SugarColumn(ColumnName="created_by")] + public string CreatedBy {get;set;} + + /// + /// Desc:创建时间 + /// Default:CURRENT_TIMESTAMP + /// Nullable:True + /// + [SugarColumn(ColumnName="created_time")] + public DateTime? CreatedTime {get;set;} + + /// + /// Desc:更新人 + /// Default: + /// Nullable:True + /// + [SugarColumn(ColumnName="updated_by")] + public string UpdatedBy {get;set;} + + /// + /// Desc:更新时间 + /// Default:CURRENT_TIMESTAMP + /// Nullable:True + /// + [SugarColumn(ColumnName="updated_time")] + public DateTime? UpdatedTime {get;set;} + + } +} diff --git a/Entity/DbModel/Station/ElecPriceModelVersionDetail.cs b/Entity/DbModel/Station/ElecPriceModelVersionDetail.cs new file mode 100644 index 0000000..06d64f0 --- /dev/null +++ b/Entity/DbModel/Station/ElecPriceModelVersionDetail.cs @@ -0,0 +1,119 @@ +using SqlSugar; + +namespace Entity.DbModel.Station +{ + /// + ///电价模型详情 + /// + [SugarTable("elec_price_model_version_detail")] + public partial class ElecPriceModelVersionDetail + { + public ElecPriceModelVersionDetail() + { + } + + /// + /// Desc:id + /// Default: + /// Nullable:False + /// + [SugarColumn(IsPrimaryKey = true, IsIdentity = true, ColumnName = "id")] + public int Id { get; set; } + + /// + /// Desc:版本号 + /// Default: + /// Nullable:True + /// + [SugarColumn(ColumnName = "version")] + public string Version { get; set; } + + /// + /// Desc:开始时间 + /// Default: + /// Nullable:True + /// + [SugarColumn(ColumnName = "start_hour")] + public int StartHour { get; set; } + + /// + /// Desc:开始时间 + /// Default: + /// Nullable:True + /// + [SugarColumn(ColumnName = "start_minute")] + public int StartMinute { get; set; } + + /// + /// Desc:结束时间 + /// Default: + /// Nullable:True + /// + [SugarColumn(ColumnName = "end_hour")] + public int EndHour { get; set; } + + /// + /// Desc:结束时间 + /// Default: + /// Nullable:True + /// + [SugarColumn(ColumnName = "end_minute")] + public int EndMinute { get; set; } + + /// + /// Desc:价格;以分为单位存储 + /// Default: + /// Nullable:True + /// + [SugarColumn(ColumnName = "price")] + public int Price { get; set; } + + /// + /// Desc:尖峰平谷类型;1-尖;2-峰;3-平;4-谷 + /// Default: + /// Nullable:True + /// + [SugarColumn(ColumnName = "type")] + public int Type { get; set; } + + /// + /// Desc:所需电池数量 + /// Default: + /// Nullable:True + /// + [SugarColumn(ColumnName = "battery_count")] + public int BatteryCount { get; set; } + + /// + /// Desc:创建人 + /// Default: + /// Nullable:True + /// + [SugarColumn(ColumnName = "created_by")] + public string CreatedBy { get; set; } + + /// + /// Desc:创建时间 + /// Default:CURRENT_TIMESTAMP + /// Nullable:True + /// + [SugarColumn(ColumnName = "created_time")] + public DateTime? CreatedTime { get; set; } + + /// + /// Desc:更新人 + /// Default: + /// Nullable:True + /// + [SugarColumn(ColumnName = "updated_by")] + public string UpdatedBy { get; set; } + + /// + /// Desc:更新时间 + /// Default:CURRENT_TIMESTAMP + /// Nullable:True + /// + [SugarColumn(ColumnName = "updated_time")] + public DateTime? UpdatedTime { get; set; } + } +} \ No newline at end of file diff --git a/Repository/Station/ElecPriceModelVersionDetailRepository.cs b/Repository/Station/ElecPriceModelVersionDetailRepository.cs new file mode 100644 index 0000000..ab202ad --- /dev/null +++ b/Repository/Station/ElecPriceModelVersionDetailRepository.cs @@ -0,0 +1,20 @@ +using Entity.DbModel.Station; +using HybirdFrameworkCore.Autofac.Attribute; +using SqlSugar; + +namespace Repository.Station; + +/// +/// +/// +[Scope("SingleInstance")] +public class ElecPriceModelVersionDetailRepository : BaseRepository +{ + /// + /// + /// + /// + public ElecPriceModelVersionDetailRepository(ISqlSugarClient sqlSugar) : base(sqlSugar) + { + } +} \ No newline at end of file diff --git a/Repository/Station/ElecPriceModelVersionRepository.cs b/Repository/Station/ElecPriceModelVersionRepository.cs new file mode 100644 index 0000000..98e1b73 --- /dev/null +++ b/Repository/Station/ElecPriceModelVersionRepository.cs @@ -0,0 +1,20 @@ +using Entity.DbModel.Station; +using HybirdFrameworkCore.Autofac.Attribute; +using SqlSugar; + +namespace Repository.Station; + +/// +/// +/// +[Scope("SingleInstance")] +public class ElecPriceModelVersionRepository : BaseRepository +{ + /// + /// + /// + /// + public ElecPriceModelVersionRepository(ISqlSugarClient sqlSugar) : base(sqlSugar) + { + } +} \ No newline at end of file diff --git a/Service/Charger/Client/ChargerClient.cs b/Service/Charger/Client/ChargerClient.cs index 59ff050..a8cd15d 100644 --- a/Service/Charger/Client/ChargerClient.cs +++ b/Service/Charger/Client/ChargerClient.cs @@ -34,7 +34,7 @@ public class ChargerClient : TcpClient public bool IsAuthed { get; set; } = false; /// - /// 参考 ChargingStatus + /// 参考 Service.Charger.Common.ChargingStatus /// public UInt16 ChargingStatus { get; set; } /// @@ -122,11 +122,11 @@ public class ChargerClient : TcpClient /// /// 电池编码 /// - public string? BatterSn { get; set; } + public string? BatterNo { get; set; } /// /// 电池仓编号 /// - public int? BinNo { get; set; } + public string? BinNo { get; set; } /// /// 远程升级-监控网关上送升级完成确认帧 /// @@ -149,12 +149,14 @@ public class ChargerClient : TcpClient #region db private ChargeOrderRepository _chargeOrderRepository; + private BinInfoRepository _binInfoRepository; #endregion - public ChargerClient(ChargeOrderRepository chargeOrderRepository) + public ChargerClient(ChargeOrderRepository chargeOrderRepository, BinInfoRepository binInfoRepository) { _chargeOrderRepository = chargeOrderRepository; + _binInfoRepository = binInfoRepository; } /// @@ -350,14 +352,24 @@ public class ChargerClient : TcpClient /// public Result StartCharge() { + if (string.IsNullOrWhiteSpace(BinNo)) + { + return Result.Fail("charger init error with no BinNo"); + } if (!Connected) { - return Result.Fail($"充电机{BinNo}未连接"); + return Result.Fail($"charger-{BinNo} disconnect"); } - if (string.IsNullOrWhiteSpace(BatterSn)) + BinInfo binInfo = _binInfoRepository.QueryByClause(it => it.Code == BinNo); + if (binInfo == null) + { + return Result.Fail($"charger-{BinNo} not exist"); + } + BatterNo = binInfo.BatteryNo; + if (string.IsNullOrWhiteSpace(BatterNo)) { - return Result.Fail($"充电仓{BinNo}电池不存在"); + return Result.Fail($"charger-{BinNo} battery not exist"); } RedisHelper redisHelper = AppInfo.Container.Resolve(); @@ -365,7 +377,7 @@ public class ChargerClient : TcpClient string? lockKey = redisHelper.GetStrValue($"chargeNo{BinNo}Start"); if (!string.IsNullOrWhiteSpace(lockKey)) { - return Result.Fail($"充电仓{BinNo}正在启动中"); + return Result.Success($"charger-{BinNo} is starting"); } redisHelper.SetKeyValueStr($"chargeNo{BinNo}Start", DateTime.Now.ToString("f"), TimeSpan.FromMinutes(1)); @@ -376,9 +388,9 @@ public class ChargerClient : TcpClient _chargeOrderRepository.Insert(new ChargeOrder() { Sn = chargeOrderNo, - BatteryNo = BatterSn, + BatteryNo = BatterNo, CmdStatus = 0, - ChargerNo = (BinNo??-1).ToString(), + ChargerNo = BinNo, ChargeMode = 1, StartMode = 1 }); diff --git a/Service/Charger/Client/ClientMgr.cs b/Service/Charger/Client/ClientMgr.cs index c9e9eed..55600a5 100644 --- a/Service/Charger/Client/ClientMgr.cs +++ b/Service/Charger/Client/ClientMgr.cs @@ -3,6 +3,7 @@ using DotNetty.Transport.Channels; using Entity.DbModel.Station; using HybirdFrameworkCore.Autofac; using HybirdFrameworkCore.Autofac.Attribute; +using HybirdFrameworkCore.Entity; using HybirdFrameworkDriver.Session; using log4net; using Repository.Station; @@ -73,6 +74,60 @@ public static class ClientMgr } } + /// + /// + /// + public static void StartAutoChargeThread() + { + Thread thread = new Thread(AutoChargeThread) + { + Name = @"auto-charge" + }; + thread.Start(); + } + /// + /// + /// + private static void AutoChargeThread() + { + ElecPriceModelVersionRepository elecPriceModelVersionRepository = + AppInfo.Container.Resolve(); + + ElecPriceModelVersionDetailRepository elecPriceModelVersionDetailRepository = + AppInfo.Container.Resolve(); + + while (true) + { + Thread.Sleep(1000 * 30); + DateTime now = DateTime.Now; + ElecPriceModelVersion elecPriceModelVersion = elecPriceModelVersionRepository.QueryByClause(i => i.StartTime >= now && i.EndTime < now); + if (elecPriceModelVersion == null) + { + Log.Info("lack of effective elec price model"); + continue; + } + + List elecPriceModelVersionDetails = elecPriceModelVersionDetailRepository.QueryListByClause(it => it.Version == elecPriceModelVersion.Version); + ElecPriceModelVersionDetail? elecPriceModelVersionDetail = elecPriceModelVersionDetails.Where(i => i.StartHour <= now.Hour && i.StartMinute <= now.Minute + && i.EndHour > now.Hour && + i.EndMinute > now.Minute).FirstOrDefault(); + if (elecPriceModelVersionDetail == null) + { + Log.Info("lack of effective elec price model detail"); + continue; + } + + //int batteryCount = elecPriceModelVersionDetail.BatteryCount; + + + foreach (KeyValuePair pair in Dictionary) + { + Result result = pair.Value.StartCharge(); + Log.Info($"start {pair.Key} charge {result.IsSuccess}:{result.Msg}"); + } + } + } + private static void ConnClient(EquipNetInfo netInfo) { Log.Info($"begin to connect {netInfo.Code} {netInfo.NetAddr}:{netInfo.NetPort}"); diff --git a/Service/Charger/Handler/QueryBatteryBasicInfo2ResHandler.cs b/Service/Charger/Handler/QueryBatteryBasicInfo2ResHandler.cs index b5228c1..3b6d349 100644 --- a/Service/Charger/Handler/QueryBatteryBasicInfo2ResHandler.cs +++ b/Service/Charger/Handler/QueryBatteryBasicInfo2ResHandler.cs @@ -29,7 +29,7 @@ namespace Service.Charger.Handler sb.Append(Convert.ToChar(b)); } - client.BatterSn = sb.ToString(); + client.BatterNo = sb.ToString(); Log.Info($"receive {msg} from {sn}"); } diff --git a/Service/Charger/Handler/RecordChargeHandler.cs b/Service/Charger/Handler/RecordChargeHandler.cs index 87ae85e..ddf4387 100644 --- a/Service/Charger/Handler/RecordChargeHandler.cs +++ b/Service/Charger/Handler/RecordChargeHandler.cs @@ -73,7 +73,7 @@ namespace Service.Charger.Handler ChargeOrder chargeOrder = new ChargeOrder() { Sn = client.ChargeOrderNo, - BatteryNo = client.BatterSn, + BatteryNo = client.BatterNo, StartTime = new DateTime((msg.StartYear+1900) , msg.StartMonth ,msg.StartDay ,msg.StartHour, msg.StartMinute, msg.StartSecond), EndTime = new DateTime(msg.EndYear + 1900, msg.EndMonth, msg.EndDay, msg.EndHour, msg.EndMinute, msg.EndSecond), StartSoc = msg.SocBefore, diff --git a/Service/Init/StaticStationInfo.cs b/Service/Init/StaticStationInfo.cs index 7b5187a..a91f3e4 100644 --- a/Service/Init/StaticStationInfo.cs +++ b/Service/Init/StaticStationInfo.cs @@ -52,13 +52,36 @@ public class StaticStationInfo get => Resolve(StationParamConst.SwapSoc); set => Set(StationParamConst.SwapSoc, value); } - + + #region 充电相关 + public static byte ChargeSoc { get => byte.Parse(Resolve(StationParamConst.ChargeSoc)); set => Set(StationParamConst.ChargeSoc, value); } + /// + /// 0-关闭 1-开启 + /// + public static byte AutoChargeEnabled + { + get => byte.Parse(Resolve(StationParamConst.AutoChargeEnabled)); + set => Set(StationParamConst.AutoChargeEnabled, value); + } + + /// + /// 最低满电电池数量 + /// + public static int BatteryNeedCount + { + get => int.Parse(Resolve(StationParamConst.BatteryNeedCount)); + set => Set(StationParamConst.BatteryNeedCount, value); + } + + #endregion + + #region cloud diff --git a/Service/Station/StationParamConst.cs b/Service/Station/StationParamConst.cs index 825a7cf..d5d7b23 100644 --- a/Service/Station/StationParamConst.cs +++ b/Service/Station/StationParamConst.cs @@ -13,6 +13,8 @@ public class StationParamConst //充电soc public static readonly string ChargeSoc = "Station.ChargeSoc"; + public static readonly string AutoChargeEnabled = "Station.AutoChargeEnabled"; + public static readonly string BatteryNeedCount = "Station.BatteryNeedCount"; public static readonly string StationStatus = "Station.StationStatus"; diff --git a/WinFormStarter/Form2.cs b/WinFormStarter/Form2.cs index 70ef0c8..85430f8 100644 --- a/WinFormStarter/Form2.cs +++ b/WinFormStarter/Form2.cs @@ -105,7 +105,7 @@ public partial class Form2 : Form private void DisplayData() { this.SetText(this.rTxtData, JsonConvert.SerializeObject(_chargerClient, Formatting.Indented)); - this.lblBatterNo.Text = _chargerClient?.BatterSn; + this.lblBatterNo.Text = _chargerClient?.BatterNo; } private void btnConn_Click(object sender, EventArgs e)