using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IStation.Untity;
namespace IStation.DataDockingSocket
{
///
/// 处理数据辅助类
///
internal class MsgDataHelper
{
///
/// 获取
///
///
public static AppParas GetAppParas()
{
if (_appparas == null)
{
var jsonFileName = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "paras_datadocking_socket_nt.json");
var json = FileHelper.ReadAllText(jsonFileName);
_appparas = JsonHelper.Json2Object(json);
}
return _appparas;
}
private static AppParas _appparas = null;
///
///
///
///
public static bool IsSrc()
{
return GetAppParas().Src;
}
///
///
///
///
public static bool IsDebug()
{
return GetAppParas().Debug;
}
///
/// 处理数据
///
public static List HandleData
(
byte[] byteMessage
)
{
if (byteMessage[2] == 0x32)
{
return ReadMessage监控值(byteMessage);
}
//else if (byteMessage[2] == 0x46)
//{
// return ReadMessage状态值(byteMessage);
//}
else
{
return null;
}
}
///
/// 获取控制指令(关机)
///
///
public static byte[] GetControlMsg关机()
{
return null;
}
///
///
///
///
internal static IEnumerable GetInstructionQuerys()
{
var appParas = GetAppParas();
if (appParas == null)
{
LogHelper.Error("Socket 数据对接中,NT 缺少数据对接配置文件");
return null;
}
return appParas.InstructionQuerys;
}
///
/// 获取控制指令(开机)
///
///
public static byte[] GetControlMsg开机()
{
return null;
}
///
/// 获取控制指令(调频)
///
///
public static byte[] GetControlMsg调频(double frequece)
{
var iFrequece = (int)(frequece * 10);
byte[] commandSend = null;
IStation.DataDockingSocket.ModBusRtuHelper.BuildSendMessage06(0x01, 20, 2, iFrequece, ref commandSend);
return commandSend;
}
///
/// 获取查询指令(监控值)
///
///
public byte[] GetMonitorMsg监控值()
{
return new byte[] { 0x01, 0x03, 0x00, 0x19, 0x00, 0x19, 0x55, 0xC7 };
}
///
/// 获取查询指令(状态值):01-03-00-12-00-04-E4-0C
///
///
public static byte[] GetMonitorMsg状态值()
{
return new byte[] { 0x01, 0x03, 0x00, 0x12, 0x00, 0x04, 0xE4, 0x0C };
}
///
/// 字符串转16进制字符2
///
/// 字符串
/// 编码格式
///
private static List StringToHexValuve(string content, out string error_info)
{
//去掉空格
string[] arr = content.Split(' ');
if (arr.Length < 3)
{
error_info = "字符无法解析";
return null;
}
List result = new List();
for (int i = 0; i < arr.Length; i++)
{
if (string.IsNullOrWhiteSpace(arr[i]))
continue;
var dddd = Convert.ToByte(arr[i], 16);
result.Add(dddd);
}
error_info = null;
return result;
}
///
/// 读取消息(监控值)
///
///
private static List ReadMessage监控值(byte[] byteMessage)
{
var list = new List();
byte start = 0x1A;
if (byteMessage == null || byteMessage.Length < 5 + (56 - start) * 2)
{
return null;
}
try
{
//显示消息
//string strMessage = BitConverter.ToString(byteMessage, 0, byteMessage.Length);
//DisplayMessage(string.Format(" 收到的消息: {0} ", strMessage));
//DisplayMessage(string.Format(" 设备地址: {0} (0x{1})", byteMessage[0], Convert.ToString(byteMessage[0], 16)));
//DisplayMessage(string.Format(" 功能码: {0} ", byteMessage[1]));
string msg;
var now = DateTime.Now;
//当前管网压力
list.Add(new Model.MonitorDataDockingReceiveRecord()
{
SysId = 1575030496453332992,
RecordType = Model.eMonitorType.General,
SrcTime = now,
SrcValue = (GetInt2Byte(byteMessage, 3, out msg) * 0.001).ToString()
});
//变频频率
list.Add(new Model.MonitorDataDockingReceiveRecord()
{
SysId = 1575030605857558528,
RecordType = Model.eMonitorType.General,
SrcTime = now,
SrcValue = (GetInt2Byte(byteMessage, 3 + (27 - start) * 2, out msg) * 0.1).ToString()
});
//变频电流
list.Add(new Model.MonitorDataDockingReceiveRecord()
{
SysId = 1582301190702632960,
RecordType = Model.eMonitorType.General,
SrcTime = now,
SrcValue = (GetInt2Byte(byteMessage, 3 + (28 - start) * 2, out msg) * 0.1).ToString()
});
//真空当前压力
list.Add(new Model.MonitorDataDockingReceiveRecord()
{
SysId = 1575030435430404096,
RecordType = Model.eMonitorType.General,
SrcTime = now,
SrcValue = (GetInt2Byte(byteMessage, 3 + (29 - start) * 2, out msg) * 0.001).ToString()
});
//A相电压
list.Add(new Model.MonitorDataDockingReceiveRecord()
{
SysId = 1582301249292865536,
RecordType = Model.eMonitorType.General,
SrcTime = now,
SrcValue = GetDouble1(byteMessage, 3 + (30 - start) * 2, out msg).ToString()
});
//B相电压
list.Add(new Model.MonitorDataDockingReceiveRecord()
{
SysId = 1582301287175819264,
RecordType = Model.eMonitorType.General,
SrcTime = now,
SrcValue = GetDouble1(byteMessage, 3 + (32 - start) * 2, out msg).ToString()
});
//C相电压
list.Add(new Model.MonitorDataDockingReceiveRecord()
{
SysId = 1582301332780486656,
RecordType = Model.eMonitorType.General,
SrcTime = now,
SrcValue = GetDouble1(byteMessage, 3 + (34 - start) * 2, out msg).ToString()
});
//A相电流
list.Add(new Model.MonitorDataDockingReceiveRecord()
{
SysId = 1582301384118767616,
RecordType = Model.eMonitorType.General,
SrcTime = now,
SrcValue =
GetDouble1(byteMessage, 3 + (36 - start) * 2, out msg).ToString()
});
//B相电流
list.Add(new Model.MonitorDataDockingReceiveRecord()
{
SysId = 1582301478792597504,
RecordType = Model.eMonitorType.General,
SrcTime = now,
SrcValue = GetDouble1(byteMessage, 3 + (38 - start) * 2, out msg).ToString()
});
//C相电流
list.Add(new Model.MonitorDataDockingReceiveRecord()
{
SysId = 1582301691439616000,
RecordType = Model.eMonitorType.General,
SrcTime = now,
SrcValue =
GetDouble1(byteMessage, 3 + (40 - start) * 2, out msg).ToString()
});
//变频泵状态
list.Add(new Model.MonitorDataDockingReceiveRecord()
{
SysId = 1582303421711650816,
RecordType = Model.eMonitorType.General,
SrcTime = now,
SrcValue = GetInt2Byte(byteMessage, 3 + (42 - start) * 2, out msg).ToString()
});
//真空泵状态
list.Add(new Model.MonitorDataDockingReceiveRecord()
{
SysId = 1582303241251721216,
RecordType = Model.eMonitorType.General,
SrcTime = now,
SrcValue = GetInt2Byte(byteMessage, 3 + (43 - start) * 2, out msg).ToString()
});
////电动阀开阀状态
//list.Add(new Model.MonitorDataDockingReceiveRecord() { SysId = 1582301332780486656, RecordType = Model.eMonitorType.General, SrcTime = now,
// SrcValue = string.Format("########电动阀开阀状态 -----> {0} 数据二进制{1}", GetInt2Byte(byteMessage, 3 + (44 - start) * 2, out msg).ToString() });
////电动阀关阀状态
//list.Add(new Model.MonitorDataDockingReceiveRecord() { SysId = 1582301332780486656, RecordType = Model.eMonitorType.General, SrcTime = now,
// SrcValue = string.Format("########电动阀关阀状态 -----> {0} 数据二进制{1}", GetInt2Byte(byteMessage, 3 + (45 - start) * 2, out msg).ToString() });
//电磁阀状态
list.Add(new Model.MonitorDataDockingReceiveRecord()
{
SysId = 1582303081771700224,
RecordType = Model.eMonitorType.General,
SrcTime = now,
SrcValue = GetInt2Byte(byteMessage, 3 + (46 - start) * 2, out msg).ToString()
});
//水位状态
list.Add(new Model.MonitorDataDockingReceiveRecord()
{
SysId = 1582303471342850048,
RecordType = Model.eMonitorType.General,
SrcTime = now,
SrcValue = GetInt2Byte(byteMessage, 3 + (47 - start) * 2, out msg).ToString()
});
//超压状态
list.Add(new Model.MonitorDataDockingReceiveRecord()
{
SysId = 1582303606781120512,
RecordType = Model.eMonitorType.General,
SrcTime = now,
SrcValue = GetInt2Byte(byteMessage, 3 + (48 - start) * 2, out msg).ToString()
});
//报警状态
list.Add(new Model.MonitorDataDockingReceiveRecord()
{
SysId = 1582303637420511232,
RecordType = Model.eMonitorType.General,
SrcTime = now,
SrcValue = GetInt2Byte(byteMessage, 3 + (49 - start) * 2, out msg).ToString()
});
//变频器状态
list.Add(new Model.MonitorDataDockingReceiveRecord()
{
SysId = 1582303671927050240,
RecordType = Model.eMonitorType.General,
SrcTime = now,
SrcValue = GetInt2Byte(byteMessage, 3 + (50 - start) * 2, out msg).ToString()
});
//当前液位
list.Add(new Model.MonitorDataDockingReceiveRecord()
{
SysId = 1575388386963886080,
RecordType = Model.eMonitorType.General,
SrcTime = now,
SrcValue = (GetInt2Byte(byteMessage, 3 + (51 - start) * 2, out msg) * 0.01).ToString()
});
//瞬时流量
list.Add(new Model.MonitorDataDockingReceiveRecord()
{
SysId = 1575030320913321984,
RecordType = Model.eMonitorType.General,
SrcTime = now,
SrcValue = (GetInt4Byte(byteMessage, 3 + (52 - start) * 2, out msg) * 0.001).ToString()
});
//累计流量
list.Add(new Model.MonitorDataDockingReceiveRecord()
{
SysId = 1584475350807744512,
RecordType = Model.eMonitorType.General,
SrcTime = now,
SrcValue = (GetInt4Byte(byteMessage, 3 + (54 - start) * 2, out msg) * 1.0).ToString()
});
//累计流量有功功率
list.Add(new Model.MonitorDataDockingReceiveRecord()
{
SysId = 1584475756476633088,
RecordType = Model.eMonitorType.General,
SrcTime = now,
SrcValue = GetDouble1(byteMessage, 3 + (56 - start) * 2, out msg).ToString()
});
}
catch //(Exception err)
{
return null;
}
return list;
}
///
/// 读取消息(状态值)
///
///
private static List ReadMessage状态值(byte[] byteMessage)
{
var list = new List();
byte start = 0x12;
if (byteMessage == null || byteMessage.Length < 8 )
{
return null;
}
try
{
//显示消息
//string strMessage = BitConverter.ToString(byteMessage, 0, byteMessage.Length);
//DisplayMessage(string.Format(" 收到的消息: {0} ", strMessage));
//DisplayMessage(string.Format(" 设备地址: {0} (0x{1})", byteMessage[0], Convert.ToString(byteMessage[0], 16)));
//DisplayMessage(string.Format(" 功能码: {0} ", byteMessage[1]));
string msg;
var now = DateTime.Now;
//液位量程
//list.Add(new Model.MonitorDataDockingReceiveRecord()
//{
// SysId = ,
// RecordType = Model.eMonitorType.General,
// SrcTime = now,
// SrcValue = (GetInt2Byte(byteMessage, 3 + (19 - start), out msg )* 0.1).ToString()
//});
//设定频率
list.Add(new Model.MonitorDataDockingReceiveRecord()
{
SysId = 1584894751252025344,
RecordType = Model.eMonitorType.General,
SrcTime = now,
SrcValue = (GetInt2Byte(byteMessage, 3 + (20 - start), out msg) * 0.1).ToString()
});
}
catch// (Exception err)
{
return null;
}
return list;
}
#region MyRegion
private static int GetInt2Byte(byte[] byteMessage, int startPosition, out string info)
{
byte[] value = new byte[2];
Array.Copy(byteMessage, startPosition, value, 0, 2);
info = BitConverter.ToString(value, 0, value.Length);
// ushort rValueP = BitConverter.ToUInt16(value.Reverse().ToArray(), 0);
//short rValueP = 0;
//rValueP = byteMessage[ startPosition];
//rValueP <<= 8;
//rValueP += byteMessage[ startPosition + 1];
//return rValueP;
return bytesToInt2(byteMessage, startPosition);
}
private static int GetInt4Byte(byte[] byteMessage, int startPosition, out string info)
{
byte[] value = new byte[4];
Array.Copy(byteMessage, startPosition, value, 0, 4);
info = BitConverter.ToString(value, 0, value.Length);
// ushort rValueP = BitConverter.ToUInt16(value.Reverse().ToArray(), 0);
//short rValueP = 0;
//rValueP = byteMessage[ startPosition];
//rValueP <<= 8;
//rValueP += byteMessage[ startPosition + 1];
//return rValueP;
return bytesToInt4(byteMessage, startPosition);
}
/**
* byte数组中取int数值,本方法适用于(低位在前,高位在后)的顺序,和和intToBytes()配套使用
*
* @param src
* byte数组
* @param offset
* 从数组的第offset位开始
* @return int数值
*/
public static int bytesToInt2_LH(byte[] src, int offset)
{
int value;
value = (int)((src[offset] & 0xFF)
| ((src[offset + 1] & 0xFF) << 8));
return value;
}
/**
* byte数组中取int数值,本方法适用于(低位在后,高位在前)的顺序。2个字节
*/
public static int bytesToInt2(byte[] src, int offset)
{
int value;
value = (int)(
((src[offset + 0] & 0xFF) << 8)
| (src[offset + 1] & 0xFF));
return value;
}
/**
* byte数组中取int数值,本方法适用于(低位在后,高位在前)的顺序。4个字节
*/
public static int bytesToInt4(byte[] src, int offset = 0)
{
int value;
value = (int)(((src[offset] & 0xFF) << 24)
| ((src[offset + 1] & 0xFF) << 16)
| ((src[offset + 2] & 0xFF) << 8)
| (src[offset + 3] & 0xFF));
return value;
}
private static double GetDouble1(byte[] byteMessage, int startPosition, out string info)
{
byte[] value = new byte[4];
Array.Copy(byteMessage, startPosition, value, 0, 4);
info = BitConverter.ToString(value, 0, value.Length);
//return BitConverter.ToDouble(byteMessage, startPosition);
return BitConverter.ToSingle(value.Reverse().ToArray(), 0);//采用了IEEE-754二进制浮点数算术标准
}
#endregion
}
}