|
|
|
|
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.Charger.Req;
|
|
|
|
|
using Service.Charger.Msg.Host.Req;
|
|
|
|
|
|
|
|
|
|
namespace Service.Charger.Client;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 示例程序
|
|
|
|
|
/// </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; }
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 是否已经开始充电
|
|
|
|
|
/// </summary>
|
|
|
|
|
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; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///充电机遥信数据
|
|
|
|
|
/// </summary>
|
|
|
|
|
public UploadRemoteSignalData UploadRemoteSignalData = new UploadRemoteSignalData();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 充电机工作状态-从遥信数据包中得到。00H:待机 01H:工作 02H:工作完成 03H:充/放电暂停
|
|
|
|
|
/// </summary>
|
|
|
|
|
public byte Workstate { get; set; }
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 充电机故障-遥信数据包总故障 00H正常、01H故障
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool TotalError { get; set; }
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 充电机告警-遥信数据包总告警 00H正常、01H告警
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool TotalWarning { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 充电机遥测数据
|
|
|
|
|
/// </summary>
|
|
|
|
|
public UploadTelemetryData UploadTelemetryData = new UploadTelemetryData();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///充电机实时充电功率
|
|
|
|
|
/// </summary>
|
|
|
|
|
public float RealTimeChargePower { get; set; } = 0;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 心跳-桩状态
|
|
|
|
|
/// </summary>
|
|
|
|
|
public byte PileState { 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)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(chargeOrderNo))
|
|
|
|
|
{
|
|
|
|
|
chargeOrderNo = ChargerUtils.GenChargeOrderSn();
|
|
|
|
|
}
|
|
|
|
|
var remoteStartCharging = new RemoteStartCharging(socLimit, changePowerCmdType, changePower, chargeOrderNo);
|
|
|
|
|
this.Channel.WriteAndFlushAsync(remoteStartCharging);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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);
|
|
|
|
|
}
|
|
|
|
|
}
|