using System.Collections.Generic;
|
using System.Text;
|
|
namespace IStation.Server
|
{
|
/// <summary>
|
/// 接收信息辅助类
|
/// </summary>
|
internal class RequestReceivedHelper
|
{
|
|
//接收数据
|
public static async ValueTask Receive(IAppSession session, PackageInfo packageInfo)
|
{
|
await Task.Run(() =>
|
{
|
try
|
{
|
var mySession = (MySession)session;
|
var allBytes = packageInfo.Body;
|
LogHelper.Info($" {mySession.SessionName}接收到数据:{BitTransfer.ToString(allBytes)}!");
|
|
var prefix = ParsePrefix(allBytes);
|
if (prefix != ConfigHelper.Prefix)
|
{
|
LogHelper.Info($"收到一条无法识别的数据:{Encoding.Unicode.GetString(allBytes)}!");
|
return;
|
}
|
mySession.Send("OK"); //前缀认证成功 返回 “OK”
|
|
// 解析数据,每64个字节为一帧数据
|
var preLength = 9;
|
var dataCount = 64;
|
var totalCount = (allBytes.Length - preLength) / dataCount;
|
|
for (int i = 0; i < totalCount; i++)
|
{
|
var bytes = allBytes.Skip(preLength + (i * dataCount)).Take(dataCount).ToArray();
|
var model = HandleData(bytes);
|
if (model != null)
|
{
|
var bol = TransferHelper.Transfer(model);
|
if (bol)
|
{
|
LogHelper.Info($"{model.RegisterCode},成功插入{model.RecordList.Count}条数据!");
|
}
|
else
|
{
|
LogHelper.Info($"{model.RegisterCode},插入{model.RecordList.Count} 条数据失败!");
|
}
|
Thread.Sleep(1000);
|
}
|
}
|
}
|
catch (Exception ex)
|
{
|
LogHelper.Error(ex.Message);
|
}
|
|
});
|
|
}
|
|
|
#region 处理数据
|
|
/// <summary>
|
/// 处理数据
|
/// </summary>
|
/// <param name="bytes">64字节</param>
|
/// <returns></returns>
|
private static StandardModel HandleData(byte[] bytes)
|
{
|
var dataTime = ParseTime(bytes);
|
if (!dataTime.HasValue)
|
{
|
return default;
|
}
|
var code = ParseCode(bytes);
|
|
var model = new StandardModel();
|
model.RegisterCode = code.ToString();
|
model.RecordList = new List<StandardRecord>();
|
|
var value瞬时流量 = Parse瞬时流量(bytes);
|
model.RecordList.Add(new StandardRecord()
|
{
|
TransferId = "瞬时流量",
|
SrcTime = dataTime.Value,
|
SubList = new List<StandardSubRecord>() {
|
new StandardSubRecord(){
|
TransferId=string.Empty,
|
SrcValue=value瞬时流量.ToString(),
|
DataStatus=new List<string>()
|
}
|
}
|
});
|
|
var value瞬时压力 = Parse瞬时压力(bytes);
|
model.RecordList.Add(new StandardRecord()
|
{
|
TransferId = "瞬时压力",
|
SrcTime = dataTime.Value,
|
SubList = new List<StandardSubRecord>() {
|
new StandardSubRecord(){
|
TransferId=string.Empty,
|
SrcValue=value瞬时压力.ToString(),
|
DataStatus=new List<string>()
|
}
|
}
|
});
|
|
var value瞬时流速 = Parse瞬时流速(bytes);
|
model.RecordList.Add(new StandardRecord()
|
{
|
TransferId = "瞬时流速",
|
SrcTime = dataTime.Value,
|
SubList = new List<StandardSubRecord>() {
|
new StandardSubRecord(){
|
TransferId=string.Empty,
|
SrcValue=value瞬时流速.ToString(),
|
DataStatus=new List<string>()
|
}
|
}
|
});
|
|
var value正向累计 = Parse正向累计(bytes);
|
model.RecordList.Add(new StandardRecord()
|
{
|
TransferId = "正向累计",
|
SrcTime = dataTime.Value,
|
SubList = new List<StandardSubRecord>() {
|
new StandardSubRecord(){
|
TransferId=string.Empty,
|
SrcValue=value正向累计.ToString(),
|
DataStatus=new List<string>()
|
}
|
}
|
});
|
|
var value反向累计 = Parse反向累计(bytes);
|
model.RecordList.Add(new StandardRecord()
|
{
|
TransferId = "反向累计",
|
SrcTime = dataTime.Value,
|
SubList = new List<StandardSubRecord>() {
|
new StandardSubRecord(){
|
TransferId=string.Empty,
|
SrcValue=value反向累计.ToString(),
|
DataStatus=new List<string>()
|
}
|
}
|
});
|
|
var value水表电池电量 = Parse水表电池电量(bytes);
|
model.RecordList.Add(new StandardRecord()
|
{
|
TransferId = "水表电池电量",
|
SrcTime = dataTime.Value,
|
SubList = new List<StandardSubRecord>() {
|
new StandardSubRecord(){
|
TransferId=string.Empty,
|
SrcValue=value水表电池电量.ToString(),
|
DataStatus=new List<string>()
|
}
|
}
|
});
|
|
var valueGPRS电池电量 = ParseGPRS电池电量(bytes);
|
model.RecordList.Add(new StandardRecord()
|
{
|
TransferId = "GPRS电池电量",
|
SrcTime = dataTime.Value,
|
SubList = new List<StandardSubRecord>() {
|
new StandardSubRecord(){
|
TransferId=string.Empty,
|
SrcValue=valueGPRS电池电量.ToString(),
|
DataStatus=new List<string>()
|
}
|
}
|
});
|
|
var value系统报警 = Parse系统报警(bytes);
|
model.RecordList.Add(new StandardRecord()
|
{
|
TransferId = "系统报警",
|
SrcTime = dataTime.Value,
|
SubList = new List<StandardSubRecord>() {
|
new StandardSubRecord(){
|
TransferId=string.Empty,
|
SrcValue=value系统报警.ToString(),
|
DataStatus=new List<string>()
|
}
|
}
|
});
|
|
var value水表空管报警 = Parse水表空管报警(bytes);
|
model.RecordList.Add(new StandardRecord()
|
{
|
TransferId = "水表空管报警",
|
SrcTime = dataTime.Value,
|
SubList = new List<StandardSubRecord>() {
|
new StandardSubRecord(){
|
TransferId=string.Empty,
|
SrcValue=value水表空管报警.ToString(),
|
DataStatus=new List<string>()
|
}
|
}
|
});
|
|
var value断励磁报警 = Parse断励磁报警(bytes);
|
model.RecordList.Add(new StandardRecord()
|
{
|
TransferId = "断励磁报警",
|
SrcTime = dataTime.Value,
|
SubList = new List<StandardSubRecord>() {
|
new StandardSubRecord(){
|
TransferId=string.Empty,
|
SrcValue=value断励磁报警.ToString(),
|
DataStatus=new List<string>()
|
}
|
}
|
});
|
|
var value流量上限报警 = Parse流量上限报警(bytes);
|
model.RecordList.Add(new StandardRecord()
|
{
|
TransferId = "流量上限报警",
|
SrcTime = dataTime.Value,
|
SubList = new List<StandardSubRecord>() {
|
new StandardSubRecord(){
|
TransferId=string.Empty,
|
SrcValue=value流量上限报警.ToString(),
|
DataStatus=new List<string>()
|
}
|
}
|
});
|
|
var value流量下限报警 = Parse流量下限报警(bytes);
|
model.RecordList.Add(new StandardRecord()
|
{
|
TransferId = "流量下限报警",
|
SrcTime = dataTime.Value,
|
SubList = new List<StandardSubRecord>() {
|
new StandardSubRecord(){
|
TransferId=string.Empty,
|
SrcValue=value流量下限报警.ToString(),
|
DataStatus=new List<string>()
|
}
|
}
|
});
|
|
var value压力上限报警 = Parse压力上限报警(bytes);
|
model.RecordList.Add(new StandardRecord()
|
{
|
TransferId = "压力上限报警",
|
SrcTime = dataTime.Value,
|
SubList = new List<StandardSubRecord>() {
|
new StandardSubRecord(){
|
TransferId=string.Empty,
|
SrcValue=value压力上限报警.ToString(),
|
DataStatus=new List<string>()
|
}
|
}
|
});
|
|
var value压力下限报警 = Parse压力下限报警(bytes);
|
model.RecordList.Add(new StandardRecord()
|
{
|
TransferId = "压力下限报警",
|
SrcTime = dataTime.Value,
|
SubList = new List<StandardSubRecord>() {
|
new StandardSubRecord(){
|
TransferId=string.Empty,
|
SrcValue=value压力下限报警.ToString(),
|
DataStatus=new List<string>()
|
}
|
}
|
});
|
|
return model;
|
}
|
|
#endregion
|
|
#region 解析方法
|
|
/// <summary>
|
/// 解析前缀
|
/// </summary>
|
/// <param name="bts">所有字节</param>
|
/// <returns></returns>
|
private static string ParsePrefix(byte[] bts)
|
{
|
if (bts == null || bts.Length < 9)
|
{
|
return string.Empty;
|
}
|
var btc = bts.Take(6).ToArray();
|
return Encoding.ASCII.GetString(btc);
|
}
|
|
/// <summary>
|
/// 解析编码
|
/// </summary>
|
/// <param name="bts">64字节</param>
|
/// <returns></returns>
|
private static long ParseCode(byte[] bts)
|
{
|
var btc = bts.Take(4).ToArray();
|
return Bytes2Int32(btc);
|
}
|
|
/// <summary>
|
/// 解析时间
|
/// </summary>
|
/// <param name="bts">64字节</param>
|
/// <returns></returns>
|
private static DateTime? ParseTime(byte[] bts)
|
{
|
|
try
|
{
|
DateTime? dt = null;
|
var dateInt = Bytes2Int32(bts.Skip(4).Take(4).ToArray());
|
var hmsInt = Bytes2Int32(bts.Skip(8).Take(4).ToArray());
|
var hmsStr = hmsInt.ToString();
|
if (hmsStr.Length < 3)
|
{
|
hmsStr = "0000" + hmsStr;
|
}
|
if (hmsStr.Length < 5)
|
{
|
hmsStr = "00" + hmsStr;
|
}
|
if (hmsStr.Length < 6)
|
{
|
hmsStr = "0" + hmsStr;
|
}
|
var hms = hmsStr.Substring(0, 2) + ":" + hmsStr.Substring(2, 2) + ":" + hmsStr.Substring(4, 2);
|
var date = DateTime.ParseExact(dateInt.ToString(), "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
|
var time = $"{date:d} {hms}";
|
dt = Convert.ToDateTime(time);
|
return dt;
|
}
|
catch (Exception ex)
|
{
|
LogHelper.Error($"时间解析:{ex.Message}");
|
return default;
|
}
|
}
|
|
/// <summary>
|
/// 解析瞬时流量
|
/// </summary>
|
/// <param name="bts">64字节</param>
|
/// <returns></returns>
|
private static double Parse瞬时流量(byte[] bts)
|
{
|
var btc = bts.Skip(12).Take(4).ToArray();
|
return Bytes2Single(btc);
|
}
|
|
/// <summary>
|
/// 解析瞬时压力
|
/// </summary>
|
/// <param name="bts">64字节</param>
|
/// <returns></returns>
|
private static double Parse瞬时压力(byte[] bts)
|
{
|
var btc = bts.Skip(16).Take(4).ToArray();
|
return Bytes2Single(btc);
|
}
|
|
/// <summary>
|
/// 解析瞬时流速
|
/// </summary>
|
/// <param name="bts">64字节</param>
|
/// <returns></returns>
|
private static double Parse瞬时流速(byte[] bts)
|
{
|
var btc = bts.Skip(20).Take(4).ToArray();
|
return Bytes2Single(btc);
|
}
|
|
/// <summary>
|
/// 解析正向累计
|
/// </summary>
|
/// <param name="bts">64字节</param>
|
/// <returns></returns>
|
private static double Parse正向累计(byte[] bts)
|
{
|
var btc1 = bts.Skip(24).Take(4).ToArray();
|
var value1 = Bytes2Int32(btc1);
|
var btc2 = bts.Skip(32).Take(2).ToArray();
|
var value2 = Bytes2Int16(btc2) / 1000f;
|
return value1 + value2;
|
}
|
|
/// <summary>
|
/// 解析反向累计
|
/// </summary>
|
/// <param name="bts">64字节</param>
|
/// <returns></returns>
|
private static double Parse反向累计(byte[] bts)
|
{
|
var btc1 = bts.Skip(28).Take(4).ToArray();
|
var value1 = Bytes2Int32(btc1);
|
var btc2 = bts.Skip(34).Take(2).ToArray();
|
var value2 = Bytes2Int16(btc2) / 1000f;
|
return value1 + value2;
|
}
|
|
/// <summary>
|
/// 解析水表电池电量
|
/// </summary>
|
/// <param name="bts">64字节</param>
|
/// <returns></returns>
|
private static int Parse水表电池电量(byte[] bts)
|
{
|
var btc = bts.Skip(36).Take(1).ToArray();
|
return Bytes2Int8(btc);
|
}
|
|
/// <summary>
|
/// 解析GPRS电池电量
|
/// </summary>
|
/// <param name="bts">64字节</param>
|
/// <returns></returns>
|
private static int ParseGPRS电池电量(byte[] bts)
|
{
|
var btc = bts.Skip(37).Take(1).ToArray();
|
return Bytes2Int8(btc);
|
}
|
|
/// <summary>
|
/// 解析系统报警
|
/// </summary>
|
/// <param name="bts">64字节</param>
|
/// <returns></returns>
|
private static int Parse系统报警(byte[] bts)
|
{
|
var btc = bts.Skip(38).Take(1).ToArray();
|
return Bytes2Int8(btc);
|
}
|
|
/// <summary>
|
/// 解析水表空管报警
|
/// </summary>
|
/// <param name="bts">64字节</param>
|
/// <returns></returns>
|
private static int Parse水表空管报警(byte[] bts)
|
{
|
var btc = bts.Skip(39).Take(1).ToArray();
|
return Bytes2Int8(btc);
|
}
|
|
/// <summary>
|
/// 解析断励磁报警
|
/// </summary>
|
/// <param name="bts">64字节</param>
|
/// <returns></returns>
|
private static int Parse断励磁报警(byte[] bts)
|
{
|
var btc = bts.Skip(40).Take(1).ToArray();
|
return Bytes2Int8(btc);
|
}
|
|
/// <summary>
|
/// 解析流量上限报警
|
/// </summary>
|
/// <param name="bts">64字节</param>
|
/// <returns></returns>
|
private static int Parse流量上限报警(byte[] bts)
|
{
|
var btc = bts.Skip(41).Take(1).ToArray();
|
return Bytes2Int8(btc);
|
}
|
|
/// <summary>
|
/// 解析流量下限报警
|
/// </summary>
|
/// <param name="bts">64字节</param>
|
/// <returns></returns>
|
private static int Parse流量下限报警(byte[] bts)
|
{
|
var btc = bts.Skip(42).Take(1).ToArray();
|
return Bytes2Int8(btc);
|
}
|
|
/// <summary>
|
/// 解析压力上限报警
|
/// </summary>
|
/// <param name="bts">64字节</param>
|
/// <returns></returns>
|
private static int Parse压力上限报警(byte[] bts)
|
{
|
var btc = bts.Skip(43).Take(1).ToArray();
|
return Bytes2Int8(btc);
|
}
|
|
/// <summary>
|
/// 解析压力下限报警
|
/// </summary>
|
/// <param name="bts">64字节</param>
|
/// <returns></returns>
|
private static int Parse压力下限报警(byte[] bts)
|
{
|
var btc = bts.Skip(44).Take(1).ToArray();
|
return Bytes2Int8(btc);
|
}
|
|
|
#endregion
|
|
#region 字节转化方法
|
|
private static int Bytes2Int8(byte[] bts)
|
{
|
var btc = new byte[] { bts[0], 00 };
|
return BitConverter.ToInt16(btc);
|
}
|
|
private static int Bytes2Int32(byte[] bts)
|
{
|
return BitConverter.ToInt32(bts);
|
}
|
|
private static int Bytes2Int16(byte[] bts)
|
{
|
return BitConverter.ToInt16(bts);
|
}
|
|
private static double Bytes2Single(byte[] bts)
|
{
|
return BitConverter.ToSingle(bts);
|
}
|
|
|
#endregion
|
|
|
|
|
|
}
|
}
|