2023-12-18 09:31:28 +00:00
using _20230724_MBJC_upperpc.Common ;
2024-02-27 09:21:02 +00:00
using _20230724_MBJC_upperpc.DataAccess ;
2023-12-18 09:31:28 +00:00
using _20230724_MBJC_upperpc.Models ;
2023-12-23 03:35:57 +00:00
using _20230724_MBJC_upperpc.Views ;
2024-02-27 09:21:02 +00:00
using MySql.Data.MySqlClient ;
2023-12-18 09:31:28 +00:00
using System ;
using System.Collections.Generic ;
2024-03-13 07:02:46 +00:00
using System.Collections.ObjectModel ;
2023-12-18 09:31:28 +00:00
using System.Linq ;
using System.Reflection ;
using System.Runtime.Intrinsics.X86 ;
using System.Text ;
using System.Threading.Tasks ;
using System.Windows ;
namespace _20230724_MBJC_upperpc.ViewModels
{
public class MainViewModel : NotifyBase
{
#region 事 件
/// <summary>
/// 主界面标签切换事件
/// </summary>
public CommandBase NavChangedCommand { get ; set ; }
string Title_Name = "" ; //设置当前显示的标题 1 浮标 2 接驳盒 3 系统控制 4 日志记录
2024-01-22 09:48:13 +00:00
string CurrentPage = "" ; //当前页面 特质8个点位页面的信息
2024-03-06 09:46:55 +00:00
public CommandBase AlarmInfoCommand { get ; set ; }
2023-12-18 09:31:28 +00:00
#endregion
private FrameworkElement _pageContent ;
public FrameworkElement PageContent
{
get { return _pageContent ; }
set { _pageContent = value ; this . DoNotify ( ) ; }
}
2024-01-17 01:24:32 +00:00
#region
2024-01-22 05:36:57 +00:00
private BeaconViewModel beacon1 = new BeaconViewModel ( ) ;
2023-12-18 09:31:28 +00:00
2024-01-22 05:36:57 +00:00
public BeaconViewModel Beacon1
{
get { return beacon1 ; }
set { beacon1 = value ; this . DoNotify ( ) ; }
}
private BeaconViewModel beacon2 = new BeaconViewModel ( ) ;
2023-12-18 09:31:28 +00:00
2024-01-22 05:36:57 +00:00
public BeaconViewModel Beacon2
2024-01-17 01:24:32 +00:00
{
2024-01-22 05:36:57 +00:00
get { return beacon2 ; }
set { beacon2 = value ; this . DoNotify ( ) ; }
2024-01-17 01:24:32 +00:00
}
2024-01-22 05:36:57 +00:00
private BeaconViewModel beacon3 = new BeaconViewModel ( ) ;
2023-12-23 03:35:57 +00:00
2024-01-22 05:36:57 +00:00
public BeaconViewModel Beacon3
{
get { return beacon3 ; }
set { beacon3 = value ; this . DoNotify ( ) ; }
}
private BeaconViewModel beacon4 = new BeaconViewModel ( ) ;
2024-01-17 01:24:32 +00:00
2024-01-22 05:36:57 +00:00
public BeaconViewModel Beacon4
2023-12-18 09:31:28 +00:00
{
2024-01-22 05:36:57 +00:00
get { return beacon4 ; }
set { beacon4 = value ; this . DoNotify ( ) ; }
}
private BeaconViewModel beacon5 = new BeaconViewModel ( ) ;
public BeaconViewModel Beacon5
{
get { return beacon5 ; }
set { beacon5 = value ; this . DoNotify ( ) ; }
}
private BeaconViewModel beacon6 = new BeaconViewModel ( ) ;
public BeaconViewModel Beacon6
{
get { return beacon6 ; }
set { beacon6 = value ; this . DoNotify ( ) ; }
}
2024-02-21 02:24:56 +00:00
private BeaconViewModel beacon7 = new BeaconViewModel ( ) { Model_Visibility = 2 } ;
2024-01-22 05:36:57 +00:00
public BeaconViewModel Beacon7
{
get { return beacon7 ; }
set { beacon7 = value ; this . DoNotify ( ) ; }
}
private BeaconViewModel beacon8 = new BeaconViewModel ( ) { Model_Visibility = 2 } ;
public BeaconViewModel Beacon8
{
get { return beacon8 ; }
set { beacon8 = value ; this . DoNotify ( ) ; }
2023-12-18 09:31:28 +00:00
}
2024-03-06 09:46:55 +00:00
private BeaconViewModel beacon_Search = new BeaconViewModel ( ) ;
/// <summary>
/// 通过Slider查找后绑定的数据
/// </summary>
public BeaconViewModel Beacon_Search
{
get { return beacon_Search ; }
set { beacon_Search = value ; this . DoNotify ( ) ; }
}
2024-01-17 01:24:32 +00:00
#endregion
private ClientModel client = new ClientModel ( ) ;
/// <summary>
/// 连接基阵上位机软件的客户端
/// </summary>
public ClientModel Client
{
get { return client ; }
set { client = value ; this . DoNotify ( ) ; }
}
2024-03-13 07:02:46 +00:00
private ObservableCollection < AlarmInfo > alarmInfoList = new ObservableCollection < AlarmInfo > ( ) ;
/// <summary>
/// 告警信息列表
/// </summary>
public ObservableCollection < AlarmInfo > AlarmInfoList
{
get { return alarmInfoList ; }
set { alarmInfoList = value ; this . DoNotify ( ) ; }
}
2024-01-17 01:24:32 +00:00
/// <summary>
/// 连接基阵上位机软件客户端的ip和端口号
/// </summary>
public SocketInfo socketInfo = new SocketInfo ( )
{
IP = tools . GetAppSetting ( "BasicIP" ) ,
Port = int . Parse ( tools . GetAppSetting ( "BasicPort" ) ) ,
} ;
//基阵客户端连接和断开的操作
2024-01-19 05:04:06 +00:00
public CommandBase ConnectCommand { get ; set ; }
2024-01-17 01:24:32 +00:00
2023-12-18 09:31:28 +00:00
public MainViewModel ( )
{
2024-01-17 01:24:32 +00:00
//进行与基阵上位机软件连接
Client . DoConnect ( socketInfo ) ;
2023-12-18 09:31:28 +00:00
this . NavChangedCommand = new CommandBase ( ) ;
this . NavChangedCommand . DoExcute = new Action < object > ( DoNavChanged ) ;
this . NavChangedCommand . DoCanExcute = new Func < object , bool > ( ( o ) = > true ) ;
2023-12-23 03:35:57 +00:00
2024-01-17 01:24:32 +00:00
this . ConnectCommand = new CommandBase ( ) ;
this . ConnectCommand . DoExcute = new Action < object > ( DoConnect ) ;
this . ConnectCommand . DoCanExcute = new Func < object , bool > ( ( o ) = > { return true ; } ) ;
2024-03-06 09:46:55 +00:00
this . AlarmInfoCommand = new CommandBase ( ) ;
this . AlarmInfoCommand . DoExcute = new Action < object > ( SearchAlarmInfo ) ;
this . AlarmInfoCommand . DoCanExcute = new Func < object , bool > ( ( o ) = > { return true ; } ) ;
2024-03-13 09:53:50 +00:00
DoNavChanged ( "BuoyPlatformView" ) ;
2024-03-13 07:02:46 +00:00
SearchAlarmInfo ( "1" ) ;
2024-02-27 09:21:02 +00:00
#region 初 始 化 数 据
2024-03-14 09:27:41 +00:00
AlarmInfoList = new ObservableCollection < AlarmInfo > ( ) ;
Beacon1 . Beacon = InitData ( "Beacon1" ) ;
Beacon2 . Beacon = InitData ( "Beacon2" ) ;
Beacon3 . Beacon = InitData ( "Beacon3" ) ;
Beacon4 . Beacon = InitData ( "Beacon4" ) ;
Beacon5 . Beacon = InitData ( "Beacon5" ) ;
Beacon6 . Beacon = InitData ( "Beacon6" ) ;
Beacon7 . Beacon = InitData ( "Beacon7" ) ;
Beacon8 . Beacon = InitData ( "Beacon8" ) ;
#endregion
}
2024-02-27 09:21:02 +00:00
2024-03-14 09:27:41 +00:00
private BeaconModel InitData ( string stationName )
{
BeaconModel beaconModel = new BeaconModel ( ) ;
string sql = string . Format ( "select * from beaconmodel where StationID = {0} ORDER by id desc limit 1" , int . Parse ( tools . GetAppSetting ( stationName ) ) ) ;
MySqlDataReader reader = DBHelper . ExecuteReader ( sql , 1 ) ;
2024-02-27 09:21:02 +00:00
while ( reader . Read ( ) )
{
2024-03-14 09:27:41 +00:00
beaconModel . ID = Convert . ToInt32 ( reader [ "StationID" ] ) ;
beaconModel . Datetime = Convert . ToDateTime ( reader [ "Datetime" ] ) ;
beaconModel . Ralative_Heading_Angle = Convert . ToSingle ( reader [ "Ralative_Heading_Angle" ] ) ;
beaconModel . Ralative_Pitch_Angle = Convert . ToSingle ( reader [ "Ralative_Pitch_Angle" ] ) ;
beaconModel . Position_Distance = Convert . ToSingle ( reader [ "Position_Distance" ] ) ;
beaconModel . Propagationtime = Convert . ToSingle ( reader [ "Propagationtime" ] ) ;
beaconModel . BasicSite_JD = Convert . ToSingle ( reader [ "BasicSite_JD" ] ) ;
beaconModel . BasicSite_WD = Convert . ToSingle ( reader [ "BasicSite_WD" ] ) ;
beaconModel . BasicSite_Depth = Convert . ToSingle ( reader [ "BasicSite_Depth" ] ) ;
beaconModel . BasicSite_Heading_Angle = Convert . ToSingle ( reader [ "BasicSite_Heading_Angle" ] ) ;
beaconModel . BasicSite_Pitch_Angle = Convert . ToSingle ( reader [ "BasicSite_Pitch_Angle" ] ) ;
beaconModel . BasicSite_Roll_Angle = Convert . ToSingle ( reader [ "BasicSite_Roll_Angle" ] ) ;
beaconModel . BasicSite_Heading_Speed = Convert . ToSingle ( reader [ "BasicSite_Heading_Speed" ] ) ;
beaconModel . BasicSite_Pitch_Speed = Convert . ToSingle ( reader [ "BasicSite_Pitch_Speed" ] ) ;
beaconModel . BasicSite_Roll_Speed = Convert . ToSingle ( reader [ "BasicSite_Roll_Speed" ] ) ;
beaconModel . BasicSite_Forword_A = Convert . ToSingle ( reader [ "BasicSite_Forword_A" ] ) ;
beaconModel . BasicSite_Right_A = Convert . ToSingle ( reader [ "BasicSite_Right_A" ] ) ;
beaconModel . BasicSite_Sky_A = Convert . ToSingle ( reader [ "BasicSite_Sky_A" ] ) ;
beaconModel . Beacon_JD = Convert . ToSingle ( reader [ "Beacon_JD" ] ) ;
beaconModel . Beacon_WD = Convert . ToSingle ( reader [ "Beacon_WD" ] ) ;
beaconModel . Beacon_Depth = Convert . ToSingle ( reader [ "Beacon_Depth" ] ) ;
beaconModel . Beacon_Roll_Angle = Convert . ToSingle ( reader [ "Beacon_Roll_Angle" ] ) ;
beaconModel . Beacon_Pitch_Angle = Convert . ToSingle ( reader [ "Beacon_Pitch_Angle" ] ) ;
beaconModel . Beacon_Heading_Angle = Convert . ToSingle ( reader [ "Beacon_Heading_Angle" ] ) ;
beaconModel . Temp = Convert . ToSingle ( reader [ "Temp" ] ) ;
2024-02-27 09:21:02 +00:00
}
reader . Dispose ( ) ;
2024-03-14 09:27:41 +00:00
return beaconModel ;
2023-12-18 09:31:28 +00:00
}
2024-03-06 09:46:55 +00:00
public void SearchAlarmInfo ( object obj )
{
2024-03-13 07:02:46 +00:00
//清空List中的告警信息
AlarmInfoList . Clear ( ) ;
string sql = "" ;
2024-03-06 09:46:55 +00:00
//查一天的告警
if ( obj . ToString ( ) = = "1" )
{
2024-03-13 07:02:46 +00:00
sql = string . Format ( "select * from alarminfo WHERE RecordTime >= '{0}' AND RecordTime <= '{1}' ORDER by id desc;" , System . DateTime . Today . AddDays ( - 1 ) , System . DateTime . Now ) ;
2024-03-06 09:46:55 +00:00
}
else if ( obj . ToString ( ) = = "2" ) //查询一周的告警
{
2024-03-13 07:02:46 +00:00
sql = string . Format ( "select * from alarminfo WHERE RecordTime >= '{0}' AND RecordTime <= '{1}' ORDER by id desc;" , System . DateTime . Today . AddDays ( - 7 ) , System . DateTime . Now ) ;
2024-03-06 09:46:55 +00:00
}
else if ( obj . ToString ( ) = = "3" ) //查询一个月的告警
{
2024-03-13 07:02:46 +00:00
sql = string . Format ( "select * from alarminfo WHERE RecordTime >= '{0}' AND RecordTime <= '{1}' ORDER by id desc;" , System . DateTime . Today . AddDays ( - 30 ) , System . DateTime . Now ) ;
}
using ( MySqlDataReader reader = DBHelper . ExecuteReader ( sql , 1 ) )
{
while ( reader . Read ( ) )
{
AlarmInfo alarmInfo = new AlarmInfo ( )
{
StationID = Convert . ToInt32 ( reader [ "StationID" ] ) ,
RecordTime = Convert . ToDateTime ( reader [ "RecordTime" ] ) ,
ParaName = reader [ "ParaName" ] . ToString ( ) ,
ParaState = reader [ "ParaState" ] . ToString ( )
} ;
AlarmInfoList . Add ( alarmInfo ) ;
}
2024-03-06 09:46:55 +00:00
}
}
2023-12-18 09:31:28 +00:00
public void DoNavChanged ( object obj )
{
2024-01-19 05:04:06 +00:00
//如果重复点击 就禁止切换
if ( ( string ) obj = = "FirstPageView" | | ( string ) obj = = "BuoyPlatformView" )
{
if ( Title_Name = = ( string ) obj )
return ;
Title_Name = ( string ) obj ;
}
else if ( ( ( string ) obj ) . StartsWith ( "Point" ) | | ( ( string ) obj ) . StartsWith ( "Benchmark" ) )
2023-12-23 03:35:57 +00:00
{
2024-01-22 09:48:13 +00:00
if ( Title_Name = = "AnchorPointView" & & CurrentPage = = ( string ) obj )
2024-01-19 05:04:06 +00:00
return ;
2023-12-23 03:35:57 +00:00
Title_Name = "AnchorPointView" ;
2024-01-22 09:48:13 +00:00
CurrentPage = ( string ) obj ;
2023-12-23 03:35:57 +00:00
}
2024-01-19 05:04:06 +00:00
else
{
return ;
}
2023-12-18 09:31:28 +00:00
//切换页面之前清理一下内存
tools . ClearMemory ( this ) ;
2023-12-23 03:35:57 +00:00
2024-01-17 01:24:32 +00:00
////首先提示加载界面
//Type type = Type.GetType("_20230724_MBJC_upperpc.Views.ProgressView");
//ConstructorInfo cti = type.GetConstructor(System.Type.EmptyTypes);
//FrameworkElement page = (FrameworkElement)cti.Invoke(null);
//this.PageContent = page;
Type type = Type . GetType ( "_20230724_MBJC_upperpc.Views." + Title_Name ) ;
2023-12-18 09:31:28 +00:00
ConstructorInfo cti = type . GetConstructor ( System . Type . EmptyTypes ) ;
FrameworkElement page = ( FrameworkElement ) cti . Invoke ( null ) ;
2024-01-22 05:36:57 +00:00
switch ( obj . ToString ( ) )
{
case "Point1" :
page . DataContext = Beacon1 ;
break ;
case "Point2" :
page . DataContext = Beacon2 ;
break ;
case "Point3" :
page . DataContext = Beacon3 ;
break ;
case "Point4" :
page . DataContext = Beacon4 ;
break ;
case "Point5" :
page . DataContext = Beacon5 ;
break ;
case "Point6" :
page . DataContext = Beacon6 ;
break ;
case "Benchmark1" :
page . DataContext = Beacon7 ;
break ;
case "Benchmark2" :
page . DataContext = Beacon8 ;
break ;
2024-03-13 07:02:46 +00:00
case "BuoyPlatformView" :
page . DataContext = Beacon8 ;
break ;
2024-01-22 05:36:57 +00:00
default : break ;
}
2023-12-18 09:31:28 +00:00
this . PageContent = page ;
}
2024-01-17 01:24:32 +00:00
public void DoConnect ( object o )
{
if ( ( bool ) o )
{
Client = new ClientModel ( ) ;
Client . DoConnect ( socketInfo ) ;
}
else
{
Client . DisConnect ( ) ;
}
}
2024-02-27 09:21:02 +00:00
/// <summary>
/// 查找历史数据
/// </summary>
/// <param name="StartTime"></param>
/// <param name="EndTime"></param>
/// <param name="StationID"></param>
2024-03-06 09:46:55 +00:00
public bool SearchHisData ( DateTime StartTime , DateTime EndTime , int StationID )
2024-02-27 09:21:02 +00:00
{
2024-03-06 09:46:55 +00:00
//每次查询历史数据 就将该历史数据重置
Beacon_Search = new BeaconViewModel ( ) ;
2024-03-13 07:02:46 +00:00
string sql = string . Format ( "SELECT * FROM beaconmodel WHERE RecordTime >= '{0}' AND RecordTime <= '{1}' AND StationID = {2}" , StartTime , EndTime , StationID ) ;
2024-02-27 09:21:02 +00:00
using ( MySqlDataReader reader = DBHelper . ExecuteReader ( sql , 1 ) )
{
while ( reader . Read ( ) )
{
BeaconModel beacon = new BeaconModel ( )
{
ID = Convert . ToInt32 ( reader [ "StationID" ] ) ,
Datetime = Convert . ToDateTime ( reader [ "Datetime" ] ) ,
Ralative_Heading_Angle = Convert . ToSingle ( reader [ "Ralative_Heading_Angle" ] ) ,
Ralative_Pitch_Angle = Convert . ToSingle ( reader [ "Ralative_Pitch_Angle" ] ) ,
Position_Distance = Convert . ToSingle ( reader [ "Position_Distance" ] ) ,
Propagationtime = Convert . ToSingle ( reader [ "Propagationtime" ] ) ,
BasicSite_JD = Convert . ToSingle ( reader [ "BasicSite_JD" ] ) ,
BasicSite_WD = Convert . ToSingle ( reader [ "BasicSite_WD" ] ) ,
BasicSite_Depth = Convert . ToSingle ( reader [ "BasicSite_Depth" ] ) ,
BasicSite_Heading_Angle = Convert . ToSingle ( reader [ "BasicSite_Heading_Angle" ] ) ,
BasicSite_Pitch_Angle = Convert . ToSingle ( reader [ "BasicSite_Pitch_Angle" ] ) ,
BasicSite_Roll_Angle = Convert . ToSingle ( reader [ "BasicSite_Roll_Angle" ] ) ,
BasicSite_Heading_Speed = Convert . ToSingle ( reader [ "BasicSite_Heading_Speed" ] ) ,
BasicSite_Pitch_Speed = Convert . ToSingle ( reader [ "BasicSite_Pitch_Speed" ] ) ,
BasicSite_Roll_Speed = Convert . ToSingle ( reader [ "BasicSite_Roll_Speed" ] ) ,
BasicSite_Forword_A = Convert . ToSingle ( reader [ "BasicSite_Forword_A" ] ) ,
BasicSite_Right_A = Convert . ToSingle ( reader [ "BasicSite_Right_A" ] ) ,
BasicSite_Sky_A = Convert . ToSingle ( reader [ "BasicSite_Sky_A" ] ) ,
Beacon_JD = Convert . ToSingle ( reader [ "Beacon_JD" ] ) ,
Beacon_WD = Convert . ToSingle ( reader [ "Beacon_WD" ] ) ,
Beacon_Depth = Convert . ToSingle ( reader [ "Beacon_Depth" ] ) ,
Beacon_Roll_Angle = Convert . ToSingle ( reader [ "Beacon_Roll_Angle" ] ) ,
Beacon_Pitch_Angle = Convert . ToSingle ( reader [ "Beacon_Pitch_Angle" ] ) ,
Beacon_Heading_Angle = Convert . ToSingle ( reader [ "Beacon_Heading_Angle" ] ) ,
Temp = Convert . ToSingle ( reader [ "Temp" ] )
} ;
2024-03-06 09:46:55 +00:00
Beacon_Search . BeaconList . Add ( beacon ) ;
Beacon_Search . BeaconList_Count = Beacon_Search . BeaconList . Count - 1 ;
}
}
//如果查询的数据为0 那么就不切换为历史数据
if ( Beacon_Search . BeaconList . Count = = 0 )
return false ;
//默认选中第一条数据
Beacon_Search . Beacon = Beacon_Search . BeaconList [ 0 ] ;
Beacon_Search . SelectedTime = Beacon_Search . BeaconList [ 0 ] . Datetime . ToString ( "yyyy-MM-dd HH:mm:ss" ) ;
if ( StationID = = int . Parse ( tools . GetAppSetting ( "Beacon7" ) ) | | StationID = = int . Parse ( tools . GetAppSetting ( "Beacon8" ) ) )
Beacon_Search . Model_Visibility = 2 ;
return true ;
2024-02-27 09:21:02 +00:00
}
2023-12-18 09:31:28 +00:00
}
}