-
Notifications
You must be signed in to change notification settings - Fork 1
/
lifx-cli.js
executable file
·102 lines (96 loc) · 2.33 KB
/
lifx-cli.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
#! /usr/bin/env node
const yargs = require("yargs");
const lifx = require("node-lifx-lan");
function noop() {}
function exit() {
process.exit(1);
}
function demandBulbFlags(yargs) {
return yargs
.option("ip", {
describe: "Bulb IP-address"
})
.option("mac", {
describe: "Bulb MAC-address"
})
.demandOption(["ip", "mac"]);
}
function createColorObject(
hue = 1,
saturation = 1,
brightness = 1,
kelvin = 3000
) {
return {
color: {
hue: Math.min(1, Math.max(parseFloat(hue), 0)), // 0.0-1.0
saturation: Math.min(1, Math.max(parseFloat(saturation), 0)), // 0.0-1.0
brightness: Math.min(1, Math.max(parseFloat(brightness), 0)), // 0.0-1.0
kelvin: Math.min(9000, Math.max(parseInt(kelvin), 1500)) // 1500-9000
}
};
}
yargs
.version()
.help()
.command(
"color <hue> <saturation> <brightness> <kelvin>",
"Set light color",
demandBulbFlags,
({ ip, mac }) =>
lifx
.createDevice({
ip,
mac
})
.then(device =>
device.setColor(
createColorObject(hue, saturation, brightness, kelvin)
)
)
.then(exit)
)
.command("power <on|off>", "Power light on or off", yargs =>
yargs
.command(
"on",
"Power light on",
yargs =>
demandBulbFlags(yargs).option("color", {
describe:
"Color object. Example: 'hue,saturation,brightness,kelvin'"
}),
({ ip, mac, color }) =>
lifx
.createDevice({
ip,
mac
})
.then(
device =>
color
? device.turnOn(createColorObject(...color.split(",")))
: device.turnOn()
)
.then(exit)
)
.command("off", "Power light off", demandBulbFlags, ({ ip, mac }) =>
lifx
.createDevice({
ip,
mac
})
.then(device => device.turnOff())
.then(exit)
)
)
.command("discover", "Discover lights on your network", noop, () =>
lifx
.discover()
.then(list =>
list.forEach(({ mac, ip, deviceInfo: { label, productName } }) =>
console.log(`${productName} (${label})\t${ip}\t${mac}`)
)
)
.then(exit)
).argv;