private async Task ShowSpecificRtssc(bool isStartMode, BgrData bgrData)
        {
            if (bgrData == null)
            {
                throw new ArgumentNullException("bgrData");
            }

            TabControl tcAcqui    = isStartMode ? tcStartAcqui : tcStopAcqui;
            TabPage    tpRtssDdls = isStartMode ? tpStartRtssDdls : tpStopRtssDdls;

            RadDropDownList ddlRoute     = isStartMode ? ddlRouteStart : ddlRouteStop;
            RadDropDownList ddlTroncon   = isStartMode ? ddlTronconStart : ddlTronconStop;
            RadDropDownList ddlSection   = isStartMode ? ddlSectionStart : ddlSectionStop;
            RadDropDownList ddlSousRoute = isStartMode ? ddlSousRouteStart : ddlSousRouteStop;

            if (bgrData.Rtssc != null)
            {
                tcAcqui.SelectedTab = tpRtssDdls;

                if (GetSelectedDirection(isStartMode) != bgrData.Direction)
                {
                    ShowSpecificDirection(isStartMode, bgrData.Direction);
                    await HandleRouteSelectionChanged(isStartMode, (string)ddlRoute.SelectedValue);
                }

                if ((string)ddlRoute.SelectedValue != bgrData.Rtssc.Route)
                {
                    ddlRoute.SelectedValue = bgrData.Rtssc.Route;
                    await HandleRouteSelectionChanged(isStartMode, bgrData.Rtssc.Route);
                }
                if ((string)ddlTroncon.SelectedValue != bgrData.Rtssc.Troncon)
                {
                    ddlTroncon.SelectedValue = bgrData.Rtssc.Troncon;
                    await HandleTronconSelectionChanged(isStartMode);
                }
                if ((string)ddlSection.SelectedValue != bgrData.Rtssc.Section)
                {
                    ddlSection.SelectedValue = bgrData.Rtssc.Section;
                    await HandleSectionSelectionChanged(isStartMode);
                }
                if ((string)ddlSousRoute.SelectedValue != bgrData.Rtssc.SousRoute)
                {
                    ddlSousRoute.SelectedValue = bgrData.Rtssc.SousRoute;
                }
            }
            else
            {
                HideAllSpecificControls(isStartMode);
                MessageBox.Show("No RTSS to show", "Cannot show RTSS", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        private async Task ShowSpecificEndSectionRtssc(bool isStartMode, BgrData bgrData)
        {
            if (bgrData == null || bgrData.Rtssc == null)
            {
                //Stop bgrCallback
                MessageBox.Show("Cannot get RTSSC for the next section start.");
                return;
            }

            if (bgrData.Direction == DirectionBgr.Unknown)
            {
                //Stop bgrCallback
                MessageBox.Show("Cannot get the current direction which is required to get the RTSSC of the next section start. Possible solution: Vehicle must be moving to determine the current direction when the current 'sous-route' type is [000C].");
            }

            _ignoreRtssSelectedIndexChanged = true;
            try
            {
                await ShowSpecificRtssc(isStartMode, bgrData);
                await ShowEndSectionChainage(isStartMode, bgrData.Direction);
            }
            finally { _ignoreRtssSelectedIndexChanged = false; }
        }
        private async Task HandleGetCurrentRtsscCommand(bool isStartMode)
        {
            RadDropDownList ddlRoute              = isStartMode ? ddlRouteStart : ddlRouteStop;
            RadDropDownList ddlTroncon            = isStartMode ? ddlTronconStart : ddlTronconStop;
            RadDropDownList ddlSection            = isStartMode ? ddlSectionStart : ddlSectionStop;
            RadDropDownList ddlSousRoute          = isStartMode ? ddlSousRouteStart : ddlSousRouteStop;
            TextBoxBase     mtxtChainageSelection = isStartMode ? mtxtChainageSelectionStart : mtxtChainageSelectionStop;

            HandleDisengage(isStartMode);

            var bgrData = await GetCurrentBgrData();

            if (bgrData == null || bgrData.Rtssc == null)
            {
                bgrData = new BgrData {
                    Direction = DirectionBgr.Unknown, Rtssc = null
                };
                MessageBox.Show("Cannot get current RTSSC. Your current location may not be a registered road or GPS is currently not working.");
            }
            ShowSpecificDirection(isStartMode, bgrData.Direction);
            await ShowSpecificRtssc(isStartMode, bgrData);

            mtxtChainageSelection.Text = bgrData.Rtssc.Chainage.GetValueOrDefault().ToString("F0");
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.RegisterObserver(
                AgentBroker.Instance.ObserveOne <Tuple <string, string, OperationalAgentStates> >(this.ParentAgent.Id, "DependenciesOperationalStateDataSource")
                .ObserveOn(WindowsFormsSynchronizationContext.Current)
                .Subscribe(
                    t =>
            {
                var color = (t.Item3 == OperationalAgentStates.None) ? Color.Lime : Color.Red;

                var agentType = Type.GetType(t.Item1);
                if (agentType == typeof(ILocalisationAgent))
                {
                    lblLatLon.ForeColor = color;
                }
                else if (agentType == typeof(IDistanceAgent))
                {
                    lblDistance.ForeColor = color;
                }
                else if (agentType == typeof(IBgrDirectionalAgent))
                {
                    lblRoute.ForeColor          = color;
                    lblTroncon.ForeColor        = color;
                    lblSection.ForeColor        = color;
                    lblSousRoute.ForeColor      = color;
                    lblChainage.ForeColor       = color;
                    lbDirection.ForeColor       = color;
                    lbLongueurSection.ForeColor = color;
                }
            }));

            var emptyBgrData = new BgrData {
                Rtssc = new Rtssc(), Direction = DirectionBgr.Unknown
            };

            this.RegisterObserver(
                AgentBroker.Instance.ObserveAny <IBgrDirectionalAgent, BgrData>("DataSource")
                .Buffer(ReceiveDataTimeout, 1)
                .Sample(UIRefreshInterval)
                .Select(buffer => buffer.LastOrDefault() ?? emptyBgrData)
                .ObserveOn(WindowsFormsSynchronizationContext.Current)
                .Subscribe(
                    data =>
            {
                lblRoute.Text     = string.IsNullOrEmpty(data.Rtssc.Route) ? "-----" : data.Rtssc.Route;
                lblTroncon.Text   = string.IsNullOrEmpty(data.Rtssc.Troncon) ? "---" : data.Rtssc.Troncon;
                lblSection.Text   = string.IsNullOrEmpty(data.Rtssc.Section) ? "--" : data.Rtssc.Section;
                lblSousRoute.Text = string.IsNullOrEmpty(data.Rtssc.SousRoute) ? "----" : data.Rtssc.SousRoute;
                lblChainage.Text  = data.Rtssc.Chainage == null ? "--+---" : string.Format("{0:##0+000}", data.Rtssc.Chainage.Value);
                lbDirection.Text  = Convert.ToString((int)data.Direction);

                this.lbCs.Text = string.IsNullOrEmpty(data.Rtssc.CentreDeServiceName) ? "-----" : data.Rtssc.CentreDeServiceName;
                toolTipManager.SetToolTip(lbCs, lbCs.Text);

                this.lbDt.Text = string.IsNullOrEmpty(data.Rtssc.DirectionTerritorialeName) ? "-----" : data.Rtssc.DirectionTerritorialeName;
                toolTipManager.SetToolTip(lbDt, lbDt.Text);

                this.lbLongueurSection.Text = data.Rtssc.Longueur <= 0 ? "-" : data.Rtssc.Longueur.ToString("## ##0") + " m";
            }));

            var emptyDisplayGpsData = new { GpsStatus = GpsStatus.SignalLost, Latitude = new string('-', 4), Longitude = new string('-', 4) };

            this.RegisterObserver(
                AgentBroker.Instance.ObserveAny <ILocalisationAgent, LocalisationData>("DataSource")
                .Buffer(ReceiveDataTimeout, 1)
                .Sample(UIRefreshInterval)
                .Select(
                    buffer =>
            {
                var last = buffer.LastOrDefault();

                if (last == null)
                {
                    return(emptyDisplayGpsData);
                }
                else
                {
                    return new { GpsStatus = last.GpsStatus, Latitude = last.CorrectedData.PositionData.Latitude.ToString("##0.000000").Replace(',', '.'), Longitude = last.CorrectedData.PositionData.Longitude.ToString("##0.000000").Replace(',', '.') }
                };
            })
                .ObserveOn(WindowsFormsSynchronizationContext.Current)
                .Subscribe(
                    data =>
            {
                lblLatLon.Text = string.Format("{0}, {1}", data.Latitude, data.Longitude);

                switch (data.GpsStatus)
                {
                case GpsStatus.Initializing:
                    lblLatLon.ForeColor = Color.Orange;

                    break;

                case GpsStatus.Reliable:
                    lblLatLon.ForeColor = Color.Lime;
                    break;

                case GpsStatus.SignalLost:
                    lblLatLon.ForeColor = Color.Red;
                    break;

                case GpsStatus.MultiPathDetected:
                    lblLatLon.ForeColor = Color.Yellow;
                    break;
                }
            }));

            this.RegisterObserver(
                AgentBroker.Instance.ObserveAny <IAcquisitionManagerAgent, AcquisitionManagerStateChangedResult>("AcquisitionManagerStateDataSource")
                .Select(data => new { ProviderState = data.ProviderState, Parameters = data.Parameters })
                .ObserveOn(WindowsFormsSynchronizationContext.Current)
                .Subscribe(
                    data =>
            {
                switch (data.ProviderState)
                {
                case ProviderState.Started:
                    lblSequenceur.Text = "";
                    lblDistanceBeforeStartRecord.Text = "-";
                    lblDistanceBeforeStopRecord.Text  = "-";
                    lblDistance.Text = "-";
                    break;

                case ProviderState.InitializedRecord:
                    lblSequenceur.Text = data.Parameters.SequenceId;
                    lblDistanceBeforeStartRecord.Text = "-";

                    // if triggered start/stop
                    if (_lastDistanceBeforeTriggerSubscription != null)
                    {
                        _lastDistanceBeforeTriggerSubscription.Dispose();
                        _lastDistanceBeforeTriggerSubscription = null;
                    }

                    // validation case
                    if (_absoluteDistanceSubscription != null)
                    {
                        _absoluteDistanceSubscription.Dispose();
                        _absoluteDistanceSubscription    = null;
                        lblDistanceBeforeStopRecord.Text = "-";
                    }
                    break;

                case ProviderState.StartingRecord:
                    {
                        _lastDistanceBeforeTriggerSubscription = UpdateDistanceBeforeTrigger(data.ProviderState, (TriggeredAcquisitionParameter)data.Parameters);
                    }
                    break;

                case ProviderState.StartedRecord:
                    {
                        if (_lastDistanceBeforeTriggerSubscription != null)
                        {
                            _lastDistanceBeforeTriggerSubscription.Dispose();
                        }

                        lblDistanceBeforeStartRecord.Text = "-";
                        lblDistanceBeforeStopRecord.Text  = "-";

                        if (_absoluteDistanceSubscription == null)
                        {
                            _absoluteDistanceSubscription = AgentBroker.Instance.ObserveAny <IDistanceAgent, DistanceData>("DataSource")
                                                            .ObserveOn(WindowsFormsSynchronizationContext.Current)
                                                            .Subscribe(distanceData => lblDistance.Text = string.Format("{0:#,0}" + " m.", distanceData.AbsoluteDistance));
                        }
                    }
                    break;

                case ProviderState.StoppingRecord:
                    {
                        _lastDistanceBeforeTriggerSubscription = UpdateDistanceBeforeTrigger(data.ProviderState, (TriggeredAcquisitionParameter)data.Parameters);
                    }
                    break;
                }
            }));

            this.RegisterObserver(
                AgentBroker.Instance.ObserveAny <IEventPanelAgent, bool>("HotkeyModeEnabledDataSource")
                .ObserveOn(WindowsFormsSynchronizationContext.Current)
                .Subscribe(data => UpdateKeyboardHook(data)));

            var emptySpeedData = new SpeedData {
                CurrentSpeed = -1, SpeedSource = SpeedActiveMode.None
            };

            this.RegisterObserver(
                AgentBroker.Instance.ObserveAny <ISpeedAgent, SpeedData>("DataSource")
                .Buffer(ReceiveDataTimeout, 1)
                .Sample(UIRefreshInterval)
                .Select(buffer => buffer.LastOrDefault())
                .ObserveOn(WindowsFormsSynchronizationContext.Current)
                .Subscribe(
                    data =>
            {
                data = data ?? emptySpeedData;

                SetSpeedSourceImage(data.SpeedSource);
                SetSpeedImages(data.CurrentSpeed, data.IsInRange);
            }));
        }