public Stream Handler(Stream inputStream, ILambdaContext context)
        {
            StreamReader reader  = new StreamReader(inputStream);
            string       request = reader.ReadToEnd();

            //Console.Out.WriteLine("Request:");
            //Console.Out.WriteLine(request);

            if (context != null)
            {
                context.Logger.Log("Request:");
                context.Logger.Log(request);
            }

            AlexaResponse alexaResponse;

            JObject jRequest = JObject.Parse(request);

            string nameSpace = jRequest["directive"]["header"]["namespace"].Value <string>();
            string name      = jRequest["directive"]["header"]["name"].Value <string>();

            switch (nameSpace)
            {
            //case "Alexa":
            //    switch (name)
            //    {
            //        case "ReportState":
            //            string requestedEndpointId = jRequest["directive"]["endpoint"]["endpointId"].Value<string>();

            //            if (context != null)
            //                context.Logger.Log($"Alexa::ReportState Request: {requestedEndpointId}");


            //            alexaResponse = new AlexaResponse("Alexa", "StateReport");
            //            break;

            //        default:
            //            if(context != null)
            //                context.Logger.Log("INVALID name in Namespace Alexa.");

            //            alexaResponse = new AlexaResponse();
            //            break;
            //    }

            //    break;

            case "Alexa.Authorization":
                if (context != null)
                {
                    context.Logger.Log("Alexa.Authorization Request");
                }

                alexaResponse = new AlexaResponse("Alexa.Authorization", "AcceptGrant.Response");
                break;

            case "Alexa.Discovery":
                if (context != null)
                {
                    context.Logger.Log("Alexa.Discovery Request");
                }

                alexaResponse = new AlexaResponse("Alexa.Discovery", "Discover.Response", "endpoint-001");

                JObject jCapabilityAlexa = JObject.Parse(alexaResponse.CreatePayloadEndpointCapability());

                JObject jPropertyPowerstate = new JObject();
                jPropertyPowerstate.Add("name", "powerState");
                JObject jCapabilityAlexaPowerController = JObject.Parse(alexaResponse.CreatePayloadEndpointCapability("AlexaInterface", "Alexa.PowerController", "3", jPropertyPowerstate.ToString()));

                JArray capabilities = new JArray();
                capabilities.Add(jCapabilityAlexa);
                capabilities.Add(jCapabilityAlexaPowerController);

                alexaResponse.AddPayloadEndpoint("endpoint-001", capabilities.ToString());
                break;

            case "Alexa.PowerController":
                if (context != null)
                {
                    context.Logger.Log("Alexa.PowerController Request");
                }

                string correlationToken = jRequest["directive"]["header"]["correlationToken"].Value <string>();
                string endpointId       = jRequest["directive"]["endpoint"]["endpointId"].Value <string>();

                string state = (name == "TurnOff") ? "OFF" : "ON";

                bool result = StoreDeviceState(endpointId, "powerState", state);

                if (result)
                {
                    alexaResponse = new AlexaResponse("Alexa", "Response", endpointId, "INVALID", correlationToken);
                    alexaResponse.AddContextProperty("Alexa.PowerController", "powerState", state, 200);
                }
                else
                {
                    JObject jPayloadError = new JObject();
                    jPayloadError.Add("type", "ENDPOINT_UNREACHABLE");
                    jPayloadError.Add("message", "There was an error setting the device state.");
                    alexaResponse = new AlexaResponse("Alexa", "ErrorResponse");
                    alexaResponse.SetPayload(jPayloadError.ToString());
                }
                break;

            default:
                if (context != null)
                {
                    context.Logger.Log("INVALID Namespace");
                }

                alexaResponse = new AlexaResponse();
                break;
            }

            string response = alexaResponse.ToString();

            if (context != null)
            {
                context.Logger.Log("Response:");
                context.Logger.Log(response);
            }

            return(new MemoryStream(Encoding.UTF8.GetBytes(response)));
        }
Beispiel #2
0
        public Stream Handler(Stream inputStream, ILambdaContext context)
        {
            StreamReader reader  = new StreamReader(inputStream);
            string       request = reader.ReadToEnd();

            Console.Out.WriteLine("Request:");
            Console.Out.WriteLine(request);

            if (context != null)
            {
                context.Logger.Log("Request:");
                context.Logger.Log(request);
            }

            AlexaResponse ar;

            JObject jsonRequest = JObject.Parse(request);
            string  nameSpace   = jsonRequest["directive"]["header"]["namespace"].Value <string>();

            switch (nameSpace)
            {
            case "Alexa.Authorization":
                if (context != null)
                {
                    context.Logger.Log("Alexa.Authorization Request");
                }
                ar = new AlexaResponse("Alexa.Authorization", "AcceptGrant.Response");
                break;

            case "Alexa.Discovery":
                if (context != null)
                {
                    context.Logger.Log("Alexa.Discovery Request");
                }

                ar = new AlexaResponse("Alexa.Discovery", "Discover.Response", "endpoint-001");

                JObject capabilityAlexa = JObject.Parse(ar.CreatePayloadEndpointCapability());

                JObject propertyPowerstate = new JObject
                {
                    { "name", "powerState" }
                };
                JObject capabilityAlexaPowerController = JObject.Parse(ar.CreatePayloadEndpointCapability("AlexaInterface", "Alexa.PowerController", "3", propertyPowerstate.ToString()));

                JArray capabilities = new JArray
                {
                    capabilityAlexa,
                    capabilityAlexaPowerController
                };

                ar.AddPayloadEndpoint("endpoint-001", capabilities.ToString());

                break;

            case "Alexa.PowerController":
                if (context != null)
                {
                    context.Logger.Log("Alexa.PowerController Request");
                }
                string correlationToken = jsonRequest["directive"]["header"]["correlationToken"].Value <string>();
                string endpointId       = jsonRequest["directive"]["endpoint"]["endpointId"].Value <string>();
                string name             = jsonRequest["directive"]["header"]["name"].Value <string>();

                string state = (name == "TurnOff") ? "OFF" : "ON";

                bool result = SendDeviceState(endpointId, "powerState", state);
                if (result)
                {
                    ar = new AlexaResponse("Alexa", "Response", endpointId, "INVALID", correlationToken);
                    ar.AddContextProperty("Alexa.PowerController", "powerState", state, 200);
                }
                else
                {
                    JObject payloadError = new JObject
                    {
                        { "type", "ENDPOINT_UNREACHABLE" },
                        { "message", "There wa an error setting the device state." }
                    };
                    ar = new AlexaResponse("Alexa", "ErrorResponse");
                    ar.SetPayload(payloadError.ToString());
                }

                break;

            default:
                if (context != null)
                {
                    context.Logger.Log("INVALID Namespace");
                }
                ar = new AlexaResponse();
                break;
            }

            string response = ar.ToString();

            if (context != null)
            {
                context.Logger.Log("Response:");
                context.Logger.Log(response);
            }

            return(new MemoryStream(Encoding.UTF8.GetBytes(response)));
        }