-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
107 lines (85 loc) · 3.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
var Domoticz = require('./node_modules/domoticz-api/api/domoticz');
var conf = require('./conf.json');
var api = new Domoticz({
protocol: conf.protocol,
host: conf.host,
port: conf.port,
username: conf.username,
password: conf.password
});
function close(sessionAttributes, fulfillmentState, message) {
console.log("Do i get here??")
return {
sessionAttributes,
dialogAction: {
type: 'Close',
fulfillmentState,
message,
},
};
}
function getDevs(intentRequest, callback){
const sessionAttributes = intentRequest.sessionAttributes || {};
var appliances = [];
api.getDevices({}, function (error, devices) {
var devArray = devices.results;
if (devArray) {
for (var i = 0; i < devArray.length; i++) {
var device = devArray[i];
// Omit devices which aren't in a room plan
if (device.planID === '0')
continue;
var devType = device.type;
var setswitch = device.switchType;
var dz_name = device.name;
if (device.description !== "") {
// Search for Alexa_Name string, ignore casesensitive and whitespaces
var regex = /Alexa_Name:\s*(.+)/im;
var match = regex.exec(device.description);
if (match !== null) {
dz_name = match[1].trim();
}
}
var appliancename = {
friendlyName: dz_name,
status: device.data
};
appliances.push(appliancename);
}
let Devlist = JSON.stringify(appliances);
devme = Devlist.toString()
// devme = devme.replace(/[{()}]/g, '');
devme = devme.replace(/[\[\]']+/g, '');
speechText = devme;
}
callback(close(sessionAttributes, 'Fulfilled',
{ contentType: 'PlainText', content: speechText }));
})
}
function dispatch(intentRequest, callback) {
const intentName = intentRequest.currentIntent.name;
if (intentName === 'getDeviceStatus') {
return getDevs(intentRequest, callback);
}
throw new Error(`Intent with name ${intentName} not supported`);
}
// --------------- Main handler -----------------------
function loggingCallback(response, originalCallback) {
console.log(JSON.stringify(response, null, 2));
originalCallback(null, response);
}
// Route the incoming request based on intent.
// The JSON body of the request is provided in the event slot.
exports.handler = (event, context, callback) => {
try {
// By default, treat the user request as coming from the America/New_York time zone.
process.env.TZ = 'America/New_York';
console.log(`event.bot.name=${event.bot.name}`);
if (event.bot.name !== 'ControlDomoticz') {
callback('Invalid Bot Name');
}
dispatch(event, (response) => loggingCallback(response, callback));
} catch (err) {
callback(err);
}
};