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;
///
/// 示例程序
///
[Scope("InstancePerDependency")]
public class ChargerClient : TcpClient
{
public ushort AuthTimes { get; set; } = 0;
public bool IsAuthed { get; set; } = false;
///
/// 参考 ChargingStatus
///
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; }
///
///充电机遥信数据
///
public UploadRemoteSignalData UploadRemoteSignalData = new UploadRemoteSignalData();
///
/// 充电机工作状态-从遥信数据包中得到。00H:待机 01H:工作 02H:工作完成 03H:充/放电暂停
///
public byte Workstate { get; set; }
///
/// 充电机故障-遥信数据包总故障 00H正常、01H故障
///
public bool TotalError { get; set; }
///
/// 充电机告警-遥信数据包总告警 00H正常、01H告警
///
public bool TotalWarning { get; set; }
///
/// 充电机遥测数据
///
public UploadTelemetryData UploadTelemetryData = new UploadTelemetryData();
///
///充电机实时充电功率
///
public float RealTimeChargePower { get; set; } = 0;
///
/// 心跳-桩状态
///
public byte PileState { get; set; }
#region 发送指令
private ushort IncreAuthTimes()
{
if (AuthTimes < 65535)
{
AuthTimes += 1;
}
else
{
AuthTimes = 1;
}
return AuthTimes;
}
///
/// 发送鉴权
///
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);
}
///
/// 监控平台发送远程开始充电指令
///
/// SOC限制.百分比
/// 功率调节指令类型.默认1 绝对功率值
/// 0.1kw/位,默认3600
/// 充电流水号
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);
}
///
/// 监控平台发送远程停止充电指令
///
/// 0 正常停机 1 服务器发现桩异常,强制停机
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);
}
}