// IRKitデバイス情報
const IRKit_DEVICE = process.env.My_IRKit_NAME;
const clientkey = process.env.My_ClientKey;
const deviceid = process.env.My_DeviceID;
const IRKit_EndPoint_ID = IRKit_DEVICE + "-light-" + deviceid; // 照明デバイスの識別子
// エラー情報
const sendError = Error("Send error");
const unknownError = Error("Unknown error");
// リモコン信号(*2)
const LightIR = {
on: { "format":"raw",
"freq":38,
"data":[18031,8755,1190,1037,1190,3228,1190,1037,1190,1037,1190,1037,
1190,1037,1190,1037,1190,3228,1190,3228,1190,1037,1190,3228,
1190,3228,1190,1037,1190,3228,1190,3228,1190,1037,1190,3228,
1190,3228,1190,3228,1190,3228,1190,3228,1190,1037,1190,1037,
1190,1037,1190,1037,1190,1037,1190,1037,1190,1037,1190,1037,
1190,3228,1190,3228,1190,3228,1190,65535,0,18031,18031,4400,
1190,65535,0,65535,0,60108,17421,4400,1190]
},
off: {"format":"raw",
"freq":38,
"data":[18031,8755,1190,1037,1190,3228,1190,1037,1190,1037,1190,1037,
1190,1037,1190,1037,1190,3228,1190,3228,1190,1037,1190,3228,
1190,3228,1190,1037,1190,3228,1190,3228,1190,1037,1190,1037,
1190,3228,1190,3228,1190,3228,1190,3228,1190,1037,1190,1037,
1190,1037,1190,3228,1190,1037,1190,1037,1190,1037,1190,1037,
1190,3228,1190,3228,1190,3228,1190,65535,0,18031,18031,4400,
1190,65535,0,65535,0,60108,17421,4400,1190]
},
};
exports.handler = function (request, context, callback) {
var header = request.directive.header;
var req_name = header.name;
if (header.namespace === 'Alexa.Discovery' &&
req_name === 'Discover') {
log("DEGUG:", "Discover request", JSON.stringify(request));
handleDiscovery(request, context, "");
}
else if (header.namespace === 'Alexa.PowerController') {
var endpoint = request.directive.endpoint;
var deviceId = endpoint.endpointId; // 電源操作対象のデバイス識別子
if (req_name === 'TurnOn' || req_name === 'TurnOff') {
log("DEBUG:", "TurnOn or TurnOff Request", JSON.stringify(request));
log("DEBUG:", "endpointId=", deviceId);
handlePowerControl(request, context, callback);
}
}
function handleDiscovery(request, context) {
var payload = {
"endpoints":
[
{
"endpointId": IRKit_EndPoint_ID,
"manufacturerName": "IRKit",
"friendlyName": "リビングの照明", // このデバイス名で検出されます(*1)
"description": "リビングの照明をIRKitで操作できます。",
"displayCategories": ["LIGHT"], // 照明器具
"cookie": {
},
"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 sendIR(ir, callback) {
var https = require("https");
var qs = require("querystring");
var postData = qs.stringify({
clientkey: clientkey,
deviceid: deviceid,
message: JSON.stringify(ir)
});
var req = https.request({
host: "api.getirkit.com",
path: "/1/messages",
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
"Content-Length": postData.length
}
}, function (res) {
var result = res.statusCode;
if (result == 200) {
console.log("res.statusCode=" + result); // ログ出力
callback(null, "success"); // 送信成功
}
else {
console.log(postData); // ログ出力
console.log("res.statusCode=" + result); // ログ出力
callback(sendError); // 送信エラーを通知
}
});
req.write(postData);
req.end();
}
function handlePowerControl(request, context, callback) {
// get device ID passed in during discovery
var requestMethod = request.directive.header.name;
// get user token pass in request
//var requestToken = request.directive.endpoint.scope.token;
var responseHeader = request.directive.header;
var endpoint = request.directive.endpoint;
var deviceId = endpoint.endpointId; // 電源操作対象のデバイス識別子
var powerResult;
var ir;
if (requestMethod === "TurnOn") {
ir = LightIR.on;
powerResult = "ON";
}
else if (requestMethod === "TurnOff") {
ir = LightIR.off;
powerResult = "OFF";
}
var contextResult = {
"properties": [{
"namespace": "Alexa.PowerController",
"name": "powerState",
"value": powerResult,
"timeOfSample": "2017-12-24T16:20:50.52Z",
"uncertaintyInMilliseconds": 0
}]
};
responseHeader.namespace = "Alexa";
responseHeader.name = "Response";
responseHeader.messageId = responseHeader.messageId + "-R";
var response = {
context: contextResult,
event: {
header: responseHeader,
endpoint: {
"endpointId": deviceId,
}
},
payload: {}
};
sendIR(ir, function (error) {
log("DEBUG:", "Alexa.PowerController ", JSON.stringify(response));
if (error) {
callback(error);
}
else {
context.succeed(response);
}
});
}
};
|