using FujianEarthquake.Common; using FujianEarthquake.DataAccess; using FujianEarthquake.ViewModels; using FujianEarthquake.Views.UserControls; using LiveCharts.Defaults; using LiveCharts.Wpf; using LiveCharts; using Microsoft.Win32; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Media; using Application = System.Windows.Application; using MySql.Data.MySqlClient; using Color = System.Windows.Media.Color; using System.Reflection.Metadata; namespace FujianEarthquake.Models.ProtocolAnalyseModels { public class ShoreBaseStationModel { private byte fromDeviceAddress { get; set; } //从设备地址 private byte functionCode { get; set; } //功能码 private byte[] startingRegisterAddress { get; set; } = new byte[2]; //起始寄存器地址 private byte[] registerNumber { get; set; } = new byte[2]; //寄存器个数 private byte[] checkCodeCRC { get; set; } = new byte[2]; //校验码 private List checkByte { get; set; } = new List(); //校验码Byte private byte dataAreaBytes { get; set; } //数据区字节数 private List dataArea { get; set; } = new List(); //数据区 private float voltage { get; set; } private float current { get; set; } private float power { get; set; } private float posEleDegree { get; set; } private float revEleDegree { get; set; } private float uVoltage { get; set; } private float uCurrent { get; set; } private int relayStatus { get; set; } private float highResVoltage { get; set; } private float highResCurrent { get; set; } private float forAmpereHour { get; set; } private float revAmpereHour { get; set; } int ChartPointNum = 15; private int id = 1; public void ParsingData(List byteList) { if (byteList.Count == 0) return; //原始数据存储 string sql = $"insert into boosterstation_state_originaldata(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.CRCCalcRev(checkByte.ToArray()); //CRC校验失败 if (!Tools.CompareByte(checkCodeCRC, new byte[2] { byteList[byteList.Count - 2], byteList[byteList.Count - 1] })) { string content = "升压站电控通信系统数据CRC校验失败"; sql = $"insert into alarm_run_info(StationID,RecordTime,ParaName,ParaContent) values('{id}','{DateTime.Now}','升压站电控通信系统数据校验','{content}');"; DBHelper.ExecuteNonQuery(sql, 1); return; } if (byteList[1] == 0x03) //查询从设备寄存器内容 { //目前为读取寄存器 0000H-000FH,即所有数据 //长度校验 dataAreaBytes = byteList[2]; if (dataAreaBytes + 5 != byteList.Count) { string content = "升压站电控通信系统状态数据长度校验失败"; sql = $"insert into alarm_run_info(StationID,RecordTime,ParaName,ParaContent) values('{id}','{DateTime.Now}','升压站电控通信系统数据校验','{content}');"; DBHelper.ExecuteNonQuery(sql, 1); return; } DateTime recordTime = DateTime.Now; try { //获取电压电流的量程; int VoltageRange = int.Parse(Tools.GetAppSetting("SYZVoltageRange")); int CurrentRange = int.Parse(Tools.GetAppSetting("SYZCurrentRange")); voltage = BitConverter.ToInt16(new byte[2] { byteList[4], byteList[3] }) / 10000f * VoltageRange; current = BitConverter.ToInt16(new byte[2] { byteList[6], byteList[5] }) / 10000f * CurrentRange; //power = voltage * current; power = BitConverter.ToInt16(new byte[2] { byteList[8], byteList[7] }) / 10000f * VoltageRange * CurrentRange; posEleDegree = BitConverter.ToUInt32(new byte[4] { byteList[12], byteList[11], byteList[10], byteList[9] }) * VoltageRange * CurrentRange / (1000 * 3600); revEleDegree = BitConverter.ToUInt32(new byte[4] { byteList[16], byteList[15], byteList[14], byteList[13] }) * VoltageRange * CurrentRange / (1000 * 3600); uVoltage = BitConverter.ToUInt16(new byte[2] { byteList[18], byteList[17] }) / 10000f * VoltageRange; uCurrent = BitConverter.ToUInt16(new byte[2] { byteList[20], byteList[19] }) / 10000f * VoltageRange; relayStatus = byteList[21]; //highResVoltage = BitConverter.ToInt16(new byte[2] { byteList[23], byteList[22] }) / 50000f * VoltageRange; //highResCurrent = BitConverter.ToInt16(new byte[2] { byteList[25], byteList[24] }) / 50000f * CurrentRange; //forAmpereHour = BitConverter.ToUInt32(new byte[4] { byteList[29], byteList[28], byteList[27], byteList[26] }) * CurrentRange / 3600; //revAmpereHour = BitConverter.ToUInt32(new byte[4] { byteList[33], byteList[32], byteList[31], byteList[30] }) * CurrentRange / 3600; recordTime = DateTime.Now; sql = $"insert into boosterstation_state(StationID,RecordTime,In_Vol,In_Cur,Power,PosEleDegree," + $"RevEleDegree,uVoltage,uCurrent,RelayStatus,HighResVoltage,HighResCurrent,ForAmpereHour," + $"RevAmpereHour) values('{id}','{recordTime}','{voltage}','{current}','{power}','{posEleDegree}'," + $"'{revEleDegree}','{uVoltage}','{uCurrent}','{relayStatus}','{highResVoltage}'," + $"'{highResCurrent}','{forAmpereHour}','{revAmpereHour}');"; DBHelper.ExecuteNonQuery(sql, 1); //异常数据存储 if (voltage < DataLimited.VolCurCollectorVoltageMin || voltage > DataLimited.VolCurCollectorVoltageMax || current < DataLimited.VolCurCollectorCurrentMin || current > DataLimited.VolCurCollectorCurrentMax) { sql = $"insert into boosterstation_state_abnormaldata(StationID,RecordTime,In_Vol,In_Cur,Power,PosEleDegree," + $"RevEleDegree,uVoltage,uCurrent,RelayStatus,HighResVoltage,HighResCurrent,ForAmpereHour," + $"RevAmpereHour) values('{id}','{recordTime}','{voltage}','{current}','{power}','{posEleDegree}'," + $"'{revEleDegree}','{uVoltage}','{uCurrent}','{relayStatus}','{highResVoltage}'," + $"'{highResCurrent}','{forAmpereHour}','{revAmpereHour}');"; DBHelper.ExecuteNonQuery(sql, 1); } sql = $"insert into log_record(StationID,RecordTime,Device_Name,Operation_Type,Record) values('{id}','{DateTime.Now}','升压站电控通信系统','数据请求','请求升压站状态数据成功');"; DBHelper.ExecuteNonQuery(sql, 1); } catch (Exception ex) { string content = "升压站电控通信系统状态数据解析失败," + ex.Message; sql = $"insert into alarm_run_info(StationID,RecordTime,ParaName,ParaContent) values('{id}','{DateTime.Now}','升压站电控通信系统数据解析','{content}');"; DBHelper.ExecuteNonQuery(sql, 1); } Application.Current.Dispatcher.Invoke(() => { //MainViewModel.boosterStationStateDataViewModel1.AskDataBtnIsReceived = true; //MainViewModel.boosterStationStateDataViewModel1.AskDataBtnIsEnabled = true; //MainViewModel.boosterStationStateDataViewModel1.timerAskData.Stop(); //MainViewModel.boosterStationStateDataViewModel1.AskDataMsgVisibility = Visibility.Visible; //MainViewModel.boosterStationStateDataViewModel1.AskDataMsg = "请求升压站状态数据成功!"; //MainViewModel.boosterStationStateDataViewModel1.timerAskDataMsgCollapse.Start(); //MainViewModel.boosterStationStateDataViewModel1.AskDataMsgForeground = new SolidColorBrush(Colors.Green); //if (relayStatus == 1) // MainViewModel.baseStationStateModel1.JunBox_Power = (ImageSource)Application.Current.FindResource("DeviceOn"); //else // MainViewModel.baseStationStateModel1.JunBox_Power = (ImageSource)Application.Current.FindResource("DeviceOff"); //MainViewModel.realTimeDataViewModel1.BaseStationStatus[2].Value = voltage.ToString(); //MainViewModel.realTimeDataViewModel1.BaseStationStatus[5].Value = current.ToString(); //MainViewModel.realTimeDataViewModel1.DataTimeBoosterStationVol = recordTime; //MainViewModel.realTimeDataViewModel1.DataTimeBoosterStationCur = recordTime; //var chartValuesBoosterStationVol = new ChartValues(); //var chartValuesBoosterStationCur = new ChartValues(); //sql = string.Format("select * from boosterstation_state where StationID = {0} ORDER by id desc limit {1}", 1, ChartPointNum); //MySqlDataReader dataReader = DBHelper.ExecuteReader(sql, 1); //while (dataReader.Read()) //{ // if (!Convert.IsDBNull(dataReader["In_Vol"]) && !Convert.IsDBNull(dataReader["In_Cur"])) // { // chartValuesBoosterStationVol.Add(new DateTimePoint // { // Value = Convert.ToSingle(dataReader["In_Vol"]), // DateTime = Convert.ToDateTime(dataReader["RecordTime"]), // }); // chartValuesBoosterStationCur.Add(new DateTimePoint // { // Value = Convert.ToSingle(dataReader["In_Cur"]), // DateTime = Convert.ToDateTime(dataReader["RecordTime"]), // }); // } //} //dataReader.Dispose(); //MainViewModel.realTimeDataViewModel1.SeriesCollectionBoosterStationVol = new SeriesCollection // { // new LineSeries // { // Title = "输入电压", // Values = chartValuesBoosterStationVol, // LineSmoothness = 0, // ScalesYAt = 0, // Stroke = new SolidColorBrush(Color.FromArgb(255, 43, 237, 241)), // Fill = new SolidColorBrush(Colors.Transparent) // }, // }; //MainViewModel.realTimeDataViewModel1.SeriesCollectionBoosterStationCur = new SeriesCollection // { // new LineSeries // { // Title = "输入电流", // Values = chartValuesBoosterStationCur, // LineSmoothness = 0, // ScalesYAt = 0, // Stroke = new SolidColorBrush(Color.FromArgb(255, 43, 237, 241)), // Fill = new SolidColorBrush(Colors.Transparent) // }, // }; }); } else if (byteList[1] == 0x06) //对从设备单个寄存器置数 { //长度校验 if (byteList.Count != 8) { string content = "升压站电控通信系统对从设备单个寄存器置数回复长度校验失败"; sql = $"insert into alarm_run_info(StationID,RecordTime,ParaName,ParaContent) values('{id}','{DateTime.Now}','升压站电控通信系统数据校验','{content}');"; DBHelper.ExecuteNonQuery(sql, 1); return; } } else if (byteList[1] == 0x10) //对从设备多个寄存器置数 { //长度校验 if (byteList.Count != 8) return; } } } }