Skip to content

Commit 1e5bbeb

Browse files
author
Christopher
committed
Add commandline interface
1 parent fc2533e commit 1e5bbeb

File tree

5 files changed

+133
-5
lines changed

5 files changed

+133
-5
lines changed

Gemfile.lock

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
PATH
22
remote: .
33
specs:
4-
ledenet_api (0.1.11)
5-
bindata (>= 1.8)
4+
ledenet_api (1.1.0)
5+
bindata (>= 2.3)
6+
ipaddress (>= 0.8)
67

78
GEM
89
remote: https://rubygems.org/
910
specs:
1011
bindata (2.3.0)
1112
diff-lcs (1.2.5)
13+
ipaddress (0.8.3)
1214
rake (10.4.2)
1315
rspec (3.3.0)
1416
rspec-core (~> 3.3.0)

bin/ledenet-ufo

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#!/usr/bin/ruby
2+
3+
require 'optparse'
4+
require 'json'
5+
6+
require 'ledenet_api'
7+
require 'ipaddress'
8+
9+
options = {}
10+
11+
banner = <<-BANNER
12+
Usage: ledenet-ufo --list
13+
OR: ledenet-ufo [IP|HW ADDR] [OPTIONS]
14+
15+
BANNER
16+
17+
if ARGV.count > 0 && !ARGV.first.start_with?('-')
18+
arg = ARGV.shift
19+
20+
if IPAddress.valid?(arg)
21+
options[:ip] = arg
22+
elsif /^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}$/i.match(arg)
23+
options[:hw_addr] = arg
24+
else
25+
raise "Invalid device speicifier \"#{arg}\". Must be ip or mac address."
26+
end
27+
end
28+
29+
opts = OptionParser.new do |opts|
30+
opts.banner = banner
31+
32+
opts.on("-r", "--red [VALUE]", Integer, "Set red to VALUE") do |v|
33+
options[:red] = v
34+
end
35+
36+
opts.on("-g", "--green [VALUE]", Integer, "Set green to VALUE") do |v|
37+
options[:green] = v
38+
end
39+
40+
opts.on("-b", "--blue [VALUE]", Integer, "Set blue to VALUE") do |v|
41+
options[:blue] = v
42+
end
43+
44+
opts.on("-w", "--warm-white [VALUE]", Integer, "Set warm white to VALUE") do |v|
45+
options[:warm_white] = v
46+
end
47+
48+
opts.on("--on", "Turn on the controller") do |v|
49+
options[:on?] = true
50+
end
51+
52+
opts.on("--off", "Turn off the controller") do
53+
options[:on?] = false
54+
end
55+
56+
opts.on("-l", "--list", Integer, "Prints a list of available devices and exists") do |v|
57+
options[:list] = true
58+
end
59+
60+
opts.on("-s", "--status", "Prints status as JSON") do |v|
61+
options[:print_status?] = true
62+
end
63+
64+
opts.on("-h", "--help", "Prints this help message") do
65+
options[:print_help?] = true
66+
end
67+
end
68+
69+
opts.parse!
70+
71+
if options[:print_help?]
72+
puts opts
73+
exit 0
74+
end
75+
76+
begin
77+
if options[:list]
78+
if options.count > 1
79+
warn "--list is incompatible with other options!\n\n"
80+
warn opts
81+
exit 1
82+
end
83+
84+
devices = LEDENET.discover_devices(expected_devices: 1000)
85+
86+
row_format = "%16s %18s %20s\n"
87+
printf row_format, "IP ADDRESS", "HW ADDRESS", "Model #"
88+
89+
LEDENET.discover_devices.each do |device|
90+
printf row_format, device.ip, device.hw_addr, device.model
91+
end
92+
else
93+
ip = nil
94+
if options[:ip]
95+
ip = options[:ip]
96+
elsif options[:hw_addr]
97+
r = LEDENET.discover_devices.select { |x| x.hw_addr == options[:hw_addr] }
98+
if r.empty?
99+
raise "Couldn't find device with mac addr: #{options[:hw_addr]}"
100+
end
101+
ip = r.first.ip
102+
else
103+
ip = LEDENET.discover_devices.first.ip
104+
end
105+
106+
api = LEDENET::Api.new(ip)
107+
108+
api.set_power(options[:on?]) unless options[:on?].nil?
109+
api.update_color_data(options.select { |k,_| %w{red green blue warm_white}.include?(k.to_s) })
110+
111+
if options[:print_status?]
112+
status = api.current_color_data
113+
status = status.merge(is_on: api.on?)
114+
115+
puts status.to_json
116+
end
117+
end
118+
rescue Exception => e
119+
puts "Error: #{e}"
120+
puts e.backtrace.join("\n")
121+
end

ledenet_api.gemspec

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ Gem::Specification.new do |spec|
1414
spec.license = "MIT"
1515

1616
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17-
spec.bindir = "exe"
18-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
17+
spec.bindir = "bin"
18+
spec.executables = ["ledenet-ufo"]
1919
spec.require_paths = ["lib"]
2020

2121
spec.add_dependency 'bindata', '>= 2.3'
22+
spec.add_dependency 'ipaddress', '>= 0.8'
2223

2324
spec.add_development_dependency "bundler", "~> 1.10"
2425
spec.add_development_dependency "rake", "~> 10.0"

lib/ledenet/api.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ def off
2323
send_packet(LEDENET::Packets::SetPowerRequest.off_request)
2424
end
2525

26+
def set_power(v)
27+
v ? on : off
28+
end
29+
2630
def on?
2731
status.on?
2832
end

lib/ledenet/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module LEDENET
2-
VERSION = '1.0.1'
2+
VERSION = '1.1.0'
33
end

0 commit comments

Comments
 (0)