【问题标题】:Alexa Smart Home Skill - Need help to make an http requestAlexa Smart Home Skill - 需要帮助才能发出 http 请求
【发布时间】:2020-12-28 14:23:44
【问题描述】:

我正在尝试自己构建一个家庭自动化系统。为此,我使用 Blynk 服务器来控制我的硬件。我可以通过请求 blynk 服务器的 URL 来控制我的硬件。

例如:当我向https://139.59.206.133/myprivatekey/update/V1?value=1 发出请求并将虚拟引脚更新为“1”时,我的灯亮了。

我用它来制作一个 Alexa 自定义技能,它能够通过向 Blynk 服务器发出 HTTPS 请求来打开我的灯。例如,当我说“Alexa 让我的房间开灯”时。自定义技能正在发挥应有的作用。

但是自定义技能并不是我真正想要的,所以我决定构建一个 Alexa 智能家居技能。所以我设置了一个,只是尝试在调用“TurnON”或“TurnOFF”时发出 HTTPS 请求。

我的问题是每次我尝试发出 HTTPS 请求时,Alexa 都会说设备没有响应。

我尝试了很多东西,但我自己无法解决我的问题。

代码:

代码来自 node.js Alexa 智能家居示例。

    exports.handler = function (request, context) {
    if (request.directive.header.namespace === 'Alexa.Discovery' && request.directive.header.name === 'Discover') {
        log("DEBUG:", "Discover request",  JSON.stringify(request));
        handleDiscovery(request, context, "");
    }
    else if (request.directive.header.namespace === 'Alexa.PowerController') {
        if (request.directive.header.name === 'TurnOn' || request.directive.header.name === 'TurnOff') {
            log("DEBUG:", "TurnOn or TurnOff Request", JSON.stringify(request));
            handlePowerControl(request, context);
        }
    }

    function handleDiscovery(request, context) {
        var payload = {
            "endpoints":
            [
                {
                    "endpointId": "demo_id",
                    "manufacturerName": "Smart Device Company",
                    "friendlyName": "Zimmerlicht",
                    "description": "Smart Device Switch",
                    "displayCategories": ["SWITCH"],
                    "cookie": {
                        "key1": "arbitrary key/value pairs for skill to reference this endpoint.",
                        "key2": "There can be multiple entries",
                        "key3": "but they should only be used for reference purposes.",
                        "key4": "This is not a suitable place to maintain current endpoint state."
                    },
                    "capabilities":
                    [
                        {
                          "type": "AlexaInterface",
                          "interface": "Alexa",
                          "version": "3"
                        },
                        {
                            "interface": "Alexa.PowerController",
                            "version": "3",
                            "type": "AlexaInterface",
                            "properties": {
                                "supported": [{
                                    "name": "powerState"
                                }],
                                 "retrievable": true
                            }
                        }
                    ]
                }
            ]
        };
        var header = request.directive.header;
        header.name = "Discover.Response";
        log("DEBUG", "Discovery Response: ", JSON.stringify({ header: header, payload: payload }));
        context.succeed({ event: { header: header, payload: payload } });
    }

    function log(message, message1, message2) {
        console.log(message + message1 + message2);
    }

    function handlePowerControl(request, context) {
        // get device ID passed in during discovery
        var requestMethod = request.directive.header.name;
        var responseHeader = request.directive.header;
        responseHeader.namespace = "Alexa";
        responseHeader.name = "Response";
        responseHeader.messageId = responseHeader.messageId + "-R";
        // get user token pass in request
        var requestToken = request.directive.endpoint.scope.token;
        var powerResult;

        if (requestMethod === "TurnOn") {
            const Http = new XMLHttpRequest();
            const url='https://139.59.206.133/myprivatekey/update/V1?value=1';
            Http.open("GET", url);
            Http.send();
            // Make the call to your device cloud for control
            // powerResult = stubControlFunctionToYourCloud(endpointId, token, request);
            powerResult = "ON";
        }
       else if (requestMethod === "TurnOff") {
            var xmlHttp = new XMLHttpRequest();
            xmlHttp.open( "GET", 'http://myprivatekey/update/V0?value=0', false ); // false for synchronous request
            xmlHttp.send( null );
            // Make the call to your device cloud for control and check for success
            // powerResult = stubControlFunctionToYourCloud(endpointId, token, request);
            powerResult = "OFF";
        }
        var contextResult = {
            "properties": [{
                "namespace": "Alexa.PowerController",
                "name": "powerState",
                "value": powerResult,
                "timeOfSample": "2017-09-03T16:20:50.52Z", //retrieve from result.
                "uncertaintyInMilliseconds": 50
            }]
        };
        var response = {
            context: contextResult,
            event: {
                header: responseHeader,
                endpoint: {
                    scope: {
                        type: "BearerToken",
                        token: requestToken
                    },
                    endpointId: "demo_id"
                },
                payload: {}
            }
        };
        log("DEBUG", "Alexa.PowerController ", JSON.stringify(response));
        context.succeed(response);
    }
};

我的要求:

    if (requestMethod === "TurnOn") {
            const Http = new XMLHttpRequest();
            const url='https://139.59.206.133/myprivatekey/update/V1?value=1';
            Http.open("GET", url);
            Http.send();
            // Make the call to your device cloud for control
            // powerResult = stubControlFunctionToYourCloud(endpointId, token, request);
            powerResult = "ON";
        }
       else if (requestMethod === "TurnOff") {
            var xmlHttp = new XMLHttpRequest();
            xmlHttp.open( "GET", 'http://myprivatekey/update/V0?value=0', false ); // false for synchronous request
            xmlHttp.send( null );
            // Make the call to your device cloud for control and check for success
            // powerResult = stubControlFunctionToYourCloud(endpointId, token, request);
            powerResult = "OFF";
        }

其他信息:

  • 编程语言node.js
  • 应正确设置帐户关联、IAM 角色以及创建智能家居技能的所有内容

我对 Alexa 技能构建比较陌生,而且我也不太擅长 JavaScript。

【问题讨论】:

  • 死灯打开/关闭?我的意思是 https 请求是否通过?

标签: node.js alexa alexa-skill home-automation


【解决方案1】:

如果请求通过,我的意思是您的代码正在被调用,并且灯正在打开/关闭,问题在于响应。

我的猜测是您缺少相关 ID。

您需要添加如下内容:

responseHeader.correlationToken = request.directive.header.correlationToken;

您还应该实施 EndpointHealth:https://developer.amazon.com/en-US/docs/alexa/device-apis/alexa-endpointhealth.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-24
    相关资源
    最近更新 更多