-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathawsiot_handler.js
More file actions
115 lines (108 loc) · 3.51 KB
/
awsiot_handler.js
File metadata and controls
115 lines (108 loc) · 3.51 KB
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
108
109
110
111
112
113
114
115
/**
* Lambda function that sends an Email on click of a button.
* It creates a SNS topic, subscribes an endpoint (EMAIL) to the topic and publishes to the topic.
*/
// email address.
const = '*@*.*';
const AWS = require('aws-sdk');
const SNS = new AWS.SNS({ apiVersion: '2010-03-31' });
function findExistingSubscription(topicArn, nextToken, cb) {
const params = {
TopicArn: topicArn,
NextToken: nextToken || null,
};
SNS.listSubscriptionsByTopic(params, (err, data) => {
if (err) {
console.log('Error listing subscriptions.', err);
cb(err);
return;
}
const subscription = data.Subscriptions.filter((sub) => sub.Protocol === 'email' && sub.Endpoint === EMAIL)[0];
if (!subscription) {
if (!data.NextToken) {
cb(null, null); // indicate that no subscription was found
} else {
findExistingSubscription(topicArn, data.NextToken, cb); // iterate over next token
}
} else {
cb(null, subscription); // a subscription was found
}
});
}
/**
* Subscribe the specified EMAIL to a topic.
*/
function createSubscription(topicArn, cb) {
// check to see if a subscription already exists
findExistingSubscription(topicArn, null, (err, res) => {
if (err) {
console.log('Error finding existing subscription.', err);
cb(err);
return;
}
if (!res) {
// no subscription, create one
const params = {
Protocol: 'email',
TopicArn: topicArn,
Endpoint: EMAIL,
};
SNS.subscribe(params, (err, data) => {
if (err) {
console.log('Error setting up email subscription.', err);
cb(err);
return;
}
// subscription complete
console.log(`Subscribed ${EMAIL} to ${topicArn}.`);
cb(null, topicArn);
});
} else {
// subscription already exists, continue
cb(null, topicArn);
}
});
}
/**
* Create a topic.
*/
function createTopic(topicName, cb) {
SNS.createTopic({ Name: topicName }, (err, data) => {
if (err) {
console.log('Creating topic failed.', err);
cb(err);
return;
}
const topicArn = data.TopicArn;
console.log(`Created topic: ${topicArn}`);
console.log('Creating subscriptions.');
createSubscription(topicArn, (err, data) => {
if (err) {
cb(err);
return;
}
// everything is good
console.log('Topic setup complete.');
cb(null, topicArn);
});
});
}
exports.handler = (event, context, callback) => {
console.log('Received event:', event.clickType);
// create/get topic
createTopic('aws-iot-button-sns-topic', (err, topicArn) => {
if (err) {
callback(err);
return;
}
console.log(`Publishing to topic ${topicArn}`);
// publish message
const params = {
Message: `${event.serialNumber} -- processed by Lambda\nBattery voltage: ${event.batteryVoltage}`,
Subject: `Hello from your IoT Button ${event.serialNumber}: ${event.clickType}`,
TopicArn: topicArn
};
// result will go to function callback
SNS.publish(params, callback);
});
};