Skip to content

Commit

Permalink
Update only if panel is on
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlgo11 committed Apr 1, 2021
1 parent ae939fd commit 879f36e
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand All @@ -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

0 comments on commit 879f36e

Please sign in to comment.