-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
70 lines (59 loc) · 1.94 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
// Load .env config (silently fail if no .env present)
require('dotenv').config({ silent: true });
var ioLib = require('socket.io');
var http = require('http');
var path = require('path');
var express = require('express');
var noble = require('noble');
// Config
var pageConfig = {
preMessage: process.env.PRE_MESSAGE || "Hi ",
postMessage: process.env.POST_MESSAGE || ", welcome to the booth of awesome!",
partnerLogo: process.env.LOGO_2,
logo2: process.env.LOGO_2,
logo1: process.env.LOGO_1,
backgroundColor: process.env.BACKGROUND_COLOR || "#159ab5",
fontColor: process.env.FONT_COLOR || "#ffffff",
messageColor: process.env.MESSAGE_COLOR || "#e8922d"
};
var port = process.env.PORT || 8080;
var configRSSI = process.env.RSSI || -76;
// Create the express app
var app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req,res){
res.render('index', pageConfig);
});
var sockets = [];
var server = http.Server(app);
var io = ioLib(server);
// Setup sockets for updating web UI
io.on('connection', function (socket) {
// Add new client to array of client upon connection
sockets.push(socket);
});
console.log("Bluetooth State is : ",noble.state)
noble.on('stateChange',function(state){
if(state == 'poweredOn'){
console.log("Bluetooth stateChange, start scanning...");
noble.startScanning([],true);
}else{
noble.stopScanning();
}
});
noble.on('discover',function(dev){
if(dev.advertisement.localName && dev.rssi>configRSSI){
console.log("Found Device ",dev.advertisement.localName," with rssi ",dev.rssi);
sockets.forEach(function(socket){
socket.emit('found',{'name':dev.advertisement.localName,'rssi':dev.rssi});
});
} else{
console.log("...",dev.advertisement.localName,":",dev.rssi,"dB")
};
});
// Start the app
server.listen(port, function() {
console.log('listening at http://localhost:%s', port);
});