Esempio n. 1
0
        public static IObservable <BleLogEvent> CreateLogger(this IAdapter adapter, BleLogFlags flags = BleLogFlags.AdapterStatus | BleLogFlags.DeviceStatus)
        {
            return(Observable.Create <BleLogEvent>(ob =>
            {
                var list = new List <IDisposable>();
                var deviceEvents = new Dictionary <Guid, List <IDisposable> >();

                if (flags.HasFlag(BleLogFlags.AdapterStatus))
                {
                    list.Add(adapter
                             .WhenStatusChanged()
                             .Subscribe(status => Write(ob, null, BleLogFlags.AdapterStatus, null, $"Changed to {status}"))
                             );
                }
                if (flags.HasFlag(BleLogFlags.AdapterScanResults))
                {
                    list.Add(adapter
                             .ScanListen()
                             .Subscribe(scanResult => Write(ob, null, BleLogFlags.AdapterScanResults, null, $"Device: {scanResult.Device.Uuid} - RSSI: {scanResult.Rssi}"))
                             );
                }
                if (flags.HasFlag(BleLogFlags.AdapterScanStatus))
                {
                    list.Add(adapter
                             .WhenScanningStatusChanged()
                             .Subscribe(status => Write(ob, null, BleLogFlags.AdapterScanStatus, null, $"Changed to {status}"))
                             );
                }

                list.Add(adapter
                         .WhenDeviceStatusChanged()
                         .Where(x =>
                                x.Status == ConnectionStatus.Connected ||
                                x.Status == ConnectionStatus.Disconnected
                                )
                         .Subscribe(device =>
                {
                    if (device.Status == ConnectionStatus.Connected)
                    {
                        if (flags.HasFlag(BleLogFlags.DeviceConnected))
                        {
                            Write(ob, device, BleLogFlags.DeviceConnected, device.Uuid, $"Changed to {device.Status}");
                        }

                        HookDevice(device, ob, deviceEvents, flags);
                    }
                    else if (flags.HasFlag(BleLogFlags.DeviceDisconnected))
                    {
                        Write(ob, device, BleLogFlags.DeviceDisconnected, device.Uuid, $"Changed to {device.Status}");
                    }
                }));

                foreach (var device in adapter.GetConnectedDevices())
                {
                    HookDevice(device, ob, deviceEvents, flags);
                }

                return () =>
                {
                    foreach (var deviceList in deviceEvents.Values)
                    {
                        foreach (var device in deviceList)
                        {
                            device.Dispose();
                        }
                    }

                    foreach (var dispose in list)
                    {
                        dispose.Dispose();
                    }
                };
            }));
        }