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
|
import axios from 'axios';
import { AccessoryConfig, AccessoryPlugin, API, Logger, Service } from 'homebridge';
const Color = require('color');
export default class ESP8266RGBStrip implements AccessoryPlugin {
private infoService: Service;
private bulbService: Service;
private lastMessage: string;
private state: {
on: boolean;
brightness: number;
saturation: number;
hue: number;
};
public name: string;
public host: string;
constructor(
public readonly log: Logger,
public readonly config: AccessoryConfig,
public readonly api: API,
) {
this.name = config.name;
this.host = config.host;
this.state = {
on: false,
brightness: 100,
saturation: 0,
hue: 0,
};
this.infoService = new this.api.hap.Service.AccessoryInformation()
.setCharacteristic(this.api.hap.Characteristic.Manufacturer, 'nodemcu')
.setCharacteristic(this.api.hap.Characteristic.Model, 'esp8266');
this.bulbService = new this.api.hap.Service.Lightbulb(this.name);
this.registerServices();
}
registerServices() {
this.bulbService.getCharacteristic(this.api.hap.Characteristic.On)
.onGet(() => this.state.on)
.onSet((on: boolean) => {
this.state.on = on;
this.updateLamp();
});
this.bulbService.getCharacteristic(this.api.hap.Characteristic.Brightness)
.onGet(() => this.state.brightness)
.onSet((brt: number) => {
this.state.brightness = brt;
this.updateLamp();
});
this.bulbService.getCharacteristic(this.api.hap.Characteristic.Hue)
.onGet(() => this.state.hue)
.onSet((hue: number) => {
this.state.hue = hue;
this.updateLamp();
});
this.bulbService.getCharacteristic(this.api.hap.Characteristic.Saturation)
.onGet(() => this.state.saturation)
.onSet((sat: number) => {
this.state.saturation = sat;
this.updateLamp();
});
}
updateLamp() {
var rgb = Color({
h: this.state.hue,
s: this.state.saturation,
v: Number(this.state.on && this.state.brightness),
});
var color = [rgb.red(), rgb.green(), rgb.blue()].map(i => Math.floor(i).toString(16).padStart(2, '0')).join('');
if (color != this.lastMessage) {
axios.post('http://' + this.host, color)
.catch(() => this.log.warn('request failed'));
}
this.lastMessage = color;
}
getServices() {
return [
this.infoService,
this.bulbService,
];
}
}
module.exports = (api: API) => {
api.registerAccessory('ESP8266RGBStrip', ESP8266RGBStrip);
};
|