-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeacon.js
More file actions
78 lines (70 loc) · 2.24 KB
/
beacon.js
File metadata and controls
78 lines (70 loc) · 2.24 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
var ZONES = {
'HOT' : 2,
'WARM' : 1,
'COOL' : 0,
'COLD' : -1
}
function Beacon(macAddress,name,x,y){
this.signalHistory = [];
this.macAddress = macAddress
this.x = x
this.y = y
if(name){
this.btName = name
} else {
this.btName = macAddress
}
this.currentRSSI = null
this.rssiAtAssociationRange = null
this.rssiAtPeriphery = null
this.zone = ZONES['COLD']
this.name = function() { return "Beacon ("+ this.macAddress + ") "+this.btName }
this.normalizedRSSI = function() { return parseInt(this.currentRSSI*100 / this.rssiAtAssociationRange) }
this.influencing = function() { return this.zone!=ZONES['COLD'] }
}
Beacon.prototype.updateRSSI = function (rssi){
this.currentRSSI = rssi;
this.signalHistory.push(rssi);
//if(this.rssiAtAssociationRange==null||this.rssiAtPeriphery==null) throw new Error(this.name()+" is not calibrated!");
if(this.currentRSSI>=this.rssiAtAssociationRange){
this.zone = ZONES['HOT'];
} else if(this.currentRSSI<this.rssiAtAssociationRange&&(this.currentRSSI>=this.rssiAtPeriphery)) {
this.zone = ZONES['WARM'];
} else if(this.currentRSSI<this.rssiAtPeriphery) {
this.zone = ZONES['COOL'];
}
}
Beacon.prototype.setName = function(n){
this.btName = n;
}
Beacon.prototype.setXY = function(x,y){
this.x = x;
this.y = y;
}
Beacon.prototype.isHot = function(){
return this.zone==ZONES['HOT'];
}
Beacon.prototype.selfCalibrate = function(){
if(this.signalHistory.length>=2){
this.rssiAtAssociationRange = Math.ceil(this.signalHistory
.map(function(el){return parseInt(el);})
.sort(function(a,b){return a<b;})
.slice(0,2)
.reduce(function(a,b){return a+b})/2);
this.rssiAtPeriphery = this.rssiAtAssociationRange-20;
}
}
Beacon.prototype.calibrate = function(zone,rssi){
if(ZONES[zone]==undefined){
throw new Error("invalid Zone!")
}
switch(zone){
case 'HOT':
this.rssiAtAssociationRange = rssi;
break;
case 'WARM':
this.rssiAtPeriphery = rssi;
break;
}
}
module.exports = Beacon;