diff --git a/main.rb b/main.rb index e6e4ca3..2cf8071 100755 --- a/main.rb +++ b/main.rb @@ -9,7 +9,7 @@ require 'open-uri' colors = %w[#ebedf0 #9be9a8 #40c463 #30a14e #216e39s] -panels = %w[7678 24134 14755 43428 12412 55637 36730 28113 25017] +panels = %w[14755 24134 7678 43428 12412 55637 36730 28113 25017] data = [] def fetch_var(name) @@ -26,7 +26,7 @@ def fetch_github_activity(length) end # rubocop:disable Metrics/AbcSize -def send_data(data) +def set_colors(data) effect = JSON.parse(File.read('effect.json')) effect['write']['animData'] = data url = URI("http://#{fetch_var('NANOLEAF_HOST')}/api/v1/#{fetch_var('NANOLEAF_TOKEN')}/effects") @@ -35,9 +35,20 @@ def send_data(data) response = Net::HTTP.new(url.host, url.port).start { |http| http.request(request) } throw("Unable to change colors (#{response.code} #{response.body})") unless response.code.eql?('204') end -# rubocop:enable Metrics/AbcSize -days = fetch_github_activity(panels.length) -panels.each_with_index { |id, i| data << "#{id} 1 #{ColorConverter.rgb(colors[days[i]]).join(' ')} 0 5" } +def fetch_state + url = URI("http://#{fetch_var('NANOLEAF_HOST')}/api/v1/#{fetch_var('NANOLEAF_TOKEN')}/state") + request = Net::HTTP::Get.new(url) + response = Net::HTTP.new(url.host, url.port).start { |http| http.request(request) } + throw("Unable to change colors (#{response.code} #{response.body})") unless response.code.eql?('200') + body = JSON.parse(response.body) + throw("Power state not available (#{response.code})") if body['on'].nil? || body['on']['value'].nil? + body['on']['value'] +end +# rubocop:enable Metrics/AbcSize -send_data("#{panels.length} #{data.join(' ')}") +if fetch_state + puts days = fetch_github_activity(panels.length) + panels.each_with_index { |id, i| data << "#{id} 1 #{ColorConverter.rgb(colors[days[i]]).join(' ')} 0 5" } + set_colors("#{panels.length} #{data.join(' ')}") +end