This repository has been archived by the owner on Nov 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (43 loc) · 1.73 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
const noble = require('noble');
const shared = require('./lib/shared.js');
const exec = require('./lib/exec.js');
const AwoxController = require('./controller/AwoxController.js');
const AwoxControllerUtils = require('./controller/AwoxControllerUtils.js');
module.exports = function (sails) {
gladys.on('ready', function () {
// Check bluetooth state
noble.on('stateChange', function (state) {
if (state === 'poweredOn') {
shared.bluetoothOn = true;
sails.log.info('AwoX module: Bluetooth device available');
} else if (state === 'poweredOff') {
shared.bluetoothOn = false;
shared.scanning = false;
if (shared.scanTimer) {
clearTimeout(shared.scanTimer);
shared.scanTimer = undefined;
}
sails.log.warn('AwoX module: Bluetooth device not available');
noble.stopScanning();
}
gladys.socket.emit('awoxStatus', AwoxControllerUtils.generateStatus());
});
});
return {
exec: exec,
routes: {
before: {
'post /awox/scan': (req, res, next) => sails.hooks.policies.middleware.checktoken(req, res, next),
'get /awox/setup': (req, res, next) => sails.hooks.policies.middleware.checktoken(req, res, next),
'post /awox/create': (req, res, next) => sails.hooks.policies.middleware.checktoken(req, res, next),
'get /awox/remotes': (req, res, next) => sails.hooks.policies.middleware.checktoken(req, res, next)
},
after: {
'post /awox/scan': AwoxController.scan,
'get /awox/setup': AwoxController.setup,
'post /awox/create': AwoxController.createMeshDevice,
'get /awox/remotes': AwoxController.getRemotes
}
}
};
};