-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.js
74 lines (67 loc) · 2.5 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
65
66
67
68
69
70
71
72
73
74
var request = require('request');
var eddystoneBeacon = require('eddystone-beacon');
var iBeacon = require('bleacon');
var app = [];
var fs = require('fs');
var array = fs.readFileSync('/etc/default/piBeacon').toString().split("\n");
for(i in array) {
if (array[i].toString()) {
var variabile = array[i].toString().split('="');
var key = variabile[0];
var val = variabile[1].replace('"', '');
app[key] = val;
}
}
app['INTERVAL'] = app.INTERVAL * 1000;
app['MACADDRESS'] = require('os').networkInterfaces()['eth0'][0].mac;
if ( app.NAME == undefined || app.NAME == "" ) {
throw new Error('Please fill in NAME in /etc/default/piBeacon');
} else if ( app.INTERVAL == undefined || app.INTERVAL == "" ) {
throw new Error('Please fill in INTERVAL in /etc/default/piBeacon');
} else if ( app.DEVICES == undefined || app.DEVICES == "" ) {
throw new Error('Please fill in DEVICES in /etc/default/piBeacon');
} else if ( app.DEVICES > 4 ) {
throw new Error('Raspberry Pi has only 4 usb ports so maximum 4 devices');
} else if ( app.MACADDRESS == undefined || app.MACADDRESS == "" ) {
throw new Error('Cannot get device eth0 mac address');
}
app.daemon = function(DEVICE_ID)
{
app.run(DEVICE_ID);
setInterval(app.run(DEVICE_ID), app.INTERVAL);
}
app.run = function(DEVICE_ID)
{
if (app.SERVER){
request({
url: app.SERVER+'&macaddress='+app.MACADDRESS+'&name='+app.NAME+'&device_id='+DEVICE_ID,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
if (body.type == 'eddystone-url') {
if (body.url && body.url != app.urlcache) {
eddystoneBeacon.advertiseUrl(body.url);
app.urlcache = body.url;
}
} else if (body.type == 'eddystone-uid') {
if (body.namespaceId && body.instanceId && body.namespaceId+'-'+body.instanceId != app.uidcache) {
eddystoneBeacon.advertiseUid(body.namespaceId, body.instanceId);
app.uidcache = body.namespaceId+'-'+body.instanceId;
}
} else if (body.type == 'ibeacon') {
if (body.uuid && body.uuid != app.uuidcache) {
var uuid = body.uuid;
var major = body.major ? body.major : 0; // 0 - 65535
var minor = body.monor ? body.minor : 0; // 0 - 65535
var measuredPower = body.measuredPower ? body.measuredPower : -59; // -128 - 127 (measured RSSI at 1 meter)
iBeacon.startAdvertising(uuid, major, minor, measuredPower);
app.uuidcache = body.uuid;
}
}
}
});
} else {
eddystoneBeacon.advertiseUrl("http://www.example"+DEVICE_ID+".com/");
}
}
module.exports = app