20211124_ZNZT_upperpc/MonitoringTechnology/Ble/BleDevice.cs

540 lines
24 KiB
C#
Raw Normal View History

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();
public event TypedEventHandler<GattCharacteristic, byte[]> CharacteristicValueChanged;
public event TypedEventHandler<BleDevice, bool> ConnectionStateChanged;
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();
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);
}
}
ConnectionStateChanged?.Invoke(this, true);
}
}
#endregion
private static string _localtime => $"{DateTime.Now: yyyy-MM-dd HH:mm:ss.fff}";
/// <summary>
/// 收到数据
/// </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());
}
/// <summary>
/// 定义一个委托,用于传值老的版本号
/// </summary>
Action<byte[]> func = new Action<byte[]>(SystemOperationView.Setbbh);
string _message = "";
byte[] bbh = new byte[4] { 0x00, 0x00, 0x00, 0x00 };
/// <summary>
/// 解析数据
/// </summary>
/// <param name="byteList"></param>
private void ParsingData(List<byte> byteList)
{
//校验当前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)
{
//数据解析
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,2223
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);
///定时去刷新首页电气数据和故障信息
ThreadPool.QueueUserWorkItem(delegate
{
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);
});
//姿态数据刷新
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;
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) 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 + "');");
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:
_message += "获取成功";
break;
case 0x01:
_message += "空";
break;
case 0x02:
_message += "设备忙";
break;
default:
break;
}
//显示版本号
SystemOperationView.systemOperationViewModel.VersionQueryData = ((int)bbh[0]).ToString() + "." + ((int)bbh[1]).ToString() + "." + ((int)bbh[2]).ToString() + "." + ((int)bbh[3]).ToString();
break;
//设备回复文件数据指令
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;
default:
break;
}
}
}
}