class Bstick::Server
Public Class Methods
new()
click to toggle source
# File lib/bstick.rb, line 8 def initialize init end
Public Instance Methods
alarm_state()
click to toggle source
# File lib/bstick.rb, line 107 def alarm_state time = Time.parse(@state.split(' ')[1]) if Time.now < time on_state else random_state end end
black()
click to toggle source
# File lib/bstick.rb, line 27 def black @black ||= Color::RGB.from_html("#000000") end
blink_state()
click to toggle source
# File lib/bstick.rb, line 73 def blink_state @values = { green: 0, direction: 20 } if !@values color_1 = Color::RGB.new(0xFF - @values[:green], @values[:green], 0x00) color_2 = Color::RGB.new(@values[:green], 0xFF - @values[:green], 0x00) @values[:green] += @values[:direction] if(@values[:green] < 0 || @values[:green] > 0xFF) @values[:direction] *= -1 end set_colors(color_1, color_2) end
green()
click to toggle source
# File lib/bstick.rb, line 31 def green @green = Color::RGB.new(0x00, 0xFF, 0x00) end
handle_state()
click to toggle source
# File lib/bstick.rb, line 116 def handle_state if @old_state != @state @values = nil @wait = 100000000000000 end case @state.split(' ').first when 'off' then off_state when 'random'then random_state when 'on' then on_state when 'blink' then blink_state when 'ping' then ping_state when 'alarm' then alarm_state end end
init()
click to toggle source
# File lib/bstick.rb, line 12 def init system 'sudo mkdir -p /var/bstick' system 'sudo chmod 777 /var/bstick' system 'sudo touch /var/bstick/led.state' system 'sudo chmod 777 /var/bstick/led.state' @logger = ::Logger.new('/var/bstick/bstick.log', 1, 1024000) @state = '' @values = nil @b = nil end
off_state()
click to toggle source
# File lib/bstick.rb, line 65 def off_state set_colors(black, black) end
on_state()
click to toggle source
# File lib/bstick.rb, line 69 def on_state set_colors(green, green) end
ping_state()
click to toggle source
# File lib/bstick.rb, line 84 def ping_state @wait += 1 return if @wait < 15 * 33 @wait = 0 url = @state.split(' ')[1] || "http://www.undefine.io" ping = Net::Ping::HTTP.new(url) @logger.info "ping #{url} #{ping.ping?}" if result = ping.ping? set_colors(green, green) else set_colors(red, red) end end
random_state()
click to toggle source
# File lib/bstick.rb, line 98 def random_state @wait += 1 return if @wait < 0.5 * 33 @wait = 0 color_1 = Color::RGB.new(rand(0..0xFF), rand(0..0xFF), rand(0..0xFF)) color_2 = Color::RGB.new(rand(0..0xFF), rand(0..0xFF), rand(0..0xFF)) set_colors(color_1, color_2) end
read_state()
click to toggle source
# File lib/bstick.rb, line 39 def read_state @old_state = @state file = File.expand_path("/var/bstick/led.state", __FILE__) @file = File.open(file, "a+") File.chmod(0777, file) @file.seek(0) @state = @file.readline.strip rescue 'off' @file.close end
red()
click to toggle source
# File lib/bstick.rb, line 35 def red @red = Color::RGB.new(0xFF, 0x00, 0x00) end
run()
click to toggle source
# File lib/bstick.rb, line 131 def run @logger.info 'started' loop do begin read_state handle_state sleep 1/33.0 rescue => e @logger.error e.message sleep 10 init end end end
set_colors(color_1, color_2)
click to toggle source
# File lib/bstick.rb, line 49 def set_colors(color_1, color_2) if stick if (color_1 != @color_1 || color_2 != @color_2) @logger.info @state stick.set_color(0, 0, color_1) stick.set_color(0, 1, color_2) @color_1 = color_1 @color_2 = color_2 end else @logger.error 'no stick found' sleep 10 init end end
stick()
click to toggle source
# File lib/bstick.rb, line 23 def stick @b ||= BlinkStick.find_all.first end