2024-09-03 08:30:34 +00:00
using JiangsuEarthquake.Common ;
using JiangsuEarthquake.DataAccess ;
using JiangsuEarthquake.Models ;
using JiangsuEarthquake.Views ;
using JiangsuEarthquake.Views.UserControls ;
using LiveCharts ;
using LiveCharts.Defaults ;
using LiveCharts.Wpf ;
using MySql.Data.MySqlClient ;
using System.Collections.ObjectModel ;
using System.Windows ;
using System.Windows.Media ;
using System.Windows.Threading ;
using Application = System . Windows . Application ;
using Brush = System . Windows . Media . Brush ;
using Color = System . Windows . Media . Color ;
namespace JiangsuEarthquake.ViewModels
{
public class BoosterStationStateDataViewModel : NotifyBase
{
public BoosterStationStateDataModel boosterStationStateDataModel { get ; set ; } = new BoosterStationStateDataModel ( ) ;
public BoosterStationStateDataView boosterStationStateDataView { get ; set ; } = new BoosterStationStateDataView ( ) ;
#region Search Data
private DateTime _startDateTime ;
public DateTime StartDateTime
{
get { return _startDateTime ; }
set { Set ( ref _startDateTime , value ) ; }
}
private DateTime _endDateTime ;
public DateTime EndDateTime
{
get { return _endDateTime ; }
set { Set ( ref _endDateTime , value ) ; }
}
int PageIndex = 0 ;
private int recordCount ;
public int RecordCount
{
get { return recordCount ; }
set { recordCount = value ; this . DoNotify ( ) ; }
}
private int totalPage ;
public int TotalPage
{
get { return totalPage ; }
set { totalPage = value ; this . DoNotify ( ) ; }
}
private string boosterStationStateMsg ;
public string BoosterStationStateMsg
{
get { return boosterStationStateMsg ; }
set { boosterStationStateMsg = value ; this . DoNotify ( ) ; }
}
#endregion
#region Coordinate Axis
public SeriesCollection seriesCollection ;
public SeriesCollection SeriesCollection
{
get { return seriesCollection ; }
set
{
seriesCollection = value ;
this . DoNotify ( ) ;
}
}
private Func < double , string > _xFormatter ;
public Func < double , string > XFormatter
{
get { return _xFormatter ; }
set
{
_xFormatter = value ;
this . DoNotify ( ) ;
}
}
private Func < double , string > _yFormatter1 ;
public Func < double , string > YFormatter1
{
get { return _yFormatter1 ; }
set
{
_yFormatter1 = value ;
this . DoNotify ( ) ;
}
}
private Func < double , string > _yFormatter2 ;
public Func < double , string > YFormatter2
{
get { return _yFormatter2 ; }
set
{
_yFormatter2 = value ;
this . DoNotify ( ) ;
}
}
#endregion
private ObservableCollection < BoosterStationStateDataModel > boosterStationStateDataList = new ObservableCollection < BoosterStationStateDataModel > ( ) ;
public ObservableCollection < BoosterStationStateDataModel > BoosterStationStateDataList
{
get { return boosterStationStateDataList ; }
set { boosterStationStateDataList = value ; this . DoNotify ( ) ; }
}
private ObservableCollection < BoosterStationStateDataModel > totalBoosterStationStateDataList = new ObservableCollection < BoosterStationStateDataModel > ( ) ;
public ObservableCollection < BoosterStationStateDataModel > TotalBoosterStationStateDataList
{
get { return totalBoosterStationStateDataList ; }
set { totalBoosterStationStateDataList = value ; this . DoNotify ( ) ; }
}
#region CommandBase
public CommandBase ForwordBoosterStationStateCommand { get ; set ; } //读取日志信息
public CommandBase NextBoosterStationStateCommand { get ; set ; } //读取日志信息
public CommandBase RelayActivationCommand { get ; set ; }
public CommandBase RelayShutdownCommand { get ; set ; }
public CommandBase AskDataCommand { get ; set ; } //获取数据
public CommandBase OtherSettingCommand { get ; set ; }
public CommandBase AlarmSettingCommand { get ; set ; } //显示报警设置窗体
public CommandBase AlarmSetButtonCommand { get ; set ; } //发送报警设置信息
public CommandBase RefreshDataCommand { get ; set ; }
public CommandBase DownloadDataCommand { get ; set ; }
#endregion
int station_id = 1 ;
#region Timer Define
public DispatcherTimer timerAskData = new DispatcherTimer ( ) ;
public DispatcherTimer timerAskDataMsgCollapse = new DispatcherTimer ( ) ;
public DispatcherTimer timerRelayActivation = new DispatcherTimer ( ) ;
public DispatcherTimer timerRelayShutdown = new DispatcherTimer ( ) ;
public DispatcherTimer timerRelayControl = new DispatcherTimer ( ) ;
public DispatcherTimer timerAlarmSet = new DispatcherTimer ( ) ;
public DispatcherTimer timerAlarmSetMsgCollapse = new DispatcherTimer ( ) ;
public DispatcherTimer timerDownloadDataMsgHidden = new DispatcherTimer ( ) ;
public DispatcherTimer timerCycleAskData1 = new DispatcherTimer ( ) ;
public DispatcherTimer timerCycleAskData2 = new DispatcherTimer ( ) ;
#endregion
#region VolCurCollectorModel
private byte fromDeviceAddress { get ; set ; } //从设备地址
private byte functionCode { get ; set ; } //功能码
private byte [ ] startingRegisterAddress { get ; set ; } //起始寄存器地址
private byte [ ] registerNumber { get ; set ; } //寄存器个数
private byte [ ] registerAddress { get ; set ; } //寄存器地址
private byte [ ] checkCodeCRC { get ; set ; } //校验码
private List < byte > checkByte { get ; set ; } = new List < byte > ( ) ; //校验码Byte
private byte [ ] writeData { get ; set ; } //数据
private List < byte > sendBytes { get ; set ; } = new List < byte > ( ) ; //发送数据
#endregion
#region Data Filtering
public static DialogViewModel vm ;
private bool _isChecked ;
public bool IsChecked
{
get { return _isChecked ; }
set
{
_isChecked = value ;
this . DoNotify ( ) ;
string timeSearch = "" ;
if ( IsChecked )
{
if ( string . IsNullOrEmpty ( EndDateTime . ToString ( ) ) | | string . IsNullOrEmpty ( StartDateTime . ToString ( ) ) )
{
IsChecked = false ;
return ;
}
if ( EndDateTime < StartDateTime )
{
HandyControl . Controls . Dialog . Show ( new TextDialog ( "起始时间大于结束时间,\n请重新输入! " ) ) ;
//this.EndDateTime = DateTime.Now;
//this.StartDateTime = DateTime.Now.AddDays(-1);
IsChecked = false ;
return ;
}
2024-09-12 01:48:49 +00:00
timeSearch = " and RecordTime BETWEEN '" + StartDateTime + "' and '" + EndDateTime + "' " ;
2024-09-03 08:30:34 +00:00
}
else
{
timeSearch = "" ;
}
//设置当前页为1
PageIndex = 1 ;
TotalBoosterStationStateDataList . Clear ( ) ;
BoosterStationStateDataList . Clear ( ) ;
var chartValuesTeam1 = new ChartValues < DateTimePoint > ( ) ;
var chartValuesTeam2 = new ChartValues < DateTimePoint > ( ) ;
string sql = String . Format ( "select RecordTime,In_Vol,In_Cur,Power,RelayStatus from boosterstation_state where StationID = {0} {1} ORDER by id desc limit 100" , station_id , timeSearch ) ;
MySqlDataReader dataReader = DBHelper . ExecuteReader ( sql , 1 ) ;
int index = 1 ;
while ( dataReader . Read ( ) )
{
BoosterStationStateDataModel boosterStationStateDataModel = new BoosterStationStateDataModel ( ) ;
boosterStationStateDataModel . Index = index + + ;
boosterStationStateDataModel . RecordTime = Convert . ToDateTime ( dataReader [ "RecordTime" ] ) ;
boosterStationStateDataModel . In_Vol = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "In_Vol" ] . ToString ( ) ) ? "0" : dataReader [ "In_Vol" ] ) ;
boosterStationStateDataModel . In_Cur = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "In_Cur" ] . ToString ( ) ) ? "0" : dataReader [ "In_Cur" ] ) ;
boosterStationStateDataModel . Power = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Power" ] . ToString ( ) ) ? "0" : dataReader [ "Power" ] ) ;
if ( dataReader [ "RelayStatus" ] . ToString ( ) = = "0" )
boosterStationStateDataModel . RelayStatus = ( ImageSource ) Application . Current . FindResource ( "DeviceOn" ) ;
else
boosterStationStateDataModel . RelayStatus = ( ImageSource ) Application . Current . FindResource ( "DeviceOff" ) ;
TotalBoosterStationStateDataList . Add ( boosterStationStateDataModel ) ;
if ( ! Convert . IsDBNull ( dataReader [ "In_Vol" ] ) & & ! Convert . IsDBNull ( dataReader [ "In_Cur" ] ) )
{
chartValuesTeam1 . Add ( new DateTimePoint
{
Value = Convert . ToSingle ( dataReader [ "In_Vol" ] ) ,
DateTime = Convert . ToDateTime ( dataReader [ "RecordTime" ] ) ,
} ) ;
chartValuesTeam2 . Add ( new DateTimePoint
{
Value = Convert . ToSingle ( dataReader [ "In_Cur" ] ) ,
DateTime = Convert . ToDateTime ( dataReader [ "RecordTime" ] ) ,
} ) ;
}
}
dataReader . Dispose ( ) ;
RecordCount = index - 1 ;
if ( RecordCount < = 10 )
{
TotalPage = 1 ;
2024-09-12 01:48:49 +00:00
BoosterStationStateDataList = new ObservableCollection < BoosterStationStateDataModel > ( ) ;
2024-09-03 08:30:34 +00:00
BoosterStationStateDataList = TotalBoosterStationStateDataList ;
}
else
{
TotalPage = ( int ) Math . Ceiling ( ( double ) RecordCount / 10 ) ;
2024-09-12 01:48:49 +00:00
BoosterStationStateDataList = new ObservableCollection < BoosterStationStateDataModel > ( ) ;
2024-09-03 08:30:34 +00:00
for ( int i = 0 ; i < 10 ; i + + )
{
BoosterStationStateDataList . Add ( TotalBoosterStationStateDataList [ i ] ) ;
}
}
BoosterStationStateMsg = string . Format ( "共计{0}页,当前第{1}页" , TotalPage , PageIndex ) ;
SeriesCollection = new SeriesCollection
{
new LineSeries
{
Title = "输入电压" ,
Values = chartValuesTeam1 ,
LineSmoothness = 0 ,
ScalesYAt = 0 ,
Stroke = new SolidColorBrush ( Color . FromArgb ( 255 , 43 , 237 , 241 ) ) ,
Fill = new SolidColorBrush ( Colors . Transparent )
} ,
new LineSeries
{
Title = "输入电流" ,
Values = chartValuesTeam2 ,
LineSmoothness = 0 ,
ScalesYAt = 1 ,
Stroke = new SolidColorBrush ( Color . FromArgb ( 255 , 255 , 165 , 0 ) ) ,
Fill = new SolidColorBrush ( Colors . Transparent )
} ,
} ;
//XFormatter = val => new DateTime((long)val).ToString("MM/dd HH:mm");
//YFormatter1 = val => val.ToString("F2") + " V";
//YFormatter2 = val => val.ToString("F2") + " A";
string record = "查询升压站状态数据历史数据,查询时间范围为:" + StartDateTime + "至" + EndDateTime + ",共查询到" + RecordCount + "条历史数据" ;
sql = $"insert into log_record(StationID,RecordTime,Device_Name,Operation_Type,Record) values('{station_id}','{DateTime.Now}','升压站电控通信系统','数据查询','{record}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
}
}
#endregion
public BoosterStationStateDataViewModel ( int id )
{
// 默认查询1天内的日志
this . EndDateTime = DateTime . Now ;
this . StartDateTime = DateTime . Now . AddDays ( - 1 ) ;
PageIndex = 1 ;
station_id = id ;
vm = new DialogViewModel
{
Content = ""
} ;
#region Command Set
this . ForwordBoosterStationStateCommand = new CommandBase ( ) ;
this . ForwordBoosterStationStateCommand . DoExcute = new Action < object > ( ForwordBoosterStationState ) ;
this . ForwordBoosterStationStateCommand . DoCanExcute = new Func < object , bool > ( ( o ) = > true ) ;
this . NextBoosterStationStateCommand = new CommandBase ( ) ;
this . NextBoosterStationStateCommand . DoExcute = new Action < object > ( NextBoosterStationState ) ;
this . NextBoosterStationStateCommand . DoCanExcute = new Func < object , bool > ( ( o ) = > true ) ;
this . AskDataCommand = new CommandBase ( ) ;
this . AskDataCommand . DoExcute = new Action < object > ( AskData ) ;
this . AskDataCommand . DoCanExcute = new Func < object , bool > ( ( o ) = > true ) ;
this . OtherSettingCommand = new CommandBase ( ) ;
this . OtherSettingCommand . DoExcute = new Action < object > ( OtherSetting ) ;
this . OtherSettingCommand . DoCanExcute = new Func < object , bool > ( ( o ) = > true ) ;
this . RelayActivationCommand = new CommandBase ( ) ;
this . RelayActivationCommand . DoExcute = new Action < object > ( RelayActivation ) ;
this . RelayActivationCommand . DoCanExcute = new Func < object , bool > ( ( o ) = > true ) ;
this . RelayShutdownCommand = new CommandBase ( ) ;
this . RelayShutdownCommand . DoExcute = new Action < object > ( RelayShutdown ) ;
this . RelayShutdownCommand . DoCanExcute = new Func < object , bool > ( ( o ) = > true ) ;
this . AlarmSettingCommand = new CommandBase ( ) ;
this . AlarmSettingCommand . DoExcute = new Action < object > ( AlarmSetting ) ;
this . AlarmSettingCommand . DoCanExcute = new Func < object , bool > ( ( o ) = > true ) ;
this . AlarmSetButtonCommand = new CommandBase ( ) ;
this . AlarmSetButtonCommand . DoExcute = new Action < object > ( AlarmSetButton ) ;
this . AlarmSetButtonCommand . DoCanExcute = new Func < object , bool > ( ( o ) = > true ) ;
this . RefreshDataCommand = new CommandBase ( ) ;
this . RefreshDataCommand . DoExcute = new Action < object > ( RefreshData ) ;
this . RefreshDataCommand . DoCanExcute = new Func < object , bool > ( ( o ) = > true ) ;
this . DownloadDataCommand = new CommandBase ( ) ;
this . DownloadDataCommand . DoExcute = new Action < object > ( DownloadData ) ;
this . DownloadDataCommand . DoCanExcute = new Func < object , bool > ( ( o ) = > true ) ;
#endregion
#region Timer Set
timerAskData . Interval = TimeSpan . FromSeconds ( 20 ) ;
timerAskData . Tick + = TimerAskData_Tick ;
timerAskDataMsgCollapse . Interval = TimeSpan . FromSeconds ( 2 ) ;
timerAskDataMsgCollapse . Tick + = TimerAskDataMsgCollapse_Tick ;
timerRelayActivation . Interval = TimeSpan . FromSeconds ( 20 ) ;
timerRelayActivation . Tick + = TimerRelayActivationa_Tick ;
timerRelayShutdown . Interval = TimeSpan . FromSeconds ( 20 ) ;
timerRelayShutdown . Tick + = TimerRelayShutdown_Tick ;
timerRelayControl . Interval = TimeSpan . FromSeconds ( 2 ) ;
timerRelayControl . Tick + = TimerRelayControl_Tick ;
timerAlarmSet . Interval = TimeSpan . FromSeconds ( 20 ) ;
timerAlarmSet . Tick + = TimerAlarmSet_Tick ;
timerAlarmSetMsgCollapse . Interval = TimeSpan . FromSeconds ( 2 ) ;
timerAlarmSetMsgCollapse . Tick + = TimerAlarmSetMsgCollapse_Tick ;
timerDownloadDataMsgHidden . Interval = TimeSpan . FromSeconds ( 2 ) ;
timerDownloadDataMsgHidden . Tick + = TimerDownloadDataMsgHidden_Tick ;
#endregion
}
#region Ask Data
private void TimerAskData_Tick ( object sender , EventArgs e )
{
if ( ! AskDataBtnIsReceived )
{
AskDataMsgVisibility = Visibility . Visible ;
AskDataMsg = "请求数据发送成功,但未接收到数据!" ;
timerAskDataMsgCollapse . Start ( ) ;
AskDataMsgForeground = new SolidColorBrush ( Colors . Red ) ;
AskDataBtnIsEnabled = true ;
}
// 停止定时器
( sender as DispatcherTimer ) . Stop ( ) ;
}
private void TimerAskDataMsgCollapse_Tick ( object sender , EventArgs e )
{
AskDataMsgVisibility = Visibility . Hidden ;
// 停止定时器
( sender as DispatcherTimer ) . Stop ( ) ;
}
private bool cycleRequestIsChecked ;
public bool CycleRequestIsChecked
{
get { return cycleRequestIsChecked ; }
set
{
cycleRequestIsChecked = value ;
this . DoNotify ( ) ;
if ( CycleRequestIsChecked )
{
if ( string . IsNullOrEmpty ( CycleRequestCycle ) )
{
CycleRequestIsChecked = false ;
return ;
//可改为自动设定周期
}
//周期获取数据
if ( station_id = = 1 )
{
timerCycleAskData1 . Interval = TimeSpan . FromSeconds ( int . Parse ( CycleRequestCycle ) ) ;
timerCycleAskData1 . Tick + = TimerCycleAskData1_Tick ;
timerCycleAskData1 . Start ( ) ;
}
else
{
timerCycleAskData2 . Interval = TimeSpan . FromSeconds ( int . Parse ( CycleRequestCycle ) ) ;
timerCycleAskData2 . Tick + = TimerCycleAskData2_Tick ;
timerCycleAskData2 . Start ( ) ;
}
string record = "周期请求升压站状态数据已开启,请求周期为:" + CycleRequestCycle ;
string sql = $"insert into log_record(StationID,RecordTime,Device_Name,Operation_Type,Record) values('{station_id}','{DateTime.Now}','升压站电控通信系统','数据请求','{record}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
}
else
{
//停止定时器
if ( station_id = = 1 )
{
timerCycleAskData1 . Stop ( ) ;
}
else
{
timerCycleAskData2 . Stop ( ) ;
}
string sql = $"insert into log_record(StationID,RecordTime,Device_Name,Operation_Type,Record) values('{station_id}','{DateTime.Now}','升压站电控通信系统','数据请求','周期请求升压站状态数据已关闭');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
}
}
}
private void TimerCycleAskData1_Tick ( object sender , EventArgs e )
{
AskBoosterStationData ( 1 ) ;
}
private void TimerCycleAskData2_Tick ( object sender , EventArgs e )
{
AskBoosterStationData ( 2 ) ;
}
private string cycleRequestCycle ;
public string CycleRequestCycle
{
get { return cycleRequestCycle ; }
set { cycleRequestCycle = value ; this . DoNotify ( ) ; }
}
private string askDataMsg ;
public string AskDataMsg
{
get { return askDataMsg ; }
set { askDataMsg = value ; this . DoNotify ( ) ; }
}
private Brush askDataMsgForeground ;
public Brush AskDataMsgForeground
{
get { return askDataMsgForeground ; }
set { askDataMsgForeground = value ; this . DoNotify ( ) ; }
}
private bool askDataBtnIsEnabled = true ;
public bool AskDataBtnIsEnabled
{
get { return askDataBtnIsEnabled ; }
set { askDataBtnIsEnabled = value ; this . DoNotify ( ) ; }
}
private bool askDataBtnIsReceived = false ;
public bool AskDataBtnIsReceived
{
get { return askDataBtnIsReceived ; }
set { askDataBtnIsReceived = value ; this . DoNotify ( ) ; }
}
private Visibility askDataMsgVisibility = Visibility . Visible ;
public Visibility AskDataMsgVisibility
{
get { return askDataMsgVisibility ; }
set { askDataMsgVisibility = value ; this . DoNotify ( ) ; }
}
public void AskData ( object o )
{
AskBoosterStationData ( station_id ) ;
}
private void AskBoosterStationData ( int id )
{
AskDataBtnIsEnabled = false ;
AskDataBtnIsReceived = false ;
//查询从设备寄存器内容
fromDeviceAddress = 0x01 ;
//if (id == 1)
//{
// deviceAddress = Convert.ToByte(Tools.GetAppSetting("DeviceAddress1"));
// fromDeviceAddress = deviceAddress; //从设备地址
//}
//else
//{
// deviceAddress = Convert.ToByte(Tools.GetAppSetting("DeviceAddress2"));
// fromDeviceAddress = deviceAddress; //从设备地址
//}
functionCode = 0x03 ; //功能码
startingRegisterAddress = new byte [ 2 ] { 0x00 , 0x00 } ; //起始寄存器地址
registerNumber = new byte [ 2 ] { 0x00 , 0x0A } ; //寄存器个数
//校验码计算
checkByte . Clear ( ) ;
checkByte . Add ( fromDeviceAddress ) ;
checkByte . Add ( functionCode ) ;
checkByte . AddRange ( startingRegisterAddress ) ;
checkByte . AddRange ( registerNumber ) ;
checkCodeCRC = Tools . CRCCalcRev ( checkByte . ToArray ( ) ) ;
//发送指令
sendBytes . Clear ( ) ;
sendBytes . Add ( fromDeviceAddress ) ;
sendBytes . Add ( functionCode ) ;
sendBytes . AddRange ( startingRegisterAddress ) ;
sendBytes . AddRange ( registerNumber ) ;
sendBytes . AddRange ( checkCodeCRC ) ;
bool result = false ;
if ( id = = 1 )
{
//if (comModel1.IsConnected)
// result = comModel1.SendMessage(sendBytes.ToArray());
if ( MainWindow . mainViewModel . clientModel1 . IsConnected )
result = MainWindow . mainViewModel . clientModel1 . SendMessage ( sendBytes . ToArray ( ) ) ;
else
{
AskDataMsgVisibility = Visibility . Visible ;
AskDataMsg = "通信未连接!" ;
timerAskDataMsgCollapse . Start ( ) ;
AskDataMsgForeground = new SolidColorBrush ( Colors . Red ) ;
AskDataBtnIsEnabled = true ;
return ;
}
}
else
{
//if (comModel2.IsConnected)
// result = comModel2.SendMessage(sendBytes.ToArray());
if ( MainWindow . mainViewModel . clientModel2 . IsConnected )
result = MainWindow . mainViewModel . clientModel2 . SendMessage ( sendBytes . ToArray ( ) ) ;
else
{
AskDataMsgVisibility = Visibility . Visible ;
AskDataMsg = "通信未连接!" ;
timerAskDataMsgCollapse . Start ( ) ;
AskDataMsgForeground = new SolidColorBrush ( Colors . Red ) ;
AskDataBtnIsEnabled = true ;
return ;
}
}
if ( result )
{
AskDataMsgVisibility = Visibility . Visible ;
AskDataMsg = "请求升压站状态数据发送成功!" ;
timerAskDataMsgCollapse . Start ( ) ;
AskDataMsgForeground = new SolidColorBrush ( Colors . Green ) ;
timerAskData . Start ( ) ;
}
else
{
AskDataMsgVisibility = Visibility . Visible ;
AskDataMsg = "请求升压站状态数据发送失败!" ;
timerAskDataMsgCollapse . Start ( ) ;
AskDataMsgForeground = new SolidColorBrush ( Colors . Red ) ;
AskDataBtnIsEnabled = true ;
}
string sql = $"insert into log_record(StationID,RecordTime,Device_Name,Operation_Type,Record) values('{station_id}','{DateTime.Now}','升压站电控通信系统','数据请求','{AskDataMsg}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
}
#endregion
#region Relay Control
private void TimerRelayActivationa_Tick ( object sender , EventArgs e )
{
if ( ! relayActivationBtnIsReceived )
{
RelayControlMsg = "继电开启发送成功,但未收到回复!" ;
RelayControlMsgForeground = new SolidColorBrush ( Colors . Red ) ;
timerRelayControl . Start ( ) ;
RelayActivationBtnIsEnabled = true ;
}
}
private void TimerRelayShutdown_Tick ( object sender , EventArgs e )
{
if ( ! relayShutdownBtnIsReceived )
{
RelayControlMsg = "继电开启发送成功,但未收到回复!" ;
RelayControlMsgForeground = new SolidColorBrush ( Colors . Red ) ;
timerRelayControl . Start ( ) ;
RelayShutdownBtnIsEnabled = true ;
}
}
private void TimerRelayControl_Tick ( object sender , EventArgs e )
{
RelayControlMsgVisibility = Visibility . Hidden ;
// 停止定时器
( sender as DispatcherTimer ) . Stop ( ) ;
}
private string relayControlMsg ;
public string RelayControlMsg
{
get { return relayControlMsg ; }
set { relayControlMsg = value ; this . DoNotify ( ) ; }
}
private Brush relayControlMsgForeground ;
public Brush RelayControlMsgForeground
{
get { return relayControlMsgForeground ; }
set { relayControlMsgForeground = value ; this . DoNotify ( ) ; }
}
private bool relayActivationBtnIsEnabled = true ;
public bool RelayActivationBtnIsEnabled
{
get { return relayActivationBtnIsEnabled ; }
set { relayActivationBtnIsEnabled = value ; this . DoNotify ( ) ; }
}
private bool relayShutdownBtnIsEnabled = true ;
public bool RelayShutdownBtnIsEnabled
{
get { return relayShutdownBtnIsEnabled ; }
set { relayShutdownBtnIsEnabled = value ; this . DoNotify ( ) ; }
}
private bool relayActivationBtnIsReceived = false ;
public bool RelayActivationBtnIsReceived
{
get { return relayActivationBtnIsReceived ; }
set { relayActivationBtnIsReceived = value ; this . DoNotify ( ) ; }
}
private bool relayShutdownBtnIsReceived = false ;
public bool RelayShutdownBtnIsReceived
{
get { return relayShutdownBtnIsReceived ; }
set { relayShutdownBtnIsReceived = value ; this . DoNotify ( ) ; }
}
private Visibility relayControlMsgVisibility = Visibility . Visible ;
public Visibility RelayControlMsgVisibility
{
get { return relayControlMsgVisibility ; }
set { relayControlMsgVisibility = value ; this . DoNotify ( ) ; }
}
public byte [ ] RelayActivationRecv = new byte [ 8 ] ;
public byte [ ] RelayShutdownRecv = new byte [ 8 ] ;
public void OtherSetting ( object o )
{
if ( station_id = = 1 )
HandyControl . Controls . Dialog . Show ( new OtherSettingView ( 1 ) ) ;
else
HandyControl . Controls . Dialog . Show ( new OtherSettingView ( 2 ) ) ;
}
public void RelayActivation ( object o )
{
RelayActivationBtnIsEnabled = false ;
//对从设备单个寄存器置数
byte deviceAddress ;
if ( station_id = = 1 )
{
deviceAddress = Convert . ToByte ( Tools . GetAppSetting ( "DeviceAddress1" ) ) ;
fromDeviceAddress = deviceAddress ; //从设备地址
}
else
{
deviceAddress = Convert . ToByte ( Tools . GetAppSetting ( "DeviceAddress2" ) ) ;
fromDeviceAddress = deviceAddress ; //从设备地址
}
functionCode = 0x06 ; //功能码
registerAddress = new byte [ ] { 0x01 , 0x30 } ;
//值 0000: 继电器继开
writeData = new byte [ ] { 0x00 , 0x00 } ;
//校验码计算
checkByte . Clear ( ) ;
checkByte . Add ( fromDeviceAddress ) ;
checkByte . Add ( functionCode ) ;
checkByte . AddRange ( registerAddress ) ;
checkByte . AddRange ( writeData ) ;
checkCodeCRC = Tools . CRCCalcRev ( checkByte . ToArray ( ) ) ;
//发送指令
sendBytes . Clear ( ) ;
sendBytes . Add ( fromDeviceAddress ) ;
sendBytes . Add ( functionCode ) ;
sendBytes . AddRange ( registerAddress ) ;
sendBytes . AddRange ( writeData ) ;
sendBytes . AddRange ( checkCodeCRC ) ;
bool result = false ;
if ( station_id = = 1 )
{
if ( MainWindow . mainViewModel . clientModel1 . IsConnected )
result = MainWindow . mainViewModel . clientModel1 . SendMessage ( sendBytes . ToArray ( ) ) ;
else
{
RelayControlMsgVisibility = Visibility . Visible ;
RelayControlMsg = "通信未连接!" ;
timerRelayControl . Start ( ) ;
RelayControlMsgForeground = new SolidColorBrush ( Colors . Red ) ;
RelayActivationBtnIsEnabled = true ;
return ;
}
RelayActivationRecv = sendBytes . ToArray ( ) ;
}
else if ( station_id = = 2 )
{
if ( MainWindow . mainViewModel . clientModel2 . IsConnected )
result = MainWindow . mainViewModel . clientModel2 . SendMessage ( sendBytes . ToArray ( ) ) ;
else
{
RelayControlMsgVisibility = Visibility . Visible ;
RelayControlMsg = "通信未连接!" ;
timerRelayControl . Start ( ) ;
RelayControlMsgForeground = new SolidColorBrush ( Colors . Red ) ;
RelayActivationBtnIsEnabled = true ;
return ;
}
RelayActivationRecv = sendBytes . ToArray ( ) ;
}
if ( result )
{
RelayControlMsgVisibility = Visibility . Visible ;
RelayControlMsg = "继电开启发送成功!" ;
timerRelayControl . Start ( ) ;
RelayControlMsgForeground = new SolidColorBrush ( Colors . Green ) ;
timerRelayActivation . Start ( ) ;
}
else
{
RelayControlMsgVisibility = Visibility . Visible ;
RelayControlMsg = "继电开启发送失败!" ;
timerRelayControl . Start ( ) ;
RelayControlMsgForeground = new SolidColorBrush ( Colors . Red ) ;
RelayActivationBtnIsEnabled = true ;
}
}
public void RelayShutdown ( object o )
{
RelayShutdownBtnIsEnabled = false ;
//对从设备单个寄存器置数
byte deviceAddress ;
if ( station_id = = 1 )
{
deviceAddress = Convert . ToByte ( Tools . GetAppSetting ( "DeviceAddress1" ) ) ;
fromDeviceAddress = deviceAddress ; //从设备地址
}
else
{
deviceAddress = Convert . ToByte ( Tools . GetAppSetting ( "DeviceAddress2" ) ) ;
fromDeviceAddress = deviceAddress ; //从设备地址
}
functionCode = 0x06 ; //功能码
registerAddress = new byte [ ] { 0x01 , 0x30 } ;
//值 FF00(16 进制):继电器一直闭合,需手动发命令打开
writeData = new byte [ ] { 0xFF , 0x00 } ;
//校验码计算
checkByte . Clear ( ) ;
checkByte . Add ( fromDeviceAddress ) ;
checkByte . Add ( functionCode ) ;
checkByte . AddRange ( registerAddress ) ;
checkByte . AddRange ( writeData ) ;
checkCodeCRC = Tools . CRCCalcRev ( checkByte . ToArray ( ) ) ;
//发送指令
sendBytes . Clear ( ) ;
sendBytes . Add ( fromDeviceAddress ) ;
sendBytes . Add ( functionCode ) ;
sendBytes . AddRange ( registerAddress ) ;
sendBytes . AddRange ( writeData ) ;
sendBytes . AddRange ( checkCodeCRC ) ;
bool result = false ;
if ( station_id = = 1 )
{
if ( MainWindow . mainViewModel . clientModel1 . IsConnected )
result = MainWindow . mainViewModel . clientModel1 . SendMessage ( sendBytes . ToArray ( ) ) ;
else
{
RelayControlMsgVisibility = Visibility . Visible ;
RelayControlMsg = "通信未连接!" ;
timerRelayControl . Start ( ) ;
RelayControlMsgForeground = new SolidColorBrush ( Colors . Red ) ;
RelayShutdownBtnIsEnabled = true ;
return ;
}
RelayShutdownRecv = sendBytes . ToArray ( ) ;
}
else if ( station_id = = 2 )
{
if ( MainWindow . mainViewModel . clientModel2 . IsConnected )
result = MainWindow . mainViewModel . clientModel2 . SendMessage ( sendBytes . ToArray ( ) ) ;
else
{
RelayControlMsgVisibility = Visibility . Visible ;
RelayControlMsg = "通信未连接!" ;
timerRelayControl . Start ( ) ;
RelayControlMsgForeground = new SolidColorBrush ( Colors . Red ) ;
RelayShutdownBtnIsEnabled = true ;
return ;
}
RelayShutdownRecv = sendBytes . ToArray ( ) ;
}
if ( result )
{
RelayControlMsgVisibility = Visibility . Visible ;
RelayControlMsg = "继电关闭发送成功!" ;
timerRelayControl . Start ( ) ;
RelayControlMsgForeground = new SolidColorBrush ( Colors . Green ) ;
timerRelayShutdown . Start ( ) ;
}
else
{
RelayControlMsgVisibility = Visibility . Visible ;
RelayControlMsg = "继电关闭发送失败!" ;
timerRelayControl . Start ( ) ;
RelayControlMsgForeground = new SolidColorBrush ( Colors . Red ) ;
RelayShutdownBtnIsEnabled = true ;
}
}
#endregion
#region Alarm Set
private void TimerAlarmSet_Tick ( object sender , EventArgs e )
{
if ( AlarmSetBtnIsReceived1 & & AlarmSetBtnIsReceived2 & & AlarmSetBtnIsReceived3 )
{
AlarmSetMessage = "报警设置成功!" ;
AlarmSetMessageForeground = new SolidColorBrush ( Colors . Green ) ;
timerAlarmSetMsgCollapse . Start ( ) ;
}
else
{
AlarmSetMessage = "报警设置发送成功,\n但未收到回复! " ;
AlarmSetMessageForeground = new SolidColorBrush ( Colors . Red ) ;
timerAlarmSetMsgCollapse . Start ( ) ;
AlarmSetButtonIsEnabled = true ;
}
// 停止定时器
( sender as DispatcherTimer ) . Stop ( ) ;
}
private void TimerAlarmSetMsgCollapse_Tick ( object sender , EventArgs e )
{
AlarmSetMsgVisibility = Visibility . Hidden ;
// 停止定时器
( sender as DispatcherTimer ) . Stop ( ) ;
}
public string alarmParaComSelectedItem ;
public string AlarmParaComSelectedItem
{
get { return alarmParaComSelectedItem ; }
set
{
alarmParaComSelectedItem = value ;
this . DoNotify ( ) ;
}
}
public string alarmFuncComSelectedItem ;
public string AlarmFuncComSelectedItem
{
get { return alarmFuncComSelectedItem ; }
set
{
alarmFuncComSelectedItem = value ;
this . DoNotify ( ) ;
}
}
public string alarmUpTBText ;
public string AlarmUpTBText
{
get { return alarmUpTBText ; }
set
{
alarmUpTBText = value ;
this . DoNotify ( ) ;
}
}
public string alarmDownTBText ;
public string AlarmDownTBText
{
get { return alarmDownTBText ; }
set
{
alarmDownTBText = value ;
this . DoNotify ( ) ;
}
}
public string alarmSetMsg ;
public string AlarmSetMsg
{
get { return alarmSetMsg ; }
set
{
alarmSetMsg = value ;
this . DoNotify ( ) ;
}
}
public void AlarmSetting ( object o )
{
if ( station_id = = 1 )
HandyControl . Controls . Dialog . Show ( new AlarmSettingView ( 1 ) ) ;
else
HandyControl . Controls . Dialog . Show ( new AlarmSettingView ( 2 ) ) ;
}
private string alarmSetMessage ;
public string AlarmSetMessage
{
get { return alarmSetMessage ; }
set { alarmSetMessage = value ; this . DoNotify ( ) ; }
}
private Brush alarmSetMessageForeground ;
public Brush AlarmSetMessageForeground
{
get { return alarmSetMessageForeground ; }
set { alarmSetMessageForeground = value ; this . DoNotify ( ) ; }
}
private bool alarmSetButtonIsEnabled = true ;
public bool AlarmSetButtonIsEnabled
{
get { return alarmSetButtonIsEnabled ; }
set { alarmSetButtonIsEnabled = value ; this . DoNotify ( ) ; }
}
private bool alarmSetBtnIsReceived1 = false ;
public bool AlarmSetBtnIsReceived1
{
get { return alarmSetBtnIsReceived1 ; }
set { alarmSetBtnIsReceived1 = value ; this . DoNotify ( ) ; }
}
private bool alarmSetBtnIsReceived2 = false ;
public bool AlarmSetBtnIsReceived2
{
get { return alarmSetBtnIsReceived2 ; }
set { alarmSetBtnIsReceived2 = value ; this . DoNotify ( ) ; }
}
private bool alarmSetBtnIsReceived3 = false ;
public bool AlarmSetBtnIsReceived3
{
get { return alarmSetBtnIsReceived3 ; }
set { alarmSetBtnIsReceived3 = value ; this . DoNotify ( ) ; }
}
private Visibility alarmSetMsgVisibility = Visibility . Visible ;
public Visibility AlarmSetMsgVisibility
{
get { return alarmSetMsgVisibility ; }
set { alarmSetMsgVisibility = value ; this . DoNotify ( ) ; }
}
public byte [ ] AlarmParaRecv = new byte [ 8 ] ;
public byte [ ] AlarmFuncRecv = new byte [ 8 ] ;
public byte [ ] AlarmThresholdRecv = new byte [ 8 ] ;
public void AlarmSetButton ( object o )
{
#region 判 断 条 件
//对选择和输入数据进行判断
if ( string . IsNullOrEmpty ( AlarmParaComSelectedItem ) )
{
AlarmSetMsg = "请选择报警参数!" ;
return ;
}
else if ( string . IsNullOrEmpty ( AlarmFuncComSelectedItem ) )
{
AlarmSetMsg = "请选择报警功能!" ;
return ;
}
else if ( AlarmFuncComSelectedItem = = "下限报警" & & string . IsNullOrEmpty ( AlarmDownTBText ) ) //下限报警
{
AlarmSetMsg = "请输入阈值下限!" ;
return ;
}
else if ( AlarmFuncComSelectedItem = = "上限报警" & & string . IsNullOrEmpty ( AlarmUpTBText ) )
{
AlarmSetMsg = "请输入阈值上限!" ;
return ;
}
else if ( ( AlarmFuncComSelectedItem = = "上下限报警" | | AlarmFuncComSelectedItem = = "区间内报警" ) & & ( string . IsNullOrEmpty ( AlarmUpTBText ) | | string . IsNullOrEmpty ( AlarmDownTBText ) ) )
{
AlarmSetMsg = "请输入阈值上限和下限!" ;
return ;
}
else
AlarmSetMsg = "" ;
if ( ( AlarmFuncComSelectedItem = = "上下限报警" | | AlarmFuncComSelectedItem = = "区间内报警" ) & & ( int . Parse ( AlarmUpTBText ) < = int . Parse ( AlarmDownTBText ) ) )
{
AlarmSetMsg = "阈值上限应该大于阈值下限!" ;
return ;
}
else
AlarmSetMsg = "" ;
if ( AlarmParaComSelectedItem = = "电流" & & ! string . IsNullOrEmpty ( AlarmUpTBText ) & & int . Parse ( AlarmUpTBText ) > 24 )
{
AlarmSetMsg = "电流的阈值上限\n不超过24A! " ;
return ;
}
else if ( AlarmParaComSelectedItem = = "电压" & & ! string . IsNullOrEmpty ( AlarmUpTBText ) & & int . Parse ( AlarmUpTBText ) > 120 )
{
AlarmSetMsg = "电压的阈值上限\n不超过120V! " ;
return ;
}
else
AlarmSetMsg = "" ;
#endregion
AlarmSetButtonIsEnabled = false ;
AlarmSetBtnIsReceived1 = false ;
AlarmSetBtnIsReceived2 = false ;
AlarmSetBtnIsReceived3 = false ;
//对从设备单个寄存器置数
byte deviceAddress ;
if ( station_id = = 1 )
{
deviceAddress = Convert . ToByte ( Tools . GetAppSetting ( "DeviceAddress1" ) ) ;
fromDeviceAddress = deviceAddress ; //从设备地址
}
else
{
deviceAddress = Convert . ToByte ( Tools . GetAppSetting ( "DeviceAddress2" ) ) ;
fromDeviceAddress = deviceAddress ; //从设备地址
}
bool result = false ;
//先设置报警参数(300 寄存器)即对应哪个参数报警
functionCode = 0x06 ; //功能码
registerAddress = new byte [ ] { 0x01 , 0x2C } ;
if ( AlarmParaComSelectedItem = = "电压" )
writeData = new byte [ ] { 0x00 , 0x07 } ;
else if ( AlarmParaComSelectedItem = = "电流" )
writeData = new byte [ ] { 0x00 , 0x08 } ;
//校验码计算
checkByte . Clear ( ) ;
checkByte . Add ( fromDeviceAddress ) ;
checkByte . Add ( functionCode ) ;
checkByte . AddRange ( registerAddress ) ;
checkByte . AddRange ( writeData ) ;
checkCodeCRC = Tools . CRCCalcRev ( checkByte . ToArray ( ) ) ;
//发送指令
sendBytes . Clear ( ) ;
sendBytes . Add ( fromDeviceAddress ) ;
sendBytes . Add ( functionCode ) ;
sendBytes . AddRange ( registerAddress ) ;
sendBytes . AddRange ( writeData ) ;
sendBytes . AddRange ( checkCodeCRC ) ;
if ( station_id = = 1 )
{
if ( MainWindow . mainViewModel . clientModel1 . IsConnected )
result = MainWindow . mainViewModel . clientModel1 . SendMessage ( sendBytes . ToArray ( ) ) ;
else
{
AlarmSetMsgVisibility = Visibility . Collapsed ;
AlarmSetMessage = "通信未连接!" ;
timerAlarmSetMsgCollapse . Start ( ) ;
AlarmSetMessageForeground = new SolidColorBrush ( Colors . Red ) ;
AlarmSetButtonIsEnabled = true ;
return ;
}
AlarmParaRecv = sendBytes . ToArray ( ) ;
}
else if ( station_id = = 2 )
{
if ( MainWindow . mainViewModel . clientModel2 . IsConnected )
result = MainWindow . mainViewModel . clientModel2 . SendMessage ( sendBytes . ToArray ( ) ) ;
else
{
AlarmSetMessage = "通信未连接!" ;
AlarmSetMessageForeground = new SolidColorBrush ( Colors . Red ) ;
timerAlarmSetMsgCollapse . Start ( ) ;
AlarmSetButtonIsEnabled = true ;
return ;
}
AlarmParaRecv = sendBytes . ToArray ( ) ;
}
if ( result = = false )
{
AlarmSetMessage = "报警参数设置发送失败!" ;
AlarmSetMessageForeground = new SolidColorBrush ( Colors . Red ) ;
timerAlarmSetMsgCollapse . Start ( ) ;
AlarmSetButtonIsEnabled = true ;
return ;
}
//再设置报警功能(303 寄存器)即需要上 / 下限等报警功能
functionCode = 0x06 ; //功能码
registerAddress = new byte [ ] { 0x01 , 0x2F } ;
if ( AlarmFuncComSelectedItem = = "下限报警" )
writeData = new byte [ ] { 0x00 , 0x01 } ;
else if ( AlarmFuncComSelectedItem = = "上限报警" )
writeData = new byte [ ] { 0x00 , 0x02 } ;
else if ( AlarmFuncComSelectedItem = = "上下限报警" )
writeData = new byte [ ] { 0x00 , 0x03 } ;
else if ( AlarmFuncComSelectedItem = = "区间内报警" )
writeData = new byte [ ] { 0x00 , 0x04 } ;
//校验码计算
checkByte . Clear ( ) ;
checkByte . Add ( fromDeviceAddress ) ;
checkByte . Add ( functionCode ) ;
checkByte . AddRange ( registerAddress ) ;
checkByte . AddRange ( writeData ) ;
checkCodeCRC = Tools . CRCCalcRev ( checkByte . ToArray ( ) ) ;
//发送指令
sendBytes . Clear ( ) ;
sendBytes . Add ( fromDeviceAddress ) ;
sendBytes . Add ( functionCode ) ;
sendBytes . AddRange ( registerAddress ) ;
sendBytes . AddRange ( writeData ) ;
sendBytes . AddRange ( checkCodeCRC ) ;
if ( station_id = = 1 )
{
if ( MainWindow . mainViewModel . clientModel1 . IsConnected )
result = MainWindow . mainViewModel . clientModel1 . SendMessage ( sendBytes . ToArray ( ) ) ;
else
{
AlarmSetMessage = "通信未连接!" ;
AlarmSetMessageForeground = new SolidColorBrush ( Colors . Red ) ;
timerAlarmSetMsgCollapse . Start ( ) ;
AlarmSetButtonIsEnabled = true ;
return ;
}
AlarmFuncRecv = sendBytes . ToArray ( ) ;
}
else if ( station_id = = 2 )
{
if ( MainWindow . mainViewModel . clientModel2 . IsConnected )
result = MainWindow . mainViewModel . clientModel2 . SendMessage ( sendBytes . ToArray ( ) ) ;
else
{
AlarmSetMessage = "通信未连接!" ;
AlarmSetMessageForeground = new SolidColorBrush ( Colors . Red ) ;
timerAlarmSetMsgCollapse . Start ( ) ;
AlarmSetButtonIsEnabled = true ;
return ;
}
AlarmFuncRecv = sendBytes . ToArray ( ) ;
}
if ( result = = false )
{
AlarmSetMessage = "报警功能设置发送失败!" ;
AlarmSetMessageForeground = new SolidColorBrush ( Colors . Red ) ;
timerAlarmSetMsgCollapse . Start ( ) ;
AlarmSetButtonIsEnabled = true ;
return ;
}
//再设置报警阀值(301 与 302 寄存器),根据报警功能设置对应的下/ 下限报警值(报警阀值设置为量程的百分比方式)
if ( AlarmFuncComSelectedItem = = "下限报警" | | AlarmFuncComSelectedItem = = "上限报警" )
{
functionCode = 0x06 ; //功能码
if ( AlarmFuncComSelectedItem = = "下限报警" )
{
registerAddress = new byte [ ] { 0x01 , 0x2D } ;
ushort down = ushort . Parse ( AlarmDownTBText ) ;
if ( AlarmParaComSelectedItem = = "电流" )
down = ( ushort ) ( ushort . Parse ( AlarmDownTBText ) * 5 ) ;
writeData = new byte [ 2 ] ;
writeData [ 0 ] = ( byte ) ( ( down & 0xFF00 ) > > 8 ) ;
writeData [ 1 ] = ( byte ) ( down & 0x00FF ) ;
}
else if ( AlarmParaComSelectedItem = = "上限报警" )
{
registerAddress = new byte [ ] { 0x01 , 0x2D } ;
ushort up = ushort . Parse ( AlarmUpTBText ) ;
if ( AlarmParaComSelectedItem = = "电流" )
up = ( ushort ) ( ushort . Parse ( AlarmDownTBText ) * 5 ) ;
writeData = new byte [ 2 ] ;
writeData [ 0 ] = ( byte ) ( ( up & 0xFF00 ) > > 8 ) ;
writeData [ 1 ] = ( byte ) ( up & 0x00FF ) ;
}
//校验码计算
checkByte . Clear ( ) ;
checkByte . Add ( fromDeviceAddress ) ;
checkByte . Add ( functionCode ) ;
checkByte . AddRange ( registerAddress ) ;
checkByte . AddRange ( writeData ) ;
checkCodeCRC = Tools . CRCCalcRev ( checkByte . ToArray ( ) ) ;
//发送指令
sendBytes . Clear ( ) ;
sendBytes . Add ( fromDeviceAddress ) ;
sendBytes . Add ( functionCode ) ;
sendBytes . AddRange ( registerAddress ) ;
sendBytes . AddRange ( writeData ) ;
sendBytes . AddRange ( checkCodeCRC ) ;
if ( station_id = = 1 )
{
if ( MainWindow . mainViewModel . clientModel1 . IsConnected )
result = MainWindow . mainViewModel . clientModel1 . SendMessage ( sendBytes . ToArray ( ) ) ;
else
{
AlarmSetMessage = "通信未连接!" ;
AlarmSetMessageForeground = new SolidColorBrush ( Colors . Red ) ;
timerAlarmSetMsgCollapse . Start ( ) ;
AlarmSetButtonIsEnabled = true ;
return ;
}
AlarmThresholdRecv = sendBytes . ToArray ( ) ;
}
else if ( station_id = = 2 )
{
if ( MainWindow . mainViewModel . clientModel2 . IsConnected )
result = MainWindow . mainViewModel . clientModel2 . SendMessage ( sendBytes . ToArray ( ) ) ;
else
{
AlarmSetMessage = "通信未连接!" ;
AlarmSetMessageForeground = new SolidColorBrush ( Colors . Red ) ;
timerAlarmSetMsgCollapse . Start ( ) ;
AlarmSetButtonIsEnabled = true ;
return ;
}
AlarmThresholdRecv = sendBytes . ToArray ( ) ;
}
if ( result = = false )
{
AlarmSetMessage = "报警阈值设置发送失败!" ;
AlarmSetMessageForeground = new SolidColorBrush ( Colors . Red ) ;
timerAlarmSetMsgCollapse . Start ( ) ;
AlarmSetButtonIsEnabled = true ;
return ;
}
else
{
AlarmSetMessage = "报警设置发送成功!" ;
AlarmSetMessageForeground = new SolidColorBrush ( Colors . Green ) ;
timerAlarmSetMsgCollapse . Start ( ) ;
timerAlarmSet . Start ( ) ;
}
}
else if ( AlarmFuncComSelectedItem = = "上下限报警" | | AlarmFuncComSelectedItem = = "区间内报警" )
{
functionCode = 0x10 ; //功能码
startingRegisterAddress = new byte [ 2 ] { 0x01 , 0x2D } ; //起始寄存器地址
registerNumber = new byte [ 2 ] { 0x00 , 0x02 } ; //寄存器个数
byte dataNumber = 0x04 ;
ushort down = ushort . Parse ( AlarmDownTBText ) ;
ushort up = ushort . Parse ( AlarmUpTBText ) ;
if ( AlarmParaComSelectedItem = = "电流" )
{
down = ( ushort ) ( ushort . Parse ( AlarmDownTBText ) * 5 ) ;
up = ( ushort ) ( ushort . Parse ( AlarmDownTBText ) * 5 ) ;
}
writeData = new byte [ 4 ] ;
writeData [ 0 ] = ( byte ) ( ( down & 0xFF00 ) > > 8 ) ;
writeData [ 1 ] = ( byte ) ( down & 0x00FF ) ;
writeData [ 2 ] = ( byte ) ( ( up & 0xFF00 ) > > 8 ) ;
writeData [ 3 ] = ( byte ) ( up & 0x00FF ) ;
//校验码计算
checkByte . Clear ( ) ;
checkByte . Add ( fromDeviceAddress ) ;
checkByte . Add ( functionCode ) ;
checkByte . AddRange ( startingRegisterAddress ) ;
checkByte . AddRange ( registerNumber ) ;
checkByte . Add ( dataNumber ) ;
checkByte . AddRange ( writeData ) ;
checkCodeCRC = Tools . CRCCalcRev ( checkByte . ToArray ( ) ) ;
//发送指令
sendBytes . Clear ( ) ;
sendBytes . Add ( fromDeviceAddress ) ;
sendBytes . Add ( functionCode ) ;
sendBytes . AddRange ( startingRegisterAddress ) ;
sendBytes . AddRange ( registerNumber ) ;
sendBytes . Add ( dataNumber ) ;
sendBytes . AddRange ( writeData ) ;
sendBytes . AddRange ( checkCodeCRC ) ;
List < byte > AlarmThresholdRecvList = new List < byte > ( ) ;
AlarmThresholdRecvList . Add ( fromDeviceAddress ) ;
AlarmThresholdRecvList . Add ( functionCode ) ;
AlarmThresholdRecvList . AddRange ( startingRegisterAddress ) ;
AlarmThresholdRecvList . AddRange ( registerNumber ) ;
checkCodeCRC = Tools . CRCCalcRev ( AlarmThresholdRecvList . ToArray ( ) ) ;
AlarmThresholdRecvList . AddRange ( checkCodeCRC ) ;
if ( station_id = = 1 )
{
if ( MainWindow . mainViewModel . clientModel1 . IsConnected )
result = MainWindow . mainViewModel . clientModel1 . SendMessage ( sendBytes . ToArray ( ) ) ;
else
{
AlarmSetMessage = "通信未连接!" ;
AlarmSetMessageForeground = new SolidColorBrush ( Colors . Red ) ;
timerAlarmSetMsgCollapse . Start ( ) ;
AlarmSetButtonIsEnabled = true ;
return ;
}
AlarmThresholdRecv = AlarmThresholdRecvList . ToArray ( ) ;
}
else if ( station_id = = 2 )
{
if ( MainWindow . mainViewModel . clientModel2 . IsConnected )
result = MainWindow . mainViewModel . clientModel2 . SendMessage ( sendBytes . ToArray ( ) ) ;
else
{
AlarmSetMessage = "通信未连接!" ;
AlarmSetMessageForeground = new SolidColorBrush ( Colors . Red ) ;
timerAlarmSetMsgCollapse . Start ( ) ;
AlarmSetButtonIsEnabled = true ;
return ;
}
AlarmThresholdRecv = AlarmThresholdRecvList . ToArray ( ) ;
}
if ( result = = false )
{
AlarmSetMessage = "报警阈值设置发送失败!" ;
AlarmSetMessageForeground = new SolidColorBrush ( Colors . Red ) ;
timerAlarmSetMsgCollapse . Start ( ) ;
AlarmSetButtonIsEnabled = true ;
return ;
}
else
{
AlarmSetMessage = "报警设置发送成功!" ;
AlarmSetMessageForeground = new SolidColorBrush ( Colors . Green ) ;
timerAlarmSetMsgCollapse . Start ( ) ;
timerAlarmSet . Start ( ) ;
}
}
}
#endregion
#region Refresh Data
public void RefreshData ( object o )
{
string timeSearch = "" ;
if ( IsChecked )
{
2024-09-12 01:48:49 +00:00
timeSearch = " and RecordTime BETWEEN '" + StartDateTime + "' and '" + EndDateTime + "' " ;
2024-09-03 08:30:34 +00:00
}
else
{
timeSearch = "" ;
}
//设置当前页为1
PageIndex = 1 ;
TotalBoosterStationStateDataList . Clear ( ) ;
BoosterStationStateDataList . Clear ( ) ;
var chartValuesTeam1 = new ChartValues < DateTimePoint > ( ) ;
var chartValuesTeam2 = new ChartValues < DateTimePoint > ( ) ;
string sql = String . Format ( "select RecordTime,In_Vol,In_Cur,Power,RelayStatus from boosterstation_state where StationID = {0} {1} ORDER by id desc limit 100" , station_id , timeSearch ) ;
MySqlDataReader dataReader = DBHelper . ExecuteReader ( sql , 1 ) ;
int index = 1 ;
while ( dataReader . Read ( ) )
{
BoosterStationStateDataModel boosterStationStateDataModel = new BoosterStationStateDataModel ( ) ;
boosterStationStateDataModel . Index = index + + ;
boosterStationStateDataModel . RecordTime = Convert . ToDateTime ( dataReader [ "RecordTime" ] ) ;
boosterStationStateDataModel . In_Vol = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "In_Vol" ] . ToString ( ) ) ? "0.00" : dataReader [ "In_Vol" ] ) ;
boosterStationStateDataModel . In_Cur = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "In_Cur" ] . ToString ( ) ) ? "0.00" : dataReader [ "In_Cur" ] ) ;
boosterStationStateDataModel . Power = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Power" ] . ToString ( ) ) ? "0.00" : dataReader [ "Power" ] ) ;
if ( dataReader [ "RelayStatus" ] . ToString ( ) = = "0" )
boosterStationStateDataModel . RelayStatus = ( ImageSource ) Application . Current . FindResource ( "DeviceOn" ) ;
else
boosterStationStateDataModel . RelayStatus = ( ImageSource ) Application . Current . FindResource ( "DeviceOff" ) ;
TotalBoosterStationStateDataList . Add ( boosterStationStateDataModel ) ;
if ( ! Convert . IsDBNull ( boosterStationStateDataModel . In_Vol ) & & ! Convert . IsDBNull ( boosterStationStateDataModel . In_Cur ) )
{
chartValuesTeam1 . Add ( new DateTimePoint
{
Value = Convert . ToSingle ( dataReader [ "In_Vol" ] ) ,
DateTime = Convert . ToDateTime ( dataReader [ "RecordTime" ] ) ,
} ) ;
chartValuesTeam2 . Add ( new DateTimePoint
{
Value = Convert . ToSingle ( dataReader [ "In_Cur" ] ) ,
DateTime = Convert . ToDateTime ( dataReader [ "RecordTime" ] ) ,
} ) ;
}
}
dataReader . Dispose ( ) ;
RecordCount = index - 1 ;
if ( RecordCount < = 10 )
{
TotalPage = 1 ;
2024-09-12 01:48:49 +00:00
BoosterStationStateDataList = new ObservableCollection < BoosterStationStateDataModel > ( ) ;
2024-09-03 08:30:34 +00:00
BoosterStationStateDataList = TotalBoosterStationStateDataList ;
}
else
{
TotalPage = ( int ) Math . Ceiling ( ( double ) RecordCount / 10 ) ;
2024-09-12 01:48:49 +00:00
BoosterStationStateDataList = new ObservableCollection < BoosterStationStateDataModel > ( ) ;
2024-09-03 08:30:34 +00:00
for ( int i = 0 ; i < 10 ; i + + )
{
BoosterStationStateDataList . Add ( TotalBoosterStationStateDataList [ i ] ) ;
}
}
BoosterStationStateMsg = string . Format ( "共计{0}页,当前第{1}页" , TotalPage , PageIndex ) ;
SeriesCollection = new SeriesCollection
{
new LineSeries
{
Title = "输入电压" ,
Values = chartValuesTeam1 ,
LineSmoothness = 0 ,
ScalesYAt = 0 ,
Stroke = new SolidColorBrush ( Color . FromArgb ( 255 , 43 , 237 , 241 ) ) ,
Fill = new SolidColorBrush ( Colors . Transparent )
} ,
new LineSeries
{
Title = "输入电流" ,
Values = chartValuesTeam2 ,
LineSmoothness = 0 ,
ScalesYAt = 1 ,
Stroke = new SolidColorBrush ( Color . FromArgb ( 255 , 255 , 165 , 0 ) ) ,
Fill = new SolidColorBrush ( Colors . Transparent )
} ,
} ;
//XFormatter = val => new DateTime((long)val).ToString("MM/dd HH:mm");
//YFormatter1 = val => val.ToString("F2") + " V";
//YFormatter2 = val => val.ToString("F2") + " A";
sql = $"insert into log_record(StationID,RecordTime,Device_Name,Operation_Type,Record) values('{station_id}','{DateTime.Now}','升压站电控通信系统','数据刷新','刷新升压站状态数据');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
}
#endregion
#region DownloadData
private void TimerDownloadDataMsgHidden_Tick ( object sender , EventArgs e )
{
DownloadDataMsgVisibility = Visibility . Hidden ;
// 停止定时器
( sender as DispatcherTimer ) . Stop ( ) ;
}
private string downloadDataMsg ;
public string DownloadDataMsg
{
get { return downloadDataMsg ; }
set { downloadDataMsg = value ; this . DoNotify ( ) ; }
}
private Brush downloadDataMsgForeground ;
public Brush DownloadDataMsgForeground
{
get { return downloadDataMsgForeground ; }
set { downloadDataMsgForeground = value ; this . DoNotify ( ) ; }
}
private bool downloadDataBtnIsEnabled = true ;
public bool DownloadDataBtnIsEnabled
{
get { return downloadDataBtnIsEnabled ; }
set { downloadDataBtnIsEnabled = value ; this . DoNotify ( ) ; }
}
private Visibility downloadDataMsgVisibility = Visibility . Visible ;
public Visibility DownloadDataMsgVisibility
{
get { return downloadDataMsgVisibility ; }
set { downloadDataMsgVisibility = value ; this . DoNotify ( ) ; }
}
public void DownloadData ( object o )
{
DownloadDataBtnIsEnabled = false ;
string boosterStationFolder = Tools . GetAppSetting ( "BoosterStationFolder" ) ;
string savePath = CSVDownload . CreateFile ( boosterStationFolder , "BoosterStationStateData_" + DateTime . Now . ToString ( "yyyyMMddhhMMss" ) , "csv" ) ;
bool result = CSVDownload . SaveBoosterStationDataToCSVFile ( TotalBoosterStationStateDataList , savePath ) ;
if ( result )
{
DownloadDataMsg = "下载数据成功!" ;
DownloadDataMsgVisibility = Visibility . Visible ;
DownloadDataMsgForeground = new SolidColorBrush ( Colors . Green ) ;
}
else
{
DownloadDataMsg = "下载数据失败!" ;
DownloadDataMsgVisibility = Visibility . Visible ;
DownloadDataMsgForeground = new SolidColorBrush ( Colors . Red ) ;
}
timerDownloadDataMsgHidden . Start ( ) ;
DownloadDataBtnIsEnabled = true ;
string record = "下载升压站状态数据," + DownloadDataMsg ;
string sql = $"insert into log_record(StationID,RecordTime,Device_Name,Operation_Type,Record) values('{station_id}','{DateTime.Now}','升压站电控通信系统','数据下载','{record}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
}
#endregion
#region Page Switching
public void ForwordBoosterStationState ( object o )
{
if ( PageIndex = = 1 )
return ;
try
{
PageIndex - = 1 ;
BoosterStationStateDataList . Clear ( ) ;
for ( int i = 0 ; i < 10 ; i + + )
{
BoosterStationStateDataList . Add ( TotalBoosterStationStateDataList [ i + ( PageIndex - 1 ) * 10 ] ) ;
}
BoosterStationStateMsg = string . Format ( "共计{0}页,当前第{1}页" , TotalPage , PageIndex ) ;
}
catch
{
TotalBoosterStationStateDataList . Clear ( ) ;
BoosterStationStateDataList . Clear ( ) ;
}
}
public void NextBoosterStationState ( object o )
{
if ( PageIndex = = TotalPage )
return ;
try
{
PageIndex + = 1 ;
BoosterStationStateDataList . Clear ( ) ;
for ( int i = 0 ; i < ( PageIndex = = TotalPage ? ( RecordCount - ( PageIndex - 1 ) * 10 ) : 10 ) ; i + + )
{
BoosterStationStateDataList . Add ( TotalBoosterStationStateDataList [ i + ( PageIndex - 1 ) * 10 ] ) ;
}
BoosterStationStateMsg = string . Format ( "共计{0}页,当前第{1}页" , TotalPage , PageIndex ) ;
}
catch
{
TotalBoosterStationStateDataList . Clear ( ) ;
BoosterStationStateDataList . Clear ( ) ;
}
}
#endregion
}
}