This repository has been archived by the owner on Aug 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlifx-tile-effects-framework.js
240 lines (219 loc) · 7.14 KB
/
lifx-tile-effects-framework.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
require('dotenv').config()
const path = require('path')
const chalk = require('chalk')
const fs = require('fs-extra')
const Lifx = require('node-lifx-lan')
const { Select } = require('enquirer')
const { log, sleep } = require('./src/utils')
const run = async () => {
log('Validating config…')
const config = await getValidConfig()
log('Getting effects…')
const effects = await getEffects(config.effectsPath)
log('Getting options…')
const options = getOptions(effects)
log('Finding tile…')
const { device, tiles, bounds } = await findTile(options)
log('Selecting effect…')
const effect = await selectEffect(options, effects)
log('Getting power…')
const hasPower = !! await getPower(device)
if (hasPower) {
log('Fading out…')
await fadeOut({ device, tiles })
}
log('Flushing tiles…')
await flushTiles({ device, tiles, effect })
if (!hasPower) {
log('Powering on…')
await powerOn(device)
}
log('Creating effect…')
await createEffect({ device, tiles, bounds, effect })
}
const getValidConfig = async () => {
const effectsPath = process.env.EFFECTS_PATH
if (!effectsPath) validationError('Effects path required.')
if (!(await fs.pathExists(effectsPath))) validationError(`Effects path [${effectsPath}] does not exist.`)
if (!(await fs.stat(effectsPath)).isDirectory()) validationError(`Effects path [${effectsPath}] is not a directory.`)
return { effectsPath }
}
const validationError = message =>
exit(message)
const getOptions = effects => {
const effectNames = Object.keys(effects)
return require('yargs')
.usage('Usage: node $0 [options]') // usage string of application.
.option('effect', {
alias: 'e',
describe: 'Effect name',
type: 'string',
choices: effectNames,
})
.option('clear-cache', {
alias: 'c',
describe: 'Clear device cache',
type: 'boolean',
})
.option('verbose', {
alias: 'v',
describe: 'Show debug logs',
type: 'boolean',
})
.version().alias('version', 'V')
.help().alias('help', 'h')
.example(`node $0 --effect ${effectNames[0]}`)
.epilog(getCredits())
.argv
}
const getCredits = () => `Credits:
Copyright (c) James Furey (https://github.com/furey)`
const getEffects = async effectsPath => {
const paths = await fs.readdir(effectsPath, { withFileTypes: true })
const effects = paths
.filter(dirent => !dirent.isDirectory())
.reduce((effects, dirent) => {
const fileName = dirent.name
const ext = path.extname(fileName)
if (!isValidExtension(ext)) return effects
const effectName = path.basename(fileName, ext)
const effect = require(path.resolve(effectsPath, fileName))
if (!isValidEffect(effect)) return effects
effects[effectName] = effect
return effects
}, {})
if (!Object.keys(effects).length) validationError(`No valid effect files found in [${effectsPath}].`)
return effects
}
const isValidExtension = ext =>
ext.toLowerCase() === '.js'
const isValidEffect = effect => {
if (typeof effect.create !== 'function') return false
if (typeof effect.getFlushColor !== 'function') return false
return true
}
const findTile = async options => {
const tileProductId = 55
const cachePath = path.resolve(__dirname, 'cache/devices.json')
let deviceJson = undefined
let hasDiscoveredDevices = false
let hasClearedCache = false
while (!deviceJson) {
log('Looking for cache…')
if (!await fs.pathExists(cachePath)) {
console.log('Discovering LIFX devices…')
const devices = await Lifx.discover()
log('Writing cache…')
await fs.outputJson(cachePath, devices, { spaces: 2 })
hasDiscoveredDevices = true
} else if (options.clear && !hasClearedCache) {
} else if (options['clear-cache'] && !hasClearedCache) {
console.log('Clearing device cache…')
await fs.remove(cachePath)
hasClearedCache = true
continue
}
log('Reading cache…')
const cache = await fs.readJson(cachePath)
log('Searching cache for tile…')
const tiles = cache.filter(device => device.deviceInfo.productId === tileProductId)
if (tiles.length > 1) {
deviceJson = await selectTile(tiles)
break
}
if (tiles.length === 1) {
deviceJson = tiles[0]
break
}
log('Tile not found in cache.')
if (hasDiscoveredDevices) await exit('No tile found.')
log('Clearing cache…')
await fs.unlink(cachePath)
console.log('Finding tile…')
}
log('Creating device…')
const device = await Lifx.createDevice({
mac: deviceJson.mac,
ip: deviceJson.ip,
})
log('Getting tiles/bounds…')
const { tiles, bounds } = await device.tileGetTilesAndBounds()
.catch(_ => exit('Tile not responding.'))
return { device, tiles, bounds }
}
const selectTile = async devices => {
const prompt = new Select({
name: 'tile',
message: 'Choose a tile',
choices: devices.map(deviceLabel)
})
const selected = await prompt.run()
.catch(_ => exit('No tile chosen.'))
return devices.find(device => deviceLabel(device) === selected)
}
const deviceLabel = device =>
`${device.deviceInfo.label} [${device.mac}]`
const selectEffect = async (options, effects) => {
const effectNames = Object.keys(effects)
const selected = options.effect
|| (effectNames.length === 1 ? effectNames[0] : false)
|| await selectEffectName(effectNames)
onSelected(selected)
return effects[selected]
}
const selectEffectName = async effectNames => {
const prompt = new Select({
name: 'effect',
message: 'Choose an effect',
choices: effectNames
})
return await prompt.run()
.catch(_ => exit('No effect chosen.'))
}
const onSelected = effect =>
console.log(`Starting ${chalk.cyan(effect)} effect… ${chalk.gray('(press [ctrl+c] to exit)')}`)
const fadeOut = async ({ device, tiles }) => {
const duration = process.env.INSTANT ? 50 : 1000
const firstTile = tiles[0]
await device.tileSetTileState64({
tile_index: firstTile.tile_index,
length: tiles.length,
duration,
colors: Array(firstTile.width * firstTile.height).fill({ hue: 0, saturation: 0, brightness: 0, kelvin: 2500 }),
}).catch(console.error)
await sleep(duration)
}
const getPower = async device => {
const { level } = await device.deviceGetPower()
.catch(console.error)
await sleep(50)
return level
}
const powerOn = async device => {
await device.deviceSetPower({ level: 1 })
.catch(console.error)
await sleep(50)
}
const flushTiles = async ({ device, tiles, effect }) => {
const firstTile = tiles[0]
await device.tileSetTileState64({
tile_index: firstTile.tile_index,
length: tiles.length,
duration: 0,
colors: Array(firstTile.width * firstTile.height).fill(effect.getFlushColor()),
}).catch(console.error)
await sleep(500)
}
const createEffect = async ({ device, tiles, bounds, effect }) =>
await effect.create({ device, tiles, bounds })
const exit = async (message = 'Exiting…') => {
log('Closing UDP connection…')
if (Lifx._initialized) await Lifx.destroy()
console.log(chalk.red(message))
console.log(chalk.gray('Exiting…'))
process.exit()
}
module.exports = {
run,
utils: require('./src/utils')
}