private async void CommServiceOnConnectionChanged(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("CommServiceOnConnectionChanged: {0}", _commService.ConnectionState);
            
            _programMode = MoCoBusProgramMode.Invalid;
            if (_commService.ConnectionState == ConnectionState.Connected)
            {
                await UpdateState().ConfigureAwait(false);
            }
            else
            {                
                await StopUpdateTask().ConfigureAwait(false);
            }

            _dispatcherHelper.RunOnUIThread(() =>
                {
                    RaisePropertyChanged(() => IsConnected);
                    RaisePropertyChanged(() => IsConnecting);
                    RaisePropertyChanged(() => IsDisconnected);
                });
        }
        public async Task UpdateState()
        {
            System.Diagnostics.Debug.WriteLine("UpdateState");

            try
            {
                _runStatus = await _protocolService.Main.GetRunStatus().ConfigureAwait(false);

                var tmpProgramMode = _programMode;
                _programMode = await _protocolService.Main.GetProgramMode().ConfigureAwait(false);
                if (tmpProgramMode != _programMode)
                {
                    switch (_programMode)
                    {
                        case MoCoBusProgramMode.ShootMoveShoot:
                            System.Diagnostics.Debug.WriteLine("UpdateState: Call ModeSmsViewModel.InitState()");
                            await ModeSmsViewModel.InitState().ConfigureAwait(false);
                            break;
                        case MoCoBusProgramMode.Panorama:
                            System.Diagnostics.Debug.WriteLine("UpdateState: Call ModePanoViewModel.InitState()");
                            await ModePanoViewModel.InitState().ConfigureAwait(false);
                            break;
                        case MoCoBusProgramMode.Astro:
                            System.Diagnostics.Debug.WriteLine("UpdateState: Call ModeAstroViewModel.InitState()");
                            await ModeAstroViewModel.InitState().ConfigureAwait(false);
                            break;
                    }
                }

                _dispatcherHelper.RunOnUIThread(() =>
                    {
                        System.Diagnostics.Debug.WriteLine("UpdateState: Call RaisePropertyChanged for ProgramMode");
                        RaisePropertyChanged(() => ProgramMode);
                        System.Diagnostics.Debug.WriteLine("UpdateState: Call RaisePropertyChanged for RunStatus");
                        RaisePropertyChanged(() => RunStatus);
                    });
            }
            catch (TimeoutException toe)
            {
                Insights.Report(toe);
            }
        }
 protected SubDeviceTableViewControllerBase(IntPtr handle, MoCoBusProgramMode programMode, string viewPageKey)
     : base(handle)
 {
     _programMode = programMode;
     _viewPageKey = viewPageKey;
 }
 public async Task SetProgramMode(MoCoBusProgramMode mode)
 {
     await _commService
         .SendAndReceiveAsync(new MoCoBusMainCommandFrame(_address, MoCoBusMainCommand.SetProgramMode, new[] { (byte)mode }))
         .ConfigureAwait(false);
 }
        private void OnProgramModeChanged()
        {
            if (!Vm.IsConnected)
                return;

            if (_programMode == Vm.ProgramMode)
                return;

            _programMode = Vm.ProgramMode;

            System.Diagnostics.Debug.WriteLine("OnProgramModeChanged: {0}", _programMode);

            SetModeAstroButton.Enabled = true;
            SetModePanoButton.Enabled = true;
            SetModeSmsButton.Enabled = true;

            if (_programMode == MoCoBusProgramMode.ShootMoveShoot)
            {
                ShowModeSmsFragment();
            }
            else if (_programMode == MoCoBusProgramMode.Astro)
            {
                ShowModeAstroFragment();
            }
            else if (_programMode == MoCoBusProgramMode.Panorama)
            {
                ShowModePanoFragment();
            }
        }
        protected override void OnResume()
        {
            base.OnResume();

            App.Initialize(this);
            ServiceLocator.Current.GetInstance<DispatcherHelper>().SetOwner(this);

            DisconnectedLayout.Visibility = ViewStates.Visible;
            ConnectingLayout.Visibility = ViewStates.Gone;
            ConnectedLayout.Visibility = ViewStates.Gone;

            _isConnectedBinding = this.SetBinding(() => Vm.IsConnected, () => ConnectSwitch.Checked, BindingMode.TwoWay);
            _showDisconnectedBinding = this.SetBinding(() => Vm.IsDisconnected, () => DisconnectedLayout.Visibility)
                .ConvertSourceToTarget(b => b ? ViewStates.Visible : ViewStates.Gone);
            _showConnectingBinding = this.SetBinding(() => Vm.IsConnecting, () => ConnectingLayout.Visibility)
                .ConvertSourceToTarget(b => b ? ViewStates.Visible : ViewStates.Gone);
            _showConnectedBinding = this.SetBinding(() => Vm.IsConnected)
                .WhenSourceChanges(() =>
                    {
                        System.Diagnostics.Debug.WriteLine("DeviceViewActivity IsConnected Changed");

                        ConnectedLayout.Visibility = Vm.IsConnected ? ViewStates.Visible : ViewStates.Gone;

                        if (!Vm.IsConnected)
                        {
                            _programMode = MoCoBusProgramMode.Invalid;

                            System.Diagnostics.Debug.WriteLine("DeviceViewActivity IsConnected Changed: Search dialogs and close them");

                            var dlg = FragmentManager.FindFragmentByTag<DialogFragment>(Consts.DialogTag);
                            if (dlg != null)
                            {
                                System.Diagnostics.Debug.WriteLine("DeviceViewActivity IsConnected Changed: Found dialog, dismiss it");
                                dlg.DismissAllowingStateLoss();
                            }

                            System.Diagnostics.Debug.WriteLine("DeviceViewActivity IsConnected Changed: Search view fragment");
                            var f = FragmentManager.FindFragmentById<Fragment>(Resource.Id.ConnectedFragmentContainer);
                            if (f != null)
                            {
                                System.Diagnostics.Debug.WriteLine("DeviceViewActivity IsConnected Changed: Remove view fragment");
                                var ft = FragmentManager.BeginTransaction();
                                ft.Remove(f);
                                ft.SetTransition(FragmentTransit.FragmentFade);
                                ft.Commit();
                            }

                            System.Diagnostics.Debug.WriteLine("DeviceViewActivity IsConnected Changed: PopBackStackImmediate");
                            FragmentManager.PopBackStackImmediate(null, PopBackStackFlags.Inclusive);

                            System.Diagnostics.Debug.WriteLine("DeviceViewActivity IsConnected Changed: ExecutePendingTransactions");
                            FragmentManager.ExecutePendingTransactions();
                        }
                    });

            _programMode = MoCoBusProgramMode.Invalid;
            _programModeBinding = this.SetBinding(() => Vm.ProgramMode)
                .WhenSourceChanges(OnProgramModeChanged);
            _programModeBinding.ForceUpdateValueFromSourceToTarget();
        }