Пример #1
0
        public IDisposable Connect(IObservableDevice device)
        {
            DeviceType deviceType = device.DeviceType;

            var observer = device.States
                           //.Where(state => DeviceStatesHelper.IsBusy(state) == false)
                           .Throttle(TimeSpan.FromSeconds(THROTTLE_SECONDS))
                           .DistinctUntilChanged()
                           .ObserveOnDispatcher()
                           .Subscribe(onNext: state => Device_StateChanged(device, state.State));

            var currentState = device.States.Select(s => s.State).MostRecent(DeviceState.Disconnected).First();

            DeviceStatus status;

            if (_context.TryAddDevice(deviceType, currentState, out status))
            {
                StartedObservingDevice?.Invoke(this, status);
            }

            return(new CompositeDisposable
                   (
                       observer,
                       Disposable.Create(() =>
            {
                DeviceStatus lastStatus;
                if (_context.TryRemoveDevice(deviceType, out lastStatus))
                {
                    StoppedObservingDevice?.Invoke(this, lastStatus);
                }
            })
                   ));
        }