forked from pakastin/node-ruuvitag
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathruuvi.js
More file actions
129 lines (105 loc) · 3.78 KB
/
Copy pathruuvi.js
File metadata and controls
129 lines (105 loc) · 3.78 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
const noble = require('@abandonware/noble');
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;
}
}
class Ruuvi extends EventEmitter {
constructor() {
super();
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;
}
const onDiscover = (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,
});
registerTag(newRuuviTag);
this.emit('found', newRuuviTag);
}
}
// is it a RuuviTag in Eddystone mode?
else {
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,
});
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) {
let 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
});
}
}
}
noble.on('discover', onDiscover);
// start scanning
if (noble.state === 'poweredOn') {
noble.startScanning([], true);
}
else {
noble.once('stateChange', () => {
noble.startScanning([], true);
});
}
}
findTags() {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (this._foundTags.length) {
return resolve(this._foundTags);
}
reject(new Error('No beacons found'));
}, 5000);
});
}
}
const ruuvi = module.exports = new Ruuvi();