forked from danilop/lightbulbSwitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightbulbSwitch.js
50 lines (46 loc) · 1.43 KB
/
lightbulbSwitch.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
console.log('Loading function');
var config = {
"thingName": 'lightbulb',
"endpointAddress": "<PUT YOUR ENDPOINT HERE>"
}
var AWS = require('aws-sdk');
var iotdata = new AWS.IotData({endpoint: config.endpointAddress});
exports.handler = function(event, context) {
console.log('Received event:', JSON.stringify(event, null, 2));
iotdata.getThingShadow({
thingName: config.thingName
}, function(err, data) {
if (err) {
context.fail(err);
} else {
console.log(data);
var jsonPayload = JSON.parse(data.payload);
var status = jsonPayload.state.reported.status;
console.log('status: ' + status);
var newStatus;
if (status == 'ON') {
newStatus = 'OFF';
} else {
newStatus = 'ON';
}
var update = {
"state": {
"desired" : {
"status" : newStatus
}
}
};
iotdata.updateThingShadow({
payload: JSON.stringify(update),
thingName: config.thingName
}, function(err, data) {
if (err) {
context.fail(err);
} else {
console.log(data);
context.succeed('newStatus: ' + newStatus);
}
});
}
});
};