Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
140 lines (102 loc) · 2.98 KB
/
Copy pathapp.js
File metadata and controls
140 lines (102 loc) · 2.98 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*jslint node:true, vars:true, bitwise:true, unparam:true */
/*jshint unused:true */
var noble = require('noble');
var h = 0;
var found = false;
var myPeripheral;
var peripheralName = "Find Me";
var mraa = require('mraa');
var myDigitalPin = new mraa.Gpio(4);
myDigitalPin.dir(mraa.DIR_OUT);
// BIOMIO
var fs = require('fs');
var BiomioNode = require('biomio-node');
var config = require('./config');
try {
var privateKey = fs.readFileSync(__dirname + "/private.key").toString();
} catch (e) {
console.error('Can\'t find/read file "private.key"!');
process.exit(1);
}
var options = {
gateURL: config.gateURL,
appId: config.appId,
appKey: privateKey,
appType: 'extension',
/* optional parameters */
osId: 'linux',
headerOid: 'clientHeader',
devId: 'node_js_lib'
}
var userToken = 'user@gmail.com'; // for now we use end user's email
var conn = new BiomioNode(userToken, options, function() {
console.info('Ready!');
});
// BIOMIO
noble.on('stateChange', scan);
function scan(state){
if (state === 'poweredOn') {
noble.startScanning();
console.log("Started scanning...");
} else {
noble.stopScanning();
console.log("Is Bluetooth on?");
}
}
noble.on('discover', discoverPeripherals);
function discoverPeripherals(peripheral) {
if(peripheral.advertisement.localName == peripheralName){
console.log("found my device");
noble.stopScanning();
myPeripheral = peripheral;
peripheral.connect(explorePeripheral);
}
}
function explorePeripheral(error) {
console.log("connected to "+myPeripheral.advertisement.localName);
if (h == 0) h = setInterval(updateRSSI, 60);
myPeripheral.on('disconnect', disconnectPeripheral);
}
function updateRSSI(){
myPeripheral.updateRssi(function(error, rssi){
if(rssi < 0 && -rssi < 30 && !found) {
console.log("FOUND! RSSI: "+rssi);
// <-- BIOMIO
/* check if user already registered */
conn.user_exists(function(exists) {
console.info('user exists ', exists);
if (exists) {
try {
/** call RPC method "run_auth" */
conn.run_auth(function (result) {
/* callback will be called few times: in_progress, completed */
if (result.status === 'completed') {
console.log('Authentication is successful');
myDigitalPin.write(1);
setTimeout(function(){myDigitalPin.write(0);}, 2000);
}
});
} catch(ex) {
console.warn('EXCEPTION: ', ex);
}
} else {
console.log('User is not found!');
}
});
// <-- BIOMIO
found = true;
}
else if(-rssi > 50 && found) {
console.log("LOST! RSSI: "+rssi);
found = false;
}
});
}
function disconnectPeripheral(){
console.log('peripheral disconneted');
if(h != 0) {
clearInterval(h);
h = 0;
}
noble.startScanning();
}