From 5d4b6bb30c95d27b4cec7eaeaa0cf742e48793f4 Mon Sep 17 00:00:00 2001 From: smartwyy <645583145@qq.com> Date: Tue, 28 May 2024 22:25:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E9=80=81=E6=8C=87=E4=BB=A4=E5=85=88?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Service/Charger/Client/ChargerClient.cs | 116 +++++++++++++++--- .../Charger/Handler/RecordChargeHandler.cs | 4 - WinFormStarter/Form2.cs | 5 +- 3 files changed, 100 insertions(+), 25 deletions(-) diff --git a/Service/Charger/Client/ChargerClient.cs b/Service/Charger/Client/ChargerClient.cs index 0e5a4e7..b54bf31 100644 --- a/Service/Charger/Client/ChargerClient.cs +++ b/Service/Charger/Client/ChargerClient.cs @@ -28,8 +28,8 @@ namespace Service.Charger.Client; [Scope("InstancePerDependency")] public class ChargerClient : TcpClient { - private static readonly ILog Log = LogManager.GetLogger(typeof(ChargerClient)); + #region 属性 public ushort AuthTimes { get; set; } = 0; @@ -217,14 +217,20 @@ public class ChargerClient : TcpClient /// /// 发送鉴权 /// - public void SendAuth() + public Result SendAuth() { + if (!Connected) + { + return Result.Fail($"charger-{BinNo} disconnect"); + } + byte authCodeKey = ChargerUtils.GetByteRandomNum(); //鉴码KEY[随机数] byte[] authCodes = ChargerUtils.GetAuthCodesResult(ChargerConst.AuthCode, authCodeKey); //鉴权码 Auth auth = new Auth(IncreAuthTimes(), authCodes, authCodeKey); CurrentCmd = JsonConvert.SerializeObject(auth, Formatting.Indented) + "\r\n" + BitUtls.BytesToHexStr(auth.ToBytes()); this.Channel.WriteAndFlushAsync(auth); + return Result.Success(); } /// @@ -234,66 +240,96 @@ public class ChargerClient : TcpClient /// 功率调节指令类型.默认1 绝对功率值 /// 0.1kw/位,默认3600 /// 充电流水号 - public string SendRemoteStartCharging(byte socLimit, float changePower = 3600, byte changePowerCmdType = 1, + public Result SendRemoteStartCharging(byte socLimit, float changePower = 3600, byte changePowerCmdType = 1, string? chargeOrderNo = null) { + if (!Connected) + { + return Result.Fail($"charger-{BinNo} disconnect"); + } + if (string.IsNullOrWhiteSpace(chargeOrderNo)) { chargeOrderNo = ChargerUtils.GenChargeOrderSn(); } - - Log.Info($"SendRemoteStartCharging soc={socLimit}, changePower={changePower}, changePowerCmdType={changePowerCmdType}, chargeOrderNo={chargeOrderNo}"); + + Log.Info( + $"SendRemoteStartCharging soc={socLimit}, changePower={changePower}, changePowerCmdType={changePowerCmdType}, chargeOrderNo={chargeOrderNo}"); var remoteStartCharging = new RemoteStartCharging(socLimit, changePowerCmdType, changePower, chargeOrderNo); CurrentCmd = JsonConvert.SerializeObject(remoteStartCharging, Formatting.Indented) + "\r\n" + BitUtls.BytesToHexStr(remoteStartCharging.ToBytes()); this.Channel.WriteAndFlushAsync(remoteStartCharging); - return chargeOrderNo; + return Result.Success(chargeOrderNo); } /// /// 监控平台发送远程停止充电指令 /// /// 0 正常停机 1 服务器发现桩异常,强制停机 - public void SendRemoteStopCharging(byte reason = 0) + public Result SendRemoteStopCharging(byte reason = 0) { + if (!Connected) + { + return Result.Fail($"charger-{BinNo} disconnect"); + } + RemoteStopCharging remoteStopCharging = new RemoteStopCharging(reason); CurrentCmd = JsonConvert.SerializeObject(remoteStopCharging, Formatting.Indented) + "\r\n" + BitUtls.BytesToHexStr(remoteStopCharging.ToBytes()); this.Channel.WriteAndFlushAsync(remoteStopCharging); + return Result.Success(); } /// /// 监控平台发送功率调节指令 /// /// 期望运行功率 - public void SendPowerRegulation(float expectedOperatingPower) + public Result SendPowerRegulation(float expectedOperatingPower) { + if (!Connected) + { + return Result.Fail($"charger-{BinNo} disconnect"); + } + PowerRegulation powerRegulation = new PowerRegulation(expectedOperatingPower); this.Channel.WriteAndFlushAsync(powerRegulation); + return Result.Success(); } /// /// 倍率 例如,0.单5C位该0值.1C为 5 ,1C 时该值为 10 /// /// - public void SendAdjustChargeRate(float rate) + public Result SendAdjustChargeRate(float rate) { + if (!Connected) + { + return Result.Fail($"charger-{BinNo} disconnect"); + } + AdjustChargeRate adjustChargeRate = new AdjustChargeRate(rate); this.Channel.WriteAndFlushAsync(adjustChargeRate); + return Result.Success(); } /// /// 监控平台下发辅源控制指令 /// /// 打开辅助电源标志 1:电池包辅助电源导通 0:电池包辅助电源断开 - public void SendAuxiliaryPower(byte openFlag) + public Result SendAuxiliaryPower(byte openFlag) { + if (!Connected) + { + return Result.Fail($"charger-{BinNo} disconnect"); + } + AuxiliaryPower auxiliaryPower = new AuxiliaryPower(openFlag); CurrentCmd = JsonConvert.SerializeObject(auxiliaryPower, Formatting.Indented) + "\r\n" + BitUtls.BytesToHexStr(auxiliaryPower.ToBytes()); this.Channel.WriteAndFlushAsync(auxiliaryPower); + return Result.Success(); } /// @@ -302,12 +338,18 @@ public class ChargerClient : TcpClient /// 是否有电池 0:无电池 1:有电池 /// 电接头连接状态 0:未连接 1: 已连接 /// 水接头状态 0:未连接 1: 已连接 - public void SendBatteryHolderStatus(byte battery, byte connectionState, byte waterCondition) + public Result SendBatteryHolderStatus(byte battery, byte connectionState, byte waterCondition) { + if (!Connected) + { + return Result.Fail($"charger-{BinNo} disconnect"); + } + BatteryHolderStatus batteryHolderStatus = new BatteryHolderStatus(battery, connectionState, waterCondition); CurrentCmd = JsonConvert.SerializeObject(batteryHolderStatus, Formatting.Indented) + "\r\n" + BitUtls.BytesToHexStr(batteryHolderStatus.ToBytes()); this.Channel.WriteAndFlushAsync(batteryHolderStatus); + return Result.Success(); } @@ -315,10 +357,16 @@ public class ChargerClient : TcpClient /// 站控下发 VIN 鉴权的结果 /// /// VIN 鉴权结果 1:通过 2 不通过 - public void SendAuthenticationVIN(byte vinresult) + public Result SendAuthenticationVIN(byte vinresult) { + if (!Connected) + { + return Result.Fail($"charger-{BinNo} disconnect"); + } + AuthenticationVIN authenticationVIN = new AuthenticationVIN(vinresult); this.Channel.WriteAndFlushAsync(authenticationVIN); + return Result.Success(); } /// @@ -331,56 +379,86 @@ public class ChargerClient : TcpClient /// 文件大小 /// MD5校验值 /// URL(文件路径) - public void SendUpgradeRequest(byte executionControl, byte downloadTimeout, string versionNumber, + public Result SendUpgradeRequest(byte executionControl, byte downloadTimeout, string versionNumber, string fileName, uint fileSize, string mD5Verification, string url) { + if (!Connected) + { + return Result.Fail($"charger-{BinNo} disconnect"); + } + UpgradeRequest upgradeRequest = new UpgradeRequest(executionControl, downloadTimeout, versionNumber, fileName, fileSize, mD5Verification, url); this.Channel.WriteAndFlushAsync(upgradeRequest); + return Result.Success(); } /// /// 设置尖峰平谷时间段 /// /// - public void SendSetPeakValleyTime(SetPeakValleyTime setPeakValleyTime) + public Result SendSetPeakValleyTime(SetPeakValleyTime setPeakValleyTime) { + if (!Connected) + { + return Result.Fail($"charger-{BinNo} disconnect"); + } + CurrentCmd = JsonConvert.SerializeObject(setPeakValleyTime, Formatting.Indented) + "\r\n" + BitUtls.BytesToHexStr(setPeakValleyTime.ToBytes()); this.Channel.WriteAndFlushAsync(setPeakValleyTime); + return Result.Success(); } /// /// 3.4.7 监控平台下发掉线停止充电 /// /// 0:不使能 1:使能 - public void SendOfflineStopCharging(byte enabled) + public Result SendOfflineStopCharging(byte enabled) { + if (!Connected) + { + return Result.Fail($"charger-{BinNo} disconnect"); + } + OfflineStopCharging offlineStopCharging = new OfflineStopCharging(enabled); CurrentCmd = JsonConvert.SerializeObject(offlineStopCharging, Formatting.Indented) + "\r\n" + BitUtls.BytesToHexStr(offlineStopCharging.ToBytes()); this.Channel.WriteAndFlushAsync(offlineStopCharging); + return Result.Success(); } /// /// 3.4.12 站控设备切换站内/站外充电切换 /// /// 00:无效 01:站内 02:站外 - public void SendChangeChargeMode(byte chargeMode) + public Result SendChangeChargeMode(byte chargeMode) { + if (!Connected) + { + return Result.Fail($"charger-{BinNo} disconnect"); + } + ChangeChargeMode req = new ChangeChargeMode(chargeMode); this.Channel.WriteAndFlushAsync(req); + return Result.Success(); } /// /// /// - public void SendQueryBattery() + public Result SendQueryBattery() { + if (!Connected) + { + return Result.Fail($"charger-{BinNo} disconnect"); + } + QueryBattery queryBattery = new QueryBattery(ChargerConst.BatteryNo); CurrentCmd = JsonConvert.SerializeObject(queryBattery, Formatting.Indented) + "\r\n" + BitUtls.BytesToHexStr(queryBattery.ToBytes()); this.Channel.WriteAndFlushAsync(queryBattery); + return Result.Success(); } #endregion @@ -432,11 +510,11 @@ public class ChargerClient : TcpClient byte chargeSoc = StaticStationInfo.ChargeSoc; float chargePower = StaticStationInfo.ChargePower; - string chargeOrderNo = SendRemoteStartCharging(chargeSoc, chargePower); + Result chargeOrderNo = SendRemoteStartCharging(chargeSoc, chargePower); _chargeOrderRepository.Insert(new ChargeOrder() { - Sn = chargeOrderNo, + Sn = chargeOrderNo.Data, BatteryNo = BatteryNo, CmdStatus = 0, ChargerNo = BinNo, diff --git a/Service/Charger/Handler/RecordChargeHandler.cs b/Service/Charger/Handler/RecordChargeHandler.cs index dfb4a90..4cd95ba 100644 --- a/Service/Charger/Handler/RecordChargeHandler.cs +++ b/Service/Charger/Handler/RecordChargeHandler.cs @@ -41,10 +41,6 @@ namespace Service.Charger.Handler if(ClientMgr.TryGetClient(ctx.Channel, out var sn, out var client)) { - msg.StartTime = (msg.StartYear+1900) + "-" + msg.StartMonth + "-" + msg.StartDay + " " + msg.StartHour + ":" + msg.StartMinute + ":" + msg.StartSecond; - msg.EndTime = (msg.EndYear+1900) + "-" + msg.EndMonth + "-" + msg.EndDay + " " + msg.EndHour + ":" + msg.EndMinute + ":" + msg.EndSecond; - - float[] powersPeriods = new float[4] { 0, 0, 0, 0 }; //元素索引顺序代表值;1:尖;2:峰;3:平;4:谷 if (msg.FlagOfTime1 >= 1 && msg.FlagOfTime1 <= 4){powersPeriods[msg.FlagOfTime1 - 1] += msg.ChargingPowerOfTime1;} if (msg.FlagOfTime2 >= 1 && msg.FlagOfTime2 <= 4){powersPeriods[msg.FlagOfTime2 - 1] += msg.ChargingPowerOfTime2;} diff --git a/WinFormStarter/Form2.cs b/WinFormStarter/Form2.cs index 2443763..cc9cc76 100644 --- a/WinFormStarter/Form2.cs +++ b/WinFormStarter/Form2.cs @@ -1,6 +1,7 @@ using Autofac; using DotNetty.Handlers.Logging; using HybirdFrameworkCore.Autofac; +using HybirdFrameworkCore.Entity; using Newtonsoft.Json; using Service.Charger.Client; using Service.Charger.Msg.Host.Req; @@ -174,8 +175,8 @@ public partial class Form2 : Form private void btnStartCharge_Click(object sender, EventArgs e) { - string chargeOrderNo = _chargerClient.SendRemoteStartCharging(100); - _chargerClient.ChargeOrderNo = chargeOrderNo; + Result chargeOrderNo = _chargerClient.SendRemoteStartCharging(100); + _chargerClient.ChargeOrderNo = chargeOrderNo.Data; MessageBox.Show(@"发送成功"); DisplayData(); }