-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
56 lines (49 loc) · 1.33 KB
/
app.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
// imports
const request = require('request');
const io = require('socket.io-client');
const express = require('express');
const bodyParser = require('body-parser');
const blyncCore = require('blync-core');
// Blync setup
const blyncLights = blyncCore.findAllBlyncLights();
if (!blyncLights || blyncLights.length == 0) {
console.log('No Blynclight found');
process.exit();
}
const blyncLight = blyncLights[0];
// Keeps state
let IS_OUT = false;
// Use Webhooks or Socket.io
if (process.env.USE_SOCKETIO) {
console.log('Using Socket.io');
var socket = io('http://themtn.top');
socket.on('mountainChange', function (data) {
IS_OUT = data.result;
handleChange();
});
} else {
console.log('Using Webhooks');
// Express and body parser setup
const app = express();
app.use(bodyParser.json());
app.listen(3000, () => {
console.log('Listening on port 3000!')
});
app.post('/', (req, res) => {
IS_OUT = !IS_OUT;
handleChange();
});
}
// Query API to get current value
request.get('https://themtn.top/api/simple', (error, response, body) => {
IS_OUT = response.statusCode == 200;
handleChange();
});
function handleChange() {
console.log(IS_OUT);
if (IS_OUT) {
blyncLight.setColor('green');
} else {
blyncLight.setColor('red');
}
}