-
Notifications
You must be signed in to change notification settings - Fork 0
/
express_server.js
106 lines (93 loc) · 3.8 KB
/
express_server.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
// SET UP ========================
var port = process.env.PORT || 1234;
// NODE BLE CONFIGURATION
var noble = require('noble');
// INCLUDE DEPENDENCIES ========================
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var connectedSockets = 0;
var sockets = {};
app.use("/node_modules", express.static(__dirname + '/node_modules'));
// MIDDLEWARE: set cross origin sharing headers
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "http://localhost:9000");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "GET, OPTIONS");
res.header("Access-Control-Allow-Credentials", "true");
next();
});
// SOCKET SERVER CONNECTION
io.on('connection', function(socket){
socket.emit('connect', "Connection created.")
if (!sockets[socket.id]) connectedSockets++;
sockets[socket.id]={ id: socket.id };
console.log('Client Connected: ' + socket.id + '; Total Socket Count: ' + connectedSockets);
socket.on('disconnect', function (data) {
delete sockets[socket.id];
connectedSockets--;
console.log('Client Disconnected: ' + socket.id + '; Total Socket Count: ' + connectedSockets );
});
});
// CONNECT BLE INTERFACE ===============================
// start scanning after connection is established
noble.on('stateChange', function(state) {
console.log("statechange:" + state);
if (state === 'poweredOn') {
console.log("Bluetooth is on here");
noble.startScanning();
} else {
console.log("Bluetooth not on");
noble.stopScanning();
}
});
// if the connected device is recognised, call function
noble.on('discover', function(peripheral) {
var advertisement = peripheral.advertisement;
var localName = advertisement.localName;
console.log(localName);
// OSX will have `undefined` as peripheral name if not connected earlier
if (localName == undefined || localName.substring(0, 6) != "ADS129") {
return;
} else {
noble.stopScanning();
}
explore(peripheral);
});
// FUNCTION TO CALCULATE AND RETURN DATA FROM CONNECTED BLE DEVICE
function explore(peripheral) {
peripheral.on('disconnect', function() {
console.log('device disconnected');
});
peripheral.connect(function(error) {
console.log('connected to peripheral: ' + peripheral.uuid);
peripheral.writeHandle('0x0013', new Buffer([0x01, 0x00]), 0, function(error) {
console.log("handle written");
});
peripheral.discoverServices(['2d0d'], function(error, services) {
console.log('service discovered');
var service = services[0];
service.discoverCharacteristics(['2d37'], function(error, characteristics) {
//console.log(characteristics);
var characteristic = characteristics[0];
characteristic.on('read', function(data, isNotification) {
// 8 bits for each data entry X 3
x = data[0] * 65536 + data[1] * 256 + data[2];
y = data[3] * 65536 + data[4] * 256 + data[5];
// EMIT `x` and `y` using node socket server
console.log('New point on graph sent to client!');
socket.emit('pointData', { value_x: x, value_y: y });
// LISTEN to emitted data on ANGULAR application
});
});
});
});
}
// listen (start app with node server.js) ======================================
server.listen(1234, function() {
console.log('Server listening at port ', port);
setInterval(function(){
console.log(connectedSockets + ' Online Devices At ' + Date());
}, 1000 * 60 * 1);
});