2023-02-03 00:31:48 +00:00
using MonitoringTechnology.Common ;
using MonitoringTechnology.DataAccess ;
using MonitoringTechnology.Models ;
using MonitoringTechnology.ViewModels ;
using MonitoringTechnology.Views ;
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
using System.Text ;
using System.Threading ;
using System.Threading.Tasks ;
using System.Windows.Forms ;
using System.Windows.Markup ;
using System.Windows.Threading ;
using Windows.Devices.Bluetooth ;
using Windows.Devices.Bluetooth.Advertisement ;
using Windows.Devices.Bluetooth.GenericAttributeProfile ;
using Windows.Devices.Enumeration ;
using Windows.Foundation ;
using Windows.Storage.Streams ;
using Windows.UI.Xaml.Controls ;
using Windows.Web.Syndication ;
using CharacteristicDictionary = System . Collections . Generic . Dictionary < string , System . Collections . Generic . Dictionary < string , Windows . Devices . Bluetooth . GenericAttributeProfile . GattCharacteristic > > ;
//线程安全的版本,考虑到连接后不会更改。还是使用性能更好的 Dictionary
//using ConcurrentGattDictionary = System.Collections.Concurrent.ConcurrentDictionary<string, System.Collections.Concurrent.ConcurrentDictionary<string, Windows.Devices.Bluetooth.GenericAttributeProfile.GattCharacteristic>>;
namespace MonitoringTechnology.Ble
{
public class BleDevice : ObservableObject
{
private readonly DeviceInformation _deviceInfo ;
private BluetoothLEDevice _device ;
private GattCharacteristic CharacteristicWrite ;
private GattCharacteristic CharacteristicNotify ;
private string _mac ;
private string _name ;
private byte [ ] _mfrData ;
private int _rssi ;
private readonly List < GattDeviceService > GattServices = new List < GattDeviceService > ( ) ;
private readonly CharacteristicDictionary GattCharacteristics = new CharacteristicDictionary ( ) ;
public static FileModel FileModel { get ; set ; } = new FileModel ( ) ;
2023-03-11 00:34:39 +00:00
public event TypedEventHandler < GattCharacteristic , byte [ ] > CharacteristicValueChanged ; //数据解析 -- 报文返回
public event TypedEventHandler < GattCharacteristic , string > ReceiveValueChanged ; //数据解析 --自定义数据返回
public event TypedEventHandler < BleDevice , bool > ConnectionStateChanged ; //连接状态变化
2023-02-03 00:31:48 +00:00
public static MonitoringSensorModel monitoringSensorModel { get ; set ; } = new MonitoringSensorModel ( ) ;
static LocalDataAccess localDataAccess = new LocalDataAccess ( ) ;
public static SystemOperationView systemOperationView = new SystemOperationView ( ) ;
#region 蓝 牙 部 分
public BleDevice ( ) { }
public BleDevice ( DeviceInformation deviceInfo )
{
_deviceInfo = deviceInfo ;
_mac = deviceInfo . Mac ( ) ;
_name = deviceInfo . Name ;
Log . Info ( "################# BleDevice #################" ) ;
foreach ( KeyValuePair < string , object > item in deviceInfo . Properties )
{
Log . Info ( $"{item.Key}={item.Value}" ) ;
}
}
public void Update ( BleDevice other )
{
Name = other . Name ;
MfrData = other . MfrData ;
RSSI = other . RSSI ;
}
public void Update ( BluetoothLEAdvertisementReceivedEventArgs args )
{
//通过方法属性更新是为了通知UI刷新
string localName = args . LocalName ( ) ;
//有可能是广播回应包, 不带LocalName, 防止显示不出真实的名称
if ( ! string . IsNullOrEmpty ( localName ) )
{
Name = localName ;
}
byte [ ] mfrData = args . MfrData ( ) ;
if ( mfrData ! = null )
{
MfrData = args . MfrData ( ) ;
}
RSSI = args . RawSignalStrengthInDBm ;
}
public byte [ ] MfrData
{
get = > _mfrData ;
set
{
Set ( ref _mfrData , value ) ;
OnPropertyChanged ( nameof ( MfrDataHex ) ) ;
}
}
public string MfrDataHex = > _mfrData ? . ToHex ( ) ;
public int RSSI
{
get = > - _rssi ;
set = > Set ( ref _rssi , value ) ;
}
public string Name
{
get = > string . IsNullOrEmpty ( _name ) ? "Unknown Device" : _name ;
set = > Set ( ref _name , value ) ;
}
//System.Devices.Aep.DeviceAddress
public string Mac = > _mac ;
public string Id = > _deviceInfo . Id ;
public bool IsConnected = > _device ! = null & & _device . ConnectionStatus = = BluetoothConnectionStatus . Connected ;
/// <summary>
/// 连接设备
/// </summary>
public async void Connect ( )
{
//下面一行是官方文档的写法,貌似版本低所以报错
//BluetoothLEDevice bluetoothLEDevice = await BluetoothLEDevice.FromIdAsync(Id);
//使用自定义的拓展方法 GetResultAsync()
_device = await BluetoothLEDevice . FromIdAsync ( Id ) . GetResultAsync ( ) ;
//监听连接状态
_device . ConnectionStatusChanged + = _device_ConnectionStatusChanged ;
Log . Info ( $"{DateTime.Now:HH:mm:ss.fff} - 开始连接: " + _device ) ;
DiscoverServices ( ) ;
}
private void _device_ConnectionStatusChanged ( BluetoothLEDevice sender , object args )
{
Log . Info ( "_device_ConnectionStatusChanged() - ConnectionStatus=" + sender . ConnectionStatus ) ;
if ( sender . ConnectionStatus = = BluetoothConnectionStatus . Connected )
{
}
else
{
ConnectionStateChanged ? . Invoke ( this , false ) ;
}
}
private async Task < bool > EnableNotification ( GattCharacteristic characteristic )
{
GattCommunicationStatus result = await characteristic
. WriteClientCharacteristicConfigurationDescriptorAsync (
GattClientCharacteristicConfigurationDescriptorValue . Notify
) . GetResultAsync ( ) ;
return result = = GattCommunicationStatus . Success ;
}
public async Task < bool > Write ( byte [ ] data )
{
if ( CharacteristicWrite = = null )
{
return false ;
}
DataWriter writer = new DataWriter ( ) ;
writer . WriteBytes ( data ) ;
GattCommunicationStatus result = await CharacteristicWrite
. WriteValueAsync ( writer . DetachBuffer ( ) , GattWriteOption . WriteWithoutResponse )
. GetResultAsync ( ) ;
return result = = GattCommunicationStatus . Success ;
}
public async Task < byte [ ] > Read ( string serviceUuid , string characteristicUuid )
{
GattCharacteristic characteristic = GattCharacteristics [ serviceUuid ] ? [ characteristicUuid ] ;
if ( characteristic ! = null )
{
GattReadResult result = await characteristic . ReadValueAsync ( ) . GetResultAsync ( ) ;
if ( result . Status = = GattCommunicationStatus . Success )
{
byte [ ] data = result . Value . ToBytes ( ) ;
Log . Info ( $"读取数据 <<< {data.ToHex()} / {Encoding.UTF8.GetString(data)}" ) ;
return data ;
}
}
else
{
Log . Info ( $"未发现该特征: {serviceUuid} / {characteristicUuid}" ) ;
}
return null ;
}
public void Disconnect ( )
{
Log . Info ( "Disconnect()" ) ;
//必须释放服务才能断开连接
foreach ( GattDeviceService service in GattServices )
{
service . Dispose ( ) ;
}
_device ? . Dispose ( ) ;
//_device = null;//赋值为 null 会导致收不到断线的通知
//ConnectionStateChanged?.Invoke(this, false);
}
public override string ToString ( )
{
return Name + "\r\n" + Mac ;
}
private async void DiscoverServices ( )
{
GattServices . Clear ( ) ;
GattCharacteristics . Clear ( ) ;
GattDeviceServicesResult servicesResult = await _device . GetGattServicesAsync ( ) . GetResultAsync ( ) ;
2023-03-30 01:10:06 +00:00
2023-02-03 00:31:48 +00:00
if ( servicesResult . Status = = GattCommunicationStatus . Success )
{
foreach ( GattDeviceService service in servicesResult . Services )
{
//存储该服务下的特征值
Dictionary < string , GattCharacteristic > characteristics = new Dictionary < string , GattCharacteristic > ( ) ;
string serviceUuid = service . Uuid . ToString ( ) ;
Log . Info ( $"Gatt Service: {serviceUuid}" ) ;
GattCharacteristicsResult characteristicsResult = await service . GetCharacteristicsAsync ( ) . GetResultAsync ( ) ;
if ( characteristicsResult . Status = = GattCommunicationStatus . Success )
{
foreach ( GattCharacteristic characteristic in characteristicsResult . Characteristics )
{
//存储该特征值
characteristics [ characteristic . Uuid . ToString ( ) ] = characteristic ;
string characteristicUuid = characteristic . Uuid . ToString ( ) ;
Log . Info ( $"\tGatt Characteristic: {characteristicUuid}" ) ;
if ( serviceUuid = = Uuids . SERVICE )
{
if ( characteristicUuid = = Uuids . WRITE )
{
CharacteristicWrite = characteristic ;
}
else if ( characteristicUuid = = Uuids . NOTIFY )
{
CharacteristicNotify = characteristic ;
}
}
}
}
GattCharacteristics [ service . Uuid . ToString ( ) ] = characteristics ;
GattServices . Add ( service ) ;
}
//订阅通知
if ( CharacteristicNotify ! = null )
{
//需得用一个全局变量保持对 ValueChanged 回调函数的引用,不然收不到数据
CharacteristicNotify . ValueChanged + = CharacteristicNotify_ValueChanged ;
bool enabled = await EnableNotification ( CharacteristicNotify ) ;
if ( enabled )
{
Log . Info ( "订阅通知成功!" + CharacteristicNotify . Uuid ) ;
}
2023-03-30 01:10:06 +00:00
2023-02-03 00:31:48 +00:00
}
ConnectionStateChanged ? . Invoke ( this , true ) ;
}
2023-03-30 01:10:06 +00:00
else
{
string _message = "连接失败,请重新尝试连接!" ;
ReceiveValueChangedChanged ( null , _message ) ;
}
2023-02-03 00:31:48 +00:00
}
#endregion
private static string _localtime = > $"{DateTime.Now: yyyy-MM-dd HH:mm:ss.fff}" ;
/// <summary>
2023-03-17 07:06:36 +00:00
/// 收到数据 ----报文返回
2023-02-03 00:31:48 +00:00
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void CharacteristicNotify_ValueChanged ( GattCharacteristic sender , GattValueChangedEventArgs args )
{
byte [ ] data = args . CharacteristicValue . ToBytes ( ) ; //十进制
//Log.Info($"收到数据 <<< {data.ToHex()}");
//Log.Info($"收到数据 <<< {data}");
CharacteristicValueChanged ? . Invoke ( sender , data ) ;
ParsingData ( data . ToList ( ) ) ;
}
2023-03-11 00:34:39 +00:00
/// <summary>
/// 自定义数据接收--数据解析
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void ReceiveValueChangedChanged ( GattCharacteristic sender , string data )
{
ReceiveValueChanged ? . Invoke ( sender , data ) ;
}
2023-02-03 00:31:48 +00:00
/// <summary>
/// 定义一个委托,用于传值老的版本号
/// </summary>
Action < byte [ ] > func = new Action < byte [ ] > ( SystemOperationView . Setbbh ) ;
2023-03-11 00:34:39 +00:00
2023-02-03 00:31:48 +00:00
byte [ ] bbh = new byte [ 4 ] { 0x00 , 0x00 , 0x00 , 0x00 } ;
/// <summary>
/// 解析数据
/// </summary>
/// <param name="byteList"></param>
private void ParsingData ( List < byte > byteList )
{
2023-03-11 00:34:39 +00:00
string _message = "" ;
2023-03-17 07:06:36 +00:00
string _data = "" ;
2023-03-11 00:34:39 +00:00
2023-02-03 00:31:48 +00:00
//校验当前List是否为空或者长度
if ( byteList = = null | | byteList . Count = = 0 )
return ;
string rec_16 = null ;
for ( int i = 0 ; i < byteList . Count ; i + + )
{
rec_16 + = byteList [ i ] . ToString ( "X2" ) ; //16进制显示
}
//string _message = tools.HexStringToString(rec_16, Encoding.Default).Replace("\r\n", "");
//以txt文档的形式存储接收到的数据
tools . AddLgoToTXT ( FileModel . File_Name + FileModel . File_Time . ToString ( "yyyy-MM-dd" ) + ".txt" , FileModel . File_Path + FileModel . File_Time . ToString ( "yyyy-MM-dd" ) + @"\" , _localtime + ": " + rec_16 + "\r\n" ) ;
//获取当前系统时间
DateTime time_DataBase = DateTime . Now ;
Random ran = new Random ( ) ;
//帧头
byte [ ] _header = new byte [ ] { 0xEF , 0xFE , 0xFE , 0xEF } ;
//帧尾
byte _end = 0x16 ;
//长度校验
int _len = 0 ;
//校验
int _check = 0 ;
//功能码
byte _fun_code = 0 ;
//类型
byte _kind = 0 ;
//地址码
byte _adress = 0 ;
//数据类型
byte _data_type = 0 ;
//校验帧头
if ( byteList [ 0 ] ! = _header [ 0 ] & & byteList [ 0 ] ! = _header [ 2 ] )
return ;
if ( byteList [ 1 ] ! = _header [ 1 ] & & byteList [ 1 ] ! = _header [ 3 ] )
return ;
//校验帧尾
if ( byteList [ byteList . Count - 1 ] ! = _end )
return ;
//获取检验长度
_len = Convert . ToInt32 ( byteList [ 2 ] . ToString ( "X2" ) + byteList [ 3 ] . ToString ( "X2" ) , 16 ) ;
//获取检验的校验和
for ( int i = 0 ; i < byteList . Count - 2 ; i + + )
{
_check + = byteList [ i ] ;
}
_check = _check & 0xFF ;
//长度 及 累加和校验
if ( _len ! = byteList . Count - 4 | | byteList [ byteList . Count - 2 ] ! = _check )
return ;
_len = 0 ;
_check = 0 ;
//地址码
_adress = byteList [ 4 ] ;
//功能码
_fun_code = byteList [ 5 ] ;
//类型
_kind = byteList [ 6 ] ;
System . DateTime startTime = TimeZone . CurrentTimeZone . ToLocalTime ( new System . DateTime ( 1970 , 1 , 1 ) ) ; // 当地时区
DateTime dt = new DateTime ( ) ;
switch ( _fun_code )
{
2023-03-11 00:34:39 +00:00
//数据解析 ---实时数据
2023-02-03 00:31:48 +00:00
case 0x02 :
switch ( _kind )
{
//智能状态监控传感器数据
case 50 :
//UTC时间
if ( ( int ) byteList [ 8 ] = = 0 & & ( int ) byteList [ 9 ] = = 0 & & ( int ) byteList [ 10 ] = = 0 & & ( int ) byteList [ 11 ] = = 0 )
{
dt = DateTime . Now ;
}
else
{
dt = startTime . AddSeconds ( Convert . ToInt32 ( byteList [ 8 ] . ToString ( "X2" ) + byteList [ 9 ] . ToString ( "X2" ) + byteList [ 10 ] . ToString ( "X2" ) + byteList [ 11 ] . ToString ( "X2" ) , 16 ) ) ;
}
monitoringSensorModel . f_CreateDate = time_DataBase ;
monitoringSensorModel . _datetime_Sensor = dt ;
//温度 12,13,14,15
monitoringSensorModel . _Tem = tools . bytetofloat ( byteList , 12 ) ;
//湿度 16,17,18,19
monitoringSensorModel . _Hum = tools . bytetofloat ( byteList , 16 ) ;
//气压 20,21,22, 23
monitoringSensorModel . _pressure = tools . bytetofloat ( byteList , 20 ) ;
//加速度X 24,25,26,27
monitoringSensorModel . _acceleration_X = tools . bytetofloat ( byteList , 24 ) ;
//加速度Y 28,29,30,31
monitoringSensorModel . _acceleration_Y = tools . bytetofloat ( byteList , 28 ) ;
//加速度Z 32,33,34,35
monitoringSensorModel . _acceleration_Z = tools . bytetofloat ( byteList , 32 ) ;
//角速度X 36,37,38,39
monitoringSensorModel . _angularVelocity_X = tools . bytetofloat ( byteList , 36 ) ;
//角速度Y 40,41,42,43
monitoringSensorModel . _angularVelocity_Y = tools . bytetofloat ( byteList , 40 ) ;
//角速度Z 44,45,46,47
monitoringSensorModel . _angularVelocity_Z = tools . bytetofloat ( byteList , 44 ) ;
//角度X 48,49,50,51
monitoringSensorModel . degrees_x = tools . bytetofloat ( byteList , 48 ) ;
//角度Y 52,53,54,55
monitoringSensorModel . degrees_y = tools . bytetofloat ( byteList , 52 ) ;
//角度Z 56,57,58,59
monitoringSensorModel . degrees_z = tools . bytetofloat ( byteList , 56 ) ;
2023-03-17 07:06:36 +00:00
//电池电压 60,61,62,63
monitoringSensorModel . _batteryVoltage = tools . bytetofloat ( byteList , 60 ) ;
//充电电流 64,65,66,67
monitoringSensorModel . _ChargingCurrent = tools . bytetofloat ( byteList , 64 ) ;
//充电状态 68,69
string ctl = byteList [ 68 ] . ToString ( "X2" ) + byteList [ 69 ] . ToString ( "X2" ) ;
string ctl_2 = null ;
for ( int i = 0 ; i < ctl . Length ; i + + )
{
ctl_2 + = tools . HexString2BinString ( ctl . Substring ( i , 1 ) ) ;
}
ctl_2 = ctl_2 . Replace ( " " , "" ) ;
if ( ctl_2 . Length = = 16 )
{
//CHRG==0 && STDBY== 1,表示正在充电
if ( ctl_2 . Substring ( 15 , 1 ) . Equals ( "0" ) & & ctl_2 . Substring ( 14 , 1 ) . Equals ( "1" ) )
{
monitoringSensorModel . _State = "正在充电" ;
}
//CHRG==1 && STDBY== 0, 表示充满
else if ( ctl_2 . Substring ( 15 , 1 ) . Equals ( "1" ) & & ctl_2 . Substring ( 14 , 1 ) . Equals ( "0" ) )
{
monitoringSensorModel . _State = "充满" ;
}
//其他表示未充电
else
{
monitoringSensorModel . _State = "未充电" ;
}
}
2023-02-03 00:31:48 +00:00
///定时去刷新首页电气数据和故障信息
ThreadPool . QueueUserWorkItem ( delegate
{
2023-03-30 01:10:06 +00:00
SynchronizationContext . SetSynchronizationContext ( new
DispatcherSynchronizationContext ( System . Windows . Application . Current . Dispatcher ) ) ;
SynchronizationContext . Current . Post ( pl = >
{
if ( FirstPageView . firstPageView . ElectricalEnvironmentalList . Count > = 30 )
FirstPageView . firstPageView . ElectricalEnvironmentalList . RemoveAt ( 0 ) ;
FirstPageView . firstPageView . ElectricalEnvironmentalList . Add ( new ElectricalEnvironmentalModel ( )
{
F_CreateDate = ( DateTime ) monitoringSensorModel . f_CreateDate ,
F_DatetimeSensor = ( DateTime ) monitoringSensorModel . _datetime_Sensor ,
F_Tem = ( float ) monitoringSensorModel . _Tem ,
F_Hum = ( float ) monitoringSensorModel . _Hum ,
F_Pressure = ( float ) monitoringSensorModel . _pressure
} ) ;
for ( int i = 0 ; i < FirstPageView . firstPageView . ElectricalEnvironmentalList . Count ; i + + )
{
FirstPageView . firstPageView . ElectricalEnvironmentalList [ i ] . F_SerialNo = i + 1 ;
}
////告警信息
////if (FirstPageView.firstPageView.AlarmItemModelList.Count >= 30)
//// FirstPageView.firstPageView.AlarmItemModelList.RemoveAt(0);
////FirstPageView.firstPageView.AlarmItemModelList.Add(new AlarmItemModel()
////{
//// F_CreateDate =
//// F_Time =
//// F_Message =
//// F_Len =
////});
} , null ) ;
2023-02-03 00:31:48 +00:00
} ) ;
//姿态数据刷新
AttitudeDisplayView . monitoringSensor . F_DatetimeSensor = monitoringSensorModel . _datetime_Sensor ;
AttitudeDisplayView . monitoringSensor . F_AccelerationX = monitoringSensorModel . _acceleration_X ;
AttitudeDisplayView . monitoringSensor . F_AccelerationY = monitoringSensorModel . _acceleration_Y ;
AttitudeDisplayView . monitoringSensor . F_AccelerationZ = monitoringSensorModel . _acceleration_Z ;
AttitudeDisplayView . monitoringSensor . F_AngularVelocityX = monitoringSensorModel . _angularVelocity_X ;
AttitudeDisplayView . monitoringSensor . F_AngularVelocityY = monitoringSensorModel . _angularVelocity_Y ;
AttitudeDisplayView . monitoringSensor . F_AngularVelocityZ = monitoringSensorModel . _angularVelocity_Z ;
AttitudeDisplayView . monitoringSensor . F_DegreesX = monitoringSensorModel . degrees_x ;
AttitudeDisplayView . monitoringSensor . F_DegreesY = monitoringSensorModel . degrees_y ;
AttitudeDisplayView . monitoringSensor . F_DegreesZ = monitoringSensorModel . degrees_z ;
2023-03-17 07:06:36 +00:00
//电池电压
MainWindow . mainViewModel . F_BatteryVoltage = monitoringSensorModel . _batteryVoltage ;
//充电电流
MainWindow . mainViewModel . F_ChargingCurrent = monitoringSensorModel . _ChargingCurrent ;
//充电状态
MainWindow . mainViewModel . F_State = monitoringSensorModel . _State ;
2023-03-11 00:34:39 +00:00
2023-03-17 07:06:36 +00:00
localDataAccess . write ( "INSERT INTO monitoringsensormodel(F_CreateDate,F_DatetimeSensor,F_Tem,F_Hum,F_Pressure,F_AccelerationX,F_AccelerationY,F_AccelerationZ,F_AngularVelocityX,F_AngularVelocityY,F_AngularVelocityZ,F_DegreesX,F_DegreesY,F_DegreesZ,F_BatteryVoltage,F_ChargingCurrent,F_State) VALUES('" + monitoringSensorModel . f_CreateDate + "','" + monitoringSensorModel . _datetime_Sensor + "','" + monitoringSensorModel . _Tem + "','" + monitoringSensorModel . _Hum + "','" + monitoringSensorModel . _pressure + "','" + monitoringSensorModel . _acceleration_X + "','" + monitoringSensorModel . _acceleration_Y + "','" + monitoringSensorModel . _acceleration_Z + "','" + monitoringSensorModel . _angularVelocity_X + "','" + monitoringSensorModel . _angularVelocity_Y + "','" + monitoringSensorModel . _angularVelocity_Z + "','" + monitoringSensorModel . degrees_x + "','" + monitoringSensorModel . degrees_y + "','" + monitoringSensorModel . degrees_z + "','" + monitoringSensorModel . _batteryVoltage + "','" + monitoringSensorModel . _ChargingCurrent + "','" + monitoringSensorModel . _State + "');" ) ;
2023-03-30 01:10:06 +00:00
_message = "状态数据获取成功: 数据上传时间: " + monitoringSensorModel . _datetime_Sensor + " 温度:" + monitoringSensorModel . _Tem + "℃ 湿度:" + monitoringSensorModel . _Hum + "% 气压:" + monitoringSensorModel . _pressure + "KPa 俯仰角:" + monitoringSensorModel . _acceleration_X + "° 横滚角:" + monitoringSensorModel . _acceleration_Y + "° 偏航角:" + monitoringSensorModel . _acceleration_Z + "° 角速度X: " + monitoringSensorModel . _angularVelocity_X + "m/s² 角速度Y: " + monitoringSensorModel . _angularVelocity_Y + "m/s² 角速度Z: " + monitoringSensorModel . _angularVelocity_Z + "m/s² 角度X: " + monitoringSensorModel . degrees_x + "rad/s 角度Y: " + monitoringSensorModel . degrees_y + "rad/s 角度Z: " + monitoringSensorModel . degrees_z + "rad/s 电池电压:" + monitoringSensorModel . _batteryVoltage + "V 充电电流:" + monitoringSensorModel . _ChargingCurrent + "A 充电状态:" + monitoringSensorModel . _State ;
2023-03-11 00:34:39 +00:00
ReceiveValueChangedChanged ( null , _message ) ;
2023-02-03 00:31:48 +00:00
break ;
//主系统数据
case 51 :
break ;
default :
break ;
}
break ;
//升级文件请求回复
case 0x6E :
switch ( byteList [ 7 ] )
{
case 0x00 :
_message + = "获取成功" ;
break ;
case 0x01 :
_message + = "空" ;
break ;
case 0x02 :
_message + = "设备忙" ;
break ;
default :
break ;
}
break ;
//版本号查询回复
case 0x6F :
bbh [ 0 ] = byteList [ 7 ] ;
bbh [ 1 ] = byteList [ 8 ] ;
bbh [ 2 ] = byteList [ 9 ] ;
bbh [ 3 ] = byteList [ 10 ] ;
func ( bbh ) ;
switch ( byteList [ 11 ] )
{
case 0x00 :
2023-03-11 00:34:39 +00:00
_message + = "版本查询:获取成功" ;
2023-02-03 00:31:48 +00:00
break ;
case 0x01 :
2023-03-11 00:34:39 +00:00
_message + = "版本查询:空" ;
ReceiveValueChangedChanged ( null , _message ) ;
2023-02-03 00:31:48 +00:00
break ;
case 0x02 :
2023-03-11 00:34:39 +00:00
_message + = "版本查询:设备忙" ;
ReceiveValueChangedChanged ( null , _message ) ;
2023-02-03 00:31:48 +00:00
break ;
default :
break ;
}
//显示版本号
SystemOperationView . systemOperationViewModel . VersionQueryData = ( ( int ) bbh [ 0 ] ) . ToString ( ) + "." + ( ( int ) bbh [ 1 ] ) . ToString ( ) + "." + ( ( int ) bbh [ 2 ] ) . ToString ( ) + "." + ( ( int ) bbh [ 3 ] ) . ToString ( ) ;
2023-03-11 00:34:39 +00:00
_message + = " 版本号:" + SystemOperationView . systemOperationViewModel . VersionQueryData ;
ReceiveValueChangedChanged ( null , _message ) ;
2023-02-03 00:31:48 +00:00
break ;
2023-03-11 00:34:39 +00:00
2023-02-03 00:31:48 +00:00
//设备回复文件数据指令
case 0x66 :
int _start = Convert . ToInt32 ( byteList [ 6 ] . ToString ( "X2" ) + byteList [ 7 ] . ToString ( "X2" ) + byteList [ 8 ] . ToString ( "X2" ) + byteList [ 9 ] . ToString ( "X2" ) , 16 ) ;
int _Length = Convert . ToInt32 ( byteList [ 10 ] . ToString ( "X2" ) + byteList [ 11 ] . ToString ( "X2" ) , 16 ) ;
systemOperationView . _send ( _start , _Length ) ;
break ;
2023-03-11 00:34:39 +00:00
//校时
case 0x06 :
//UTC时间
dt = startTime . AddSeconds ( Convert . ToInt32 ( byteList [ 6 ] . ToString ( "X2" ) + byteList [ 7 ] . ToString ( "X2" ) + byteList [ 8 ] . ToString ( "X2" ) + byteList [ 9 ] . ToString ( "X2" ) , 16 ) ) ;
if ( ( time_DataBase - dt ) . TotalMinutes < 60 )
{
_message = "系统校时成功" ;
ReceiveValueChangedChanged ( null , _message ) ;
}
else
{
_message = "系统校时失败" ;
ReceiveValueChangedChanged ( null , _message ) ;
}
2023-03-11 04:53:23 +00:00
break ;
//擦除历史数据功能
case 0x08 :
switch ( byteList [ 6 ] )
{
case 0x00 :
_message + = "擦除完成" ;
ReceiveValueChangedChanged ( null , _message ) ;
break ;
case 0x01 :
_message + = "等待擦除" ;
ReceiveValueChangedChanged ( null , _message ) ;
break ;
case 0x02 :
_message + = "擦除失败" ;
ReceiveValueChangedChanged ( null , _message ) ;
break ;
default :
break ;
}
break ;
//查询历史数据
case 0x04 :
//UTC时间
if ( ( int ) byteList [ 6 ] = = 0 & & ( int ) byteList [ 7 ] = = 0 & & ( int ) byteList [ 8 ] = = 0 & & ( int ) byteList [ 9 ] = = 0 )
{
dt = DateTime . Now ;
}
else
{
dt = startTime . AddSeconds ( Convert . ToInt32 ( byteList [ 6 ] . ToString ( "X2" ) + byteList [ 7 ] . ToString ( "X2" ) + byteList [ 8 ] . ToString ( "X2" ) + byteList [ 9 ] . ToString ( "X2" ) , 16 ) ) ;
}
monitoringSensorModel . f_CreateDate = time_DataBase ;
monitoringSensorModel . _datetime_Sensor = dt ;
//温度 10,11,12,13
monitoringSensorModel . _Tem = tools . bytetofloat ( byteList , 10 ) ;
//湿度 14,15,16,17
monitoringSensorModel . _Hum = tools . bytetofloat ( byteList , 14 ) ;
//气压 18,19,20,21
monitoringSensorModel . _pressure = tools . bytetofloat ( byteList , 18 ) ;
//加速度X 22,23,24,25
monitoringSensorModel . _acceleration_X = tools . bytetofloat ( byteList , 22 ) ;
//加速度Y 26,27,28,29
monitoringSensorModel . _acceleration_Y = tools . bytetofloat ( byteList , 26 ) ;
//加速度Z 30,31,32,33
monitoringSensorModel . _acceleration_Z = tools . bytetofloat ( byteList , 30 ) ;
//角速度X 34,35,36,37
monitoringSensorModel . _angularVelocity_X = tools . bytetofloat ( byteList , 34 ) ;
//角速度Y 38,39,40,41
monitoringSensorModel . _angularVelocity_Y = tools . bytetofloat ( byteList , 38 ) ;
//角速度Z 42,43,44,45
monitoringSensorModel . _angularVelocity_Z = tools . bytetofloat ( byteList , 42 ) ;
//角度X 46,47,48,49
monitoringSensorModel . degrees_x = tools . bytetofloat ( byteList , 46 ) ;
//角度Y 50,51,52,53
monitoringSensorModel . degrees_y = tools . bytetofloat ( byteList , 50 ) ;
//角度Z 54,55,56,57
monitoringSensorModel . degrees_z = tools . bytetofloat ( byteList , 54 ) ;
2023-03-17 07:06:36 +00:00
//电池电压 58,59.60,61
monitoringSensorModel . _batteryVoltage = tools . bytetofloat ( byteList , 58 ) ;
2023-03-11 04:53:23 +00:00
2023-03-17 07:06:36 +00:00
//充电电流 62,63,64,65
monitoringSensorModel . _ChargingCurrent = tools . bytetofloat ( byteList , 62 ) ;
2023-03-11 04:53:23 +00:00
2023-03-17 07:06:36 +00:00
//充电状态 66,67
string ct2 = byteList [ 66 ] . ToString ( "X2" ) + byteList [ 67 ] . ToString ( "X2" ) ;
string ct2_2 = null ;
for ( int i = 0 ; i < ct2 . Length ; i + + )
{
ct2_2 + = tools . HexString2BinString ( ct2 . Substring ( i , 1 ) ) ;
}
ct2_2 = ct2_2 . Replace ( " " , "" ) ;
2023-03-11 04:53:23 +00:00
2023-03-17 07:06:36 +00:00
if ( ct2_2 . Length = = 16 )
{
//CHRG==0 && STDBY== 1,表示正在充电
if ( ct2_2 . Substring ( 15 , 1 ) . Equals ( "0" ) & & ct2_2 . Substring ( 14 , 1 ) . Equals ( "1" ) )
{
monitoringSensorModel . _State = "正在充电" ;
}
//CHRG==1 && STDBY== 0, 表示充满
else if ( ct2_2 . Substring ( 15 , 1 ) . Equals ( "1" ) & & ct2_2 . Substring ( 14 , 1 ) . Equals ( "0" ) )
{
monitoringSensorModel . _State = "充满" ;
}
//其他表示未充电
else
{
monitoringSensorModel . _State = "未充电" ;
}
}
//温度-主系统 68,69
monitoringSensorModel . tem = tools . bytetoInt ( byteList , 68 ) / ( float ) 100 ;
2023-03-11 04:53:23 +00:00
2023-03-17 07:06:36 +00:00
//湿度-主系统 70,71
monitoringSensorModel . hum = tools . bytetoUInt ( byteList , 70 ) / ( float ) 100 ;
2023-03-12 22:44:49 +00:00
2023-03-17 07:06:36 +00:00
//气压 -主系统 72,73
monitoringSensorModel . preeure = tools . bytetoUInt ( byteList , 72 ) ;
2023-03-12 22:44:49 +00:00
2023-03-17 07:06:36 +00:00
//电压1 -主系统 74,75
monitoringSensorModel . voltage1 = tools . bytetoUInt ( byteList , 74 ) / ( float ) 100 ;
2023-03-12 22:44:49 +00:00
2023-03-17 07:06:36 +00:00
//电压2 -主系统 76,77
monitoringSensorModel . voltage2 = tools . bytetoUInt ( byteList , 76 ) / ( float ) 100 ;
2023-03-12 22:44:49 +00:00
2023-03-17 07:06:36 +00:00
//电压3 -主系统 78,79
monitoringSensorModel . voltage3 = tools . bytetoUInt ( byteList , 78 ) / ( float ) 100 ;
2023-03-12 22:44:49 +00:00
2023-03-17 07:06:36 +00:00
//电压4 -主系统 80,81
monitoringSensorModel . voltage4 = tools . bytetoUInt ( byteList , 80 ) / ( float ) 100 ;
2023-03-12 22:44:49 +00:00
2023-03-17 07:06:36 +00:00
//电压5 -主系统 82,83
monitoringSensorModel . voltage5 = tools . bytetoUInt ( byteList , 82 ) / ( float ) 100 ;
2023-03-12 22:44:49 +00:00
2023-03-17 07:06:36 +00:00
//电压6 -主系统 84,85
monitoringSensorModel . voltage6 = tools . bytetoUInt ( byteList , 84 ) / ( float ) 100 ;
2023-03-12 22:44:49 +00:00
2023-03-17 07:06:36 +00:00
//电压7 -主系统 86,87
monitoringSensorModel . voltage7 = tools . bytetoUInt ( byteList , 86 ) / ( float ) 100 ;
2023-03-12 22:44:49 +00:00
2023-03-17 07:06:36 +00:00
//电压8 -主系统 88,89
monitoringSensorModel . voltage8 = tools . bytetoUInt ( byteList , 88 ) / ( float ) 100 ;
2023-03-12 22:44:49 +00:00
2023-03-17 07:06:36 +00:00
//电压9 -主系统 90,91
monitoringSensorModel . voltage9 = tools . bytetoUInt ( byteList , 90 ) / ( float ) 100 ;
2023-03-12 22:44:49 +00:00
2023-03-17 07:06:36 +00:00
//电压10 -主系统 92,93
monitoringSensorModel . voltage10 = tools . bytetoUInt ( byteList , 92 ) / ( float ) 100 ;
2023-03-12 22:44:49 +00:00
2023-03-17 07:06:36 +00:00
//电流1 -主系统 94,95
monitoringSensorModel . current1 = tools . bytetoUInt ( byteList , 94 ) / ( float ) 100 ;
2023-03-12 22:44:49 +00:00
2023-03-17 07:06:36 +00:00
//电流2 -主系统 96,97
monitoringSensorModel . current2 = tools . bytetoUInt ( byteList , 96 ) / ( float ) 100 ;
2023-03-12 22:44:49 +00:00
2023-03-17 07:06:36 +00:00
//电流3 -主系统 98,99
monitoringSensorModel . current3 = tools . bytetoUInt ( byteList , 98 ) / ( float ) 100 ;
2023-03-12 22:44:49 +00:00
2023-03-17 07:06:36 +00:00
//电流4 -主系统 100,101
monitoringSensorModel . current4 = tools . bytetoUInt ( byteList , 100 ) / ( float ) 100 ;
2023-03-12 22:44:49 +00:00
2023-03-17 07:06:36 +00:00
//电流5 -主系统 102,103
monitoringSensorModel . current5 = tools . bytetoUInt ( byteList , 102 ) / ( float ) 100 ;
2023-03-12 22:44:49 +00:00
2023-03-17 07:06:36 +00:00
//电流6 -主系统 104,105
monitoringSensorModel . current6 = tools . bytetoUInt ( byteList , 104 ) / ( float ) 100 ;
2023-03-12 22:44:49 +00:00
2023-03-17 07:06:36 +00:00
//电流7 -主系统 106,107
monitoringSensorModel . current7 = tools . bytetoUInt ( byteList , 106 ) / ( float ) 100 ;
2023-03-12 22:44:49 +00:00
2023-03-17 07:06:36 +00:00
//电流8 -主系统 108,109
monitoringSensorModel . current8 = tools . bytetoUInt ( byteList , 108 ) / ( float ) 100 ;
//电流9 -主系统 110,111
monitoringSensorModel . current9 = tools . bytetoUInt ( byteList , 110 ) / ( float ) 100 ;
//电流10 -主系统 112,113
monitoringSensorModel . current10 = tools . bytetoUInt ( byteList , 112 ) / ( float ) 100 ;
_message = "日期:" + monitoringSensorModel . _datetime_Sensor + "\r\n" + " 温度:" + monitoringSensorModel . _Tem + "℃ 湿度:" + monitoringSensorModel . _Hum + "% 气压:" + monitoringSensorModel . _pressure + "KPa " + "\r\n" + " 俯仰角:" + monitoringSensorModel . _acceleration_X + "° 横滚角:" + monitoringSensorModel . _acceleration_Y + "° 偏航角:" + monitoringSensorModel . _acceleration_Z + "°" + "\r\n" + " 角速度X: " + monitoringSensorModel . _angularVelocity_X + "m/s² 角速度Y: " + monitoringSensorModel . _angularVelocity_Y + "m/s² 角速度Z: " + monitoringSensorModel . _angularVelocity_Z + "m/s² " + "\r\n" + " 角度X: " + monitoringSensorModel . degrees_x + "rad/s 角度Y: " + monitoringSensorModel . degrees_y + "rad/s 角度Z: " + monitoringSensorModel . degrees_z + "rad/s " + "\r\n" + " 电池电压:" + monitoringSensorModel . _batteryVoltage + "V 充电电流:" + monitoringSensorModel . _ChargingCurrent + "A 充电状态:" + monitoringSensorModel . _State + "\r\n" + " 温度-主系统:" + monitoringSensorModel . tem + "℃ 湿度-主系统:" + monitoringSensorModel . hum + "% 气压 -主系统 :" + monitoringSensorModel . preeure + "KPa" + "\r\n" + " 电压1 -主系统:" + monitoringSensorModel . voltage1 + "V 电压2 -主系统:" + monitoringSensorModel . voltage2 + "V 电压3 -主系统:" + monitoringSensorModel . voltage3 + "V" + "\r\n" + " 电压4 -主系统:" + monitoringSensorModel . voltage4 + "V 电压5 -主系统:" + monitoringSensorModel . voltage5 + "V 电压6 -主系统:" + monitoringSensorModel . voltage6 + "V" + "\r\n" + " 电压7 -主系统:" + monitoringSensorModel . voltage7 + "V 电压8 -主系统:" + monitoringSensorModel . voltage8 + "V 电压9 -主系统:" + monitoringSensorModel . voltage9 + "V" + "\r\n" + " 电压10 -主系统:" + monitoringSensorModel . voltage10 + "V" + "\r\n" + " 电流1 -主系统:" + monitoringSensorModel . current1 + "A 电流2 -主系统:" + monitoringSensorModel . current2 + "A 电流3 -主系统:" + monitoringSensorModel . current3 + "A" + "\r\n" + " 电流4 -主系统:" + monitoringSensorModel . current4 + "A 电流5 -主系统:" + monitoringSensorModel . current5 + "A 电流6 -主系统:" + monitoringSensorModel . current6 + "A" + "\r\n" + " 电流7 -主系统:" + monitoringSensorModel . current7 + "A 电流8 -主系统:" + monitoringSensorModel . current8 + "A 电流9 -主系统:" + monitoringSensorModel . current9 + "A" + "\r\n" + " 电流10 -主系统:" + monitoringSensorModel . current10 + "A" + "\r\n" ;
2023-03-12 22:44:49 +00:00
//以txt文档的形式存储接收到的数据
2023-03-17 07:06:36 +00:00
tools . AddLgoToTXT ( FileModel . File_Name + FileModel . File_Time . ToString ( "yyyy-MM-dd" ) + " 历史数据查询记录" + ".txt" , FileModel . File_Path + FileModel . File_Time . ToString ( "yyyy-MM-dd" ) + @"\" , _localtime + ": " + _message + "\r\n" ) ;
_data = "收到历史数据,请查看文件:" + FileModel . File_Name + FileModel . File_Time . ToString ( "yyyy-MM-dd" ) + " 历史数据查询记录" + ".txt" ;
ReceiveValueChangedChanged ( null , _data ) ;
2023-03-11 00:34:39 +00:00
break ;
2023-02-03 00:31:48 +00:00
default :
break ;
}
}
}
}