using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using IStation.Untity;
|
using IStation.Model;
|
using SuperSocket.Server;
|
using SuperSocket;
|
using System.Reflection;
|
|
namespace IStation.Server.SZJTKT.helper.bak
|
{
|
/// <summary>
|
/// 接收信息辅助类
|
/// </summary>
|
internal class RequestReceivedHelper_bak
|
{
|
|
//接收数据
|
public static async ValueTask Receive(IAppSession session, PackageInfo packageInfo)
|
{
|
await Task.Run(async () =>
|
{
|
try
|
{
|
var mySession = (MySession)session;
|
await Task.Run(async () =>
|
{
|
var bytes = packageInfo.Body;
|
var message_bts = BitTransfer.ToString(bytes);
|
LogHelper.Info($" {mySession.SessionName}接收到数据:{message_bts}!");
|
if (!Valid(bytes))
|
{
|
LogHelper.Info($"收到一条无法识别的数据:{message_bts}!");
|
return;
|
}
|
mySession.Send("OK");
|
var list = HandleAppParasData(bytes);
|
LogHelper.Debug(JsonHelper.Object2Json(list));
|
var bol = Insert(Settings.Transfer.SZJTKT.RegisterCode, list);
|
if (!bol)
|
{
|
LogHelper.Info($" 插入{list.Count()}条数据失败!");
|
}
|
else
|
{
|
LogHelper.Info($"成功插入{list.Count()}条数据!");
|
}
|
}
|
);
|
}
|
catch (Exception ex)
|
{
|
LogHelper.Error(ex.Message);
|
}
|
|
|
});
|
|
}
|
|
//验证
|
private static bool Valid(byte[] bytes)
|
{
|
if (bytes == null || bytes.Count() < 12)
|
return false;
|
var bytePrefix = new byte[8];
|
Array.Copy(bytes, 0, bytePrefix, 0, 8);
|
var prefix = Encoding.ASCII.GetString(bytePrefix);
|
if (prefix != Settings.Transfer.SZJTKT.Prefix)
|
return false;
|
|
/*byte[] byteProductID = new byte[4];
|
Array.Copy(bytes, 9, byteProductID, 0, 4);
|
var productId = Bytes2Int4(byteProductID);
|
if (productId.ToString() != Settings.Transfer.SZJTKT.ProductId)
|
return false;*/
|
return true;
|
}
|
|
|
|
/// <summary>
|
/// 处理数据
|
/// </summary>
|
public static List<MonitorDataDockingSrcRecord> HandleAppParasData
|
(
|
byte[] bytes
|
)
|
{
|
var appParas = AppParasHelper.Get();
|
if (appParas == null || appParas.RuleItems == null)
|
{
|
LogHelper.Error(" json is null");
|
return default;
|
}
|
var byte4 = new byte[4];
|
//数据读取日期
|
Array.Copy(bytes, 13, byte4, 0, 4);
|
var date = Bytes2Int4(byte4);
|
//数据读取时间
|
Array.Copy(bytes, 17, byte4, 0, 4);
|
var time = Bytes2Int4(byte4);
|
|
var hmsStr = time.ToString();
|
if (hmsStr.Length < 6)
|
hmsStr = "0" + hmsStr;
|
var hms = hmsStr.Substring(0, 2) + ":" + hmsStr.Substring(2, 2) + ":" + hmsStr.Substring(4, 2);
|
var dt = DateTime.ParseExact(date.ToString(), "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
|
var strTime = $"{dt.ToString("d")} {hms}";
|
|
DateTime dataTime = Convert.ToDateTime(strTime);
|
var list = new List<MonitorDataDockingSrcRecord>();
|
foreach (var x in appParas.RuleItems)
|
{
|
double ratio = 1;
|
var record = new MonitorDataDockingSrcRecord();
|
record.SignId = x.SignId;
|
record.RecordType = eMonitorType.General;
|
record.SrcTime = dataTime;
|
record.SrcValue = null;
|
if (x.SignId == "kt_41" || x.SignId == "kt_43")
|
{
|
ratio = 0.001;
|
}
|
switch (x.Rule)
|
{
|
case eRule.Double:
|
{
|
var btValue = bytes.Skip(x.Index).Take(x.Size).ToArray();
|
record.SrcValue = (Bytes2Double(btValue) * ratio).ToString();
|
}
|
break;
|
case eRule.Int4:
|
{
|
var btValue = bytes.Skip(x.Index).Take(x.Size).ToArray();
|
record.SrcValue = (Bytes2Int4(btValue) * ratio).ToString();
|
}
|
break;
|
case eRule.Int32:
|
{
|
var btValue = bytes.Skip(x.Index).Take(x.Size).ToArray();
|
record.SrcValue = (Bytes2Int32(btValue) * ratio).ToString();
|
}
|
break;
|
default: break;
|
}
|
list.Add(record);
|
}
|
if (list.Count < 1)
|
{
|
LogHelper.Error("数据解析失败!");
|
return default;
|
}
|
|
var forwardCumulativeFlowInteger = list.Find(x => x.SignId == "kt_33");
|
var forwardCumulativeFlowDecimal = list.Find(x => x.SignId == "kt_41");
|
var reverseCumulativeFlowInteger = list.Find(x => x.SignId == "kt_37");
|
var reverseCumulativeFlowDecimal = list.Find(x => x.SignId == "kt_43");
|
var fcfdValue = forwardCumulativeFlowDecimal.SrcValue;
|
if (!string.IsNullOrEmpty(fcfdValue))
|
{
|
if (double.TryParse(fcfdValue, out double value))
|
{
|
if (double.TryParse(forwardCumulativeFlowInteger.SrcValue, out double fcfdValueReal))
|
{
|
|
}
|
forwardCumulativeFlowInteger.SrcValue = (fcfdValueReal + value).ToString();
|
}
|
}
|
|
var rcfdValue = reverseCumulativeFlowDecimal.SrcValue;
|
if (!string.IsNullOrEmpty(rcfdValue))
|
{
|
if (double.TryParse(rcfdValue, out double value))
|
{
|
if (double.TryParse(reverseCumulativeFlowInteger.SrcValue, out double rcfdValueReal))
|
{
|
|
}
|
reverseCumulativeFlowInteger.SrcValue = (rcfdValueReal + value).ToString();
|
}
|
}
|
return list;
|
}
|
|
public static int Bytes2Int4(byte[] src, int offset = 0)
|
{
|
src = src.Reverse().ToArray();
|
int value;
|
value = (src[offset] & 0xFF) << 24
|
| (src[offset + 1] & 0xFF) << 16
|
| (src[offset + 2] & 0xFF) << 8
|
| src[offset + 3] & 0xFF;
|
return value;
|
}
|
public static double Bytes2Double(byte[] value, int offset = 0)
|
{
|
return BitConverter.ToSingle(value, 0);//采用了IEEE-754二进制浮点数算术标准
|
}
|
public static int Bytes2Int32(byte[] bytes)
|
{
|
return Convert.ToInt32(Byte2HexStr(bytes.Reverse().ToArray()), 16); ;
|
}
|
|
|
/// <summary>
|
/// byte[]转为16进制字符串
|
/// </summary>
|
public static string Byte2HexStr(byte[] bytes)
|
{
|
string returnStr = "";
|
if (bytes != null)
|
{
|
for (int i = 0; i < bytes.Length; i++)
|
{
|
returnStr += bytes[i].ToString("X2");
|
}
|
}
|
return returnStr;
|
}
|
|
|
|
private static bool Insert(string registerCode, List<MonitorDataDockingSrcRecord> list)
|
{
|
try
|
{
|
var records = list.Select(x => new DataDockingStandardRecordDto(x.SignId, x.RecordType, x.SrcTime, x.SrcValue));
|
var model = new { RegisterCode = registerCode, Records = records };
|
var url = Settings.Transfer.SZJTKT.TransferUrl;
|
var data = JsonHelper.Object2Json(model);
|
LogHelper.Debug(url + "/" + data);
|
var responseText = HttpRequestHelper.Post(url, data);
|
var result = JsonHelper.Json2Object<Result>(responseText);
|
if (result.Code != 0)
|
{
|
throw new Exception(result.Message);
|
}
|
return result.Data;
|
}
|
catch (Exception ex)
|
{
|
LogHelper.Error(ex.Message);
|
return false;
|
}
|
}
|
|
}
|
}
|
|
|
////接收数据
|
//public static async ValueTask Receive(IAppSession session, PackageInfo packageInfo)
|
//{
|
// await Task.Run(async () =>
|
// {
|
// if (packageInfo == null)
|
// {
|
// LogHelper.Error(" packageInfo is null");
|
// return;
|
// }
|
// var bytes = packageInfo.Body;
|
// if (!Valid(bytes))
|
// {
|
// LogHelper.Error(" bytes Validation failed!");
|
// return;
|
// }
|
// var appParas = AppParasHelper.Get();
|
// if (appParas == null || appParas.RuleItems == null)
|
// {
|
// LogHelper.Error(" json is null");
|
// return;
|
// }
|
|
// var byte4 = new byte[4];
|
// //数据读取日期
|
// Array.Copy(bytes, 13, byte4, 0, 4);
|
// var date = Bytes2Int4(byte4);
|
// //数据读取时间
|
// Array.Copy(bytes, 17, byte4, 0, 4);
|
// var time = Bytes2Int4(byte4);
|
|
// var hmsStr = time.ToString();
|
// if (hmsStr.Length < 6)
|
// hmsStr = "0" + hmsStr;
|
// var hms = hmsStr.Substring(0, 2) + ":" + hmsStr.Substring(2, 2) + ":" + hmsStr.Substring(4, 2);
|
// var strTime = $"{date} {hms}";
|
// DateTime dataTime = DateTime.ParseExact(strTime.ToString(), "yyyyMMdd hh:mm:ss", System.Globalization.CultureInfo.CurrentCulture);
|
|
// var list = new List<Model.MonitorDataDockingSrcRecord>();
|
// foreach (var x in appParas.RuleItems)
|
// {
|
// var record = new Model.MonitorDataDockingSrcRecord();
|
// record.SignId = x.SignId;
|
// record.RecordType = Model.eMonitorType.General;
|
// record.SrcTime = dataTime;
|
// record.SrcValue = null;
|
// switch (x.Rule)
|
// {
|
// case eRule.Double:
|
// {
|
// var btValue = bytes.Skip((x.Index - 1)).Take(x.Size).ToArray();
|
// record.SrcValue = Bytes2Double(btValue).ToString();
|
// }
|
// break;
|
// case eRule.Int4:
|
// {
|
// var btValue = bytes.Skip((x.Index - 1)).Take(x.Size).ToArray();
|
// record.SrcValue = Bytes2Int4(btValue).ToString();
|
// }
|
// break;
|
// case eRule.Int32:
|
// {
|
// var btValue = bytes.Skip((x.Index - 1)).Take(x.Size).ToArray();
|
// record.SrcValue = Bytes2Int32(btValue).ToString();
|
// }
|
// break;
|
// default: break;
|
// }
|
// list.Add(record);
|
// }
|
|
// });
|
|
//}
|