<commit_before>﻿using System;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Text.RegularExpressions;
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.Devices.Enumeration;
using Windows.Foundation;
using Acr.Reactive;


namespace Plugin.BluetoothLE
{
    public class Device : AbstractDevice
    {
        readonly DeviceContext context;


        public Device(BluetoothLEDevice native)
        {
            this.context = new DeviceContext(this, native);

            var mac = this.ToMacAddress(native.BluetoothAddress);
            this.Uuid = this.GetDeviceId(mac);
        }


        public override string Name => this.context.NativeDevice.Name;
        public override object NativeDevice => this.context.NativeDevice;
        public override DeviceFeatures Features => DeviceFeatures.PairingRequests | DeviceFeatures.ReliableTransactions;
        public override IGattReliableWriteTransaction BeginReliableWriteTransaction() => new GattReliableWriteTransaction();


        public override void Connect(ConnectionConfig config) => this.context.Connect();
        public override async void CancelConnection() => await this.context.Disconnect();


        public override ConnectionStatus Status
        {
            get
            {
                switch (this.context.NativeDevice.ConnectionStatus)
                {
                    case BluetoothConnectionStatus.Connected:
                        return ConnectionStatus.Connected;

                    default:
                        return ConnectionStatus.Disconnected;
                }
            }
        }


        public override IObservable<IGattService> GetKnownService(Guid serviceUuid)
            => Observable.FromAsync(async ct =>
            {
                var result = await this.context.NativeDevice.GetGattServicesForUuidAsync(serviceUuid, BluetoothCacheMode.Cached);
                if (result.Status != GattCommunicationStatus.Success)
                    throw new ArgumentException("Could not find GATT service - " + result.Status);

                var wrap = new GattService(this.context, result.Services.First());
                return wrap;
            });


        IObservable<ConnectionStatus> statusOb;
        public override IObservable<ConnectionStatus> WhenStatusChanged()
        {
            this.statusOb = this.statusOb ?? Observable.Create<ConnectionStatus>(ob =>
            {
                ob.OnNext(this.Status);
                var handler = new TypedEventHandler<BluetoothLEDevice, object>(<commit_msg>[UWP] fix NRE on device status<commit_after>﻿using System;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Text.RegularExpressions;
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.Devices.Enumeration;
using Windows.Foundation;
using Acr.Reactive;


namespace Plugin.BluetoothLE
{
    public class Device : AbstractDevice
    {
        readonly DeviceContext context;


        public Device(BluetoothLEDevice native)
        {
            this.context = new DeviceContext(this, native);

            var mac = this.ToMacAddress(native.BluetoothAddress);
            this.Uuid = this.GetDeviceId(mac);
        }


        public override string Name => this.context.NativeDevice.Name;
        public override object NativeDevice => this.context.NativeDevice;
        public override DeviceFeatures Features => DeviceFeatures.PairingRequests | DeviceFeatures.ReliableTransactions;
        public override IGattReliableWriteTransaction BeginReliableWriteTransaction() => new GattReliableWriteTransaction();


        public override void Connect(ConnectionConfig config) => this.context.Connect();
        public override async void CancelConnection() => await this.context.Disconnect();


        public override ConnectionStatus Status
        {
            get
            {
                switch (this.context.NativeDevice?.ConnectionStatus)
                {
                    case BluetoothConnectionStatus.Connected:
                        return ConnectionStatus.Connected;

                    default:
                        return ConnectionStatus.Disconnected;
                }
            }
        }


        public override IObservable<IGattService> GetKnownService(Guid serviceUuid)
            => Observable.FromAsync(async ct =>
            {
                var result = await this.context.NativeDevice.GetGattServicesForUuidAsync(serviceUuid, BluetoothCacheMode.Cached);
                if (result.Status != GattCommunicationStatus.Success)
                    throw new ArgumentException("Could not find GATT service - " + result.Status);

                var wrap = new GattService(this.context, result.Services.First());
                return wrap;
            });


        IObservable<ConnectionStatus> statusOb;
        public override IObservable<ConnectionStatus> WhenStatusChanged()
        {
            this.statusOb = this.statusOb ?? Observable.Create<ConnectionStatus>(ob =>
            {
                ob.OnNext(this.Status);
                var handler = new TypedEventHandler<BluetoothLEDevice, object>(
{'fraction_non_alphanumeric': 0.07440363498674744, 'fraction_numerical': 0.0, 'mean_word_length': 2.4083870967741934, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6707969', 'n_tokens_mistral': 1268, 'n_tokens_neox': 1204, 'n_words': 274}