Exemple #1
0
        private void ProcessPowerlevelController(string name, AlexaDirective directive)
        {
            log.Info("Processing PowerlevelController request with name '{0}'", name);
            if (name == "SetPowerLevel")
            {
                string targetPowerLevelProp = directive.Payload["powerLevel"].ToString();
                bool   valueParseable       = Int32.TryParse(targetPowerLevelProp, out int targetPowerLevel);


                this.loxoneDispatcher.Tell(new LoxoneMessage.ControlDimmer(AlexaUuidTranslator.ToLoxoneUuid(directive.Endpoint.EndpointId), LoxoneMessage.ControlDimmer.DimType.Set, targetPowerLevel));

                var response = CreateResponse(directive.Header.CorrelationId, directive.Endpoint.EndpointId);
                response.Context.Properties.Add(new AlexaProperty("Alexa.PowerLevelController", "powerLevel", targetPowerLevelProp, DateTime.Now));
                SendResponseToAlexa(response);
            }
            else if (name == "AdjustPowerLevel")
            {
                // TODO
                SendResponseToAlexa(CreateError(directive.Endpoint.EndpointId, AlexaErrorType.INVALID_DIRECTIVE, "Adapter does not support adjusting power level"));
            }
            else
            {
                SendResponseToAlexa(CreateError(directive.Endpoint.EndpointId, AlexaErrorType.INVALID_DIRECTIVE, $"PowerLevelAdapter does not support '{name}'"));
            }
        }
Exemple #2
0
        private void ProcessPowerController(string name, AlexaDirective directive)
        {
            log.Info("Processing PowerController request with name '{0}'", name);
            string targetValue = "";

            if (name == "TurnOn")
            {
                this.loxoneDispatcher.Tell(new LoxoneMessage.ControlSwitch(AlexaUuidTranslator.ToLoxoneUuid(directive.Endpoint.EndpointId), LoxoneMessage.ControlSwitch.DesiredStateType.On));
                targetValue = "ON";
            }
            else if (name == "TurnOff")
            {
                this.loxoneDispatcher.Tell(new LoxoneMessage.ControlSwitch(AlexaUuidTranslator.ToLoxoneUuid(directive.Endpoint.EndpointId), LoxoneMessage.ControlSwitch.DesiredStateType.Off));
                targetValue = "OFF";
            }
            else
            {
                string message = $"Unknown control request '{name}'";
                log.Error(message);
            }
            var response = CreateResponse(directive.Header.CorrelationId, directive.Endpoint.EndpointId);

            response.Context.Properties.Add(new AlexaProperty("Alexa.PowerController", "powerState", targetValue, DateTime.Now));
            SendResponseToAlexa(response);
        }
Exemple #3
0
        private void ProcessDiscovery(string name, AlexaDirective directive)
        {
            log.Info("Processing Discovery request with name '{0}'", name);
            if (name != "Discover")
            {
                string message = $"Unknown discovery request '{name}'";
                log.Error(message);
                return;
            }

            this.discoveryResponseHandler.Tell(new AlexaMessage.PublishDiscoveryResponse());
        }
Exemple #4
0
        private void ProcessModeController(AlexaDirective directive)
        {
            string name       = directive.Header.Name;
            string instance   = directive.Header.Instance;
            string targetMode = directive.Payload["mode"]?.ToString();

            log.Info("Processing MopdeController request with name '{0}' for instance '{1}' targetting '{2}'", name, instance, targetMode);

            if (instance == "Blinds.BlindTargetState")
            {
                if (name == "SetMode")
                {
                    LoxoneMessage.ControlBlinds.BlindCmd command = LoxoneMessage.ControlBlinds.BlindCmd.FullUp;
                    switch (targetMode)
                    {
                    case "BlindTargetState.FullUp":
                        command = LoxoneMessage.ControlBlinds.BlindCmd.FullUp;
                        break;

                    case "BlindTargetState.FullDown":
                        command = LoxoneMessage.ControlBlinds.BlindCmd.FullDown;
                        break;

                    case "BlindTargetState.Stop":
                        command = LoxoneMessage.ControlBlinds.BlindCmd.Stop;
                        break;
                    }
                    this.loxoneDispatcher.Tell(new LoxoneMessage.ControlBlinds(AlexaUuidTranslator.ToLoxoneUuid(directive.Endpoint.EndpointId), command));
                    var response = CreateResponse(directive.Header.CorrelationId, directive.Endpoint.EndpointId);
                    response.Context.Properties.Add(new AlexaProperty("Alexa.ModeController", "mode", targetMode, DateTime.Now));
                    SendResponseToAlexa(response);
                    return;
                }
            }

            SendResponseToAlexa(CreateError(directive.Endpoint.EndpointId, AlexaErrorType.INVALID_DIRECTIVE, $"Directive {name} for instance {instance} is not implemented!"));
        }
Exemple #5
0
        public ReportStateResponse ReportState(ReportStateRequest request)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();

            AlexaDirective directive = request.directive;

            var response = new ReportStateResponse(directive);

            #region Validate request

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

            #endregion Validate request

            #region Connect To Premise Server

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

            #endregion Connect To Premise Server

            #region Verify Access

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

                if (!PremiseServer.CheckAccessTokenAsync(directive.endpoint.scope.localAccessToken).GetAwaiter().GetResult())
                {
                    response.context        = null;
                    [email protected] = new AlexaErrorResponsePayload(AlexaErrorTypes.INVALID_AUTHORIZATION_CREDENTIAL, "Not authorized on local premise server.");
                    return(response);
                }
            }
            catch
            {
                response.context        = null;
                [email protected] = new AlexaErrorResponsePayload(AlexaErrorTypes.INTERNAL_ERROR, "Cannot find Alexa home object on local Premise server.");
                return(response);
            }

            #endregion Verify Access

            #region Get Premise Object

            IPremiseObject endpoint;
            try
            {
                Guid premiseId = new Guid(directive.endpoint.endpointId);
                endpoint = PremiseServer.HomeObject.GetObjectAsync(premiseId.ToString("B")).GetAwaiter().GetResult();
                if (!endpoint.IsValidObject())
                {
                    response.context        = null;
                    [email protected] = new AlexaErrorResponsePayload(AlexaErrorTypes.NO_SUCH_ENDPOINT,
                                                                            $"Cannot find device {directive.endpoint.endpointId} on server.");
                    return(response);
                }
            }
            catch
            {
                response.context        = null;
                [email protected] = new AlexaErrorResponsePayload(AlexaErrorTypes.NO_SUCH_ENDPOINT,
                                                                        $"Cannot find device {directive.endpoint.endpointId} on server.");
                return(response);
            }

            if (directive.header.name != "ReportState")
            {
                response.context        = null;
                [email protected] = new AlexaErrorResponsePayload(AlexaErrorTypes.INVALID_DIRECTIVE, "Invalid Directive");
                return(response);
            }

            #endregion Get Premise Object

            DiscoveryEndpoint discoveryEndpoint = PremiseServer.GetDiscoveryEndpointAsync(endpoint).GetAwaiter().GetResult();
            if (discoveryEndpoint == null)
            {
                response.context        = null;
                [email protected] = new AlexaErrorResponsePayload(AlexaErrorTypes.INTERNAL_ERROR,
                                                                        $"Cannot find or invalid discoveryJson for {directive.endpoint.endpointId} on server.");
                return(response);
            }
            [email protected] = discoveryEndpoint.cookie;
            try
            {
                // use reflection to instantiate all device type controllers
                var interfaceType = typeof(IAlexaDeviceType);
                var all           = AppDomain.CurrentDomain.GetAssemblies()
                                    .SelectMany(x => x.GetTypes())
                                    .Where(x => interfaceType.IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract)
                                    .Select(Activator.CreateInstance);

                foreach (IAlexaDeviceType deviceType in all)
                {
                    var related = deviceType.FindRelatedProperties(endpoint, "");
                    foreach (AlexaProperty property in related)
                    {
                        if (!response.context.propertiesInternal.ContainsKey(property.@namespace + "." + property.name))
                        {
                            response.context.propertiesInternal.Add(property.@namespace + "." + property.name, property);
                        }
                    }
                }

                foreach (Capability capability in discoveryEndpoint.capabilities)
                {
                    switch (capability.@interface)  // scenes are special cased
                    {
                    case "Alexa.SceneController":
                    {
                        AlexaSetSceneController controller = new AlexaSetSceneController(endpoint);
                        AlexaProperty           prop       = controller.GetPropertyStates()[0];
                        [email protected]   = (string)prop.value;
                        [email protected] = new ChangeReportCause
                        {
                            type = "VOICE_INTERACTION"
                        };
                        [email protected] = prop.timeOfSample;
                    }
                    break;
                    }
                }
            }
            catch (Exception ex)
            {
                response.context        = null;
                [email protected] = new AlexaErrorResponsePayload(AlexaErrorTypes.INTERNAL_ERROR, ex.Message);
                return(response);
            }

            [email protected] = "StateReport";
            PremiseServer.InformLastContactAsync($"StateReport: {response.@event?.endpoint?.cookie?.path}").GetAwaiter().GetResult();
            stopwatch.Stop();
            if ([email protected] != null)
            {
                [email protected] = stopwatch.ElapsedMilliseconds;
            }
            return(response);
        }