105 lines
3.3 KiB
TypeScript
105 lines
3.3 KiB
TypeScript
import { IConfig } from "./models/iConfig";
|
|
import { v3 } from 'node-hue-api';
|
|
import LocalBootstrap = require("node-hue-api/lib/api/http/LocalBootstrap");
|
|
import Api = require("node-hue-api/lib/api/Api");
|
|
import { Sleep } from "./sleep";
|
|
import { IAccessory } from "./models/iAccessory";
|
|
import { FluxAccessory } from "./fluxAccessory";
|
|
|
|
let Accessory: any;
|
|
let Homebridge: any;
|
|
|
|
/**
|
|
* Main entry.
|
|
* @param homebridge
|
|
*/
|
|
export default function (homebridge: any) {
|
|
Homebridge = homebridge;
|
|
Accessory = homebridge.platformAccessory;
|
|
homebridge.registerPlatform(
|
|
'homebridge-flux',
|
|
'Flux',
|
|
FluxPlatform,
|
|
true
|
|
);
|
|
};
|
|
|
|
class FluxPlatform {
|
|
log: any = {};
|
|
api: any;
|
|
accessoryList: Array<IAccessory> = [];
|
|
config: IConfig;
|
|
hue: Api | undefined;
|
|
|
|
|
|
constructor(log: any, config: any, api: any) {
|
|
this.log = log;
|
|
this.api = api;
|
|
this.config = config;
|
|
this.log('INFO - Registering Flux platform');
|
|
this.api.on('didFinishLaunching', this.didFinishLaunching.bind(this));
|
|
}
|
|
|
|
private connectHue = async () => {
|
|
if (!this.config) {
|
|
return;
|
|
}
|
|
|
|
if (this.config.userName && this.config.clientKey) {
|
|
this.hue = await v3.api.createLocal(this.config.ipAddress).connect(this.config.userName, this.config.clientKey, undefined);
|
|
this.log("Using existing connection info");
|
|
} else {
|
|
const unauthenticatedApi = await v3.api.createLocal(this.config.ipAddress).connect(undefined, undefined, undefined);
|
|
let createdUser;
|
|
let connected = false
|
|
while (!connected) {
|
|
try {
|
|
this.log("Creating hue user. Push link button")
|
|
createdUser = await unauthenticatedApi.users.createUser("homebridge", "HueChase");
|
|
|
|
this.hue = await v3.api.createLocal(this.config.ipAddress).connect(createdUser.username, createdUser.clientKey, undefined);
|
|
this.log("Connected to Hue Bridge");
|
|
this.log(`UserName: ${createdUser.username}, ClientKey: ${createdUser.clientkey}`)
|
|
connected = true;
|
|
|
|
} catch (err) {
|
|
if (err.getHueErrorType() === 101) {
|
|
this.log('The Link button on the bridge was not pressed. Please press the Link button and try again.');
|
|
Sleep(5000);
|
|
} else {
|
|
this.log(`Unexpected Error: ${err.message}`);
|
|
break;
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Handler for didFinishLaunching
|
|
* Happens after constructor
|
|
*/
|
|
private didFinishLaunching() {
|
|
this.log(`INFO - Done registering Flux platform`);
|
|
}
|
|
|
|
/**
|
|
* Called by homebridge to gather accessories.
|
|
* @param callback
|
|
*/
|
|
public accessories = async (callback: (accessories: Array<IAccessory>) => void) => {
|
|
//Connect to hue bridge
|
|
await this.connectHue();
|
|
|
|
this.accessoryList.push(new FluxAccessory({
|
|
api: this.api,
|
|
log: this.log,
|
|
homebridge: Homebridge,
|
|
hue: this.hue!,
|
|
config: this.config
|
|
}));
|
|
|
|
callback(this.accessoryList);
|
|
}
|
|
} |