-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
50 lines (42 loc) · 1.42 KB
/
main.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
var mongoClient = require('mongodb').MongoClient,
mubsub = require('mubsub'),
config = require('./conf/config'),
Ntwitter = require('ntwitter');
var twitter = new Ntwitter({
consumer_key: config.twitter_consumer_key,
consumer_secret: config.twitter_consumer_secret,
access_token_key: config.twitter_access_token,
access_token_secret: config.twitter_token_secret
});
mongoClient.connect('mongodb://' + config.mongo_host + ':' + config.mongo_port + '/' + config.mongo_database, function(err, conn) {
if(err){
console.log(err.message);
throw new Error(err);
} else {
db = conn;
var channel = mubsub(db).channel('pubsub');
channel.on('error', console.error);
main(channel);
}
});
function main(mongopubsub) {
mongopubsub.subscribe('events', function (event) {
console.log(event);
if(event.type == 'trigger' && event.level == 'alarmed') {
var message = 'Server ' + event.hostname + ' ' + event.level + ' with ' + event.value + ' ' + event.sensor;
twitter.updateStatus(message, function (err, data) {
if(err) return console.log(err);
});
} else if(event.type == 'feed') {
twitter.updateStatus('Feed ' + event.feed + ' reporting ' + event.url, function (err, data) {
if(err) return console.log(err);
});
}
});
/*
mongopubsub.subscribe('messages', function (message) {
console.log(message);
});
*/
console.log('Notifier started.');
}