-
Notifications
You must be signed in to change notification settings - Fork 25
/
ruuvi.js
132 lines (117 loc) · 4.16 KB
/
ruuvi.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
const EventEmitter = require('events').EventEmitter;
const parser = require('./lib/parse');
const parseEddystoneBeacon = require('./lib/eddystone');
class RuuviTag extends EventEmitter {
constructor (data) {
super();
this.id = data.id;
this.address = data.address;
this.addressType = data.addressType;
this.connectable = data.connectable;
}
}
class Ruuvi extends EventEmitter {
constructor (adapter) {
super();
this._adapter = adapter;
this._foundTags = []; // this array will contain registered RuuviTags
this._tagLookup = {};
this.scanning = false;
this.listenerAttached = false;
const registerTag = tag => {
this._foundTags.push(tag);
this._tagLookup[tag.id] = tag;
};
this._adapter.on('warning', warning => {
console.error(new Error(warning));
});
this._adapter.on('discover', peripheral => {
let newRuuviTag;
// Scan for new RuuviTags, add them to the array of found tags
// is it a RuuviTag in RAW mode?
const manufacturerData = peripheral.advertisement ? peripheral.advertisement.manufacturerData : undefined;
if (manufacturerData && manufacturerData[0] === 0x99 && manufacturerData[1] === 0x04) {
if (!this._tagLookup[peripheral.id]) {
newRuuviTag = new RuuviTag({
id: peripheral.id,
address: peripheral.address,
addressType: peripheral.addressType,
connectable: peripheral.connectable
});
registerTag(newRuuviTag);
this.emit('found', newRuuviTag);
}
} else {
// is it a RuuviTag in Eddystone mode?
const serviceDataArray = peripheral.advertisement ? peripheral.advertisement.serviceData : undefined;
const serviceData = serviceDataArray && serviceDataArray.length ? serviceDataArray[0] : undefined;
if (serviceData && serviceData.uuid === 'feaa') {
const url = parseEddystoneBeacon(serviceData.data);
if (url && url.match(/ruu\.vi/)) {
if (!this._tagLookup[peripheral.id]) {
newRuuviTag = new RuuviTag({
id: peripheral.id,
address: peripheral.address,
addressType: peripheral.addressType,
connectable: peripheral.connectable
});
registerTag(newRuuviTag);
this.emit('found', newRuuviTag);
}
}
}
}
// Check if it is an advertisement by an already found RuuviTag, emit "updated" event
const ruuviTag = this._tagLookup[peripheral.id];
if (ruuviTag) {
if (peripheral.advertisement && peripheral.advertisement.manufacturerData) {
const dataFormat = peripheral.advertisement.manufacturerData[2];
return ruuviTag.emit(
'updated',
Object.assign(
{ dataFormat: dataFormat, rssi: peripheral.rssi },
parser.parseManufacturerData(peripheral.advertisement.manufacturerData)
)
);
}
// is data format 2 or 4
const serviceDataArray = peripheral.advertisement.serviceData;
const serviceData = serviceDataArray && serviceDataArray.length ? serviceDataArray[0] : undefined;
const url = serviceData ? parseEddystoneBeacon(serviceData.data) : undefined;
const parsed = url ? parser.parseUrl(url) : undefined;
if (parsed && !(parsed instanceof Error)) {
ruuviTag.emit('updated', {
url: url,
dataFormat: parsed.dataFormat,
rssi: peripheral.rssi,
humidity: parsed.humidity,
temperature: parsed.temperature,
pressure: parsed.pressure,
eddystoneId: parsed.eddystoneId
});
}
}
});
}
findTags () {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (this._foundTags.length) {
return resolve(this._foundTags);
}
reject(new Error('No beacons found'));
}, 5000);
});
}
start () {
if (!this.scanning) {
this.scanning = true;
}
}
stop () {
if (this.scanning) {
this.scanning = false;
}
}
}
module.exports = Ruuvi;