Exemplo n.º 1
0
        public DiscoveryControllerResponse Discovery(AlexaDiscoveryControllerRequest request)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();
            AlexaDiscoveryController controller = new AlexaDiscoveryController(request);

            if (controller.ValidateDirective())
            {
                controller.ProcessControllerDirective();
            }
            stopwatch.Stop();
            PremiseServer.WriteToWindowsApplicationEventLog(EventLogEntryType.Information, $"Discovery processing time {stopwatch.ElapsedMilliseconds}ms", 51);
            return(controller.Response);
        }
Exemplo n.º 2
0
        public AuthorizationResponse Authorization(AuthorizationRequest request)
        {
            AuthorizationDirective directive = request.directive;

            var response = new AuthorizationResponse(directive);

            #region Validate request

            if (directive.header.payloadVersion != "3")
            {
                [email protected] = new AlexaErrorResponsePayload(AlexaErrorTypes.INVALID_DIRECTIVE, "Invalid payload version.");
                return(response);
            }

            #endregion Validate request

            #region Verify Access

            if ((directive.payload.grantee == null) || (directive.payload.grantee.type != "BearerToken"))
            {
                [email protected] = new AlexaErrorResponsePayload(AlexaErrorTypes.INVALID_DIRECTIVE, "Invalid bearer token.");
                return(response);
            }

            try
            {
                if (!PremiseServer.CheckAccessTokenAsync(directive.payload.grantee.localAccessToken).GetAwaiter().GetResult())
                {
                    [email protected] = new AlexaErrorResponsePayload(AlexaErrorTypes.INVALID_AUTHORIZATION_CREDENTIAL, "Not authorized on local premise server.");
                    return(response);
                }
            }
            catch (Exception ex)
            {
                [email protected] = new AlexaErrorResponsePayload(AlexaErrorTypes.INTERNAL_ERROR, ex.Message);
                return(response);
            }

            #endregion Verify Access

            try
            {
                if (PremiseServer.HomeObject == null)
                {
                    [email protected] = new AlexaErrorResponsePayload(AlexaErrorTypes.ENDPOINT_UNREACHABLE, "Premise Server.");
                    return(response);
                }

                using (PremiseServer.AsyncObjectsLock.Lock())
                {
                    PremiseServer.HomeObject.SetValueAsync("AlexaAsyncAuthorizationCode", directive.payload.grant.access_token).GetAwaiter().GetResult();
                    PremiseServer.HomeObject.SetValueAsync("AlexaAsyncAuthorizationRefreshToken", directive.payload.grant.refresh_token).GetAwaiter().GetResult();
                    PremiseServer.HomeObject.SetValueAsync("AlexaAsyncAuthorizationClientId", directive.payload.grant.client_id).GetAwaiter().GetResult();
                    PremiseServer.HomeObject.SetValueAsync("AlexaAsyncAuthorizationSecret", directive.payload.grant.client_secret).GetAwaiter().GetResult();

                    DateTime expiry = DateTime.UtcNow.AddSeconds(directive.payload.grant.expires_in);
                    PremiseServer.HomeObject.SetValueAsync("AlexaAsyncAuthorizationCodeExpiry", expiry.ToString(CultureInfo.InvariantCulture)).GetAwaiter().GetResult();
                }

                const string message = "Skill is now enabled and authorized to send async updates to Alexa. A task has been started to subscribe to property change events.";
                PremiseServer.InformLastContactAsync(message).GetAwaiter().GetResult();
                PremiseServer.WriteToWindowsApplicationEventLog(EventLogEntryType.Information, message, 60);

                Task.Run(async() =>
                {
                    // Generate Discovery Json
                    await PremiseServer.HomeObject.SetValueAsync("GenerateDiscoveryJson", "True").ConfigureAwait(false);
                    // Signal sending async property change events - this will also subscribe to all properties
                    await PremiseServer.HomeObject.SetValueAsync("SendAsyncEventsToAlexa", "True").ConfigureAwait(false);
                });
            }
            catch (Exception ex)
            {
                [email protected] = new AlexaErrorResponsePayload(AlexaErrorTypes.INTERNAL_ERROR, ex.Message);
                return(response);
            }

            return(response);
        }