我在发现Alexa智能家居技能的设备时遇到问题。
工作步骤:
- 激活Alexa技能
- OAuth登录屏幕出现。成功登录后,将触发设备的发现
- 在lambda函数中,我获得了用于调用/设备端点
- 我从REST端点获取设备,并构造有效负载,如中所述https://developer.amazon.com/de/docs/smarthome/steps-to-build-a-smart-home-skill.html
- 将有效载荷(与示例中的结构相同(提供给context.success
我的问题:Alexa技能从发现设备任务返回后,Alexa技巧中看不到新设备
当我使用样本中的代码时(其中没有发生对外部Rest API的请求(,在Alexa发现任务之后,设备在Alexas技能中可见。
var https = require('https');
const AWS = require('aws-sdk');
exports.handler = function(request, context) {
var options = {
method: 'GET',
hostname: 'xyz.azurewebsites.net',
path: '/devices',
headers: {
Authorization: 'Bearer ' + request.directive.payload.scope.token,
'Content-Type': 'application/json'
}
};
var req = https.get(options, (response) => {
var data = '';
response.setEncoding('utf8');
response.on('data', function(x) { data += x; } );
response.on('error', console.error);
response.on('end', () => {
var dataObj = JSON.parse(data);
console.log("Retrieved response: " + JSON.stringify(dataObj.items));
const payload = {
"endpoints": []
};
dataObj.items.forEach(item => {
const device = {
"endpointId": item.id,
"manufacturerName": item.manufacturer,
"friendlyName": item.displayName,
"description": item.description,
"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
}
}
]
};
payload.endpoints.push(device);
});
console.log('payload ' + JSON.stringify(payload));
var header = request.directive.header;
header.name = "Discover.Response";
console.log("DEBUG", "Discovery Response: ", JSON.stringify({ header: header, payload: payload }));
//NEXT LINE IS EXECUTED WITHOUT ANY ERROR
context.succeed({ event: { header: header, payload: payload } });
});
});
req.on('error', (e) => {
console.log('problem with request: ' + e.message);
});
};
我发现了问题。。。属性"endpointId"的值包含一个"@"。然后我把名字改成只有字母,这就奏效了。尽管在这篇文章中说可以使用"@",但设备的发现也存在问题。希望这个答案能帮助其他人不要浪费时间。。。
我发现了同样症状的另一个原因:对于实体的附加属性(制造商、型号等(,不能使用非英文字符。实际上,您可以使用从32到126的任意字符ASCII(空格到波浪号(,但不能使用反斜杠。因此,不允许使用重音字符(国际或扩展ASCII(。
另一方面,我可以在endpointId中包含一个带有"@"的实体。我无法解释你为什么不能。