-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmicrowave_webapp.rb
executable file
·113 lines (94 loc) · 2.33 KB
/
microwave_webapp.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env ruby
require 'sinatra'
require 'yaml'
require 'json'
require File.expand_path('../../microwave_daemon/lib/client', __FILE__)
configure do
set server: 'thin', bind: '0.0.0.0', port: ENV['PORT'] || '80', connections: []
connected = false
while !connected
begin
set :microwave, Microwave::Daemon::Client.new
connected = true
rescue
puts "Could not connect to microwave daemon... retrying after 1s"
sleep 1
end
end
set :barcodes_file, File.expand_path("../recent_barcodes.yml", __FILE__)
end
def fetch_info
begin
settings.microwave.fetch_info
info = settings.microwave.info
seconds = info[:time].to_i % 60
minutes = info[:time].to_i / 60
info[:formatted_time] = format("%d:%02d", minutes, seconds)
info[:power_string] = case info[:power]
when 0
"Off"
when 3
"Defrost"
when 5
"Low"
when 7
"Medium"
when 10
"High"
else
info[:power]
end
rescue
info = nil
end
return info
end
get '/' do
if File.exists?(settings.barcodes_file)
@barcodes = YAML.load_file(settings.barcodes_file)
else
@barcodes = {known: [], unknown: []}
end
@info = fetch_info
erb :touchpad
end
post '/clear_barcodes' do
if File.exists?(settings.barcodes_file)
File.delete(settings.barcodes_file)
end
redirect to('/')
end
get '/events', provides: 'text/event-stream' do
response.headers['X-Accel-Buffering'] = 'no' # Disable buffering for nginx
stream :keep_open do |out|
settings.connections << out
out.callback { settings.connections.delete(out) }
end
end
def format_event(body)
"data: #{body}\n\n"
end
info_thread = Thread.new do
previous_data = nil
while true
if settings.connections.any?
if File.exists?(settings.barcodes_file)
barcodes = YAML.load_file(settings.barcodes_file)
else
barcodes = {known: [], unknown: []}
end
info = fetch_info || {error: true}
data = {info: info, barcodes: barcodes}
if data != previous_data
event = format_event(data.to_json)
settings.connections.each { |out| out << event }
previous_data = data
end
end
sleep 0.2
end
end
get '/button/:name' do
puts "Pressing button: #{params[:name]}"
settings.microwave.send_request(:command => {new_button: params[:name]})
end