20240301_JSEQ_upperpc/JiangsuEarthquakeJM/JiangsuEarthquake/Models/LowerComputerModel.cs
春风过客 d7f51483a7 新增功能:
1 将地震仪数据读取功能单独成一个页面;
2024-05-13 18:51:05 +08:00

279 lines
14 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Google.Protobuf.WellKnownTypes;
using JiangsuEarthquake.Common;
using JiangsuEarthquake.DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JiangsuEarthquake.Models
{
public class LowerComputerModel
{
private byte functionCode { get; set; } //功能码
private byte SensorType { get; set; } //功能码
private byte checkCodeCRC { get; set; } //校验码
private List<byte> checkByte { get; set; } = new List<byte>(); //校验码Byte
private int byteLength { get; set; }
private DateTime DataTime { get; set; }
private float temperature { get; set; }
private float humidity { get; set; }
private float gestureX { get; set; }
private float gestureY { get; set; }
private float gestureZ { get; set; }
public void ParsingData(List<byte> byteList, int id)
{
if (byteList.Count == 0)
return;
//原始数据存储
string sql = $"insert into conboxoriginaldata(StationID,RecordTime,OriginalData) values('{id}','{DateTime.Now}','{BitConverter.ToString(byteList.ToArray())}');";
DBHelper.ExecuteNonQuery(sql, 1);
//校验和校验
checkByte.Clear();
//CRC校验
for (int i = 0; i < byteList.Count - 2; i++)
checkByte.Add(byteList[i]);
checkCodeCRC = Tools.CheckSum(checkByte.ToArray());
//CRC校验失败
if (checkCodeCRC != byteList[^1])
{
//发送失败信息
if (id == 1)
MainWindow.mainViewModel.serverModel1.SendMess(ServerReplyData(byteList[5], byteList[6], 0x00).ToArray());
else if (id == 2)
MainWindow.mainViewModel.serverModel2.SendMess(ServerReplyData(byteList[5], byteList[6], 0x00).ToArray());
return;
}
functionCode = byteList[5]; //功能码
switch (functionCode)
{
case 0x02:
//浮标端发送数据给服务器
SensorType = byteList[6];
switch (SensorType)
{
case 0x30:
//接驳盒端其他传感器集合
byteLength = BitConverter.ToInt16(new byte[] { byteList[3], byteList[2] }, 0);
if(byteLength!=106) //长度校验失败
{
//发送失败信息
if (id == 1)
MainWindow.mainViewModel.serverModel1.SendMess(ServerReplyData(byteList[5], byteList[6], 0x00).ToArray());
else if (id == 2)
MainWindow.mainViewModel.serverModel2.SendMess(ServerReplyData(byteList[5], byteList[6], 0x00).ToArray());
}
if (byteList[7] == 0x00) //数据类型(0-hex格式1-string)
{
try
{
int timeUTC = BitConverter.ToInt32(new byte[4] { byteList[11], byteList[10], byteList[9], byteList[8] }, 0);
// Unix时间戳起始时间
DateTime epoch = new DateTime(1970, 1, 1, 8, 0, 0, DateTimeKind.Utc);
// 将时间戳转换为UTC时间
DataTime = epoch.AddSeconds(timeUTC);
temperature = BitConverter.ToSingle(new byte[4] { byteList[15], byteList[14], byteList[13], byteList[12] }, 0);
humidity = BitConverter.ToSingle(new byte[4] { byteList[19], byteList[18], byteList[17], byteList[16] }, 0);
gestureX = BitConverter.ToSingle(new byte[4] { byteList[23], byteList[22], byteList[21], byteList[20] }, 0);
gestureY = BitConverter.ToSingle(new byte[4] { byteList[27], byteList[26], byteList[25], byteList[24] }, 0);
gestureZ = BitConverter.ToSingle(new byte[4] { byteList[31], byteList[30], byteList[29], byteList[28] }, 0);
//发送成功信息
if (id == 1)
MainWindow.mainViewModel.serverModel1.SendMess(ServerReplyData(byteList[5], byteList[6], 0x01).ToArray());
else if (id == 2)
MainWindow.mainViewModel.serverModel2.SendMess(ServerReplyData(byteList[5], byteList[6], 0x01).ToArray());
}
catch (Exception ex)
{
//发送失败信息
if (id == 1)
MainWindow.mainViewModel.serverModel1.SendMess(ServerReplyData(byteList[5], byteList[6], 0x00).ToArray());
else if (id == 2)
MainWindow.mainViewModel.serverModel2.SendMess(ServerReplyData(byteList[5], byteList[6], 0x00).ToArray());
}
//数据存储
sql = $"insert into conboxenvpara(StationID,RecordTime,DataTime,JBH_Tem,JBH_Hum," +
$"JBH_Attitude_x,JBH_Attitude_y,JBH_Attitude_z) values('{id}','{DateTime.Now}','{DataTime}','{temperature}','{humidity}','{gestureX}'," +
$"'{gestureY}','{gestureZ}');";
DBHelper.ExecuteNonQuery(sql, 1);
}
break;
case 0x31:
//接驳盒中ADCP数据
break;
}
break;
case 0x03:
//读取状态
byteLength = BitConverter.ToInt16(new byte[] { byteList[3], byteList[2] }, 0);
if (byteLength != 45) //长度校验失败
{
//发送失败信息
if (id == 1)
MainWindow.mainViewModel.serverModel1.SendMess(ServerReplyData(byteList[5], byteList[6], 0x00).ToArray());
else if (id == 2)
MainWindow.mainViewModel.serverModel2.SendMess(ServerReplyData(byteList[5], byteList[6], 0x00).ToArray());
}
try
{
byte bhb_dlzb = 0; //接驳盒-保护板电力载波-状态反馈
byte bhb_gdjhj = 0; //接驳盒-保护板-光电交换机-状态反馈
byte bhb_dzy1 = 0; //接驳盒-保护板-地震仪1-状态反馈
byte bhb_dzy2 = 0; //接驳盒-保护板-地震仪2-状态反馈
byte bhb_hlj = 0; //接驳盒-保护板-海流计-状态反馈
byte dzy_fkdy = 0; //地震仪-反馈电源状态
byte dzy_fkgd = 0; //地震仪-反馈供电方式
byte jbh_dlzb = 0; //接驳盒-电力载波
byte jbh_gdjhj = 0; //接驳盒-光电交换机
byte jbh_dzy1 = 0; //接驳盒-地震仪1
byte jbh_dzy2 = 0; //接驳盒-地震仪2
byte jbh_hlj = 0; //接驳盒-海流计
byte[] state1 = new byte[] { byteList[7], byteList[8], byteList[9], byteList[10] };
if (Tools.IsBitSet(state1, 32))
bhb_dlzb = 1;
if (Tools.IsBitSet(state1, 31))
bhb_gdjhj = 1;
if (Tools.IsBitSet(state1, 30))
bhb_dzy1 = 1;
if (Tools.IsBitSet(state1, 29))
bhb_dzy2 = 1;
if (Tools.IsBitSet(state1, 28))
bhb_hlj = 1;
if (Tools.IsBitSet(state1, 27))
dzy_fkdy = 1;
if (Tools.IsBitSet(state1, 26))
dzy_fkgd = 1;
if (Tools.IsBitSet(state1, 25))
jbh_dlzb = 1;
if (Tools.IsBitSet(state1, 24))
jbh_gdjhj = 1;
if (Tools.IsBitSet(state1, 23))
jbh_dzy1 = 1;
if (Tools.IsBitSet(state1, 22))
jbh_dzy2 = 1;
if (Tools.IsBitSet(state1, 21))
jbh_hlj = 1;
byte leakage = 0;
byte state = 0;
byte[] state2 = new byte[] { byteList[11], byteList[12], byteList[13], byteList[14] };
if (Tools.IsBitSet(state2, 32))
leakage = 1;
if (Tools.IsBitSet(state2, 31))
state = 1;
float voltage; //24V输出电压4-蓄电池电压
float current_dzy1; //地震仪1电流
float current_dzy2; //地震仪2电流
float current_hlj; //海流计-电流
float current_dlzb; //电力载波-电流
float voltage121; //12V输出电压1-预留
float voltage122; //12V输出电压2-预留
voltage = BitConverter.ToSingle(new byte[] { byteList[18], byteList[17], byteList[16], byteList[15] }, 0);
current_dzy1 = BitConverter.ToSingle(new byte[] { byteList[22], byteList[21], byteList[20], byteList[19] }, 0);
current_dzy2 = BitConverter.ToSingle(new byte[] { byteList[26], byteList[25], byteList[24], byteList[23] }, 0);
current_hlj = BitConverter.ToSingle(new byte[] { byteList[30], byteList[29], byteList[28], byteList[27] }, 0);
current_dlzb = BitConverter.ToSingle(new byte[] { byteList[18], byteList[17], byteList[16], byteList[15] }, 0);
voltage121 = BitConverter.ToSingle(new byte[] { byteList[18], byteList[17], byteList[16], byteList[15] }, 0);
voltage122 = BitConverter.ToSingle(new byte[] { byteList[18], byteList[17], byteList[16], byteList[15] }, 0);
//发送成功信息
if (id == 1)
MainWindow.mainViewModel.serverModel1.SendMess(ServerReplyData(byteList[5], byteList[6], 0x01).ToArray());
else if (id == 2)
MainWindow.mainViewModel.serverModel2.SendMess(ServerReplyData(byteList[5], byteList[6], 0x01).ToArray());
}
catch(Exception ex)
{
//发送失败信息
if (id == 1)
MainWindow.mainViewModel.serverModel1.SendMess(ServerReplyData(byteList[5], byteList[6], 0x00).ToArray());
else if (id == 2)
MainWindow.mainViewModel.serverModel2.SendMess(ServerReplyData(byteList[5], byteList[6], 0x00).ToArray());
}
//数据存储
break;
case 0x07:
//读取时间
byteLength = BitConverter.ToInt16(new byte[] { byteList[3], byteList[2] }, 0);
if (byteLength != 8) //长度校验失败
{
//发送失败信息
if (id == 1)
MainWindow.mainViewModel.serverModel1.SendMess(ServerReplyData(byteList[5], byteList[6], 0x00).ToArray());
else if (id == 2)
MainWindow.mainViewModel.serverModel2.SendMess(ServerReplyData(byteList[5], byteList[6], 0x00).ToArray());
}
List<byte> SendData = new List<byte>();
SendData.AddRange(new byte[2] { 0xEF, 0xFE }); //帧头H=0xEF 0xFE)
SendData.AddRange(new byte[2] { 0x00, 0x08 }); //长度L=6)(MSH MSL(从地址码到最后)
SendData.Add(0x01); //地址码(ADDR=00)
SendData.Add(0x07); //功能码FUNC=02)
TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 8, 0, 0, 0);
int totalSeconds = (int)ts.TotalSeconds;
string hexSeconds = totalSeconds.ToString("X2");
byte[] timeByte = Tools.HexStringToByteArray(hexSeconds);
SendData.AddRange(timeByte); //UTC时间
byte checkCode = Tools.CheckSum(SendData.ToArray());
SendData.Add(checkCode); //校验
SendData.Add(0x16); //结束符(0x16)
break;
}
}
public List<byte> ServerReplyData(byte functionCode, byte sensorType, byte replyState)
{
List<byte> SendData = new List<byte>();
SendData.AddRange(new byte[2] { 0xEF, 0xFE }); //帧头H=0xEF 0xFE)
SendData.AddRange(new byte[2] { 0x00, 0x06 }); //长度L=6)(MSH MSL(从地址码到最后)
SendData.Add(0x00); //地址码(ADDR=00)
SendData.Add(functionCode); //功能码FUNC=02)
SendData.Add(sensorType); //类型KIND )
SendData.Add(replyState); //接收应答ACK:0-解析失败1-解析成功)
byte checkCode = Tools.CheckSum(SendData.ToArray());
SendData.Add(checkCode); //校验(CS = 帧头 ...+... 应答)
SendData.Add(0x16); //结束符(0x16)
return SendData;
}
}
}