-
Notifications
You must be signed in to change notification settings - Fork 0
/
action-handler.js
51 lines (48 loc) · 2.14 KB
/
action-handler.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
var path = require('path');
var { client } = require(path.join(__dirname, 'sparql'));
var { selectThermostatByLocation, insertTemperature } = require(path.join(__dirname, 'sparql-queries'));
var updateTemperature = function(location, amount, unit, calcTemperature) {
const query = selectThermostatByLocation(location);
console.log('Query thermostat: ' + query);
client.query(query)
.execute()
.then(function (response) {
if (response.results.bindings[0] && response.results.bindings[0].thermostat) {
currTemperature = parseInt(response.results.bindings[0].temperature.value);
newTemperature = calcTemperature(currTemperature, amount, unit);
thermostat = response.results.bindings[0].thermostat.value;
const updateQuery = insertTemperature(thermostat, newTemperature);
console.log('Update temperature: ' + updateQuery);
client.query(updateQuery).execute();
} else {
console.log("No thermostat found");
}
})
.catch(function (error) {
console.log(error);
});
}
var handleAction = function(action, params) {
if (params.temperature == undefined) { params.temperature = { amount: 2, unit: 'C' }; }
switch (action) {
case 'temperature.increase':
console.log('Handle temperature increase action');
var calcTemperature = function(oldTemperature, amount, unit) { return oldTemperature + amount; }
updateTemperature(params.location, params.temperature.amount, params.temperature.unit, calcTemperature);
break;
case 'temperature.decrease':
console.log('Handle temperature decrease action');
var calcTemperature = function(oldTemperature, amount, unit) { return oldTemperature - amount; }
updateTemperature(params.location, params.temperature.amount, params.temperature.unit, calcTemperature);
break;
case 'temperature.set':
console.log('Handle set temperature to ' + params.temperature.amount);
var calcTemperature = function(oldTemperature, amount, unit) { return amount; }
updateTemperature(params.location, params.temperature.amount, params.temperature.unit, calcTemperature);
break;
default:
console.log('No action handler found');
break;
}
}
module.exports = handleAction;