272 lines
10 KiB
C#
272 lines
10 KiB
C#
using System.Collections;
|
||
using System.Runtime.CompilerServices;
|
||
using Common;
|
||
using CommonEntity.CacheEntity;
|
||
using CommonEntity.RCUEntity;
|
||
using IOT_JieKou;
|
||
using LogCap.Common;
|
||
using Orleans.Serialization.Buffers;
|
||
|
||
namespace NewCRICS.Genie
|
||
{
|
||
/// <summary>
|
||
/// EndPointGrain
|
||
/// </summary>
|
||
public class EndPointGrain : Grain, IDataTran
|
||
{
|
||
private string EndPoint { get; set; } = string.Empty;
|
||
private string HostNumber { get; set; } = string.Empty;
|
||
|
||
public MyData_Cache _cache { get; set; } = new MyData_Cache();
|
||
public async ValueTask TongXin(RCU_UDPData_With_String data)
|
||
{
|
||
try
|
||
{
|
||
byte[]? hexdata = data.data;
|
||
string? endpoint = data.endpoint;
|
||
string? hostnum = data.hostnum;
|
||
long? timestamp = data.currenttimestamp;
|
||
|
||
//AA 55 3C 00 54 33 53 41 34 32 F8 3D 04 81 FC
|
||
//01 01 A3 06 01 30 20 00 00 00 00 00 02 02 02 39 01 01 00 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 07 01 00 00 02 5B 0B
|
||
//79 CF
|
||
//固定 0xAA 0x55
|
||
//3C 00数据的总长度,包括包头和 CRC 校验。低地址在前
|
||
//54 33 53 41 系统 ID 的四个字节。固定“T3SA”
|
||
//34 命令字
|
||
//32 F8 帧号 帧号,应答时的帧号为接收到数据的帧号。低地址在前
|
||
//3D 04 项目编码。低地址在前
|
||
//81 MAC
|
||
//FC MAC
|
||
//79 CF CRC 校验。低地址在前
|
||
|
||
byte[] header = hexdata[0..2];
|
||
byte[] len = hexdata[2..4];
|
||
byte[] guding = hexdata[4..8];
|
||
byte cmd = hexdata[8];
|
||
byte[] frame1 = hexdata[9..11];
|
||
byte[] hotel_code = hexdata[11..13];
|
||
byte[] mac = hexdata[13..15];
|
||
byte[] byte_data = hexdata[15..^2];
|
||
byte[] crc = hexdata[^2..];
|
||
|
||
|
||
if (cmd == 0x32)
|
||
{
|
||
|
||
}
|
||
else if (cmd == 0x33)
|
||
{
|
||
|
||
}
|
||
//定时上报
|
||
|
||
// AA 55 35 00 54 33 53 41 34 10 80 EB 03 6B 24
|
||
// 01 //协议版本
|
||
// 01 //取电状态
|
||
// 00 //身份信息
|
||
// 00 //无卡逻辑
|
||
// 00 00 00 00 00 00 00 00 //服务信息全部回路状态 1~64 64Bit 0 :关 1:开
|
||
// 01 //PMS状态
|
||
// 01 //碳达人
|
||
// 01 //上报设备数
|
||
|
||
// 39 //设备类型 - 能耗设备
|
||
// 01 //设备地址
|
||
// 01 00 //设备回路
|
||
// 10 //设备数据长度
|
||
// 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 //设备数据
|
||
|
||
// 07 //设备类型 - 温控
|
||
// 01 //设备地址
|
||
// 01 00 //设备回路
|
||
// 02 //设备数据长度
|
||
// 00 00 //设备数据
|
||
|
||
// F4 AA //CRC16
|
||
|
||
//能耗设备 - 上报数据格式
|
||
//P15:设备类型
|
||
//P16:设备地址
|
||
//P17~P18:设备回路
|
||
//P19:设备数据长度
|
||
//P20~P21:通道电压,单位:10mV
|
||
//P22~P23:通道电流,单位:10mA
|
||
//P24~P25:通道有功功率,单位:mW
|
||
//P26~P29:通道能耗,单位:Wh(1度电 = 1KWh)
|
||
//P30~P33:通道总能耗,单位:Wh(1度电 = 1KWh)
|
||
|
||
|
||
else if (cmd == 0x34)
|
||
{
|
||
ParseDeviceReport(byte_data, this._cache, this.HostNumber);
|
||
|
||
Common.Cache.Cache.SetCache(HostNumber, this._cache);
|
||
}
|
||
else if (cmd == 0x35)
|
||
{
|
||
|
||
}
|
||
else if (cmd == 0x36)
|
||
{
|
||
|
||
}
|
||
}
|
||
catch (Exception)
|
||
{
|
||
|
||
}
|
||
await ValueTask.CompletedTask;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 解析数据
|
||
/// </summary>
|
||
/// <param name="byte_data"></param>
|
||
private static void ParseDeviceReport(byte[] byte_data, MyData_Cache cache, string HostNumber)
|
||
{
|
||
using (MemoryStream stream = new MemoryStream(byte_data))
|
||
{
|
||
using (BinaryReader reader = new BinaryReader(stream))
|
||
{
|
||
//版本
|
||
var Version = reader.ReadByte();
|
||
|
||
//取电
|
||
var TakeCardStatus = reader.ReadByte();
|
||
|
||
//身份
|
||
var IdentityInfo = reader.ReadByte();
|
||
var NOCardInfo = reader.ReadByte();
|
||
|
||
|
||
cache.Version = Version.ToString("X2");
|
||
cache.TakeCardInfo.TakeCardStatus = TakeCardStatus;
|
||
cache.TakeCardInfo.IdentityInfo = IdentityInfo;
|
||
cache.TakeCardInfo.NOCardInfo = NOCardInfo;
|
||
|
||
|
||
//服务信息全部回路状态 1~64 64Bit 0 :关 1:开
|
||
//把8个字节转换成64个二进制位
|
||
var N = reader.ReadBytes(8);
|
||
BitArray bitlist = new BitArray(N);
|
||
|
||
for (int i = 0; i < bitlist.Length; i++)
|
||
{
|
||
if (i == 1)
|
||
{
|
||
continue;
|
||
}
|
||
string SerNo = i.ToString("000");
|
||
}
|
||
|
||
//PMS房态
|
||
//定时上报,之后要设置房态,要用0C
|
||
//只会上报 01 出租,02退房
|
||
var PMS = reader.ReadByte();
|
||
cache.PMS_RoomStatus.PMSStatus = PMS;
|
||
//if (PMS != 0x01 && PMS != 0x02)
|
||
if (true)
|
||
{
|
||
|
||
}
|
||
|
||
//碳达人
|
||
var CarbonVIP = reader.ReadByte();
|
||
cache.TanDaRen.CarbonVIP_Status = CarbonVIP;
|
||
|
||
var DeviceCount = reader.ReadByte();
|
||
int DeviceCount_I = (int)DeviceCount;
|
||
|
||
for (int i = 0; i < DeviceCount_I; i++)
|
||
{
|
||
var QA = reader.ReadBytes(4);
|
||
//说明是能耗设备
|
||
#region 能耗
|
||
if (QA[0] == 0x39)
|
||
{
|
||
string address = DeviceConvert.Convert_To_Address(QA);
|
||
//设备数据长度
|
||
var DeviceInfoLen = reader.ReadByte();
|
||
var Len = Convert.ToInt32(DeviceInfoLen);
|
||
//设备数据
|
||
var Data = reader.ReadBytes(Len);
|
||
|
||
byte[] DianYa = Data.Take(2).ToArray();
|
||
byte[] DianLiu = Data.Skip(2).Take(2).ToArray();
|
||
byte[] Power = Data.Skip(4).Take(4).ToArray();
|
||
byte[] PowerUsed = Data.Skip(8).Take(4).ToArray();
|
||
byte[] TotalPowerUsed = Data.Skip(12).Take(4).ToArray();
|
||
uint dianya = BitConverter.ToUInt16(DianYa, 0);
|
||
uint dianliu = BitConverter.ToUInt16(DianLiu, 0);
|
||
uint gonglv = BitConverter.ToUInt32(Power, 0);
|
||
|
||
uint nenghao = BitConverter.ToUInt32(PowerUsed, 0);
|
||
uint zongnenghao = BitConverter.ToUInt32(TotalPowerUsed, 0);
|
||
|
||
double V = (double)dianya * 10 / 1000;
|
||
double A = (double)dianliu * 10 / 1000;
|
||
double P = (double)gonglv / 1000;
|
||
|
||
double KW_H = (double)nenghao / 1000;
|
||
double Sum_KW_H = (double)zongnenghao / 1000;
|
||
}
|
||
#endregion
|
||
|
||
#region 是温控
|
||
//定期上报,
|
||
//原本的0E 修改了命令字,改成其它命令字,内容不变,但是空调 只有操作上报。
|
||
//空调定期上报的数据,改为在这里上报
|
||
else if (QA[0] == 0x07)
|
||
{
|
||
//07 //设备类型 - 温控
|
||
//01 //设备地址
|
||
//01 00 //设备回路
|
||
//02 //设备数据长度
|
||
//00 00 //设备数据
|
||
|
||
string address = DeviceConvert.Convert_To_Address(QA);
|
||
//设备数据长度
|
||
var WenKongQiDataLen = reader.ReadByte();
|
||
//温控器
|
||
var DataLen1 = Convert.ToInt32(WenKongQiDataLen);
|
||
|
||
///ushort StatusReceiver = reader.ReadUInt16();
|
||
ushort StatusReceiver = BitConverter.ToUInt16(reader.ReadBytes(2).Reverse().ToArray(), 0);
|
||
|
||
}
|
||
#endregion
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
public override Task OnActivateAsync(CancellationToken cancellationToken)
|
||
{
|
||
string KKK = this.GetPrimaryKeyString();
|
||
|
||
|
||
var ssa= this.GetStreamProvider("");
|
||
StreamId id= StreamId.Create("ns",Guid.NewGuid().ToString());
|
||
//ssa.GetStream(id).OnNextAsync("");
|
||
this.HostNumber = KKK;
|
||
var take = Common.Cache.Cache.GetCache(KKK).Result;
|
||
if (take != null)
|
||
{
|
||
this._cache = take;
|
||
}
|
||
else
|
||
{
|
||
this._cache = new MyData_Cache();
|
||
}
|
||
this.EndPoint = KKK;
|
||
return base.OnActivateAsync(cancellationToken);
|
||
}
|
||
|
||
public override Task OnDeactivateAsync(DeactivationReason reason, CancellationToken cancellationToken)
|
||
{
|
||
return base.OnDeactivateAsync(reason, cancellationToken);
|
||
}
|
||
}
|
||
}
|