现场代码提交

zw
CZ 2 months ago
parent 6da9e9a770
commit b09cc678ed

@ -6,7 +6,6 @@ using HybirdFrameworkCore.Utils;
using HybirdFrameworkDriver.Session;
using log4net;
using Newtonsoft.Json;
using Service.Cloud.Msg;
using Service.Fire.Common;
using Service.Fire.Msg;
using Service.Fire.Msg.Fire;

@ -1,11 +1,47 @@
using System.Text;
using HslCommunication.Core;
namespace Service.Fire.Common;
public class CRC16
{
private const ushort Polynomial = 0x1021;
private ushort[] crcTable = new ushort[256];
private const ushort Polynomial = 0x1021;
private ushort[] crcTable = new ushort[256]
{
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0,
};
public CRC16()
{
@ -29,7 +65,7 @@ public class CRC16
}
}
public ushort ComputeChecksum(byte[] bytes)
/*public ushort ComputeChecksum(byte[] bytes)
{
ushort crc = 0xFFFF; // 初始值
for (int i = 0; i < bytes.Length; i++)
@ -37,6 +73,20 @@ public class CRC16
byte index = (byte)((crc ^ bytes[i]) & 0xff);
crc = (ushort)((crc >> 8) ^ crcTable[index]);
}
return crc;
} */
public /*static*/ ushort ComputeChecksum(byte[] data)
{
ushort crc = 0;
int length = data.Length;
for (int i = 0; i < length; i++)
{
byte tableIndex = (byte)((crc >> 8) ^ data[i]);
crc = (ushort)((crcTable[tableIndex] ^ (crc << 8)) & 0xFFFF); // 确保CRC值在ushort范围内
}
return crc;
}
}

@ -12,8 +12,8 @@ public class FireConst
//public static readonly AttributeKey<string> DestAddr = AttributeKey<string>.ValueOf("dest_addr");//
public static readonly byte[] StartChar = { 0xF0 /* ,0xEE*/ };
public static string Ip = "127.0.0.1";
public static int Port = 502;
public static string Ip = "127.0.0.1";//"172.0.50.102";
public static int Port = 4196;
public static string Sn = "fire";
public static string No = "1";
public static string Code = "5";

@ -16,7 +16,11 @@ public abstract class APCI : IToBytes
public byte CrcHight { get; set; }
public byte CrcLow { get; set; }
public byte[] crc16=new byte[2]
{
0xcc,0x3E
};
public byte[] ToBytes()
{
var bodyBytes = GetBytes();
@ -29,8 +33,9 @@ public abstract class APCI : IToBytes
CRC16 CRC16 = new();
list.AddRange(crc16);
//进行CRC校验
list.AddRange(BitConverter.GetBytes(CRC16.ComputeChecksum(list.ToArray())));
//list.AddRange(BitConverter.GetBytes((ushort)52273/*CRC16.ComputeChecksum(list.ToArray())*/));
return list.ToArray();
}

@ -8,13 +8,8 @@ namespace Service.Fire.Msg.Fire.Req;
public class PollingReq:ASDU
{
/// <summary>
/// 数据段
/// 命令字
/// </summary>
[Property(0, 8)]
public byte DataSegment1 { get; set; }
/// <summary>
/// 数据段
/// </summary>
[Property(8, 8)]
public byte DataSegment2 { get; set; }
}

@ -16,7 +16,7 @@ public class PollingResp: ASDU
[Property(8, 8)]
public byte Resp1 { get; set; }
/// <summary>
/// 信息体2
/// 信息体1
/// </summary>
[Property(16, 8)]
public byte Resp2 { get; set; }
@ -24,5 +24,7 @@ public class PollingResp: ASDU
public PollingResp()
{
CommandWord = 0xA2;
Resp1 = 0XA1;
Resp2 = 0X00;
}
}

@ -0,0 +1,47 @@
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.AutoTask;
using log4net;
using Service.Ups.Client;
namespace Service.MyTask;
[Scope]
public class UpsTask : ITask
{
private volatile bool _stop;
private static readonly ILog Log = LogManager.GetLogger(typeof(UpsTask));
public string Name()
{
return "UpsTask";
}
public int Interval()
{
return 1000 * 1;
}
public void Handle()
{
var upsClient = UpsMgr.GetBySn("Ups");
if (upsClient != null)
{
upsClient.Send();
}
}
public bool Stoped()
{
return _stop;
}
public void Stop()
{
_stop = true;
}
public void ResetStop()
{
_stop = false;
}
}

@ -1,9 +1,14 @@
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Entity;
using HybirdFrameworkCore.Redis;
using HybirdFrameworkCore.Utils;
using HybirdFrameworkDriver.Session;
using HybirdFrameworkDriver.TcpClient;
using log4net;
using Service.Ups.Codec;
using Service.Ups.Common;
using Service.Ups.Msg;
namespace Service.Ups.Client;
@ -46,4 +51,21 @@ public class UpsClient: TcpClient<IBaseHandler, Decoder, Encoder>
ChannelUtils.AddAttr(Channel, UpsConst.EqmTypeNo, sn);
ChannelUtils.AddAttr(Channel, UpsConst.EqmCode, sn);
}
private static byte[] _bytes = new byte[]
{
0x51, 0x31, 0x0D,
};
public bool Send()
{
if (Connected)
{
var sendInfo =new SendInfo();
this.Channel.WriteAndFlushAsync(sendInfo);
return true;
}
return false;
}
}

@ -39,7 +39,7 @@ public class Decoder: ByteToMessageDecoder
//计算帧长
int frameLengthIndex = delimiterFinish - delimiterIndex + 1;
int frameLengthIndex = delimiterFinish+1;
if (delimiterIndex > 0)
{
@ -47,29 +47,6 @@ public class Decoder: ByteToMessageDecoder
return;
}
//没有帧长
/*if (buffer.ReadableBytes < frameLengthIndex + 2)
{
// 数据不足,等待更多数据
return;
}*/
// 读取长度字段
/*byte frameLength = buffer.GetByte(buffer.ReaderIndex + frameLengthIndex);
//总帧长
int totalFrameLength = delimiterIndex + delimiter.Capacity + 2 + 2 + frameLength;*/
// 最小总帧长过滤
/*if (totalFrameLength < 5)
{
return;
}*/
/*if (buffer.ReadableBytes < totalFrameLength)
{
// 数据不足,等待更多数据
return;
}*/
byte[]? data = null;
try
{
@ -152,26 +129,20 @@ public class Decoder: ByteToMessageDecoder
public ASDU Parse(IByteBuffer byteBuffer, int totalFrameLength, IByteBuffer delimiter, out byte[] data)
{
//这里转成ascll码
data = new byte[totalFrameLength];
byteBuffer.ReadBytes(data);
//data就是数据
string stRAsicll= Encoding.ASCII.GetString(data ).ToUpper();
string[] strLst = stRAsicll.Split(" ");
//string stRAsicll= Encoding.ASCII.GetString(data ).ToUpper();
int removeIndex = delimiter.Capacity;
//TODO::后续处理
ushort cmd = data[2 + removeIndex];
byte[] bytes = new byte[data[2]];
Array.Copy(data, 2 + removeIndex, bytes, 0, bytes.Length);
//ushort cmd = data[2 + removeIndex];
byte[] bytes = new byte[data.Length-2];
Array.Copy(data, removeIndex, bytes, 0, bytes.Length);
byte length = data[2];
ASDU asdu = cmd switch
{
_ => new ASDU(),
};
ASDU asdu = ModelConvert.Decode<UpsInfo>(bytes);
ASDU.ParseHeader(data, asdu);
return asdu;

@ -5,8 +5,8 @@ using HybirdFrameworkCore.Utils;
using HybirdFrameworkDriver.Session;
using log4net;
using Newtonsoft.Json;
using Service.Fire.Common;
using Service.Fire.Msg;
using Service.Ups.Common;
using Service.Ups.Msg;
namespace Service.Ups.Codec;
@ -29,13 +29,11 @@ public class Encoder: MessageToByteEncoder<APCI>
/// <param name="output"></param>
protected override void Encode(IChannelHandlerContext context, APCI obj, IByteBuffer output)
{
string? sn = ChannelUtils.GetAttr(context.Channel, FireConst.FireSn);
byte[] bytes = obj.ToBytes();
string? sn = ChannelUtils.GetAttr(context.Channel, UpsConst.UpsSn);
byte[] bytes = new byte[3] { 0x51, 0x31, 0x0D, };//obj.ToBytes();
Log(sn)?.Info(
$"send {BitUtls.BytesToHexStr(bytes)}:{JsonConvert.SerializeObject(obj)} to {ChannelUtils.GetAttr(context.Channel, FireConst.FireSn)}");
$"send {BitUtls.BytesToHexStr(bytes)}:{JsonConvert.SerializeObject(obj)} to {ChannelUtils.GetAttr(context.Channel, UpsConst.UpsSn)}");
context.WriteAndFlushAsync(bytes);
output.WriteBytes(bytes);
}
}

@ -9,11 +9,11 @@ public class UpsConst
public static readonly AttributeKey<string> EqmCode = AttributeKey<string>.ValueOf("eqm_code");//fire
//public static readonly AttributeKey<string> DestAddr = AttributeKey<string>.ValueOf("dest_addr");//
public static readonly byte[] StartChar = { 0x28 /* ,0xEE*/ };
public static readonly byte[] EndChar = { 0x3E /* ,0xEE*/ };
public static readonly byte[] EndChar = { 0x0D /* ,0xEE*/ };
public static string Ip = "127.0.0.1";
public static int Port = 505;
public static string Sn = "fire";
public static string Sn = "Ups";
public static string No = "1";
public static string Code = "5";
}

@ -0,0 +1,21 @@
using DotNetty.Transport.Channels;
using HybirdFrameworkCore.Autofac.Attribute;
using log4net;
using Service.Ups.Client;
using Service.Ups.Msg;
namespace Service.Ups.Handler;
[Order(8)]
[Scope("InstancePerDependency")]
public class UpsInfoHandler: SimpleChannelInboundHandler<UpsInfo>, IBaseHandler
{
private static readonly ILog Log = LogManager.GetLogger(typeof(UpsInfoHandler));
protected override void ChannelRead0(IChannelHandlerContext ctx, UpsInfo msg)
{
if (UpsMgr.TryGetClient(ctx.Channel, out var sn, out var client))
{
Log.Info($"receive {msg} from {sn}");
}
}
}

@ -0,0 +1,22 @@
using HybirdFrameworkCore.Autofac.Attribute;
namespace Service.Ups.Msg;
public class SendInfo:ASDU
{
[Property(0, 8)]
public byte Send1 { get; set; }
[Property(8, 8)]
public byte Send2 { get; set; }
[Property(16, 8)]
public byte Send3 { get; set; }
public SendInfo()
{
Send1 = 0;
Send2 = 0;
Send3 = 0;
}
}

@ -0,0 +1,115 @@
using HybirdFrameworkCore.Autofac.Attribute;
namespace Service.Ups.Msg;
public class UpsInfo:ASDU
{
/// <summary>
/// 输入电压
/// </summary>
[Property(0, 40)]
public string InputVoltage { get; set; }
/// <summary>
/// 空格
/// </summary>
[Property(40, 8)]
public string BlankSpace1 { get; set; }
/// <summary>
/// 输入电压故障
/// </summary>
[Property(48, 40)]
public string InputVoltageFault { get; set; }
[Property(88, 8)]
public string BlankSpace2 { get; set; }
/// <summary>
/// 输出电压
/// </summary>
[Property(96, 40)]
public string OutputVoltage { get; set; }
[Property(136, 8)]
public string BlankSpace3 { get; set; }
/// <summary>
/// 电流
/// </summary>
[Property(144, 24)]
public string ElectricCurrent { get; set; }
[Property(168, 8)]
public string BlankSpace4 { get; set; }
/// <summary>
/// 频率
/// </summary>
[Property(176, 32)]
public string Frequency { get; set; }
[Property(208, 8)]
public string BlankSpace5 { get; set; }
/// <summary>
/// 电池电压
/// </summary>
[Property(216, 32)]
public string BatteryVoltage { get; set; }
[Property(248, 8)]
public string BlankSpace6 { get; set; }
/// <summary>
/// 机内温度
/// </summary>
[Property(256, 32)]
public string InternalTemperature { get; set; }
[Property(288, 8)]
public string BlankSpace7 { get; set; }
/// <summary>
/// ups开关常量
/// </summary>
//public string UpsSwitchingConstant { get; set; }
/// <summary>
/// 1 : 蜂鸣器开
/// </summary>
[Property(296, 8)]
public string MainsVoltageAbnormal{get;set;}
/// <summary>
/// 1 : UPS 关机
/// </summary>
[Property(304, 8)]
public string BatteryUndervoltage{get;set;}
/// <summary>
/// 1 : 测试中
/// </summary>
[Property(312, 8)]
public string Bypass{get;set;}
/// <summary>
/// 1 : UPS 为后备式0 UPS 为在线式
/// </summary>
[Property(320, 8)]
public string UPSFault{get;set;}
/// <summary>
/// 1 : UPS 故障
/// </summary>
[Property(328, 8)]
public string Online{get;set;}
/// <summary>
/// 1 : Bypass 或 Buck Active
/// </summary>
[Property(336, 8)]
public string InTesting{get;set;}
/// <summary>
/// 1 : 电池低电压
/// </summary>
[Property(344, 8)]
public string UPSShutdown{get;set;}
/// <summary>
/// 1 : 市电电压异常
/// </summary>
[Property(352, 8)]
public string BuzzerOn{get;set;}
}

@ -2,7 +2,7 @@
"ConnectionStrings": {
"ConfigId": "master",
"DbType": "MySql",
"SqlConnection": "server=192.168.2.2;Port=3306;Database=huanneng_dev;Uid=root;Pwd=Rszn123;Charset=utf8;"
"SqlConnection": "server=127.0.0.1;Port=3306;Database=huanneng_dev;Uid=root;Pwd=123456;Charset=utf8;"
},
"Update": {
"AutoUpdate": "false",

Loading…
Cancel
Save