You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.3 KiB

8 months ago
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkDriver.Session;
using HybirdFrameworkDriver.TcpClient;
using Service.Charger.Codec;
using Service.Charger.Common;
using Service.Charger.Handler;
using Service.Charger.Msg.Host.Req;
8 months ago
namespace Service.Charger.Client;
8 months ago
/// <summary>
7 months ago
/// 示例程序
8 months ago
/// </summary>
[Scope("InstancePerDependency")]
public class ChargerClient : TcpClient<IBaseHandler, Decoder, Encoder>
{
public ushort AuthTimes { get; set; } = 0;
public bool IsAuthed { get; set; } = false;
/// <summary>
/// 参考 ChargingStatus
/// </summary>
public UInt16 ChargingStatus { get; set; }
public bool IsCharged { get; set; } = false;
public bool IsStopped { get; set; } = false;
public bool IsCanSendStopCmd { get; set; } = true;
public DateTime? ChargingStartTime { get; set; }
public DateTime? ChargingStopTime { get; set; }
#region 发送指令
private ushort IncreAuthTimes()
{
if (AuthTimes < 65535)
{
AuthTimes += 1;
}
else
{
AuthTimes = 1;
}
return AuthTimes;
}
/// <summary>
/// 发送鉴权
/// </summary>
public void SendAuth()
{
byte authCodeKey = ChargerUtils.GetByteRandomNum(); //鉴码KEY[随机数]
byte[] authCodes = ChargerUtils.GetAuthCodesResult(ChargerConst.AuthCode, authCodeKey); //鉴权码
Auth auth = new Auth(IncreAuthTimes(), authCodes, authCodeKey);
this.Channel.WriteAndFlushAsync(auth);
}
/// <summary>
/// 监控平台发送远程开始充电指令
/// </summary>
/// <param name="socLimit">SOC限制.百分比</param>
/// <param name="changePowerCmdType">功率调节指令类型.默认1 绝对功率值</param>
/// <param name="changePower">0.1kw/位,默认3600</param>
/// <param name="chargeOrderNo">充电流水号</param>
public void SendRemoteStartCharging(byte socLimit, float changePower, byte changePowerCmdType=1,
string? chargeOrderNo = null)
8 months ago
{
if (string.IsNullOrWhiteSpace(chargeOrderNo))
8 months ago
{
chargeOrderNo = ChargerUtils.GenChargeOrderSn();
8 months ago
}
var remoteStartCharging = new RemoteStartCharging(socLimit, changePowerCmdType, changePower, chargeOrderNo);
this.Channel.WriteAndFlushAsync(remoteStartCharging);
}
8 months ago
/// <summary>
/// 监控平台发送远程停止充电指令
/// </summary>
/// <param name="reason">0 正常停机 1 服务器发现桩异常,强制停机</param>
public void SendRemoteStopCharging(byte reason)
{
RemoteStopCharging remoteStopCharging = new RemoteStopCharging(reason);
this.Channel.WriteAndFlushAsync(remoteStopCharging);
}
public bool Connect()
{
base.BaseConnect();
if (Connected)
{
SendAuth();
}
return Connected;
}
#endregion
public void SessionAttr(int sn, int eqmTypeNo, string eqmCode, string destAddr)
{
ChannelUtils.AddAttr(Channel, ChargerConst.ChargerSn, sn.ToString());
ChannelUtils.AddAttr(Channel, ChargerConst.EqmTypeNo, eqmTypeNo.ToString());
ChannelUtils.AddAttr(Channel, ChargerConst.EqmCode, eqmCode);
ChannelUtils.AddAttr(Channel, ChargerConst.DestAddr, destAddr);
8 months ago
}
}