master
tq 4 months ago
parent 9d235c6123
commit cf62970006

@ -0,0 +1,40 @@
using DotNetty.Buffers;
using DotNetty.Codecs;
using DotNetty.Transport.Channels;
using HybirdFrameworkCore.Utils;
using log4net;
using Newtonsoft.Json;
using Service.WaterCool.Msg;
using Service.WaterCool.Msg.WaterCool;
namespace Service.WaterCool.Codec;
public class Decoder : ByteToMessageDecoder
{
private static readonly ILog Log = LogManager.GetLogger(typeof(Decoder));
private readonly int _fixedLength = 13;
protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List<object> output)
{
if (_fixedLength <= input.ReadableBytes)
{
byte[] bytes = new byte[_fixedLength - 1];
input.SkipBytes(1);
input.ReadBytes(bytes);
Log.Info($"receive {BitUtls.BytesToHexStr(bytes)}");
int id = BitConverter.ToInt32(new byte[] { bytes[3], bytes[2], bytes[1], bytes[0] }, 0);
BaseMsg baseMsg = id switch
{
0x18FFC13A => ModelConvert.Decode<WaterCoolStatus>(bytes),
_ => ModelConvert.Decode<WaterCoolStatus>(bytes),
};
Log.Info($"receive={BitUtls.BytesToHexStr(bytes)}, msg={JsonConvert.SerializeObject(baseMsg)}");
output.Add(baseMsg);
}
}
}

@ -0,0 +1,25 @@
using DotNetty.Buffers;
using DotNetty.Codecs;
using DotNetty.Transport.Channels;
using HybirdFrameworkCore.Utils;
using log4net;
using Service.WaterCool.Msg;
namespace Service.WaterCool.Codec;
/// <summary>
///
/// </summary>
public class Encoder : MessageToByteEncoder<BaseMsg>
{
private static readonly ILog Log = LogManager.GetLogger(typeof(Encoder));
protected override void Encode(IChannelHandlerContext context, BaseMsg obj, IByteBuffer output)
{
byte[] bytes = obj.ToBytes();
byte[] result =
{ 0x88, bytes[3], bytes[2], bytes[1], bytes[0], bytes[4], bytes[5], bytes[6], bytes[7], bytes[8], bytes[9], bytes[10], bytes[11] };
Log.Info($"send={BitUtls.BytesToHexStr(result)}");
output.WriteBytes(result); }
}

@ -0,0 +1,23 @@
using DotNetty.Transport.Channels;
using HybirdFrameworkCore.Autofac.Attribute;
using log4net;
using Service.Charger.Handler;
using Service.WaterCool.Msg.WaterCool;
using Service.WaterCool.Server;
namespace Service.WaterCool.Handler;
[Order(8)]
[Scope("InstancePerDependency")]
public class WaterCoolStatusHandler : SimpleChannelInboundHandler<WaterCoolStatus>, IBaseHandler
{
private static readonly ILog Log = LogManager.GetLogger(typeof(WaterCoolStatusHandler));
protected override void ChannelRead0(IChannelHandlerContext ctx, WaterCoolStatus msg)
{
Log.Info($"receive OtherMsg={msg}");
var server = WaterCoolServerMgr.Server;
if (server != null) server.WaterCoolStatus = msg;
}
}

@ -0,0 +1,15 @@
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Utils;
using HybirdFrameworkDriver.Common;
namespace Service.WaterCool.Msg;
public class BaseMsg: IToBytes
{
[Property(0, 32)] public UInt32 Id { get; set; }
public byte[] ToBytes()
{
return ModelConvert.Encode(this);
}
}

@ -0,0 +1,97 @@
using HybirdFrameworkCore.Autofac.Attribute;
namespace Service.WaterCool.Msg.Host;
/// <summary>
/// 站控向水冷发送模式设定指令
/// 消息id 18FF45A7
/// </summary>
public class ModeMsg : BaseMsg
{
/// <summary>
/// BMS 请求设定模式
/// 0关机模式1制冷模式2制热模式3自循环模式
/// </summary>
[Property(32, 2)]
public byte Mode { get; set; }
/// <summary>
/// 高压请求
/// 0 上高压请求1 下高压请求2~3 无效
/// </summary>
[Property(34, 2)]
public byte HighTension { get; set; }
/// <summary>
/// 充电状态
/// 0 非充电模式1 充电模式2~3 无效
/// </summary>
[Property(36, 2)]
public byte ChargState { get; set; }
/// <summary>
/// BMS 高压继电器状态
/// 0 断开状态1 闭合状态2~3 无效
/// </summary>
[Property(38, 2)]
public byte BMSState { get; set; }
/// <summary>
/// BMS 当前电压高字节
/// 分辨率1V /bit 偏移量0 范围0V~1000V 类型:测量 65535无效
/// </summary>
[Property(40, 8)]
public byte BMSHighByte { get; set; }
/// <summary>
/// BMS 当前电压低字节
/// 分辨率1V /bit 偏移量0 范围0V~1000V 类型:测量 65535无效
/// </summary>
[Property(48, 8)]
public byte BMSLowByte { get; set; }
/// <summary>
/// 保留
/// </summary>
[Property(56, 8)]
public byte ReserveOne { get; set; }
/// <summary>
/// 设定温度
/// 分辨率1℃\bit 偏移量:-40℃ 单位:℃
/// </summary>
[Property(64, 8, PropertyReadConstant.Bit, 1, 0, 40)]
public sbyte SetTemperature { get; set; }
/// <summary>
/// 保留
/// </summary>
[Property(72, 8)]
public byte ReserveTwo { get; set; }
/// <summary>
/// 生命帧
/// </summary>
[Property(80, 8)]
public byte HP { get; set; }
/// <summary>
/// CRC 效验值
/// </summary>
[Property(88, 8)]
public byte CrcCheckValue { get; set; }
public ModeMsg(byte mode,byte highTension,byte chargState,byte bmsState,sbyte setTemperature,byte hp,byte crcCheckValue)
{
Id = 0x18FF45A7;
Mode = mode;
HighTension = highTension;
ChargState= chargState;
BMSState= bmsState;
SetTemperature = setTemperature;
HP = hp;
CrcCheckValue = crcCheckValue;
}
}

@ -0,0 +1,78 @@
using HybirdFrameworkCore.Autofac.Attribute;
namespace Service.WaterCool.Msg.WaterCool;
/// <summary>
/// 水冷机数据主动上报
/// </summary>
public class WaterCoolStatus : BaseMsg
{
/// <summary>
/// TMS 工作状态
/// 0关机模式1制冷模式2制热模式3自循环模式
/// </summary>
[Property(32, 2)]
public byte TmsStatus { get; set; }
/// <summary>
/// TMS 高压继电器状态
/// 0 断开状态1 闭合状态2~3 无效
/// </summary>
[Property(34, 2)]
public byte TmsHighStatus { get; set; }
/// <summary>
/// 出水温度(机组到电池)
/// 分辨率1 ℃/bit 偏移量40℃范围40℃~210℃ 类型测量255无效
/// </summary>
[Property(40, 8, PropertyReadConstant.Bit, 1, 0, 40)]
public sbyte GetOutTemperature { get; set; }
/// <summary>
/// 回水温度(电池到机组)
/// 分辨率1 ℃/bit 偏移量40℃范围40℃~210℃ 类型测量255无效
/// </summary>
[Property(48, 8, PropertyReadConstant.Bit, 1, 0, 40)]
public sbyte ComeBackTemperature { get; set; }
/// <summary>
/// 供应商编号
/// </summary>
[Property(56, 8)]
public byte SupplierNumber { get; set; }
/// <summary>
/// 软件版本
/// </summary>
[Property(64, 8)]
public byte SoftwareVersion { get; set; }
/// <summary>
/// TMS 需求功率高字节
/// 分辨率0.1 Kw/bit 偏移量0范围0Kw~100.0Kw 类型测量65535无效按照实时发送
/// </summary>
[Property(72, 8)]
public byte TmsHighPower { get; set; }
/// <summary>
/// TMS 需求功率低字节
/// 分辨率0.1 Kw/bit 偏移量0范围0Kw~100.0Kw 类型测量65535无效按照实时发送
/// </summary>
[Property(80, 8)]
public byte TmsLowPower { get; set; }
/// <summary>
/// TMS 故障码
/// 0无故障 出现 1 种以上循环发送
/// </summary>
[Property(88, 6)]
public byte TmsTroubleCode { get; set; }
/// <summary>
/// crc校验帧
/// </summary>
[Property(94, 2)]
public byte CrcCheckValue { get; set; }
}

@ -0,0 +1,18 @@
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkDriver.TcpServer;
using log4net;
using Service.Charger.Handler;
using Service.WaterCool.Codec;
using Service.WaterCool.Msg.WaterCool;
namespace Service.WaterCool.Server;
[Scope]
public class WaterCoolServer: TcpServer<IBaseHandler, Decoder, Encoder>
{
private static readonly ILog Log = LogManager.GetLogger(typeof(WaterCoolServer));
public WaterCoolStatus? WaterCoolStatus { get; set; }
}

@ -0,0 +1,18 @@
using Autofac;
using HybirdFrameworkCore.Autofac;
namespace Service.WaterCool.Server;
public class WaterCoolServerMgr
{
public static WaterCoolServer? Server { get; private set; }
public static void InitWaterCoolServer(int port)
{
if (Server == null)
{
Server = AppInfo.Container.Resolve<WaterCoolServer>();
Server.Start(port);
}
}
}
Loading…
Cancel
Save