-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment.js
151 lines (135 loc) · 4.15 KB
/
environment.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
var WebSocketServer = require('ws').Server,
csv = require('csv');
var mediator = require('./mediator'),
logger = require('./logger'),
Beacon = require('./beacon'),
Sound = require('./sound');
var _ = require('underscore'),
beacons = {},
strongestBeacon = null,
updateBeacon = updateBeacon;
var getBeacon = function(macAddr){
return beacons[macAddr];
}
var getStrongestBeacon = function(){
return strongestBeacon;
}
var addBeacon = function(beacon){
logger.log('data','+ Beacon: '+beacon.name());
beacons[beacon.macAddress]=beacon;
}
var updateBeacon = function(macAddress,rssi){
var beacon = beacons[macAddress];
if(beacon){
beacon.updateRSSI(rssi);
}
}
var dump = function(){
console.log(beacons);
}
var nominateStrongestBeacon = function(){
var bs = [];
for(var i in beacons){
bs.push(beacons[i]);
}
bs = bs.filter(function(b){
return b.influencing()==true
})
//console.log("competing beacons"+bs.map(function(el){return el.name()+" "+el.normalizedRSSI();}))
//var sorted_beacons = bs.sort(function(a,b){
// return a.normalizedRSSI()>b.normalizedRSSI();
//});
var sorted_beacons = _.sortBy(bs,function(el){ return el.normalizedRSSI(); });
bs = sorted_beacons;
//console.log(bs.map(function(el){
// return el.name()+" "+el.currentRSSI+" "+el.normalizedRSSI();
//}).join("\n"));
//console.log(bs);
var change = false;
if(strongestBeacon != bs[0]){
change = true;
}
strongestBeacon = bs[0];
if(change||(strongestBeacon&&strongestBeacon.isHot())){
mediator.pubsub.emit('strongestBeaconChange');
}
}
var dumpToCSV = function(){
var rows = [];
for(var i in beacons){
var beacon = beacons[i];
beacon.selfCalibrate();
if(!beacon.rssiAtAssociationRange||!beacon.rssiAtPeriphery)
continue;
rows.push([
1,
beacon.macAddress,
beacon.btName,
beacon.x,
beacon.y,
'HOT',
beacon.rssiAtAssociationRange,
'WARM',
beacon.rssiAtPeriphery
])
}
console.log(rows);
csv().from(rows).to('calibration.csv');
}
var startListeningForCalibration = function(){
var cal = new WebSocketServer({port: 9998});
cal.on('connection', function(ws) {
ws.on('close',function(){
dumpToCSV();
});
ws.on('message', function(message) {
var beacons = JSON.parse(message);
console.log(beacons);
for(var b in beacons){
var macAddress = b,
name = beacons[b]['name'],
xy = beacons[b]['x,y'].split(',');
var beacon = getBeacon(b);
if(beacon){
beacon.setName(name);
beacon.setXY(xy[0],xy[1]);
} else {
beacon = new Beacon(macAddress,name,xy[0],xy[1]);
addBeacon(beacon);
}
}
ws.send('ACK');
});
});
}
var startListening = function(){
var wss = new WebSocketServer({port: 9999});
wss.on('connection', function(ws) {
ws.on('message', function(message) {
message = JSON.parse(message);
//console.log(message);
for(var i in message){
macAddress = message[i][0];
rssi = message[i][1];
updateBeacon(macAddress,rssi)
}
nominateStrongestBeacon();
if(strongestBeacon){
//console.log('strongest Beacon is: '+strongestBeacon.name());
} else {
logger.log('warn','No beacon!');
}
Sound.bleep();
ws.send('ACK');
});
});
}
function parseMessage(message){
message = JSON.parse(message);
}
module.exports.startListening = startListening;
module.exports.startListeningForCalibration = startListeningForCalibration;
module.exports.dump = dump;
module.exports.addBeacon = addBeacon;
module.exports.getStrongestBeacon = getStrongestBeacon;
module.exports.getBeacon = getBeacon;