aboutsummaryrefslogtreecommitdiff
path: root/lamp.ts
blob: fc4e7afbc6be2266b65f575ff7d9838bac25d3bf (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
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;
	private last: string;

	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;
		var message = this.colorToString();
		if (this.last == message) return; // prevent duplicate messages
		this.log.info(message);
		this.subpr.stdin.write(message + '\n');
		this.last = message;
	}

	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);
// }