forked from ayshe/timeflip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
64 lines (58 loc) · 2.21 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
57
58
59
60
61
62
63
64
import noble from "@s524797336/noble-mac";
import async from "async";
import express from "express";
import bodyParser from "body-parser";
import template from "./templates/main";
const app = express();
const TIMEFLIP = 'ffff544676332e3100';
const FACETS = 'f1196f5071a411e6bdf40800200c9a66';
const ACCELEROMETER = 'f1196f5171a411e6bdf40800200c9a66';
const DEVICE = '180a';
const BATTERY = '180f';
app.use(express.static('public'));
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.get('/', function (req, res) {
res.send(template)
});
const connect = function (peripheral) {
peripheral.connect(function (error) {
peripheral.discoverServices([], function (error, services) {
services.forEach(service => {
if (service.uuid === FACETS) {
console.log(service._noble._characteristics[FACETS]);
// console.log(service._noble._events.stateChange);
}
});
});
});
};
const discover = function (peripheral) {
if (peripheral.advertisement.manufacturerData) {
if (peripheral.advertisement.manufacturerData.toString('hex') == TIMEFLIP) {
// console.log('\there is my manufacturer data:');
// console.log('\t\t' + JSON.stringify(peripheral.advertisement.manufacturerData.toString('hex')));
console.log(peripheral.id +
' with address <' + peripheral.address + ', ' + peripheral.addressType + '>,' +
' connectable ' + peripheral.connectable + ',' +
' RSSI ' + peripheral.rssi + ':');
// if (peripheral.advertisement.txPowerLevel !== undefined) {
// console.log('\tmy TX power level is:');
// console.log('\t\t' + peripheral.advertisement.txPowerLevel);
// }
connect(peripheral);
}
}
};
app.listen(3020, function () {
console.log('Timeflip listening on port 3020');
noble.on('stateChange', function (state) {
console.log('------------------------ state change ------------------------');
if (state === 'poweredOn') {
noble.startScanning([], true);
} else {
noble.stopScanning();
}
});
noble.on('discover', discover);
});