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.

146 lines
4.4 KiB

7 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.Charger.Req;
using Service.Charger.Msg.Host.Req;
7 months ago
namespace Service.Charger.Client;
7 months ago
/// <summary>
7 months ago
/// 示例程序
7 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; }
/// <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)
7 months ago
{
if (string.IsNullOrWhiteSpace(chargeOrderNo))
7 months ago
{
chargeOrderNo = ChargerUtils.GenChargeOrderSn();
7 months ago
}
var remoteStartCharging = new RemoteStartCharging(socLimit, changePowerCmdType, changePower, chargeOrderNo);
this.Channel.WriteAndFlushAsync(remoteStartCharging);
}
7 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);
7 months ago
}
}