public IHttpActionResult Get()
        {
            StandardFr8TerminalCM curStandardFr8TerminalCM = new StandardFr8TerminalCM
            {
                Definition = _activityStore.Terminal,
                Activities = _activityStore.GetAllTemplates()
            };

            if (Request.Headers.Contains("Fr8HubCallBackUrl") && Request.Headers.Contains("Fr8HubCallbackSecret"))
            {
                var hubUrl = Request.Headers.GetValues("Fr8HubCallBackUrl").First();
                var secret = Request.Headers.GetValues("Fr8HubCallbackSecret").First();

                _hubDiscovery.SetHubSecret(hubUrl, secret);
            }

            return(Json(curStandardFr8TerminalCM, new JsonSerializerSettings()
            {
                ContractResolver = new ExcludeTerminalContractResolver()
            }));
        }
        //private async Task<StandardFr8TerminalCM> SendDiscoveryRequest(string terminalUrl, Dictionary<string, string> headers = null)
        //{
        //    // Use a custom HttpClient to query the terminal with a low timeout (10 sec)
        //    var innerClient = new HttpClient();
        //    innerClient.Timeout = new TimeSpan(0, 0, 10);
        //    try
        //    {
        //        var response = await innerClient.GetAsync(new Uri(terminalUrl + "/discover", UriKind.Absolute));
        //        response.EnsureSuccessStatusCode();
        //        var responseContent = await response.Content.ReadAsStringAsync();
        //        return JsonConvert.DeserializeObject<StandardFr8TerminalCM>(responseContent);
        //    }
        //    catch (Exception) // Expect an exception if the request failed
        //    {
        //        throw;
        //    }
        //}

        private async Task <DiscoveryResult> DiscoverInternal(TerminalDO terminalDo, bool isUserInitiated)
        {
            var terminalUrl = terminalDo.Endpoint;

            Logger.Info($"Starting discovering terminal at '{terminalUrl}'. Reporting about self as the Hub at '{_serverUrl}'.");


            string secret = null;

            if (terminalDo.ParticipationState == ParticipationState.Blocked)
            {
                var message = $"Discovery for terminal '{terminalUrl}' will not happen because the terminal is blocked.";

                Logger.Info(message);
                return(DiscoveryResult.Error(message));
            }

            if (terminalDo.ParticipationState == ParticipationState.Deleted)
            {
                var message = $"Discovery for terminal '{terminalUrl}' will not happen because the terminal is deleted.";
                Logger.Info(message);
                return(DiscoveryResult.Error(message));
            }

            if (!string.IsNullOrWhiteSpace(terminalDo?.Secret))
            {
                secret = terminalDo.Secret;
            }

            if (secret == null)
            {
                Logger.Info($"Generating new secret for terminal at '{terminalUrl}'");
                secret = Guid.NewGuid().ToString("N");
            }

            var headers = new Dictionary <string, string>
            {
                { "Fr8HubCallbackSecret", secret },
                { "Fr8HubCallBackUrl", _serverUrl }
            };

            StandardFr8TerminalCM terminalRegistrationInfo = null;

            try
            {
                terminalRegistrationInfo = await SendDiscoveryRequest(terminalUrl, headers);
            }
            catch (Exception ex)
            {
                Logger.Warn("Failed to call terminal discovery endpoint", ex);

                _eventReporter.ActivityTemplateTerminalRegistrationError($"Failed terminal service: {terminalUrl}. Error Message: {ex.Message} ", ex.GetType().Name);
                terminalRegistrationInfo = null;
            }

            if (terminalRegistrationInfo == null || terminalRegistrationInfo.Definition == null || terminalRegistrationInfo.Activities == null)
            {
                // Discovery failed
                var message = $"Terminal at '{terminalUrl}'  didn't return a valid response to the discovery request.";

                Logger.Warn(message);
                // Set terminal status inactive
                terminalDo.OperationalState = OperationalState.Inactive;

                try
                {
                    _terminal.RegisterOrUpdate(terminalDo, isUserInitiated);
                }
                catch (Exception ex)
                {
                    Logger.Error("Failed to update information about the terminal.", ex);
                }

                return(DiscoveryResult.Error(message));
            }

            terminalDo.Secret             = secret;
            terminalDo.OperationalState   = OperationalState.Active;
            terminalDo.AuthenticationType = terminalRegistrationInfo.Definition.AuthenticationType;
            terminalDo.Description        = terminalRegistrationInfo.Definition.Description;
            terminalDo.Label          = terminalRegistrationInfo.Definition.Label;
            terminalDo.Name           = terminalRegistrationInfo.Definition.Name;
            terminalDo.Version        = terminalRegistrationInfo.Definition.Version;
            terminalDo.TerminalStatus = terminalRegistrationInfo.Definition.TerminalStatus;

            if (string.IsNullOrWhiteSpace(terminalDo.Label))
            {
                terminalDo.Label = terminalDo.Name;
            }

            try
            {
                terminalDo = _terminal.RegisterOrUpdate(terminalDo, isUserInitiated);
            }
            catch (Exception ex)
            {
                Logger.Error("Failed to update information about the terminal.", ex);
                return(DiscoveryResult.Error($"Internal error updating the information about the terminal at '{terminalUrl}'"));
            }

            var activityTemplates = terminalRegistrationInfo.Activities.Select(Mapper.Map <ActivityTemplateDO>).ToList();
            var result            = new DiscoveryResult(true, null);

            foreach (var curItem in activityTemplates)
            {
                Logger.Info($"Registering activity '{curItem.Name}' from terminal at '{terminalUrl}'");

                try
                {
                    curItem.Terminal   = terminalDo;
                    curItem.TerminalId = terminalDo.Id;

                    _activityTemplateService.RegisterOrUpdate(curItem);
                    result.SucceededTemplates.Add(curItem);
                }
                catch (Exception ex)
                {
                    _eventReporter.ActivityTemplateTerminalRegistrationError($"Failed to register activity {curItem.Name} of version {curItem.Version} for terminal {terminalDo.Name}. Error Message: {ex.Message}", ex.GetType().Name);

                    Logger.Warn($"Failed to register activity {curItem.Name} of version {curItem.Version} for terminal {terminalDo.Name}", ex);
                    result.FailedTemplates.Add(curItem);
                }
            }

            _activityTemplateService.RemoveInactiveActivities(terminalDo, activityTemplates);

            Logger.Info($"Successfully discovered terminal at '{terminalUrl}'.");

            return(result);
        }