发送指令先判断状态

zw
smartwyy 6 months ago
parent 2a0d2b0917
commit 5d4b6bb30c

@ -28,8 +28,8 @@ namespace Service.Charger.Client;
[Scope("InstancePerDependency")]
public class ChargerClient : TcpClient<IBaseHandler, Decoder, Encoder>
{
private static readonly ILog Log = LogManager.GetLogger(typeof(ChargerClient));
#region 属性
public ushort AuthTimes { get; set; } = 0;
@ -217,14 +217,20 @@ public class ChargerClient : TcpClient<IBaseHandler, Decoder, Encoder>
/// <summary>
/// 发送鉴权
/// </summary>
public void SendAuth()
public Result<bool> SendAuth()
{
if (!Connected)
{
return Result<bool>.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<bool>.Success();
}
/// <summary>
@ -234,66 +240,96 @@ public class ChargerClient : TcpClient<IBaseHandler, Decoder, Encoder>
/// <param name="changePowerCmdType">功率调节指令类型.默认1 绝对功率值</param>
/// <param name="changePower">0.1kw/位,默认3600</param>
/// <param name="chargeOrderNo">充电流水号</param>
public string SendRemoteStartCharging(byte socLimit, float changePower = 3600, byte changePowerCmdType = 1,
public Result<string> SendRemoteStartCharging(byte socLimit, float changePower = 3600, byte changePowerCmdType = 1,
string? chargeOrderNo = null)
{
if (!Connected)
{
return Result<string>.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<string>.Success(chargeOrderNo);
}
/// <summary>
/// 监控平台发送远程停止充电指令
/// </summary>
/// <param name="reason">0 正常停机 1 服务器发现桩异常,强制停机</param>
public void SendRemoteStopCharging(byte reason = 0)
public Result<bool> SendRemoteStopCharging(byte reason = 0)
{
if (!Connected)
{
return Result<bool>.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<bool>.Success();
}
/// <summary>
/// 监控平台发送功率调节指令
/// </summary>
/// <param name="expectedOperatingPower">期望运行功率</param>
public void SendPowerRegulation(float expectedOperatingPower)
public Result<bool> SendPowerRegulation(float expectedOperatingPower)
{
if (!Connected)
{
return Result<bool>.Fail($"charger-{BinNo} disconnect");
}
PowerRegulation powerRegulation = new PowerRegulation(expectedOperatingPower);
this.Channel.WriteAndFlushAsync(powerRegulation);
return Result<bool>.Success();
}
/// <summary>
/// 倍率 例如0.单5C位该0值.1C为 5 ,1C 时该值为 10
/// </summary>
/// <param name="rate"></param>
public void SendAdjustChargeRate(float rate)
public Result<bool> SendAdjustChargeRate(float rate)
{
if (!Connected)
{
return Result<bool>.Fail($"charger-{BinNo} disconnect");
}
AdjustChargeRate adjustChargeRate = new AdjustChargeRate(rate);
this.Channel.WriteAndFlushAsync(adjustChargeRate);
return Result<bool>.Success();
}
/// <summary>
/// 监控平台下发辅源控制指令
/// </summary>
/// <param name="openFlag">打开辅助电源标志 1电池包辅助电源导通 0电池包辅助电源断开</param>
public void SendAuxiliaryPower(byte openFlag)
public Result<bool> SendAuxiliaryPower(byte openFlag)
{
if (!Connected)
{
return Result<bool>.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<bool>.Success();
}
/// <summary>
@ -302,12 +338,18 @@ public class ChargerClient : TcpClient<IBaseHandler, Decoder, Encoder>
/// <param name="battery">是否有电池 0:无电池 1有电池</param>
/// <param name="connectionState">电接头连接状态 0:未连接 1: 已连接</param>
/// <param name="waterCondition">水接头状态 0:未连接 1: 已连接</param>
public void SendBatteryHolderStatus(byte battery, byte connectionState, byte waterCondition)
public Result<bool> SendBatteryHolderStatus(byte battery, byte connectionState, byte waterCondition)
{
if (!Connected)
{
return Result<bool>.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<bool>.Success();
}
@ -315,10 +357,16 @@ public class ChargerClient : TcpClient<IBaseHandler, Decoder, Encoder>
/// 站控下发 VIN 鉴权的结果
/// </summary>
/// <param name="vinresult">VIN 鉴权结果 1:通过 2 不通过</param>
public void SendAuthenticationVIN(byte vinresult)
public Result<bool> SendAuthenticationVIN(byte vinresult)
{
if (!Connected)
{
return Result<bool>.Fail($"charger-{BinNo} disconnect");
}
AuthenticationVIN authenticationVIN = new AuthenticationVIN(vinresult);
this.Channel.WriteAndFlushAsync(authenticationVIN);
return Result<bool>.Success();
}
/// <summary>
@ -331,56 +379,86 @@ public class ChargerClient : TcpClient<IBaseHandler, Decoder, Encoder>
/// <param name="fileSize">文件大小</param>
/// <param name="mD5Verification">MD5校验值</param>
/// <param name="url">URL文件路径</param>
public void SendUpgradeRequest(byte executionControl, byte downloadTimeout, string versionNumber,
public Result<bool> SendUpgradeRequest(byte executionControl, byte downloadTimeout, string versionNumber,
string fileName, uint fileSize, string mD5Verification, string url)
{
if (!Connected)
{
return Result<bool>.Fail($"charger-{BinNo} disconnect");
}
UpgradeRequest upgradeRequest = new UpgradeRequest(executionControl, downloadTimeout, versionNumber, fileName,
fileSize, mD5Verification, url);
this.Channel.WriteAndFlushAsync(upgradeRequest);
return Result<bool>.Success();
}
/// <summary>
/// 设置尖峰平谷时间段
/// </summary>
/// <param name="setPeakValleyTime"></param>
public void SendSetPeakValleyTime(SetPeakValleyTime setPeakValleyTime)
public Result<bool> SendSetPeakValleyTime(SetPeakValleyTime setPeakValleyTime)
{
if (!Connected)
{
return Result<bool>.Fail($"charger-{BinNo} disconnect");
}
CurrentCmd = JsonConvert.SerializeObject(setPeakValleyTime, Formatting.Indented) + "\r\n" +
BitUtls.BytesToHexStr(setPeakValleyTime.ToBytes());
this.Channel.WriteAndFlushAsync(setPeakValleyTime);
return Result<bool>.Success();
}
/// <summary>
/// 3.4.7 监控平台下发掉线停止充电
/// </summary>
/// <param name="enabled"> 0不使能 1使能</param>
public void SendOfflineStopCharging(byte enabled)
public Result<bool> SendOfflineStopCharging(byte enabled)
{
if (!Connected)
{
return Result<bool>.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<bool>.Success();
}
/// <summary>
/// 3.4.12 站控设备切换站内/站外充电切换
/// </summary>
/// <param name="chargeMode">00:无效 01:站内 02:站外</param>
public void SendChangeChargeMode(byte chargeMode)
public Result<bool> SendChangeChargeMode(byte chargeMode)
{
if (!Connected)
{
return Result<bool>.Fail($"charger-{BinNo} disconnect");
}
ChangeChargeMode req = new ChangeChargeMode(chargeMode);
this.Channel.WriteAndFlushAsync(req);
return Result<bool>.Success();
}
/// <summary>
///
/// </summary>
public void SendQueryBattery()
public Result<bool> SendQueryBattery()
{
if (!Connected)
{
return Result<bool>.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<bool>.Success();
}
#endregion
@ -432,11 +510,11 @@ public class ChargerClient : TcpClient<IBaseHandler, Decoder, Encoder>
byte chargeSoc = StaticStationInfo.ChargeSoc;
float chargePower = StaticStationInfo.ChargePower;
string chargeOrderNo = SendRemoteStartCharging(chargeSoc, chargePower);
Result<string> chargeOrderNo = SendRemoteStartCharging(chargeSoc, chargePower);
_chargeOrderRepository.Insert(new ChargeOrder()
{
Sn = chargeOrderNo,
Sn = chargeOrderNo.Data,
BatteryNo = BatteryNo,
CmdStatus = 0,
ChargerNo = BinNo,

@ -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 }; //元素索引顺序代表值1234
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;}

@ -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<string> chargeOrderNo = _chargerClient.SendRemoteStartCharging(100);
_chargerClient.ChargeOrderNo = chargeOrderNo.Data;
MessageBox.Show(@"发送成功");
DisplayData();
}

Loading…
Cancel
Save