This repository has been archived by the owner on Sep 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
163 lines (132 loc) · 4.43 KB
/
index.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
152
153
154
155
156
157
158
159
160
161
162
163
const noble = require('noble');
const debug = require('debug');
const fakegatoHistory = require('fakegato-history');
const identify = require('./identify');
const pkginfo = require('./package');
let Characteristic;
let Service;
let storagePath;
let FakeGatoHistoryService;
class AutomationBluetoothPresence {
constructor(log, config) {
this.log = log;
this.name = config.name;
this.debug = debug(`homebridge-automation-bluetooth-presence-${this.name}`);
this.deviceId = config.deviceId;
const configGracePeriod = config.gracePeriod || 10 * 60; // 10 minutes;
this.gracePeriod = configGracePeriod * 1000;
this.device = false;
this.deviceFound = false;
this.rescanTimeout = null;
this.services = this.createServices();
this.discoverDevices();
}
discoverDevices() {
noble.on('stateChange', (state) => {
if (state === 'poweredOn') {
this.log('Looking for Bluetooth devices');
noble.startScanning([], false);
} else {
noble.stopScanning();
}
});
noble.on('discover', this.deviceDiscovered.bind(this));
setInterval(this.deviceTimer.bind(this), 1000);
}
deviceDiscovered(peripheral) {
const { id, address } = peripheral;
if (id !== this.deviceId && address !== this.deviceId) {
// ignore, not the right device
this.debug(`Device ignored, wrong ID (looking for ${this.deviceId}) - ID: ${id} - Address: ${address} - Name: ${peripheral.advertisement.localName}`);
return;
}
const entered = !this.device;
if (entered) {
this.device = {
peripheral,
};
this.log(`Device "${peripheral.advertisement.localName}" entered`);
}
this.setPresence(true);
this.device.lastSeen = Date.now();
this.debug(`Device seen - Entered: ${entered} - Last seen: ${new Date()}`);
this.startRescanTimer();
}
startRescanTimer() {
if (this.rescanTimeout) {
clearTimeout(this.rescanTimeout);
}
this.rescanTimeout = setTimeout(() => {
this.debug('Restart scanning');
noble.stopScanning();
noble.startScanning([], false);
}, 60 * 1000);
}
deviceTimer() {
if (!this.device) {
return;
}
if (this.device.lastSeen < (Date.now() - this.gracePeriod)) {
this.log('Device exited');
this.device = false;
this.setPresence(false);
}
}
setPresence(present) {
if (this.deviceFound !== present) {
this.loggingService.addEntry({ time: new Date().getTime(), status: present ? 1 : 0 });
}
this.deviceFound = present;
this.motionSensor
.getCharacteristic(Characteristic.MotionDetected)
.updateValue(present);
}
createServices() {
this.motionSensor = new Service.MotionSensor(this.name);
this.motionSensor
.getCharacteristic(Characteristic.MotionDetected)
.on('get', callback => callback(null, this.deviceFound));
this.motionSensor.log = this.log;
const accessoryInformationService = new Service.AccessoryInformation();
accessoryInformationService
.setCharacteristic(Characteristic.Name, this.name)
.setCharacteristic(Characteristic.Manufacturer, pkginfo.author.name || pkginfo.author)
.setCharacteristic(Characteristic.Model, pkginfo.name)
.setCharacteristic(Characteristic.SerialNumber, this.deviceId)
.setCharacteristic(Characteristic.FirmwareRevision, pkginfo.version)
.setCharacteristic(Characteristic.HardwareRevision, pkginfo.version);
this.loggingService = new FakeGatoHistoryService(
'motion',
this.motionSensor,
{
storage: 'fs',
path: `${storagePath}/accessories`,
filename: `history_bp_${this.deviceId}.json`,
},
);
return [
this.motionSensor,
accessoryInformationService,
this.loggingService,
];
}
getServices() {
return this.services;
}
identify(callback) {
if (this.device) {
this.log('Issuing identify request');
identify(this.device);
} else {
this.log('Device not found');
}
callback();
}
}
module.exports = (homebridge) => {
Service = homebridge.hap.Service; // eslint-disable-line
Characteristic = homebridge.hap.Characteristic; // eslint-disable-line
storagePath = homebridge.user.storagePath(); // eslint-disable-line
FakeGatoHistoryService = fakegatoHistory(homebridge);
homebridge.registerAccessory('homebridge-automation-bluetooth-presence', 'AutomationBluetoothPresence', AutomationBluetoothPresence);
};