generated from homebridge/homebridge-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform.ts
77 lines (63 loc) · 3.05 KB
/
platform.ts
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
import { API, DynamicPlatformPlugin, Logger, PlatformAccessory, PlatformConfig, Service, Characteristic } from 'homebridge';
import noble from '@abandonware/noble';
import { PLATFORM_NAME, PLUGIN_NAME } from './settings';
import { VeritablePotagerAccessory } from './platformAccessory';
/**
* HomebridgePlatform
* This class is the main constructor for your plugin, this is where you should
* parse the user config and discover/register accessories with Homebridge.
*/
export class VeritableHomebridgePlatform implements DynamicPlatformPlugin {
public readonly Service: typeof Service = this.api.hap.Service;
public readonly Characteristic: typeof Characteristic = this.api.hap.Characteristic;
// this is used to track restored cached accessories
public readonly accessories: PlatformAccessory[] = [];
constructor(
public readonly log: Logger,
public readonly config: PlatformConfig,
public readonly api: API,
) {
this.log.debug('Finished initializing platform:', this.config.name);
// When this event is fired it means Homebridge has restored all cached accessories from disk.
// Dynamic Platform plugins should only register new accessories after this event was fired,
// in order to ensure they weren't added to homebridge already. This event can also be used
// to start discovery of new accessories.
this.api.on('didFinishLaunching', () => {
log.debug('Executed didFinishLaunching callback');
// run the method to discover / register your devices as accessories
noble.on('discover', async (peripheral) => {
this.discoverDevice(peripheral);
});
noble.on('stateChange', async (state) => {
if (state === 'poweredOn') {
await noble.startScanningAsync([], false);
}
});
});
}
configureAccessory(accessory: PlatformAccessory) {
this.log.info('Loading accessory from cache:', accessory.displayName);
this.accessories.push(accessory);
}
async discoverDevice(peripheral: noble.Peripheral) {
if (!peripheral.advertisement.localName || !peripheral.advertisement.localName.startsWith('VERITABLE')) {
return;
}
await peripheral.connectAsync();
const uuid = this.api.hap.uuid.generate(peripheral.address);
const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid);
if (existingAccessory) {
this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName);
new VeritablePotagerAccessory(this, existingAccessory, peripheral);
} else {
this.log.info('Adding new accessory:', peripheral.advertisement.localName);
// create a new accessory
const accessory = new this.api.platformAccessory(peripheral.advertisement.localName, uuid);
// create the accessory handler for the newly create accessory
// this is imported from `platformAccessory.ts`
new VeritablePotagerAccessory(this, accessory, peripheral);
// link the accessory to your platform
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
}
}
}