generated from homebridge/homebridge-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform.ts
80 lines (67 loc) · 3 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
78
79
80
import { API, DynamicPlatformPlugin, Logger, PlatformAccessory, PlatformConfig, Service, Characteristic } from 'homebridge';
import Bonjour from 'bonjour';
import { PLATFORM_NAME, PLUGIN_NAME } from './settings';
import { HomebridgeEsp8266RancilioAccessory } 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 HomebridgeEsp8266RancilioPlatform 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.api.on('didFinishLaunching', () => {
// run the method to discover / register your devices as accessories
this.discoverDevices();
});
}
/**
* This function is invoked when homebridge restores cached accessories from disk at startup.
* It should be used to setup event handlers for characteristics and update respective values.
*/
configureAccessory(accessory: PlatformAccessory) {
this.log.info('Loading accessory from cache:', accessory.displayName);
// add the restored accessory to the accessories cache so we can track if it has already been registered
this.accessories.push(accessory);
}
discoverDevices() {
const bonjour = Bonjour();
const browser = bonjour.find({ type: 'oznu-platform' });
browser.on('up', this.foundAccessory.bind(this));
// Check bonjour again 5 seconds after launch
setTimeout(() => {
browser.update();
}, 5000);
// Check bonjour every 60 seconds
setInterval(() => {
browser.update();
}, 60000);
}
foundAccessory(service) {
if (service.txt.type && service.txt.type === 'rancilio') {
const UUID = this.api.hap.uuid.generate(service.txt.mac);
const host = service.host;
const accessoryConfig = { host, port: service.port, name: service.name, serial: service.txt.mac };
// check if it already exists
const existingAccessory = this.accessories.find(x => x.UUID === UUID);
if (existingAccessory) {
// Existing Accessory
this.log.info(`Found existing Rancilio at ${service.host}:${service.port} [${service.txt.mac}]`);
new HomebridgeEsp8266RancilioAccessory(this, existingAccessory, accessoryConfig);
} else {
// New Accessory
this.log.info(`Found new Rancilio at ${service.host}:${service.port} [${service.txt.mac}]`);
const accessory = new this.api.platformAccessory(accessoryConfig.name, UUID);
new HomebridgeEsp8266RancilioAccessory(this, accessory, accessoryConfig);
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
}
}
}
}