forked from jrudio/smart-light-disco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbulb.js
130 lines (107 loc) · 2.24 KB
/
bulb.js
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
// BulbWrapper wraps around the bulb from 'tplink-smarthome-api'
class BulbWrapper {
constructor (bulb) {
this.bulb = bulb
this.hue = 0
this.saturation = 0
this.colorTemp = 0
this.brightness = 0
this.discoInterval = null
}
static Blue () {
return {
hue: 239,
saturation: 99,
colorTemp: 0,
}
}
static Red () {
return {
hue: 0,
saturation: 100,
colorTemp: 0,
}
}
static Green () {
return {
hue: 120,
saturation: 100,
colorTemp: 0,
}
}
static Purple () {
return {
hue: 300,
saturation: 100,
colorTemp: 0,
}
}
// Pink not pink, it's like a peach
static Pink () {
return {
hue: 349,
saturation: 24,
colorTemp: 0,
}
}
static Brown () {
return {
hue: 30,
saturation: 100,
colorTemp: 0,
}
}
static White () {
return {
hue: 0,
saturation: 0,
colorTemp: 5000,
}
}
setColor (color) {
if (!color) {
throw new Error('invalid bulb color')
}
const defaults = {
brightness: 100
}
const lightSettings = Object.assign(
{},
defaults,
color
)
lightSettings.color_temp = lightSettings.colorTemp
return this.bulb.lighting.setLightState(lightSettings)
}
startDisco (changeInterval = 1000) {
const getRandomColor = (previousColor = undefined) => {
const colors = [
'brown',
'red',
'blue',
'purple',
'pink',
'green',
]
const randomIndex = () => Math.floor(Math.random() * colors.length)
const toMethodName = (color) => {
const firstToUpper = color[0].toUpperCase()
return firstToUpper + color.slice(1)
}
let index = randomIndex()
let color = colors[index]
if (!color) {
return ''
} else if (previousColor === color) {
color = getRandomColor(previousColor)
}
return toMethodName(color)
}
this.discoInterval = setInterval(async () => {
let color = getRandomColor()
// console.log(`setting bulb color to ${color}`)
await this.setColor(BulbWrapper[color]())
}, changeInterval);
}
}
module.exports = BulbWrapper