Skip to content

Instantly share code, notes, and snippets.

@JustinAiken
Created January 13, 2015 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JustinAiken/3bcf2c11844d99e07ee8 to your computer and use it in GitHub Desktop.
Save JustinAiken/3bcf2c11844d99e07ee8 to your computer and use it in GitHub Desktop.
status updater
require 'csv'
require 'httparty'
require 'timers'
class LightChecker
LIGHTS = {
"Office" => {
wink_id: '1',
hab_name: 'itm_light_office',
hab_level: 'itm_light_lvl_office'
}
}
BASE_HAB = "http://pi:8080/rest/items/"
BASE_CMD = '/path/to/wink_pass_thru/wink '
HEADERS = {"Content-Type" => "text/plain"}
attr_accessor :wink_id, :hab_name, :hab_level, :attributes, :status, :level
def initialize(params)
@wink_id = params[:wink_id]
@hab_name = params[:hab_name]
@hab_level = params[:hab_level]
end
def get_output
@output = `#{BASE_CMD} '-u -l -m#{wink_id}'`
end
def strip_output
@output = @output.split("\n")[2..-1].join("\n")
end
def parse
@attributes = CSV.parse(@output,
col_sep: "|",
headers: true,
skip_blanks: true,
header_converters: ->h { h.strip },
converters: ->c { c.strip }
)
@status = attributes[0]['GET']
@level = attributes[1]['GET']
end
def update_status!
response = HTTParty.put "#{BASE_HAB}#{hab_name}/state/", headers: HEADERS, body: @status
end
def update_level!
lvl = ((@level.to_i / 255.0) * 100).to_i
response = HTTParty.put "#{BASE_HAB}#{hab_level}/state/", headers: HEADERS, body: lvl.to_s
end
def self.update_all!
LIGHTS.each do |name, params|
LightChecker.new(params).tap do |checker|
checker.get_output
checker.strip_output
checker.parse
puts "#{name}: Status - #{checker.status} / Level (#{checker.level})"
checker.update_status!
checker.update_level!
end
end
end
end
timers = Timers::Group.new
timers.every(3) { LightChecker.update_all! }
loop { timers.wait }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment