2024-09-03 08:30:34 +00:00
using Google.Protobuf.WellKnownTypes ;
using JiangsuEarthquake.Common ;
using JiangsuEarthquake.DataAccess ;
using JiangsuEarthquake.ViewModels ;
using LiveCharts.Defaults ;
using LiveCharts.Wpf ;
using LiveCharts ;
using System ;
using System.Collections.Generic ;
using System.ComponentModel.DataAnnotations ;
using System.Linq ;
using System.Runtime.CompilerServices ;
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 ;
using System.Xml.Linq ;
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 ; }
int ChartPointNum = 15 ;
public void ParsingData ( List < byte > byteList , int id )
{
if ( byteList . Count = = 0 )
return ;
//原始数据存储
string sql = $"insert into juncbox_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 . CheckSum ( checkByte . ToArray ( ) ) ;
//CRC校验失败
if ( checkCodeCRC ! = byteList [ byteList . Count - 2 ] )
{
string content = "接驳盒数据CRC校验失败" ;
sql = $"insert into alarm_run_info(StationID,RecordTime,ParaName,ParaContent) values('{id}','{DateTime.Now}','接驳盒数据校验','{content}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
try
{
//发送失败信息
if ( id = = 1 )
{
if ( MainWindow . mainViewModel . serverModel1 ! = null & & MainWindow . mainViewModel . serverModel1 . IsOpened )
MainWindow . mainViewModel . serverModel1 . SendMessage ( ServerReplyData ( byteList [ 5 ] , byteList [ 6 ] , 0x00 ) . ToArray ( ) ) ;
}
else if ( id = = 2 )
{
if ( MainWindow . mainViewModel . serverModel2 ! = null & & MainWindow . mainViewModel . serverModel2 . IsOpened )
MainWindow . mainViewModel . serverModel2 . SendMessage ( ServerReplyData ( byteList [ 5 ] , byteList [ 6 ] , 0x00 ) . ToArray ( ) ) ;
}
}
catch ( Exception ex )
{
content = "接驳盒数据CRC校验失败信息发送失败, " + ex . Message ;
sql = $"insert into alarm_run_info(StationID,RecordTime,ParaName,ParaContent) values('{id}','{DateTime.Now}','接驳盒数据发送','{content}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
}
return ;
}
functionCode = byteList [ 5 ] ; //功能码
switch ( functionCode )
{
case 0x02 :
SensorType = byteList [ 6 ] ;
switch ( SensorType )
{
case 0x1E :
//接驳盒端其他传感器集合
byteLength = BitConverter . ToInt16 ( new byte [ ] { byteList [ 3 ] , byteList [ 2 ] } , 0 ) ;
if ( byteLength ! = 106 ) //长度校验失败
{
string content = "接驳盒其他传感器集合数据长度校验失败" ;
sql = $"insert into alarm_run_info(StationID,RecordTime,ParaName,ParaContent) values('{id}','{DateTime.Now}','接驳盒数据校验','{content}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
try
{
//发送失败信息
if ( id = = 1 )
{
if ( MainWindow . mainViewModel . serverModel1 ! = null & & MainWindow . mainViewModel . serverModel1 . IsOpened )
MainWindow . mainViewModel . serverModel1 . SendMessage ( ServerReplyData ( byteList [ 5 ] , byteList [ 6 ] , 0x00 ) . ToArray ( ) ) ;
}
else if ( id = = 2 )
{
if ( MainWindow . mainViewModel . serverModel2 ! = null & & MainWindow . mainViewModel . serverModel2 . IsOpened )
MainWindow . mainViewModel . serverModel2 . SendMessage ( ServerReplyData ( byteList [ 5 ] , byteList [ 6 ] , 0x00 ) . ToArray ( ) ) ;
}
}
catch ( Exception ex )
{
content = "接驳盒其他传感器集合数据长度校验失败信息发送失败," + ex . Message ;
sql = $"insert into alarm_run_info(StationID,RecordTime,ParaName,ParaContent) values('{id}','{DateTime.Now}','接驳盒数据发送','{content}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
}
}
else
{
DateTime DataTime ;
float temperature ;
float humidity ;
float gestureX ;
float gestureY ;
float gestureZ ;
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 ) ;
try
{
//发送成功信息
if ( id = = 1 )
{
if ( MainWindow . mainViewModel . serverModel1 ! = null & & MainWindow . mainViewModel . serverModel1 . IsOpened )
MainWindow . mainViewModel . serverModel1 . SendMessage ( ServerReplyData ( byteList [ 5 ] , byteList [ 6 ] , 0x01 ) . ToArray ( ) ) ;
}
else if ( id = = 2 )
{
if ( MainWindow . mainViewModel . serverModel2 ! = null & & MainWindow . mainViewModel . serverModel2 . IsOpened )
MainWindow . mainViewModel . serverModel2 . SendMessage ( ServerReplyData ( byteList [ 5 ] , byteList [ 6 ] , 0x01 ) . ToArray ( ) ) ;
}
}
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 ) ;
}
}
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 ) ;
//try
//{
// //发送失败信息
// if (id == 1)
// {
// if (MainWindow.mainViewModel.serverModel1 != null && MainWindow.mainViewModel.serverModel1.IsOpened)
// MainWindow.mainViewModel.serverModel1.SendMessage(ServerReplyData(byteList[5], byteList[6], 0x00).ToArray());
// }
// else if (id == 2)
// {
// if (MainWindow.mainViewModel.serverModel2 != null && MainWindow.mainViewModel.serverModel2.IsOpened)
// MainWindow.mainViewModel.serverModel2.SendMessage(ServerReplyData(byteList[5], byteList[6], 0x00).ToArray());
// }
//}
//catch
//{
//}
break ;
}
//数据存储
sql = $"insert into juncbox_env(StationID,RecordTime,DataTime,Temperature,Humidity,AttitudeX,AttitudeY,AttitudeZ) values('{id}','{DateTime.Now}','{DataTime}','{temperature}','{humidity}','{gestureX}','{gestureY}','{gestureZ}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
//异常数据存储
if ( temperature < TotalMessage . LowerComputerTemperatureMin | | temperature > TotalMessage . LowerComputerTemperatureMax | | humidity < TotalMessage . LowerComputerHumidityMin | | humidity > TotalMessage . LowerComputerHumidityMax | | gestureX < TotalMessage . LowerComputerGestureXMin | | gestureX > TotalMessage . LowerComputerGestureXMax | | gestureY < TotalMessage . LowerComputerGestureYMin | | gestureY > TotalMessage . LowerComputerGestureYMax | |
gestureZ < TotalMessage . LowerComputerGestureZMin | | gestureZ > TotalMessage . LowerComputerGestureZMax )
{
sql = $"insert into juncbox_env_abnormaldata(StationID,RecordTime,DataTime,Temperature,Humidity,AttitudeX,AttitudeY,AttitudeZ) values('{id}','{DateTime.Now}','{DataTime}','{temperature}','{humidity}','{gestureX}','{gestureY}','{gestureZ}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
}
#region 更 新 到 实 时 数 据 显 示 页 面
//更新到实时数据显示页面
App . Current . Dispatcher . Invoke ( ( ) = >
{
var chartValuesConboxTem = new ChartValues < DateTimePoint > ( ) ;
var chartValuesConboxHum = new ChartValues < DateTimePoint > ( ) ;
if ( id = = 1 )
{
2024-09-12 01:48:49 +00:00
MainViewModel . realTimeDataViewModel1 . BaseStationStatus [ 1 ] . Value = temperature . ToString ( "F2" ) ;
MainViewModel . realTimeDataViewModel1 . BaseStationStatus [ 4 ] . Value = humidity . ToString ( "F2" ) ;
2024-09-03 08:30:34 +00:00
MainViewModel . realTimeDataViewModel1 . DataTimeConboxTem = DataTime ;
MainViewModel . realTimeDataViewModel1 . DataTimeConboxHum = DataTime ;
MainViewModel . realTimeDataViewModel1 . BaseStationGesture . RollAngle = gestureX ;
MainViewModel . realTimeDataViewModel1 . BaseStationGesture . PitchAngle = gestureY ;
MainViewModel . realTimeDataViewModel1 . BaseStationGesture . HeadingAngle = gestureZ ;
MainViewModel . baseStationModelViewModel1 . BaseStationGesture . RollAngle = MainViewModel . realTimeDataViewModel1 . BaseStationGesture . RollAngle ;
MainViewModel . baseStationModelViewModel1 . BaseStationGesture . PitchAngle = MainViewModel . realTimeDataViewModel1 . BaseStationGesture . PitchAngle ;
MainViewModel . baseStationModelViewModel1 . BaseStationGesture . HeadingAngle = MainViewModel . realTimeDataViewModel1 . BaseStationGesture . HeadingAngle ;
float CalibrationHG = Convert . ToSingle ( Tools . GetAppSetting ( "CalibrationHG" ) ) ;
float CalibrationFY = Convert . ToSingle ( Tools . GetAppSetting ( "CalibrationFY" ) ) ;
float CalibrationPH = Convert . ToSingle ( Tools . GetAppSetting ( "CalibrationPH" ) ) ;
MainViewModel . realTimeDataViewModel1 . BaseStationGesture . PY_HG = MainViewModel . realTimeDataViewModel1 . BaseStationGesture . RollAngle - CalibrationHG ;
MainViewModel . realTimeDataViewModel1 . BaseStationGesture . PY_FY = MainViewModel . realTimeDataViewModel1 . BaseStationGesture . PitchAngle - CalibrationFY ;
MainViewModel . realTimeDataViewModel1 . BaseStationGesture . PY_PH = MainViewModel . realTimeDataViewModel1 . BaseStationGesture . HeadingAngle - CalibrationPH ;
if ( Math . Abs ( MainViewModel . realTimeDataViewModel1 . BaseStationGesture . PY_HG ) < = 0.75 )
MainViewModel . realTimeDataViewModel1 . AngleBackgroundX = new SolidColorBrush ( Colors . Green ) ;
else if ( Math . Abs ( MainViewModel . realTimeDataViewModel1 . BaseStationGesture . PY_HG ) > 0.75 & & Math . Abs ( MainViewModel . realTimeDataViewModel1 . BaseStationGesture . PY_HG ) < = 1 )
MainViewModel . realTimeDataViewModel1 . AngleBackgroundX = new SolidColorBrush ( Colors . Yellow ) ;
else
MainViewModel . realTimeDataViewModel1 . AngleBackgroundX = new SolidColorBrush ( Colors . Red ) ;
if ( Math . Abs ( MainViewModel . realTimeDataViewModel1 . BaseStationGesture . PY_FY ) < = 0.75 )
MainViewModel . realTimeDataViewModel1 . AngleBackgroundY = new SolidColorBrush ( Colors . Green ) ;
else if ( Math . Abs ( MainViewModel . realTimeDataViewModel1 . BaseStationGesture . PY_FY ) > 0.75 & & Math . Abs ( MainViewModel . realTimeDataViewModel1 . BaseStationGesture . PY_FY ) < = 1 )
MainViewModel . realTimeDataViewModel1 . AngleBackgroundY = new SolidColorBrush ( Colors . Yellow ) ;
else
MainViewModel . realTimeDataViewModel1 . AngleBackgroundY = new SolidColorBrush ( Colors . Red ) ;
if ( Math . Abs ( MainViewModel . realTimeDataViewModel1 . BaseStationGesture . PY_PH ) < = 0.75 )
MainViewModel . realTimeDataViewModel1 . AngleBackgroundZ = new SolidColorBrush ( Colors . Green ) ;
else if ( Math . Abs ( MainViewModel . realTimeDataViewModel1 . BaseStationGesture . PY_PH ) > 0.75 & & Math . Abs ( MainViewModel . realTimeDataViewModel1 . BaseStationGesture . PY_PH ) < = 1 )
MainViewModel . realTimeDataViewModel1 . AngleBackgroundZ = new SolidColorBrush ( Colors . Yellow ) ;
else
MainViewModel . realTimeDataViewModel1 . AngleBackgroundZ = new SolidColorBrush ( Colors . Red ) ;
sql = String . Format ( "select * from juncbox_env where StationID = {0} ORDER by id desc limit {1}" , 1 , ChartPointNum ) ;
MySqlDataReader dataReader = DBHelper . ExecuteReader ( sql , 1 ) ;
while ( dataReader . Read ( ) )
{
if ( ! Convert . IsDBNull ( dataReader [ "Temperature" ] ) & & ! Convert . IsDBNull ( dataReader [ "Humidity" ] ) )
{
chartValuesConboxTem . Add ( new DateTimePoint
{
Value = Convert . ToSingle ( dataReader [ "Temperature" ] ) ,
DateTime = Convert . ToDateTime ( dataReader [ "DataTime" ] ) ,
} ) ;
chartValuesConboxHum . Add ( new DateTimePoint
{
Value = Convert . ToSingle ( dataReader [ "Humidity" ] ) ,
DateTime = Convert . ToDateTime ( dataReader [ "DataTime" ] ) ,
} ) ;
}
}
dataReader . Dispose ( ) ;
MainViewModel . realTimeDataViewModel1 . SeriesCollectionConboxTem = new SeriesCollection
{
new LineSeries
{
Title = "温度" ,
Values = chartValuesConboxTem ,
LineSmoothness = 0 ,
ScalesYAt = 0 ,
Stroke = new SolidColorBrush ( Color . FromArgb ( 255 , 43 , 237 , 241 ) ) ,
Fill = new SolidColorBrush ( Colors . Transparent )
} ,
} ;
MainViewModel . realTimeDataViewModel1 . SeriesCollectionConboxHum = new SeriesCollection
{
new LineSeries
{
Title = "湿度" ,
Values = chartValuesConboxHum ,
LineSmoothness = 0 ,
ScalesYAt = 0 ,
Stroke = new SolidColorBrush ( Color . FromArgb ( 255 , 43 , 237 , 241 ) ) ,
Fill = new SolidColorBrush ( Colors . Transparent )
} ,
} ;
}
else
{
2024-09-12 01:48:49 +00:00
MainViewModel . realTimeDataViewModel2 . BaseStationStatus [ 1 ] . Value = temperature . ToString ( "F2" ) ;
MainViewModel . realTimeDataViewModel2 . BaseStationStatus [ 4 ] . Value = humidity . ToString ( "F2" ) ;
2024-09-03 08:30:34 +00:00
MainViewModel . realTimeDataViewModel2 . DataTimeConboxTem = DataTime ;
MainViewModel . realTimeDataViewModel2 . DataTimeConboxHum = DataTime ;
MainViewModel . realTimeDataViewModel2 . BaseStationGesture . RollAngle = gestureX ;
MainViewModel . realTimeDataViewModel2 . BaseStationGesture . PitchAngle = gestureY ;
MainViewModel . realTimeDataViewModel2 . BaseStationGesture . HeadingAngle = gestureZ ;
MainViewModel . baseStationModelViewModel2 . BaseStationGesture . RollAngle = MainViewModel . realTimeDataViewModel2 . BaseStationGesture . RollAngle ;
MainViewModel . baseStationModelViewModel2 . BaseStationGesture . PitchAngle = MainViewModel . realTimeDataViewModel2 . BaseStationGesture . PitchAngle ;
MainViewModel . baseStationModelViewModel2 . BaseStationGesture . HeadingAngle = MainViewModel . realTimeDataViewModel2 . BaseStationGesture . HeadingAngle ;
float CalibrationHG = Convert . ToSingle ( Tools . GetAppSetting ( "CalibrationHG" ) ) ;
float CalibrationFY = Convert . ToSingle ( Tools . GetAppSetting ( "CalibrationFY" ) ) ;
float CalibrationPH = Convert . ToSingle ( Tools . GetAppSetting ( "CalibrationPH" ) ) ;
MainViewModel . realTimeDataViewModel2 . BaseStationGesture . PY_HG = MainViewModel . realTimeDataViewModel2 . BaseStationGesture . RollAngle - CalibrationHG ;
MainViewModel . realTimeDataViewModel2 . BaseStationGesture . PY_FY = MainViewModel . realTimeDataViewModel2 . BaseStationGesture . PitchAngle - CalibrationFY ;
MainViewModel . realTimeDataViewModel2 . BaseStationGesture . PY_PH = MainViewModel . realTimeDataViewModel2 . BaseStationGesture . HeadingAngle - CalibrationPH ;
if ( Math . Abs ( MainViewModel . realTimeDataViewModel2 . BaseStationGesture . PY_HG ) < = 0.75 )
MainViewModel . realTimeDataViewModel2 . AngleBackgroundX = new SolidColorBrush ( Colors . Green ) ;
else if ( Math . Abs ( MainViewModel . realTimeDataViewModel2 . BaseStationGesture . PY_HG ) > 0.75 & & Math . Abs ( MainViewModel . realTimeDataViewModel2 . BaseStationGesture . PY_HG ) < = 1 )
MainViewModel . realTimeDataViewModel2 . AngleBackgroundX = new SolidColorBrush ( Colors . Yellow ) ;
else
MainViewModel . realTimeDataViewModel2 . AngleBackgroundX = new SolidColorBrush ( Colors . Red ) ;
if ( Math . Abs ( MainViewModel . realTimeDataViewModel2 . BaseStationGesture . PY_FY ) < = 0.75 )
MainViewModel . realTimeDataViewModel2 . AngleBackgroundY = new SolidColorBrush ( Colors . Green ) ;
else if ( Math . Abs ( MainViewModel . realTimeDataViewModel2 . BaseStationGesture . PY_FY ) > 0.75 & & Math . Abs ( MainViewModel . realTimeDataViewModel2 . BaseStationGesture . PY_FY ) < = 1 )
MainViewModel . realTimeDataViewModel2 . AngleBackgroundY = new SolidColorBrush ( Colors . Yellow ) ;
else
MainViewModel . realTimeDataViewModel2 . AngleBackgroundY = new SolidColorBrush ( Colors . Red ) ;
if ( Math . Abs ( MainViewModel . realTimeDataViewModel2 . BaseStationGesture . PY_PH ) < = 0.75 )
MainViewModel . realTimeDataViewModel2 . AngleBackgroundZ = new SolidColorBrush ( Colors . Green ) ;
else if ( Math . Abs ( MainViewModel . realTimeDataViewModel2 . BaseStationGesture . PY_PH ) > 0.75 & & Math . Abs ( MainViewModel . realTimeDataViewModel2 . BaseStationGesture . PY_PH ) < = 1 )
MainViewModel . realTimeDataViewModel2 . AngleBackgroundZ = new SolidColorBrush ( Colors . Yellow ) ;
else
MainViewModel . realTimeDataViewModel2 . AngleBackgroundZ = new SolidColorBrush ( Colors . Red ) ;
sql = String . Format ( "select * from juncbox_env where StationID = {0} ORDER by id desc limit {1}" , 2 , ChartPointNum ) ;
MySqlDataReader dataReader = DBHelper . ExecuteReader ( sql , 1 ) ;
while ( dataReader . Read ( ) )
{
if ( ! Convert . IsDBNull ( dataReader [ "Temperature" ] ) & & ! Convert . IsDBNull ( dataReader [ "Humidity" ] ) )
{
chartValuesConboxTem . Add ( new DateTimePoint
{
Value = Convert . ToSingle ( dataReader [ "Temperature" ] ) ,
DateTime = Convert . ToDateTime ( dataReader [ "DataTime" ] ) ,
} ) ;
chartValuesConboxHum . Add ( new DateTimePoint
{
Value = Convert . ToSingle ( dataReader [ "Humidity" ] ) ,
DateTime = Convert . ToDateTime ( dataReader [ "DataTime" ] ) ,
} ) ;
}
}
dataReader . Dispose ( ) ;
MainViewModel . realTimeDataViewModel2 . SeriesCollectionConboxTem = new SeriesCollection
{
new LineSeries
{
Title = "温度" ,
Values = chartValuesConboxTem ,
LineSmoothness = 0 ,
ScalesYAt = 0 ,
Stroke = new SolidColorBrush ( Color . FromArgb ( 255 , 43 , 237 , 241 ) ) ,
Fill = new SolidColorBrush ( Colors . Transparent )
} ,
} ;
MainViewModel . realTimeDataViewModel2 . SeriesCollectionConboxHum = new SeriesCollection
{
new LineSeries
{
Title = "湿度" ,
Values = chartValuesConboxHum ,
LineSmoothness = 0 ,
ScalesYAt = 0 ,
Stroke = new SolidColorBrush ( Color . FromArgb ( 255 , 43 , 237 , 241 ) ) ,
Fill = new SolidColorBrush ( Colors . Transparent )
} ,
} ;
}
} ) ;
#endregion
}
}
break ;
case 0x1F :
//接驳盒中ADCP数据
break ;
}
break ;
case 0x03 :
byte operFunc = byteList [ 6 ] ; //操作功能
switch ( operFunc )
{
case 0x01 :
//远程控制
//读取长度
byteLength = BitConverter . ToInt16 ( new byte [ ] { byteList [ 3 ] , byteList [ 2 ] } , 0 ) ;
if ( byteLength ! = 9 ) //长度校验失败
{
string content = "接驳盒远程控制回复长度校验失败" ;
sql = $"insert into alarm_run_info(StationID,RecordTime,ParaName,ParaContent) values('{id}','{DateTime.Now}','接驳盒数据校验','{content}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
//try
//{
// //发送失败信息
// if (id == 1)
// {
// if (MainWindow.mainViewModel.serverModel1 != null && MainWindow.mainViewModel.serverModel1.IsOpened)
// MainWindow.mainViewModel.serverModel1.SendMessage(ServerReplyData(byteList[5], byteList[6], 0x00).ToArray());
// }
// else if (id == 2)
// {
// if (MainWindow.mainViewModel.serverModel2 != null && MainWindow.mainViewModel.serverModel2.IsOpened)
// MainWindow.mainViewModel.serverModel2.SendMessage(ServerReplyData(byteList[5], byteList[6], 0x00).ToArray());
// }
//}
//catch
//{
//}
break ;
}
byte sensorId = byteList [ 8 ] ; //传感器ID
byte ActionKind = byteList [ 9 ] ; //动作类型( ACT,0-关闭; 1-开启; 2-失败)
byte ActionType = byteList [ 10 ] ; //动作种类( 0x00-电源, 0x01-数据回传, 2-失败)
switch ( sensorId )
{
case 0x08 : //8-接驳盒-地震仪1
if ( ActionType = = 0x00 )
{
string state ;
if ( ActionKind = = 0x00 ) //关闭
{
App . Current . Dispatcher . Invoke ( ( ) = >
{
if ( id = = 1 )
MainViewModel . baseStationStateModel1 . JunBox_Seis1 = ( ImageSource ) Application . Current . FindResource ( "DeviceOff" ) ;
else
MainViewModel . baseStationStateModel2 . JunBox_Seis1 = ( ImageSource ) Application . Current . FindResource ( "DeviceOff" ) ;
MainWindow . mainViewModel . MainSeisPowerOffIsReceived = true ;
MainWindow . mainViewModel . MainSeisPowerOpenBtnIsEnabled = true ;
MainWindow . mainViewModel . MainSeisPowerCloseBtnIsEnabled = true ;
MainWindow . mainViewModel . timerMainSeisPowerOff . Stop ( ) ;
MainWindow . mainViewModel . MainSeisMsgVisibility = Visibility . Visible ;
MainWindow . mainViewModel . MainSeisMsg = "主地震仪电源关闭成功!" ;
MainWindow . mainViewModel . timerMainSeisMsgHidden . Start ( ) ;
MainWindow . mainViewModel . MainSeisMsgForeground = new SolidColorBrush ( Colors . Green ) ;
} ) ;
state = "Off" ;
}
else if ( ActionKind = = 0x01 ) //开启
{
App . Current . Dispatcher . Invoke ( ( ) = >
{
if ( id = = 1 )
MainViewModel . baseStationStateModel1 . JunBox_Seis1 = ( ImageSource ) Application . Current . FindResource ( "DeviceOn" ) ;
else
MainViewModel . baseStationStateModel2 . JunBox_Seis1 = ( ImageSource ) Application . Current . FindResource ( "DeviceOn" ) ;
MainWindow . mainViewModel . MainSeisPowerOnIsReceived = true ;
MainWindow . mainViewModel . MainSeisPowerOpenBtnIsEnabled = true ;
MainWindow . mainViewModel . MainSeisPowerCloseBtnIsEnabled = true ;
MainWindow . mainViewModel . timerMainSeisPowerOff . Stop ( ) ;
MainWindow . mainViewModel . MainSeisMsgVisibility = Visibility . Visible ;
MainWindow . mainViewModel . MainSeisMsg = "主地震仪电源开启成功!" ;
MainWindow . mainViewModel . timerMainSeisMsgHidden . Start ( ) ;
MainWindow . mainViewModel . MainSeisMsgForeground = new SolidColorBrush ( Colors . Green ) ;
} ) ;
state = "On" ;
}
else
break ;
//存储历史设置
sql = $"insert into switch_info_his(StationID,RecordTime,SwitchName,SwitchState) values('{id}','{DateTime.Now}','MainSeisPower','{state}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
//存储最新设置
sql = $"update switch_info set SwitchState='{state}',RecordTime='{DateTime.Now}' where SwitchName='MainSeisPower' and StationID={id};" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
}
break ;
case 0x09 : //9-接驳盒-地震仪2
if ( ActionType = = 0x00 )
{
string state ;
if ( ActionKind = = 0x00 ) //关闭
{
App . Current . Dispatcher . Invoke ( ( ) = >
{
if ( id = = 1 )
MainViewModel . baseStationStateModel1 . JunBox_Seis2 = ( ImageSource ) Application . Current . FindResource ( "DeviceOff" ) ;
else
MainViewModel . baseStationStateModel2 . JunBox_Seis2 = ( ImageSource ) Application . Current . FindResource ( "DeviceOff" ) ;
MainWindow . mainViewModel . BackupSeisPowerOffIsReceived = true ;
MainWindow . mainViewModel . BackupSeisPowerOpenBtnIsEnabled = true ;
MainWindow . mainViewModel . BackupSeisPowerCloseBtnIsEnabled = true ;
MainWindow . mainViewModel . timerBackupSeisPowerOff . Stop ( ) ;
MainWindow . mainViewModel . BackupSeisMsgVisibility = Visibility . Visible ;
MainWindow . mainViewModel . BackupSeisMsg = "备地震仪电源关闭成功!" ;
MainWindow . mainViewModel . timerBackupSeisMsgHidden . Start ( ) ;
MainWindow . mainViewModel . BackupSeisMsgForeground = new SolidColorBrush ( Colors . Green ) ;
} ) ;
state = "Off" ;
}
else if ( ActionKind = = 0x01 ) //开启
{
App . Current . Dispatcher . Invoke ( ( ) = >
{
if ( id = = 1 )
MainViewModel . baseStationStateModel1 . JunBox_Seis2 = ( ImageSource ) Application . Current . FindResource ( "DeviceOn" ) ;
else
MainViewModel . baseStationStateModel2 . JunBox_Seis2 = ( ImageSource ) Application . Current . FindResource ( "DeviceOn" ) ;
MainWindow . mainViewModel . BackupSeisPowerOnIsReceived = true ;
MainWindow . mainViewModel . BackupSeisPowerOpenBtnIsEnabled = true ;
MainWindow . mainViewModel . BackupSeisPowerCloseBtnIsEnabled = true ;
MainWindow . mainViewModel . timerBackupSeisPowerOn . Stop ( ) ;
MainWindow . mainViewModel . BackupSeisMsgVisibility = Visibility . Visible ;
MainWindow . mainViewModel . BackupSeisMsg = "备地震仪电源开启成功!" ;
MainWindow . mainViewModel . timerBackupSeisMsgHidden . Start ( ) ;
MainWindow . mainViewModel . BackupSeisMsgForeground = new SolidColorBrush ( Colors . Green ) ;
} ) ;
state = "On" ;
}
else
break ;
//存储历史设置
sql = $"insert into switch_info_his(StationID,RecordTime,SwitchName,SwitchState) values('{id}','{DateTime.Now}','BackupSeisPower','{state}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
//存储最新设置
sql = $"update switch_info set SwitchState='{state}',RecordTime='{DateTime.Now}' where SwitchName='BackupSeisPower' and StationID={id};" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
}
break ;
case 0x0A : //10-接驳盒-电磁(原来是海流计)
if ( ActionType = = 0x00 )
{
string state ;
if ( ActionKind = = 0x00 ) //关闭
{
App . Current . Dispatcher . Invoke ( ( ) = >
{
if ( id = = 1 )
return ;
else
MainViewModel . baseStationStateModel2 . JunBox_Elect = ( ImageSource ) Application . Current . FindResource ( "DeviceOff" ) ;
MainWindow . mainViewModel . ElectPowerOffIsReceived = true ;
MainWindow . mainViewModel . ElectPowerOpenBtnIsEnabled = true ;
MainWindow . mainViewModel . ElectPowerCloseBtnIsEnabled = true ;
MainWindow . mainViewModel . timerElectPowerOff . Stop ( ) ;
MainWindow . mainViewModel . ElectMsgVisibility = Visibility . Visible ;
MainWindow . mainViewModel . ElectMsg = "电磁电源关闭成功!" ;
MainWindow . mainViewModel . timerElectMsgHidden . Start ( ) ;
MainWindow . mainViewModel . ElectMsgForeground = new SolidColorBrush ( Colors . Green ) ;
} ) ;
state = "Off" ;
}
else if ( ActionKind = = 0x01 ) //开启
{
App . Current . Dispatcher . Invoke ( ( ) = >
{
if ( id = = 1 )
return ;
else
MainViewModel . baseStationStateModel2 . JunBox_Elect = ( ImageSource ) Application . Current . FindResource ( "DeviceOn" ) ;
MainWindow . mainViewModel . ElectPowerOnIsReceived = true ;
MainWindow . mainViewModel . ElectPowerOpenBtnIsEnabled = true ;
MainWindow . mainViewModel . ElectPowerCloseBtnIsEnabled = true ;
MainWindow . mainViewModel . timerElectPowerOn . Stop ( ) ;
MainWindow . mainViewModel . ElectMsgVisibility = Visibility . Visible ;
MainWindow . mainViewModel . ElectMsg = "电磁电源开启成功!" ;
MainWindow . mainViewModel . timerElectMsgHidden . Start ( ) ;
MainWindow . mainViewModel . ElectMsgForeground = new SolidColorBrush ( Colors . Green ) ;
} ) ;
state = "On" ;
}
else
break ;
//存储历史设置
sql = $"insert into switch_info_his(StationID,RecordTime,SwitchName,SwitchState) values('{id}','{DateTime.Now}','ElectPower','{state}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
//存储最新设置
sql = $"update switch_info set SwitchState='{state}',RecordTime='{DateTime.Now}' where SwitchName='ElectPower' and StationID={id};" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
}
break ;
}
break ;
case 0x00 :
//读取状态
//读取长度
byteLength = BitConverter . ToInt16 ( new byte [ ] { byteList [ 3 ] , byteList [ 2 ] } , 0 ) ;
if ( byteLength ! = 45 ) //长度校验失败
{
string content = "接驳盒读取状态回复长度校验失败" ;
sql = $"insert into alarm_run_info(StationID,RecordTime,ParaName,ParaContent) values('{id}','{DateTime.Now}','接驳盒数据校验','{content}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
try
{
//发送失败信息
if ( id = = 1 )
{
if ( MainWindow . mainViewModel . serverModel1 ! = null & & MainWindow . mainViewModel . serverModel1 . IsOpened )
MainWindow . mainViewModel . serverModel1 . SendMessage ( ServerReplyData ( byteList [ 5 ] , byteList [ 6 ] , 0x00 ) . ToArray ( ) ) ;
}
else if ( id = = 2 )
{
if ( MainWindow . mainViewModel . serverModel2 ! = null & & MainWindow . mainViewModel . serverModel2 . IsOpened )
MainWindow . mainViewModel . serverModel2 . SendMessage ( ServerReplyData ( byteList [ 5 ] , byteList [ 6 ] , 0x00 ) . ToArray ( ) ) ;
}
}
catch ( Exception ex )
{
content = "接驳盒状态数据长度校验失败信息发送失败," + ex . Message ;
sql = $"insert into alarm_run_info(StationID,RecordTime,ParaName,ParaContent) values('{id}','{DateTime.Now}','接驳盒数据发送','{content}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
}
break ;
}
//byte JunBox_ProBoard_PowCarrier_State = 0; //接驳盒-保护板-电力载波-状态反馈
//byte JunBox_ProBoard_OptSwitch_State = 0; //接驳盒-保护板-光电交换机-状态反馈
byte JunBox_ProBoard_Seis1_State = 0 ; //接驳盒-保护板-地震仪1-状态反馈
byte JunBox_ProBoard_Seis2_State = 0 ; //接驳盒-保护板-地震仪2-状态反馈
byte JunBox_ProBoard_Elect_State = 0 ; //接驳盒-保护板-电磁-状态反馈
//byte Seis_Power_State = 0; //地震仪-反馈电源状态
//byte Seis_Power_SupplyMethod = 0; //地震仪-反馈供电方式
byte JunBox_PowCarrier = 0 ; //接驳盒-电力载波
byte JunBox_OptSwitch = 0 ; //接驳盒-光电交换机
byte JunBox_Seis1 = 0 ; //接驳盒-地震仪1
byte JunBox_Seis2 = 0 ; //接驳盒-地震仪2
byte JunBox_Elect = 0 ; //接驳盒-电磁
//byte Hatch_State = 0;
byte Leakage = 0 ;
float Seis2_Voltage ; //24V输出电压4-蓄电池电压->地震仪2电压
float Seis1_Current ; //地震仪1电流
float Seis2_Current ; //地震仪2电流
float Elect_Current ; //海流计-电流
float Seis1_Voltage ; //电力载波-电流->地震仪1电压
float Out_Voltage12_Reserved1 ; //12V输出电压1-预留
float Out_Voltage12_Reserved2 ; //12V输出电压2-预留
float Reserved ;
try
{
byte [ ] state1 = new byte [ ] { byteList [ 7 ] , byteList [ 8 ] , byteList [ 9 ] , byteList [ 10 ] } ;
state1 = Tools . ConvertHexToBinary ( state1 ) ;
//if (Tools.IsBitSet(state1, 32))
// JunBox_ProBoard_PowCarrier_State = 1;
//if (Tools.IsBitSet(state1, 31))
// JunBox_ProBoard_OptSwitch_State = 1;
if ( state1 [ 29 ] = = 0x01 )
JunBox_ProBoard_Seis1_State = 1 ;
if ( state1 [ 28 ] = = 0x01 )
JunBox_ProBoard_Seis2_State = 1 ;
if ( state1 [ 27 ] = = 0x01 )
JunBox_ProBoard_Elect_State = 1 ;
//if (Tools.IsBitSet(state1, 27))
// Seis_Power_State = 1;
//if (Tools.IsBitSet(state1, 26))
// Seis_Power_SupplyMethod = 1;
if ( state1 [ 24 ] = = 0x01 )
JunBox_PowCarrier = 1 ;
if ( state1 [ 23 ] = = 0x01 )
JunBox_OptSwitch = 1 ;
if ( state1 [ 22 ] = = 0x01 )
JunBox_Seis1 = 1 ;
if ( state1 [ 21 ] = = 0x01 )
JunBox_Seis2 = 1 ;
if ( state1 [ 20 ] = = 0x01 )
JunBox_Elect = 1 ;
byte [ ] state2 = new byte [ ] { byteList [ 11 ] , byteList [ 12 ] , byteList [ 13 ] , byteList [ 14 ] } ;
state2 = Tools . ConvertHexToBinary ( state2 ) ;
if ( state2 [ 31 ] = = 0x01 )
Leakage = 1 ;
//if (Tools.IsBitSet(state2, 31))
// Hatch_State = 1;
Seis2_Voltage = BitConverter . ToSingle ( new byte [ ] { byteList [ 18 ] , byteList [ 17 ] , byteList [ 16 ] , byteList [ 15 ] } , 0 ) ;
Seis2_Voltage = ( 38.4f * ( Seis2_Voltage - 2.53f ) ) < 0 ? 0 : ( 38.4f * ( Seis2_Voltage - 2.53f ) ) ;
Seis1_Current = BitConverter . ToSingle ( new byte [ ] { byteList [ 22 ] , byteList [ 21 ] , byteList [ 20 ] , byteList [ 19 ] } , 0 ) ;
Seis1_Current = ( 5 * ( 2.5f - Seis1_Current ) ) < 0 ? 0 : ( 5 * ( 2.5f - Seis1_Current ) ) ;
Seis2_Current = BitConverter . ToSingle ( new byte [ ] { byteList [ 26 ] , byteList [ 25 ] , byteList [ 24 ] , byteList [ 23 ] } , 0 ) ;
Seis2_Current = ( 5 * ( 2.5f - Seis2_Current ) ) < 0 ? 0 : ( 5 * ( 2.5f - Seis2_Current ) ) ;
Elect_Current = BitConverter . ToSingle ( new byte [ ] { byteList [ 30 ] , byteList [ 29 ] , byteList [ 28 ] , byteList [ 27 ] } , 0 ) ;
Elect_Current = ( 5 * ( 2.5f - Elect_Current ) ) < 0 ? 0 : ( 5 * ( 2.5f - Elect_Current ) ) ;
Seis1_Voltage = BitConverter . ToSingle ( new byte [ ] { byteList [ 34 ] , byteList [ 33 ] , byteList [ 32 ] , byteList [ 31 ] } , 0 ) ;
Seis1_Voltage = ( 38.4f * ( Seis1_Voltage - 2.5f ) ) < 0 ? 0 : ( 38.4f * ( Seis1_Voltage - 2.5f ) ) ;
Out_Voltage12_Reserved1 = BitConverter . ToSingle ( new byte [ ] { byteList [ 38 ] , byteList [ 37 ] , byteList [ 36 ] , byteList [ 35 ] } , 0 ) ;
Out_Voltage12_Reserved1 = ( 38.4f * ( Out_Voltage12_Reserved1 - 2.5f ) ) < 0 ? 0 : ( 38.4f * ( Out_Voltage12_Reserved1 - 2.5f ) ) ;
Out_Voltage12_Reserved2 = BitConverter . ToSingle ( new byte [ ] { byteList [ 42 ] , byteList [ 41 ] , byteList [ 40 ] , byteList [ 39 ] } , 0 ) ;
Out_Voltage12_Reserved2 = ( 38.4f * ( Out_Voltage12_Reserved2 - 2.5f ) ) < 0 ? 0 : ( 38.4f * ( Out_Voltage12_Reserved2 - 2.5f ) ) ;
Reserved = BitConverter . ToSingle ( new byte [ ] { byteList [ 46 ] , byteList [ 45 ] , byteList [ 44 ] , byteList [ 43 ] } , 0 ) ;
try
{
//发送成功信息
if ( id = = 1 )
{
if ( MainWindow . mainViewModel . serverModel1 ! = null & & MainWindow . mainViewModel . serverModel1 . IsOpened )
MainWindow . mainViewModel . serverModel1 . SendMessage ( ServerReplyData ( byteList [ 5 ] , byteList [ 6 ] , 0x01 ) . ToArray ( ) ) ;
}
else if ( id = = 2 )
{
if ( MainWindow . mainViewModel . serverModel2 ! = null & & MainWindow . mainViewModel . serverModel2 . IsOpened )
MainWindow . mainViewModel . serverModel2 . SendMessage ( ServerReplyData ( byteList [ 5 ] , byteList [ 6 ] , 0x01 ) . ToArray ( ) ) ;
}
}
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 ) ;
}
}
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 ) ;
//try
//{
// //发送失败信息
// if (id == 1)
// {
// if (MainWindow.mainViewModel.serverModel1 != null && MainWindow.mainViewModel.serverModel1.IsOpened)
// MainWindow.mainViewModel.serverModel1.SendMessage(ServerReplyData(byteList[5], byteList[6], 0x00).ToArray());
// }
// else if (id == 2)
// {
// if (MainWindow.mainViewModel.serverModel2 != null && MainWindow.mainViewModel.serverModel2.IsOpened)
// MainWindow.mainViewModel.serverModel2.SendMessage(ServerReplyData(byteList[5], byteList[6], 0x00).ToArray());
// }
//}
//catch
//{
// content = "接驳盒状态数据解析失败信息发送失败," + ex.Message;
// sql = $"insert into alarm_run_info(StationID,RecordTime,ParaName,ParaContent) values('{id}','{DateTime.Now}','接驳盒数据发送','{content}');";
// DBHelper.ExecuteNonQuery(sql, 1);
//}
break ;
}
DateTime recordTime = DateTime . Now ;
//数据存储
sql = $"insert into juncbox_state(StationID,RecordTime,JunBox_ProBoard_PowCarrier_State," +
$"JunBox_ProBoard_OptSwitch_State,JunBox_ProBoard_Seis1_State,JunBox_ProBoard_Seis2_State," +
$"JunBox_ProBoard_Elect_State,Seis_Power_State,Seis_Power_SupplyMethod,JunBox_PowCarrier," +
$"JunBox_OptSwitch,JunBox_Seis1,JunBox_Seis2,JunBox_Elect) values('{id}','{recordTime}','0','0'," +
$"'{JunBox_ProBoard_Seis1_State}','{JunBox_ProBoard_Seis2_State}','{JunBox_ProBoard_Elect_State}'," +
$"'0','0','{JunBox_PowCarrier}','{JunBox_OptSwitch}','{JunBox_Seis1}','{JunBox_Seis2}','{JunBox_Elect}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
sql = $"insert into juncbox_cavity_state(StationID,RecordTime,Hatch_State,Leakage) values('{id}','{recordTime}','0','{Leakage}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
sql = $"insert into juncbox_monitor(StationID,RecordTime,Seis2_Voltage,Seis1_Current,Seis2_Current,Elect_Current,Seis1_Voltage,Out_Voltage12_Reserved1,Out_Voltage12_Reserved2,Reserved) values('{id}','{recordTime}','{Seis2_Voltage}','{Seis1_Current}','{Seis2_Current}','{Elect_Current}','{Seis1_Voltage}','{Out_Voltage12_Reserved1}','{Out_Voltage12_Reserved2}','{Reserved}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
//异常数据存储
if ( Seis1_Voltage < TotalMessage . SeismographVoltageMin | | Seis1_Voltage > TotalMessage . SeismographVoltageMax | | Seis2_Voltage < TotalMessage . SeismographVoltageMin | | Seis2_Voltage > TotalMessage . SeismographVoltageMax | | Seis1_Current > TotalMessage . SeismographCurrentMin | | Seis1_Current > TotalMessage . SeismographCurrentMax | | Seis2_Current > TotalMessage . SeismographCurrentMin | | Seis2_Current > TotalMessage . SeismographCurrentMax | | Elect_Current < TotalMessage . ElectromagnetismCurrentMin | | Elect_Current > TotalMessage . ElectromagnetismCurrentMax )
{
sql = $"insert into juncbox_monitor_abnormaldata(StationID,RecordTime,Seis2_Voltage,Seis1_Current,Seis2_Current,Elect_Current,Seis1_Voltage,Out_Voltage12_Reserved1,Out_Voltage12_Reserved2,Reserved) values('{id}','{recordTime}','{Seis2_Voltage}','{Seis1_Current}','{Seis2_Current}','{Elect_Current}','{Seis1_Voltage}','{Out_Voltage12_Reserved1}','{Out_Voltage12_Reserved2}','{Reserved}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
}
#region 更 新 到 实 时 数 据 显 示 页 面
//更新到页面
if ( id = = 1 )
{
App . Current . Dispatcher . Invoke ( ( ) = >
{
if ( JunBox_Seis1 = = 1 )
MainViewModel . baseStationStateModel1 . JunBox_Seis1 = ( ImageSource ) Application . Current . FindResource ( "DeviceOn" ) ;
else
MainViewModel . baseStationStateModel1 . JunBox_Seis1 = ( ImageSource ) Application . Current . FindResource ( "DeviceOff" ) ;
if ( JunBox_Seis2 = = 1 )
MainViewModel . baseStationStateModel1 . JunBox_Seis2 = ( ImageSource ) Application . Current . FindResource ( "DeviceOn" ) ;
else
MainViewModel . baseStationStateModel1 . JunBox_Seis2 = ( ImageSource ) Application . Current . FindResource ( "DeviceOff" ) ;
2024-09-12 01:48:49 +00:00
//if (JunBox_Elect == 1)
// MainViewModel.baseStationStateModel1.JunBox_Elect = (ImageSource)Application.Current.FindResource("DeviceOn");
//else
// MainViewModel.baseStationStateModel1.JunBox_Elect = (ImageSource)Application.Current.FindResource("DeviceOff");
2024-09-03 08:30:34 +00:00
//if (JunBox_ProBoard_PowCarrier_State == 0)
// MainViewModel.baseStationStateModel1.JunBox_ProBoard_PowCarrier_State = new SolidColorBrush(Colors.Green);
//else if (JunBox_ProBoard_PowCarrier_State == 1)
// MainViewModel.baseStationStateModel1.JunBox_ProBoard_PowCarrier_State = new SolidColorBrush(Colors.Red);
//else
// MainViewModel.baseStationStateModel1.JunBox_ProBoard_PowCarrier_State = new SolidColorBrush(Colors.Gray);
//if (JunBox_ProBoard_OptSwitch_State == 0)
// MainViewModel.baseStationStateModel1.JunBox_ProBoard_OptSwitch_State = new SolidColorBrush(Colors.Green);
//else if (JunBox_ProBoard_OptSwitch_State == 1)
// MainViewModel.baseStationStateModel1.JunBox_ProBoard_OptSwitch_State = new SolidColorBrush(Colors.Red);
//else
// MainViewModel.baseStationStateModel1.JunBox_ProBoard_OptSwitch_State = new SolidColorBrush(Colors.Gray);
if ( JunBox_ProBoard_Seis1_State = = 0 )
MainViewModel . baseStationStateModel1 . JunBox_ProBoard_Seis1_State = new SolidColorBrush ( Colors . Green ) ;
else if ( JunBox_ProBoard_Seis1_State = = 1 )
MainViewModel . baseStationStateModel1 . JunBox_ProBoard_Seis1_State = new SolidColorBrush ( Colors . Red ) ;
else
MainViewModel . baseStationStateModel1 . JunBox_ProBoard_Seis1_State = new SolidColorBrush ( Colors . Gray ) ;
if ( JunBox_ProBoard_Seis2_State = = 0 )
MainViewModel . baseStationStateModel1 . JunBox_ProBoard_Seis2_State = new SolidColorBrush ( Colors . Green ) ;
else if ( JunBox_ProBoard_Seis2_State = = 1 )
MainViewModel . baseStationStateModel1 . JunBox_ProBoard_Seis2_State = new SolidColorBrush ( Colors . Red ) ;
else
MainViewModel . baseStationStateModel1 . JunBox_ProBoard_Seis2_State = new SolidColorBrush ( Colors . Gray ) ;
if ( JunBox_ProBoard_Elect_State = = 0 )
MainViewModel . baseStationStateModel1 . JunBox_ProBoard_Elect_State = new SolidColorBrush ( Colors . Green ) ;
else if ( JunBox_ProBoard_Elect_State = = 1 )
MainViewModel . baseStationStateModel1 . JunBox_ProBoard_Elect_State = new SolidColorBrush ( Colors . Red ) ;
else
MainViewModel . baseStationStateModel1 . JunBox_ProBoard_Elect_State = new SolidColorBrush ( Colors . Gray ) ;
//if (Seis_Power_State == 0)
// MainViewModel.baseStationStateModel1.Seis_Power_State = "备";
//else if (Seis_Power_State == 1)
// MainViewModel.baseStationStateModel1.Seis_Power_State = "主";
//else
// MainViewModel.baseStationStateModel1.Seis_Power_State = "/";
//if (Seis_Power_SupplyMethod == 0)
// MainViewModel.baseStationStateModel1.Seis_Power_SupplyMethod = "电池";
//else if (Seis_Power_SupplyMethod == 1)
// MainViewModel.baseStationStateModel1.Seis_Power_SupplyMethod = "外部";
//else
// MainViewModel.baseStationStateModel1.Seis_Power_SupplyMethod = "/";
if ( JunBox_PowCarrier = = 1 )
MainViewModel . baseStationStateModel1 . JunBox_PowCarrier = ( ImageSource ) Application . Current . FindResource ( "DeviceOn" ) ;
else
MainViewModel . baseStationStateModel1 . JunBox_PowCarrier = ( ImageSource ) Application . Current . FindResource ( "DeviceOff" ) ;
if ( JunBox_OptSwitch = = 1 )
MainViewModel . baseStationStateModel1 . JunBox_OptSwitch = ( ImageSource ) Application . Current . FindResource ( "DeviceOn" ) ;
else
MainViewModel . baseStationStateModel1 . JunBox_OptSwitch = ( ImageSource ) Application . Current . FindResource ( "DeviceOff" ) ;
2024-09-12 01:48:49 +00:00
MainViewModel . realTimeDataViewModel1 . BaseStationStatus [ 0 ] . Value = Seis1_Voltage . ToString ( "F3" ) + " / " + Seis1_Current . ToString ( "F3" ) ;
MainViewModel . realTimeDataViewModel1 . BaseStationStatus [ 3 ] . Value = Seis2_Voltage . ToString ( "F3" ) + " / " + Seis2_Current . ToString ( "F3" ) ;
2024-09-03 08:30:34 +00:00
MainViewModel . realTimeDataViewModel1 . DataTimeMainSeis = recordTime ;
MainViewModel . realTimeDataViewModel1 . DataTimeBackupSeis = recordTime ;
if ( Leakage = = 1 )
MainViewModel . realTimeDataViewModel1 . LeakageImage = ( ImageSource ) Application . Current . FindResource ( "CycleRed" ) ;
else if ( Leakage = = 0 )
MainViewModel . realTimeDataViewModel1 . LeakageImage = ( ImageSource ) Application . Current . FindResource ( "CycleGreen" ) ;
else
MainViewModel . realTimeDataViewModel1 . LeakageImage = ( ImageSource ) Application . Current . FindResource ( "CycleGray" ) ;
//if (Hatch_State == 1)
// MainViewModel.realTimeDataViewModel1.HatchStateImage = (ImageSource)Application.Current.FindResource("CycleRed");
//else if (Hatch_State == 0)
// MainViewModel.realTimeDataViewModel1.HatchStateImage = (ImageSource)Application.Current.FindResource("CycleGreen");
//else
// MainViewModel.realTimeDataViewModel1.HatchStateImage = (ImageSource)Application.Current.FindResource("CycleGray");
} ) ;
}
else
{
App . Current . Dispatcher . Invoke ( ( ) = >
{
if ( JunBox_Seis1 = = 1 )
MainViewModel . baseStationStateModel2 . JunBox_Seis1 = ( ImageSource ) Application . Current . FindResource ( "DeviceOn" ) ;
else
MainViewModel . baseStationStateModel2 . JunBox_Seis1 = ( ImageSource ) Application . Current . FindResource ( "DeviceOff" ) ;
if ( JunBox_Seis2 = = 1 )
MainViewModel . baseStationStateModel2 . JunBox_Seis2 = ( ImageSource ) Application . Current . FindResource ( "DeviceOn" ) ;
else
MainViewModel . baseStationStateModel2 . JunBox_Seis2 = ( ImageSource ) Application . Current . FindResource ( "DeviceOff" ) ;
if ( JunBox_Elect = = 1 )
MainViewModel . baseStationStateModel2 . JunBox_Elect = ( ImageSource ) Application . Current . FindResource ( "DeviceOn" ) ;
else
MainViewModel . baseStationStateModel2 . JunBox_Elect = ( ImageSource ) Application . Current . FindResource ( "DeviceOff" ) ;
//if (JunBox_ProBoard_PowCarrier_State == 0)
// MainViewModel.baseStationStateModel2.JunBox_ProBoard_PowCarrier_State = new SolidColorBrush(Colors.Green);
//else if (JunBox_ProBoard_PowCarrier_State == 1)
// MainViewModel.baseStationStateModel2.JunBox_ProBoard_PowCarrier_State = new SolidColorBrush(Colors.Red);
//else
// MainViewModel.baseStationStateModel2.JunBox_ProBoard_PowCarrier_State = new SolidColorBrush(Colors.Gray);
//if (JunBox_ProBoard_OptSwitch_State == 0)
// MainViewModel.baseStationStateModel2.JunBox_ProBoard_OptSwitch_State = new SolidColorBrush(Colors.Green);
//else if (JunBox_ProBoard_OptSwitch_State == 1)
// MainViewModel.baseStationStateModel2.JunBox_ProBoard_OptSwitch_State = new SolidColorBrush(Colors.Red);
//else
// MainViewModel.baseStationStateModel2.JunBox_ProBoard_OptSwitch_State = new SolidColorBrush(Colors.Gray);
if ( JunBox_ProBoard_Seis1_State = = 0 )
MainViewModel . baseStationStateModel2 . JunBox_ProBoard_Seis1_State = new SolidColorBrush ( Colors . Green ) ;
else if ( JunBox_ProBoard_Seis1_State = = 1 )
MainViewModel . baseStationStateModel2 . JunBox_ProBoard_Seis1_State = new SolidColorBrush ( Colors . Red ) ;
else
MainViewModel . baseStationStateModel2 . JunBox_ProBoard_Seis1_State = new SolidColorBrush ( Colors . Gray ) ;
if ( JunBox_ProBoard_Seis2_State = = 0 )
MainViewModel . baseStationStateModel2 . JunBox_ProBoard_Seis2_State = new SolidColorBrush ( Colors . Green ) ;
else if ( JunBox_ProBoard_Seis2_State = = 1 )
MainViewModel . baseStationStateModel2 . JunBox_ProBoard_Seis2_State = new SolidColorBrush ( Colors . Red ) ;
else
MainViewModel . baseStationStateModel2 . JunBox_ProBoard_Seis2_State = new SolidColorBrush ( Colors . Gray ) ;
if ( JunBox_ProBoard_Elect_State = = 0 )
MainViewModel . baseStationStateModel2 . JunBox_ProBoard_Elect_State = new SolidColorBrush ( Colors . Green ) ;
else if ( JunBox_ProBoard_Elect_State = = 1 )
MainViewModel . baseStationStateModel2 . JunBox_ProBoard_Elect_State = new SolidColorBrush ( Colors . Red ) ;
else
MainViewModel . baseStationStateModel2 . JunBox_ProBoard_Elect_State = new SolidColorBrush ( Colors . Gray ) ;
//if (Seis_Power_State == 0)
// MainViewModel.baseStationStateModel2.Seis_Power_State = "备";
//else if (Seis_Power_State == 1)
// MainViewModel.baseStationStateModel2.Seis_Power_State = "主";
//else
// MainViewModel.baseStationStateModel2.Seis_Power_State = "/";
//if (Seis_Power_SupplyMethod == 0)
// MainViewModel.baseStationStateModel2.Seis_Power_SupplyMethod = "电池";
//else if (Seis_Power_SupplyMethod == 1)
// MainViewModel.baseStationStateModel2.Seis_Power_SupplyMethod = "外部";
//else
// MainViewModel.baseStationStateModel2.Seis_Power_SupplyMethod = "/";
if ( JunBox_PowCarrier = = 1 )
MainViewModel . baseStationStateModel2 . JunBox_PowCarrier = ( ImageSource ) Application . Current . FindResource ( "DeviceOn" ) ;
else
MainViewModel . baseStationStateModel2 . JunBox_PowCarrier = ( ImageSource ) Application . Current . FindResource ( "DeviceOff" ) ;
if ( JunBox_OptSwitch = = 1 )
MainViewModel . baseStationStateModel2 . JunBox_OptSwitch = ( ImageSource ) Application . Current . FindResource ( "DeviceOn" ) ;
else
MainViewModel . baseStationStateModel2 . JunBox_OptSwitch = ( ImageSource ) Application . Current . FindResource ( "DeviceOff" ) ;
2024-09-12 01:48:49 +00:00
MainViewModel . realTimeDataViewModel2 . BaseStationStatus [ 0 ] . Value = Seis1_Voltage . ToString ( "F3" ) + " / " + Seis1_Current . ToString ( "F3" ) ;
MainViewModel . realTimeDataViewModel2 . BaseStationStatus [ 3 ] . Value = Seis2_Voltage . ToString ( "F3" ) + " / " + Seis2_Current . ToString ( "F3" ) ;
2024-09-03 08:30:34 +00:00
MainViewModel . realTimeDataViewModel2 . DataTimeMainSeis = recordTime ;
MainViewModel . realTimeDataViewModel2 . DataTimeBackupSeis = recordTime ;
if ( Leakage = = 1 )
MainViewModel . realTimeDataViewModel2 . LeakageImage = ( ImageSource ) Application . Current . FindResource ( "CycleRed" ) ;
else if ( Leakage = = 0 )
MainViewModel . realTimeDataViewModel2 . LeakageImage = ( ImageSource ) Application . Current . FindResource ( "CycleGreen" ) ;
else
MainViewModel . realTimeDataViewModel2 . LeakageImage = ( ImageSource ) Application . Current . FindResource ( "CycleGray" ) ;
//if (Hatch_State == 1)
// MainViewModel.realTimeDataViewModel2.HatchStateImage = (ImageSource)Application.Current.FindResource("CycleRed");
//else if (Hatch_State == 0)
// MainViewModel.realTimeDataViewModel2.HatchStateImage = (ImageSource)Application.Current.FindResource("CycleGreen");
//else
// MainViewModel.realTimeDataViewModel2.HatchStateImage = (ImageSource)Application.Current.FindResource("CycleGray");
} ) ;
}
#endregion
break ;
}
break ;
case 0x06 :
//接驳盒回复上位机主动下发的校时信息
byteLength = BitConverter . ToInt16 ( new byte [ ] { byteList [ 3 ] , byteList [ 2 ] } , 0 ) ;
if ( byteLength ! = 8 ) //长度校验失败
{
string content = "接驳盒回复上位机主动下发的校时信息长度校验失败" ;
sql = $"insert into alarm_run_info(StationID,RecordTime,ParaName,ParaContent) values('{id}','{DateTime.Now}','接驳盒数据校验','{content}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
try
{
//发送失败信息
if ( id = = 1 )
{
if ( MainWindow . mainViewModel . serverModel1 ! = null & & MainWindow . mainViewModel . serverModel1 . IsOpened )
MainWindow . mainViewModel . serverModel1 . SendMessage ( ServerReplyData ( byteList [ 5 ] , byteList [ 6 ] , 0x00 ) . ToArray ( ) ) ;
}
else if ( id = = 2 )
{
if ( MainWindow . mainViewModel . serverModel2 ! = null & & MainWindow . mainViewModel . serverModel2 . IsOpened )
MainWindow . mainViewModel . serverModel2 . SendMessage ( ServerReplyData ( byteList [ 5 ] , byteList [ 6 ] , 0x00 ) . ToArray ( ) ) ;
}
}
catch ( Exception ex )
{
content = "接驳盒回复上位机主动下发的校时信息长度校验失败信息发送失败," + ex . Message ;
sql = $"insert into alarm_run_info(StationID,RecordTime,ParaName,ParaContent) values('{id}','{DateTime.Now}','接驳盒数据发送','{content}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
}
}
else
{
int time = BitConverter . ToInt32 ( new byte [ ] { byteList [ 9 ] , byteList [ 8 ] , byteList [ 7 ] , byteList [ 6 ] } , 0 ) ;
DateTime epoch = new DateTime ( 1970 , 1 , 1 , 0 , 0 , 0 , DateTimeKind . Utc ) ;
DateTime checkTime = epoch . AddSeconds ( time ) . ToLocalTime ( ) ;
string record = "接驳盒回复上位机主动下发的校时信息,时间为:" + checkTime . ToString ( ) ;
sql = $"insert into log_record(StationID,RecordTime,Device_Name,Operation_Type,Record) values('{id}','{DateTime.Now}','海底地震监测基站-接驳盒','校时','{record}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
}
break ;
case 0x07 :
//接驳盒请求读取上位机现在时间
//读取时间
byteLength = BitConverter . ToInt16 ( new byte [ ] { byteList [ 3 ] , byteList [ 2 ] } , 0 ) ;
if ( byteLength ! = 8 ) //长度校验失败
{
string content = "接驳盒请求读取上位机现在时间数据长度校验失败" ;
sql = $"insert into alarm_run_info(StationID,RecordTime,ParaName,ParaContent) values('{id}','{DateTime.Now}','接驳盒数据校验','{content}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
try
{
//发送失败信息
if ( id = = 1 )
{
if ( MainWindow . mainViewModel . serverModel1 ! = null & & MainWindow . mainViewModel . serverModel1 . IsOpened )
MainWindow . mainViewModel . serverModel1 . SendMessage ( ServerReplyData ( byteList [ 5 ] , byteList [ 6 ] , 0x00 ) . ToArray ( ) ) ;
}
else if ( id = = 2 )
{
if ( MainWindow . mainViewModel . serverModel2 ! = null & & MainWindow . mainViewModel . serverModel2 . IsOpened )
MainWindow . mainViewModel . serverModel2 . SendMessage ( ServerReplyData ( byteList [ 5 ] , byteList [ 6 ] , 0x00 ) . ToArray ( ) ) ;
}
}
catch ( Exception ex )
{
content = "接驳盒读取上位机现在时间长度校验失败信息发送失败," + ex . Message ;
sql = $"insert into alarm_run_info(StationID,RecordTime,ParaName,ParaContent) values('{id}','{DateTime.Now}','接驳盒数据发送','{content}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
}
}
else
{
List < byte > SendData = new List < byte > ( ) ;
SendData . AddRange ( new byte [ 2 ] { 0xEF , 0xFE } ) ; //帧头( H=0xEF 0xFE)
SendData . AddRange ( new byte [ 2 ] { 0x00 , 0x08 } ) ; //长度( L=8)(MSH MSL) (从地址码到最后)
SendData . Add ( 0x01 ) ; //地址码
SendData . Add ( 0x07 ) ; //功能码
TimeSpan ts = DateTime . Now - new DateTime ( 1970 , 1 , 1 , 8 , 0 , 0 , 0 ) ;
int totalSeconds = ( int ) ts . TotalSeconds + 28800 ;
string hexSeconds = totalSeconds . ToString ( "X2" ) ;
byte [ ] timeByte = Tools . HexStringToByteArray ( hexSeconds ) ;
timeByte = Tools . PadArrayWithZeros ( timeByte , 4 ) ;
SendData . AddRange ( timeByte ) ; //UTC时间
byte checkCode = Tools . CheckSum ( SendData . ToArray ( ) ) ;
SendData . Add ( checkCode ) ; //校验
SendData . Add ( 0x16 ) ; //结束符(0x16)
try
{
if ( id = = 1 )
{
if ( MainWindow . mainViewModel . serverModel1 ! = null & & MainWindow . mainViewModel . serverModel1 . IsOpened )
MainWindow . mainViewModel . serverModel1 . SendMessage ( SendData . ToArray ( ) ) ;
}
else if ( id = = 2 )
{
if ( MainWindow . mainViewModel . serverModel2 ! = null & & MainWindow . mainViewModel . serverModel2 . IsOpened )
MainWindow . mainViewModel . serverModel2 . SendMessage ( SendData . ToArray ( ) ) ;
}
}
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 ) ;
}
}
break ;
case 0x14 :
//接驳盒发送数据给上位机
//传输事件
byteLength = BitConverter . ToInt16 ( new byte [ ] { byteList [ 3 ] , byteList [ 2 ] } , 0 ) ;
if ( byteLength ! = 18 ) //长度校验失败
{
string content = "接驳盒告警数据长度校验失败" ;
sql = $"insert into alarm_run_info(StationID,RecordTime,ParaName,ParaContent) values('{id}','{DateTime.Now}','接驳盒数据校验','{content}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
try
{
//发送失败信息
if ( id = = 1 )
{
if ( MainWindow . mainViewModel . serverModel1 ! = null & & MainWindow . mainViewModel . serverModel1 . IsOpened )
MainWindow . mainViewModel . serverModel1 . SendMessage ( ServerReplyData ( byteList [ 5 ] , byteList [ 6 ] , 0x00 ) . ToArray ( ) ) ;
}
else if ( id = = 2 )
{
if ( MainWindow . mainViewModel . serverModel2 ! = null & & MainWindow . mainViewModel . serverModel2 . IsOpened )
MainWindow . mainViewModel . serverModel2 . SendMessage ( ServerReplyData ( byteList [ 5 ] , byteList [ 6 ] , 0x00 ) . ToArray ( ) ) ;
}
}
catch ( Exception ex )
{
content = "接驳盒告警数据长度校验失败信息发送失败," + ex . Message ;
sql = $"insert into alarm_run_info(StationID,RecordTime,ParaName,ParaContent) values('{id}','{DateTime.Now}','接驳盒数据发送','{content}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
}
}
else
{
string ParaName ;
int ParaNum ;
DateTime DataTime ;
string ParaContent ;
string eventData = "" ;
try
{
ParaName = ReturnEvent ( byteList [ 6 ] , byteList [ 7 ] ) ;
ParaNum = byteList [ 8 ] ;
int timeUTC = BitConverter . ToInt32 ( new byte [ 4 ] { byteList [ 12 ] , byteList [ 11 ] , byteList [ 10 ] , byteList [ 9 ] } , 0 ) ;
// Unix时间戳起始时间
DateTime epoch = new DateTime ( 1970 , 1 , 1 , 8 , 0 , 0 , DateTimeKind . Utc ) ;
// 将时间戳转换为UTC时间
DataTime = epoch . AddSeconds ( timeUTC ) ;
int millisecond = BitConverter . ToInt16 ( new byte [ 2 ] { byteList [ 14 ] , byteList [ 13 ] } , 0 ) ;
ParaContent = ReturnEventName ( byteList [ 19 ] ) ;
if ( byteList [ 6 ] = = 0x08 )
{
eventData = BitConverter . ToInt32 ( new byte [ 4 ] { byteList [ 15 ] , byteList [ 16 ] , byteList [ 17 ] , byteList [ 18 ] } , 0 ) . ToString ( ) ;
}
else
{
float data = BitConverter . ToSingle ( new byte [ 4 ] { byteList [ 15 ] , byteList [ 16 ] , byteList [ 17 ] , byteList [ 18 ] } , 0 ) ;
data = ( 38.4f * ( data - 2.5f ) ) < 0 ? 0 : ( 38.4f * ( data - 2.5f ) ) ;
eventData = data . ToString ( ) ;
}
try
{
//发送成功信息
List < byte > SendData = new List < byte > ( ) ;
SendData . AddRange ( new byte [ 2 ] { 0xEF , 0xFE } ) ; //帧头( H=0xEF 0xFE)
SendData . AddRange ( new byte [ 2 ] { 0x00 , 0x07 } ) ; //长度( L=6)(MSH MSL) (从地址码到最后)
SendData . Add ( 0x01 ) ; //地址码(ADDR=00)
SendData . Add ( functionCode ) ; //功能码( FUNC=02)
SendData . AddRange ( new byte [ 2 ] { byteList [ 6 ] , byteList [ 7 ] } ) ; //事件码( x,y)
SendData . Add ( 0x01 ) ; //接收应答( ACK:0-解析失败, 1-解析成功)
byte checkCode = Tools . CheckSum ( SendData . ToArray ( ) ) ;
SendData . Add ( checkCode ) ; //校验(CS = 帧头 ...+... 应答)
SendData . Add ( 0x16 ) ; //结束符(0x16)
if ( id = = 1 )
{
if ( MainWindow . mainViewModel . serverModel1 ! = null & & MainWindow . mainViewModel . serverModel1 . IsOpened )
MainWindow . mainViewModel . serverModel1 . SendMessage ( SendData . ToArray ( ) ) ;
}
else if ( id = = 2 )
{
if ( MainWindow . mainViewModel . serverModel2 ! = null & & MainWindow . mainViewModel . serverModel2 . IsOpened )
MainWindow . mainViewModel . serverModel2 . SendMessage ( SendData . ToArray ( ) ) ;
}
}
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 ) ;
}
#region 更 新 到 实 时 数 据 显 示 页 面
//更新到前端
App . Current . Dispatcher . Invoke ( ( ) = >
{
if ( id = = 1 )
MainViewModel . realTimeDataViewModel1 . AlarmList . Clear ( ) ;
sql = String . Format ( "select * from alarm_info where StationID = {0} ORDER by id desc limit 7" , id ) ;
MySqlDataReader dataReader = DBHelper . ExecuteReader ( sql , 1 ) ;
int index = 1 ;
while ( dataReader . Read ( ) )
{
AlarmRecordModel alarmRecordModel = new AlarmRecordModel ( ) ;
alarmRecordModel . Index = index + + ;
alarmRecordModel . RecordTime = Convert . ToDateTime ( dataReader [ "RecordTime" ] ) ;
alarmRecordModel . ParaName = dataReader [ "ParaName" ] . ToString ( ) ;
alarmRecordModel . ParaContent = dataReader [ "ParaContent" ] . ToString ( ) ;
alarmRecordModel . ProcessingMethod = dataReader [ "ProcessingMethod" ] . ToString ( ) ;
if ( dataReader [ "UrgencyLevel" ] . ToString ( ) = = "1" )
{
alarmRecordModel . UrgencyLevel = ( ImageSource ) Application . Current . FindResource ( "CycleYellow" ) ;
}
else if ( dataReader [ "UrgencyLevel" ] . ToString ( ) = = "2" )
{
alarmRecordModel . UrgencyLevel = ( ImageSource ) Application . Current . FindResource ( "CycleOrange" ) ;
}
else
{
alarmRecordModel . UrgencyLevel = ( ImageSource ) Application . Current . FindResource ( "CycleRed" ) ;
}
if ( id = = 1 )
MainViewModel . realTimeDataViewModel1 . AlarmList . Add ( alarmRecordModel ) ;
else
MainViewModel . realTimeDataViewModel2 . AlarmList . Add ( alarmRecordModel ) ;
}
dataReader . Dispose ( ) ;
} ) ;
#endregion
}
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 ) ;
//try
//{
// //发送失败信息
// if (id == 1)
// {
// if (MainWindow.mainViewModel.serverModel1 != null && MainWindow.mainViewModel.serverModel1.IsOpened)
// MainWindow.mainViewModel.serverModel1.SendMessage(ServerReplyData(byteList[5], byteList[6], 0x00).ToArray());
// }
// else if (id == 2)
// {
// if (MainWindow.mainViewModel.serverModel2 != null && MainWindow.mainViewModel.serverModel2.IsOpened)
// MainWindow.mainViewModel.serverModel2.SendMessage(ServerReplyData(byteList[5], byteList[6], 0x00).ToArray());
// }
//}
//catch
//{
//}
break ;
}
//数据存储
sql = $"insert into alarm_info(StationID,RecordTime,DataTime,ParaName,ParaNum," +
$"ParaContent,ParaData,IsHandled) values('{id}','{DateTime.Now}','{DataTime}','{ParaName}','{ParaNum}'," +
$"'{ParaContent}','{eventData}','0');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
}
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 ;
}
/// <summary>
/// 返回事件代码
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public string ReturnEvent ( byte byte1 , byte byte2 )
{
string events = "" ;
if ( byte1 = = 0x08 )
{
if ( byte2 = = 0x07 )
events = "接驳盒电力载波机电源状态" ;
else if ( byte2 = = 0x08 )
events = "接驳盒主地震仪电源状态" ;
else if ( byte2 = = 0x09 )
events = "接驳盒备地震仪电源状态" ;
else if ( byte2 = = 0x0A )
events = "接驳盒电磁电源状态" ;
else if ( byte2 = = 0x0B )
events = "接驳盒光电交换机电源状态" ;
else if ( byte2 = = 0x01 )
events = "地震仪电源状态" ;
else if ( byte2 = = 0x02 )
events = "地震仪供电方式" ;
}
else if ( byte1 = = 0x09 )
{
if ( byte2 = = 0x03 )
events = "接驳盒舱门状态" ;
else if ( byte2 = = 0x05 )
events = "接驳盒漏水状态" ;
else if ( byte2 = = 0x14 )
events = "保护板-接驳盒-电力载波机状态" ;
else if ( byte2 = = 0x15 )
events = "保护板-接驳盒-光电交换机状态" ;
else if ( byte2 = = 0x16 )
events = "保护板-主地震仪状态" ;
else if ( byte2 = = 0x17 )
events = "保护板-备地震仪状态" ;
else if ( byte2 = = 0x18 )
events = "保护板-电磁状态" ;
}
return events ;
}
/// <summary>
/// 返回事件名称
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public string ReturnEventName ( byte bytes )
{
string eventName = "" ;
if ( bytes = = 0x00 )
eventName = "恢复" ;
else if ( bytes = = 0x01 )
eventName = "欠压" ;
else if ( bytes = = 0x02 )
eventName = "过压" ;
else if ( bytes = = 0x03 )
eventName = "过流" ;
else if ( bytes = = 0x04 )
eventName = "烟雾" ;
else if ( bytes = = 0x05 )
eventName = "舱门" ;
else if ( bytes = = 0x06 )
eventName = "漏水" ;
else if ( bytes = = 0x07 )
eventName = "保护故障" ;
return eventName ;
}
}
}