-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
124 lines (110 loc) · 3.1 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
var serialport = require('serialport');
var express = require('express');
var app = express();
var Client = require('node-rest-client').Client;
var client = new Client();
var w_api = ""; // get your api posts url from waaffle!
var w_events = [];
// create serial port, open immediately
// and write status message to server console
var SerialPort = serialport.SerialPort;
var port = new SerialPort('/dev/cu.usbmodem1421', {
baudrate: 9600,
parser: serialport.parsers.readline("\n")
}, true, function(err, bytesWritten) {
if (err) {
return console.log('Error: ', err.message);
}
console.log(bytesWritten, 'bytes written');
});
port.on('open', showPortOpen);
port.on('data', sendSerialData);
port.on('close', showPortClose);
port.on('error', showError);
function showPortOpen() {
console.log('port open. Data rate: ' + port.options.baudRate);
}
function sendSerialData(data) {
console.log(data);
}
function showPortClose() {
console.log('port closed.');
}
function showError(error) {
console.log('Serial port error: ' + error);
}
function trigger(platform, activity, res) {
console.log('From internet: ' + platform + '/' + activity);
port.write(platform + '/' + activity);
// res.send('Logged ' + platform + ' - ' + activity);
}
function sendEvents() {
if (w_events.length > 0) {
var my_event = w_events.shift();
if (my_event.platform=='instagram') {
trigger('INST', 'lik');
} else if (my_event.platform=='twitter') {
trigger('TWIT', 'ret');
}
setTimeout(sendEvents,2000);
}
}
try {
client.get(w_api, {}, function(data, response) {
data.data.forEach(function(current, index, arr) {
w_events.push({
platform: current.source,
});
console.log(current.source);
});
console.log('--- done ---');
sendEvents();
});
} catch (e) {
if (w_api == '') {
console.log('go to waaffle and get your API url first');
process.exit();
}
}
// app.use('/', function (req, res) {
// res.send('Hello World!');
// });
app.use(express.static(__dirname + '/public'));
app.use('/', express.static('/public/index.html'))
app.all('/twitter/like', function (req, res) {
trigger('TWIT', 'lik', res);
});
app.all('/twitter/retweet', function (req, res) {
trigger('TWIT', 'ret', res);
});
app.all('/twitter/follow', function (req, res) {
trigger('TWIT', 'fol', res);
});
app.all('/twitter/mention', function (req, res) {
trigger('TWIT', 'men', res);
});
app.all('/twitter/message', function (req, res) {
trigger('TWIT', 'msg', res);
});
app.all('/instagram/like', function (req, res) {
trigger('INST', 'lik', res);
});
app.all('/instagram/follow', function (req, res) {
trigger('INST', 'fol', res);
});
app.all('/instagram/mention', function (req, res) {
trigger('INST', 'men', res);
});
app.all('/instagram/tag', function (req, res) {
trigger('INST', 'tag', res);
});
app.all('/instagram/message', function (req, res) {
trigger('INST', 'msg', res);
});
app.use(function(req, res) {
console.error('misc event');
res.send('are you trying to hack?');
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});