using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkDriver.Session;
using HybirdFrameworkDriver.TcpClient;
using log4net;
using Service.Charger.Client;
using Service.Charger.Common;
using Service.Charger.Handler;
using Service.Init;
using Service.MyTask;
using Service.WaterCool.Msg.Host;
using Service.WaterCool.Msg.WaterCool;
using Decoder = Service.WaterCool.Codec.Decoder;
using Encoder = Service.WaterCool.Codec.Encoder;
namespace Service.WaterCool.Client;
///
/// 水冷客户端
///
[Scope("InstancePerDependency")]
public class WaterCoolClient : TcpClient
{
///
/// 水冷数据主动上报
///
public WaterCoolStatus? WaterCoolStatus { get; set; }
///
/// 水冷是否连接成功
///
public bool IsConnect { get; set; } = false;
///
/// 是否发送水冷固定帧
///
public bool IsWaterCoolSend { get; set; } = true;
///
/// 上下电的时间
///
public DateTime? PowerChangeTime { get; set; }
//上下电的时间间隔(单位/s)
public int Interval = 15;
///
/// 停止标识
///
public bool StopFlag = true;
///
///制冷策略
///
public bool ColdStrategyFlag = false;
///
/// 制热策略
///
public bool HortStrategyFlag = false;
///
/// 自循环
///
public bool LoopStrategyFlag = false;
///
/// 消息
///
public ModeMsg msg { get; set; } = new ModeMsg();
///
/// 水冷机sn
///
public string ChargeSn { get; set; }
private ILog Log()
{
var name = "WaterCool" + this.Sn;
ILog logger = LogManager.GetLogger(name);
Console.WriteLine(name + "-" + logger.GetHashCode());
return logger;
}
//闭合
public bool IsOpen()
{
return WaterCoolStatus?.TmsHighStatus == 1;
}
public bool WaitIsClose()
{
int count = 0;
bool IsClose = this.IsClose();
while (!IsClose)
{
if (count > 5)
{
break;
}
IsClose = this.IsClose();
Thread.Sleep(1000);
count++;
}
return IsClose;
}
public bool IsClose()
{
return WaterCoolStatus?.TmsHighStatus == 0;
}
///
/// 水冷机编号
///
public string Sn { get; set; }
///
/// 水冷发送
///
public void Send()
{
Channel.WriteAndFlushAsync(msg);
}
///
///
///
///
///
public void SessionAttr(string sn, string destAddr)
{
ChannelUtils.AddAttr(Channel, WaterCoolerConst.WaterCoolSn, sn);
ChannelUtils.AddAttr(Channel, ChargerConst.EqmTypeNo, sn);
ChannelUtils.AddAttr(Channel, ChargerConst.EqmCode, sn);
ChannelUtils.AddAttr(Channel, ChargerConst.DestAddr, destAddr);
}
///
/// 热管理策略(开始充电调用)
/// 闭合继电器状态-延时300->上高压->按照策略开始液冷模式
///
public void HeartManageStart()
{
Thread.Sleep(100);
int count = 0;
while (!IsOpen())
{
if (count > 5)
{
Log().Info("water Client Open err");
return;
}
Thread.Sleep(1000);
Log().Info("wait waterClient Open");
msg.BMSState = 1;
msg.ChargState = 1;
count++;
}
Thread.Sleep(300);
//上高压
PowerOperate(0);
}
///
/// 热管理策略(停止充电调用)
/// 下高压->监测继电器状态(5s)->断开继电器 (停止失败做报警,手动处理报警)
///
public bool HeartManageStop()
{
Thread.Sleep(100);
//下高压
PowerOperate(1);
int count = 0;
bool IsClose = this.IsClose();
while (!IsClose)
{
if (count > 5)
{
Log().Info("water Client Open err");
return IsClose;
}
IsClose = this.IsClose();
Thread.Sleep(1000);
Log().Info("wait waterClient Open");
msg.BMSState = 0;
msg.ChargState = 0;
count++;
}
Thread.Sleep(300);
return IsClose;
}
///
/// 高压请求
///
///
public void PowerOperate(byte value)
{
if (msg.HighTension == value)
{
return;
}
var dateTime = DateTime.Now;
if (PowerChangeTime != null)
{
while ((dateTime - PowerChangeTime).Value.Seconds < Interval)
{
Thread.Sleep(1000);
dateTime = DateTime.Now;
}
}
PowerChangeTime = dateTime;
msg.HighTension = value;
}
public bool Connect()
{
base.BaseConnect();
Log().Info($"WaterCool {Sn} connect succeed");
return Connected;
}
}