/// <summary> /// Start the synchronizer /// </summary> public void Enable() { if (Interlocked.CompareExchange(ref _isEnabled, 1, 0) == 0) { var smartthings = default((IAppEndpoint endpoint, Device[] devices)); var stToMqtt = GetAndObserveSmartthings() .Do(st => smartthings = st) .Select(st => Observable.FromAsync(ct => SmartthingToMqtt(ct, st.devices), _scheduler)) .Switch() .Retry(TimeSpan.FromMinutes(1), _scheduler) .Publish(); var mqttToSt = _mqtt .ObserveAll(_mqttConfig.TopicNamespace) // We skip the first batch of items until the current state from ST init the cache .SkipUntil(stToMqtt.FirstAsync()) .SelectMany(async(message, ct) => MqttToSmartthings(ct, message, smartthings.devices, smartthings.endpoint)) .Retry(TimeSpan.FromMinutes(1), _scheduler); _subscription.Disposable = new CompositeDisposable( mqttToSt.Subscribe(), stToMqtt.Connect()); } }