-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush.js
105 lines (100 loc) · 2.2 KB
/
push.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
var arduino = require('duino'),
request = require('request'),
socketio = require('socket.io-client'),
board = new arduino.Board({
debug: false
}),
redLed = new arduino.Led({
board: board,
pin: 12
}),
yellowLed = new arduino.Led({
board: board,
pin: 13
}),
button = new arduino.Button({
board: board,
pin: 11
}),
timer = null,
waitIndicator = function(on) {
if (on) {
var nextColor = 'red';
timer = setInterval(function() {
if ('red' == nextColor) {
redLed.on();
yellowLed.off();
nextColor = 'yellow';
} else {
redLed.off();
yellowLed.on();
nextColor = 'red';
}
}, 1000);
} else if (timer) {
clearInterval(timer);
}
},
applicationKey = 'fafabce43109b76d05cda2f8b8ab7e874bbc3f546c675db88d57284ebcf32bcd',
token = '543508cf4a0aadc1f21ab862e9db5dea99a8b33e044c2e3e8782fcaa32076a78',
buttonHandler = function() {
// Switch off the leds
console.log('down');
waitIndicator(false);
redLed.off();
yellowLed.off();
// Send a push to Notificare
var notification = {
message: "Arduino button pushed",
fullMessage: "Somebody pushed the button on your Arduino",
actions: [{
id: "red",
action: "Red Light",
message: false
},{
id: "yellow",
action: "Yellow Light",
message: false
}]
};
request.post({
url: "https://apps.notifica.re/webhooks/arduino/" + applicationKey,
qs: {
token: token
},
json: true,
body: notification
}, function(err, res, body) {
if (err) {
redLed.fade(500);
} else if (200 == res.statusCode) {
waitIndicator(true);
} else {
redLed.fade(500);
}
});
};
// Handle a button press
button.on('down', buttonHandler);
// Start when board is ready
board.on('ready', function(){
console.log('ready');
// Switch off the leds
redLed.off();
yellowLed.off();
var socket = socketio.connect('http://websocket.notifica.re');
socket.on(applicationKey, function(data) {
console.log(data);
if (data && data.action) {
waitIndicator(false);
if ('red' == data.action) {
redLed.on();
yellowLed.off();
} else if ('yellow' == data.action) {
yellowLed.on();
redLed.off();
}
}
});
socket.emit('subscribe', applicationKey);
});