Class: Bstick::Server
- Inherits:
-
Object
- Object
- Bstick::Server
- Defined in:
- lib/bstick.rb
Instance Method Summary collapse
- #alarm_state ⇒ Object
- #black ⇒ Object
- #blink_state ⇒ Object
- #green ⇒ Object
- #handle_state ⇒ Object
- #init ⇒ Object
-
#initialize ⇒ Server
constructor
A new instance of Server.
- #off_state ⇒ Object
- #on_state ⇒ Object
- #ping_state ⇒ Object
- #random_state ⇒ Object
- #read_state ⇒ Object
- #red ⇒ Object
- #run ⇒ Object
- #set_colors(color_1, color_2) ⇒ Object
- #stick ⇒ Object
Constructor Details
#initialize ⇒ Server
Returns a new instance of Server.
8 9 10 |
# File 'lib/bstick.rb', line 8 def initialize init end |
Instance Method Details
#alarm_state ⇒ Object
107 108 109 110 111 112 113 114 |
# 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 ⇒ Object
27 28 29 |
# File 'lib/bstick.rb', line 27 def black @black ||= Color::RGB.from_html("#000000") end |
#blink_state ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# 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 ⇒ Object
31 32 33 |
# File 'lib/bstick.rb', line 31 def green @green = Color::RGB.new(0x00, 0xFF, 0x00) end |
#handle_state ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# 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 ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# 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 ⇒ Object
65 66 67 |
# File 'lib/bstick.rb', line 65 def off_state set_colors(black, black) end |
#on_state ⇒ Object
69 70 71 |
# File 'lib/bstick.rb', line 69 def on_state set_colors(green, green) end |
#ping_state ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# 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 ⇒ Object
98 99 100 101 102 103 104 105 |
# 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 ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/bstick.rb', line 39 def read_state @old_state = @state file = File.("/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 ⇒ Object
35 36 37 |
# File 'lib/bstick.rb', line 35 def red @red = Color::RGB.new(0xFF, 0x00, 0x00) end |
#run ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# 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. sleep 10 init end end end |
#set_colors(color_1, color_2) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# 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 ⇒ Object
23 24 25 |
# File 'lib/bstick.rb', line 23 def stick @b ||= BlinkStick.find_all.first end |