コード例 #1
0
        public override void Initialize()
        {
            this.WhenAnyValue(vm => vm.SelectedIoTModuleIdentityInfo)
            .Subscribe(x => ioTModuleIdentityInfoSelectionService.Select(x))
            .AddDisposableTo(Disposables);

            // ReSharper disable once InvokeAsExtensionMethod
            var deviceHubInfo = Observable.CombineLatest(
                ioTDeviceSelectionService.SelectedObject,
                ioTHubInfoSelectionService.SelectedObject,
                (device, iotHub) => new { DeviceInfo = device, IoTHubInfo = iotHub })
                                .Where(arg => arg.DeviceInfo != null && arg.IoTHubInfo != null);


            ioTDeviceSelectionService.SelectedObject
            .Where(deviceInfo => deviceInfo != null)
            .ObserveOnDispatcher()
            .Do(_ => Loading  = true)
            .Do(s => deviceId = s.DeviceId)
            .Subscribe()
            .AddDisposableTo(Disposables);

            ioTHubInfoSelectionService.SelectedObject
            .Where(name => name != null)
            .ObserveOnDispatcher()
            .Do(identityInfos => IoTModuleIdentityInfos = new IoTModuleIdentityInfo[] { })
            .Do(s => hubName = s.Name)
            .Subscribe()
            .AddDisposableTo(Disposables);

            deviceHubInfo
            .Do(arg => Logger.Info($"Received Hub '{arg.IoTHubInfo.Name}' and Device '{arg.DeviceInfo.DeviceId}' for retrieving Modules"))
            .SelectMany(arg => azureService.GetIoTModules(arg.IoTHubInfo.Name, arg.DeviceInfo.DeviceId))
            .ObserveOnDispatcher()
            .Do(identityInfos => IoTModuleIdentityInfos = identityInfos)
            .Do(_ => Loading = false)
            .LogAndRetryAfterDelay(Logger, TimeSpan.FromSeconds(1), "Error while retrieving device modules information")
            .Subscribe()
            .AddDisposableTo(Disposables);

            ReloadCommand = ReactiveCommand.CreateFromTask(Reload, ioTDeviceSelectionService.SelectedObject.Select(deviceInfo => deviceInfo != null))
                            .AddDisposableTo(Disposables);

            // Observable created from ModuleId
            var selectedModuleId = this.WhenAnyValue(model => model.SelectedIoTModuleIdentityInfo)
                                   .Select(model => model?.ModuleId);

            // ReSharper disable once InvokeAsExtensionMethod
            // Observable - combines Observable.Latest and selectedModuleId (Device + Hub + ModuleId)
            Observable.CombineLatest(deviceHubInfo, selectedModuleId,
                                     (deviceHub, module) =>
                                     new
            {
                ModuleId = module,
                HubName  = deviceHub.IoTHubInfo.Name,
                DeviceId = deviceHub.DeviceInfo.DeviceId
            })

            .ObserveOnDispatcher()
            .Do(_ => CurrentModuleTwin = null)
            .Where(moduleInfo => !string.IsNullOrEmpty(moduleInfo.ModuleId))
            .Do(arg => Logger.Info($"Received Hub '{arg.HubName}', Device '{arg.DeviceId}' and module id {arg.ModuleId} for retrieving module twin"))
            .SelectMany(moduleInfo => azureService.GetIoTModelTwinProperties(moduleInfo.HubName, moduleInfo.DeviceId, moduleInfo.ModuleId))
            .Do(twin => CurrentModuleTwin = twin)
            .LogAndRetryAfterDelay(Logger, TimeSpan.FromMilliseconds(100), "Error while retrieving module twin for selection")
            .Subscribe()
            .AddDisposableTo(Disposables)
            ;
        }