aboutsummaryrefslogtreecommitdiff
path: root/lamp.ts
blob: 25841927fcb949712ff06bf155782aa99b8e43e0 (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
import { ChildProcess, spawn } from 'child_process';
import { Logger } from 'homebridge';
import { join } from 'path';

export type LampColor = [number, number, number, number];

export default class Lamp {
	#color: LampColor;
	private subpr: ChildProcess;

	constructor(public addr: string, public log: Logger) {
		this.subpr = spawn(join(__dirname, '/venv/bin/python3'), [join(__dirname, './main.py'), addr]);

		// debug
		this.subpr.stderr.on('data', this.log.error);
		this.subpr.stdout.on('data', this.log.log);
	}

	set color(newColor: LampColor) {
		this.#color = newColor.map(c => Math.floor(c)) as LampColor;
		this.subpr.stdin.write(this.colorToString() + '\n');
	}

	get color() {
		return this.#color;
	}

	private colorToString() {
		return this.color.map(i => i.toString(16).padStart(2, '0')).join('');
	}
}

// ! DEBUG
// if (typeof require !== 'undefined' && require.main === module) {
// 	var lamp = new Lamp("FC:58:FA:A1:CF:F1");
//
// 	setInterval(() => {
// 		lamp.color = [0, 0, 0, Math.floor(Math.random() * 255)];
// 	}, 100);
// }