Example #1
0
        /// <summary>
        /// Set up the service and query the state of the person directory etc
        /// </summary>
        /// <returns></returns>
        private async Task InitializeAzureService()
        {
            Logging.Log("Starting Azure Face Service...");
            bool exists = false;

            try
            {
                var groups = await _transThrottle.Call("List", _faceClient.PersonDirectory.ListDynamicPersonGroupsAsync());

                if (groups.Any(x => x.DynamicPersonGroupId == GroupId))
                {
                    var faces = await _transThrottle.Call("List", _faceClient.PersonDirectory.GetPersonsAsync());

                    _persistedFaces = faces.Count();
                    Logging.Log($"Azure directory currently contains {_persistedFaces} recognisable faces.");
                    exists = true;
                }
                Logging.Log($"Azure - Found {_persistedFaces} in PersonDirectory Group: {GroupId}");
            }
            catch (APIErrorException ex)
            {
                Logging.LogWarning($"Failed to initialise Azure Group: {ex.Message} [{ex.Response.Content}]");
                exists = false;
            }

            if (!exists)
            {
                try
                {
                    var body = new DynamicPersonGroupCreateRequest
                    {
                        Name = GroupId
                    };
                    var response = await _transThrottle.Call("CreateGroup", _faceClient.PersonDirectory.CreateDynamicPersonGroupAsync(GroupId, body));

                    // TODO: Use response.OperationLocation to wait for it to complete

                    Logging.Log($"Created Azure person group: {GroupId}");
                    exists = true;
                }
                catch (Exception ex)
                {
                    Logging.LogError($"Failed to create person group: {ex.Message}");
                }
            }

            if (!exists)
            {
                Logging.LogError("Unable to create Azure face group. Azure face detection will be disabled.");
                _faceClient    = null;
                _detectionType = AzureDetection.Disabled;
            }
        }
Example #2
0
        private void InitFromConfig()
        {
            var endpoint = _configService.Get(ConfigSettings.AzureEndpoint);
            var key      = _configService.Get(ConfigSettings.AzureApiKey);

            if (!string.IsNullOrEmpty(endpoint) && !string.IsNullOrEmpty(key))
            {
                // We have config and have set up the service, now figure out what we're going to use it for
                _detectionType = _configService.Get(ConfigSettings.AzureDetectionType, AzureDetection.Disabled);
            }
            else
            {
                // No config, so we're disabled.
                _detectionType = AzureDetection.Disabled;
            }

            if (_detectionType != AzureDetection.Disabled)
            {
                var useFreeTier = _configService.GetBool(ConfigSettings.AzureUseFreeTier, true);

                if (useFreeTier)
                {
                    // Free tier allows 20 trans/min, and a max of 30k per month
                    _transThrottle.SetLimits(20, 30000);
                }
                else
                {
                    // Standard paid tier allows 10 trans/sec, and unlimited. But at 10 txn/sec,
                    // the actual max is 30 million per month (about 26784000 per month). So
                    // limit to 30 million.
                    _transThrottle.SetLimits(600, 30000000);
                }

                // This is a bit sucky - it basically ignores cert issues completely, which is a bit of a security risk.
                HttpClientHandler clientHandler = new HttpClientHandler();
                clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return(true); };

                _httpClient = new HttpClient(clientHandler);
                var agent = new ProductInfoHeaderValue("Damselfly", $"{Assembly.GetExecutingAssembly().GetName().Version}");
                _httpClient.DefaultRequestHeaders.UserAgent.Add(agent);

                ApiKeyServiceClientCredentials creds = new ApiKeyServiceClientCredentials(key);
                _faceClient          = new FaceClient(creds, _httpClient, true);
                _faceClient.Endpoint = endpoint;
            }
        }