aboutsummaryrefslogtreecommitdiff
path: root/plugin.ts
blob: 65abe0dd2d1435f658d2a46d17c9df58aa98010d (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
133
134
135
import { AccessoryConfig, AccessoryPlugin, API, Logger, Service } from 'homebridge';

import Lamp, { LampColor } 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 whiteState: {
		brt: number;
		on: boolean;
	};

	private rgbState: {
		hue: number;
		sat: number;
		brt: number;
		on: boolean;
	};

	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.whiteState = {
			on: false,
			brt: 100,
		};

		this.rgbState = {
			on: false,
			brt: 100,
			sat: 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 setOn = (on: boolean) => {
			this.whiteState.on = on;
			if (this.rgbState.on) {
				this.RGBBulbService.getCharacteristic(this.api.hap.Characteristic.On).setValue(false);
			}
		};
		this.whiteBulbService.getCharacteristic(this.api.hap.Characteristic.On)
			.onGet(() => this.whiteState.on)
			.onSet((on: boolean) => {
				setOn(on);
				this.updateLamp();
			});
		this.whiteBulbService.getCharacteristic(this.api.hap.Characteristic.Brightness)
			.onGet(() => this.whiteState.brt)
			.onSet((brt: number) => {
				setOn(true);
				this.whiteState.brt = brt;
				this.updateLamp();
			});
	}

	registerRGBBulbServices() {
		var setOn = (on: boolean) => {
			this.rgbState.on = on;
			if (this.whiteState.on) {
				this.whiteBulbService.getCharacteristic(this.api.hap.Characteristic.On).setValue(false);
			}
		};
		this.RGBBulbService.getCharacteristic(this.api.hap.Characteristic.On)
			.onGet(() => this.rgbState.on)
			.onSet((on: boolean) => {
				setOn(on);
				this.updateLamp();
			});
		this.RGBBulbService.getCharacteristic(this.api.hap.Characteristic.Brightness)
			.onGet(() => this.rgbState.brt)
			.onSet((brt: number) => {
				setOn(true);
				this.rgbState.brt = brt;
				this.updateLamp();
			});
		this.RGBBulbService.getCharacteristic(this.api.hap.Characteristic.Hue)
			.onGet(() => this.rgbState.hue)
			.onSet((hue: number) => {
				setOn(true);
				this.rgbState.hue = hue;
				this.updateLamp();
			});
		this.RGBBulbService.getCharacteristic(this.api.hap.Characteristic.Saturation)
			.onGet(() => this.rgbState.sat)
			.onSet((sat: number) => {
				setOn(true);
				this.rgbState.sat = sat;
				this.updateLamp();
			});
	}

	updateLamp() {
		if (this.rgbState.on) {
			var rgb = Color({ h: this.rgbState.hue, s: this.rgbState.sat, v: this.rgbState.brt });
			this.lamp.color = [rgb.red(), rgb.green(), rgb.blue(), 0];
		} else if (this.whiteState.on) {
			var value = Math.floor(this.whiteState.brt / 100 * 255);
			this.lamp.color = [0, 0, 0, value];
		} else {
			this.lamp.color = [0, 0, 0, 0];
		}
	}

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