aboutsummaryrefslogtreecommitdiff
path: root/plugin.ts
blob: 20189b1e1c547bac684798c8c9f9e1a8d431002c (plain)
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
import { AccessoryConfig, AccessoryPlugin, API, Logger, Service } from 'homebridge';

import Lamp from './lamp';
const Color = require('color');

export default class BekenBridge implements AccessoryPlugin {
	private lamp: Lamp;
	private infoService: Service;

	private whiteBulbService: Service;
	private RGBBulbService: Service;

	private state: {
		on: boolean;
		brightness: number;
		lamp: 'white' | 'rgb';
		saturation: number;
		hue: number;
	};

	public name: string;

	constructor(
		public readonly log: Logger,
		public readonly config: AccessoryConfig,
		public readonly api: API,
	) {
		this.name = config.name;
		this.lamp = new Lamp(config.address, log);

		this.state = {
			on: false,
			brightness: 100,
			lamp: 'white',
			saturation: 0,
			hue: 0,
		};

		this.infoService = new this.api.hap.Service.AccessoryInformation()
			.setCharacteristic(this.api.hap.Characteristic.Manufacturer, 'Beken')
			.setCharacteristic(this.api.hap.Characteristic.Model, 'Beken LED');

		this.whiteBulbService = new this.api.hap.Service.Lightbulb('White', 'normal');
		this.RGBBulbService = new this.api.hap.Service.Lightbulb('RGB', 'rgb');

		this.registerWhiteBulbServices();
		this.registerRGBBulbServices();
	}

	registerWhiteBulbServices() {
		var done = () => {
			if (this.state.lamp == 'rgb') {
				this.RGBBulbService.getCharacteristic(this.api.hap.Characteristic.On).setValue(false);
			}
			this.state.lamp = 'white';
			this.updateLamp();
		};
		this.whiteBulbService.getCharacteristic(this.api.hap.Characteristic.On)
			.onGet(() => this.state.on && this.state.lamp == 'white')
			.onSet((on: boolean) => {
				this.state.on = on;
				done();
			});
		this.whiteBulbService.getCharacteristic(this.api.hap.Characteristic.Brightness)
			.onGet(() => this.state.brightness && this.state.lamp == 'white')
			.onSet((brt: number) => {
				this.state.brightness = brt;
				done();
			});
	}

	registerRGBBulbServices() {
		var done = () => {
			if (this.state.lamp == 'white') {
				this.whiteBulbService.getCharacteristic(this.api.hap.Characteristic.On).setValue(false);
			}
			this.state.lamp = 'rgb';
			this.updateLamp();
		};
		this.RGBBulbService.getCharacteristic(this.api.hap.Characteristic.On)
			.onGet(() => this.state.on && this.state.lamp == 'rgb')
			.onSet((on: boolean) => {
				this.state.on = on;
				done();
			});
		this.RGBBulbService.getCharacteristic(this.api.hap.Characteristic.Brightness)
			.onGet(() => this.state.brightness && this.state.lamp == 'rgb')
			.onSet((brt: number) => {
				this.state.brightness = brt;
				done();
			});
		this.RGBBulbService.getCharacteristic(this.api.hap.Characteristic.Hue)
			.onGet(() => this.state.hue)
			.onSet((hue: number) => {
				this.state.hue = hue;
				done();
			});
		this.RGBBulbService.getCharacteristic(this.api.hap.Characteristic.Saturation)
			.onGet(() => this.state.saturation)
			.onSet((sat: number) => {
				this.state.saturation = sat;
				done();
			});
	}

	updateLamp() {
		if (!this.state.on) {
			this.lamp.color = [0, 0, 0, 0];
			return;
		}
		switch (this.state.lamp) {
			case 'rgb': {
				var rgb = Color({ h: this.state.hue, s: this.state.saturation, v: this.state.brightness });
				this.lamp.color = [rgb.red(), rgb.green(), rgb.blue(), 0];
				break;
			}
			case 'white': {
				var value = Math.floor(this.state.brightness / 100 * 255);
				this.lamp.color = [0, 0, 0, value];
				break;
			}
		}
	}

	getServices() {
		return [
			this.infoService,
			this.whiteBulbService,
			this.RGBBulbService,
		];
	}
}