-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
264 lines (224 loc) · 6.8 KB
/
app.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#
# Heatman - Heat manager system
# Copyleft 2014 - Nicolas AGIUS <[email protected]>
#
###########################################################################
#
# This file is part of Heatman.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###########################################################################
require 'sinatra/base'
require 'sinatra/json'
require 'sinatra/config_file'
require 'rufus/scheduler'
require 'logger'
require_relative 'app/heatman'
require_relative 'app/logs'
class App < Sinatra::Base
register Sinatra::ConfigFile
helpers Sinatra::JSON
helpers Heatman
# Framework configuration
configure :production do
enable :logging
set :logging, Logger::DEBUG
end
configure :development do
set :show_exceptions, :after_handler
enable :logging
set :logging, Logger::DEBUG
end
# Application configuration
config_file "config/config.yml"
before do
# Load saved status at startup
@@overrides ||= load_overrides()
@@schedules ||= load_schedules()
# Logging configuration
logger.formatter = CustomFormatter.new request.env
end
# Error handling
error Heatman::Forbidden do |e|
halt 405, json(:err => e.message)
end
error Heatman::InternalError do |e|
halt 500, json(:err => e.message)
end
# Global variable (to be initialized at first request)
@@overrides = nil
@@schedules = nil
# Setup the timer
if settings.respond_to? :timer
Rufus::Scheduler.new.every(settings.timer, :first_in => "1s") do
call(
'REQUEST_METHOD' => 'POST',
'PATH_INFO' => '/api/tictac',
'rack.input' => StringIO.new,
'rack.errors' => $stderr
)
end
end
## Application routes
# Main page
get '/' do
erb :index
end
# Get the list of available channels
get '/api/channels' do
channels = Hash.new
settings.channels.each do |channel, options|
# Filter only wanted keys
channels[channel]=options.select do |k,v|
%w[label modes].include? k
end
end
json channels
end
# Get the current mode
get '/api/channel/:channel/?' do |channel|
sanitize_channel!(channel)
json(
:mode => get_current_mode(channel),
:time => get_last_change(channel),
:override => @@overrides.has_key?(channel),
:persistent => @@overrides[channel].nil? ? false : @@overrides[channel][:persistent])
end
# Reset override
post '/api/channel/:channel/auto' do |channel|
sanitize_channel!(channel)
@@overrides.delete(channel)
save_overrides(@@overrides)
logger.info "Manual override deleted for channel #{channel}"
switch(channel, get_scheduled_mode(channel))
end
# Override scheduled mode
post '/api/channel/:channel/:mode' do |channel, mode|
sanitize_channel!(channel)
begin
sanitize_mode!(channel, mode)
rescue Heatman::InternalError
raise Heatman::Forbidden, "Mode not allowed"
end
# Remember if we have a manual override
@@overrides[channel] = {
:mode => mode,
:persistent => params['persistent'] == "true"
}
save_overrides(@@overrides)
logger.info "Manual override set for channel #{channel} : #{params['persistent'] == "true"?"persistent ":"" }#{mode}"
switch(channel, mode)
end
# Schedule a mode override
post '/api/channel/:channel/schedule/:mode' do |channel, mode|
sanitize_channel!(channel)
if mode != "auto"
begin
sanitize_mode!(channel, mode)
rescue Heatman::InternalError
raise Heatman::Forbidden, "Mode not allowed"
end
end
# Convert timestamp string to Time object
time = Time.at(params['timestamp'].to_i)
# Remember this schedule with a random ID
@@schedules[rand(36**8).to_s(36).to_sym] = {
:channel => channel,
:mode => mode,
:time => time
}
save_schedules(@@schedules)
logger.info "Scheduled override saved for channel #{channel} : #{mode} at #{time.rfc2822}"
status 200
end
# Get the list of current schedules
get '/api/schedules' do
# Return nested hash with time as timestamp instead of string
json Hash[@@schedules.map {|id,sched| [id, Hash[sched.map {|k,v| [k, k.eql?(:time) ? v.to_i : v]}]]}]
end
# Cancel specified schedule
delete '/api/schedule/:id' do |id|
id=id.to_sym
if @@schedules.has_key? id
logger.info "Scheduled override cancelled for channel #{@@schedules[id][:channel]} : #{@@schedules[id][:mode]} at #{@@schedules[id][:time].rfc2822}"
@@schedules.delete(id)
save_schedules(@@schedules)
end
status 204
end
# Route used by the timer (crontab)
post '/api/tictac' do
# Execute scheduled overrides
@@schedules.each do |id, schedule|
if schedule[:time] <= Time.now
if schedule[:mode] == "auto"
@@overrides.delete(schedule[:channel])
else
@@overrides[schedule[:channel]] = {
:mode => schedule[:mode],
:persistent => false
}
end
logger.info "Scheduled override set for channel #{schedule[:channel]} : #{schedule[:mode]}"
@@schedules.delete(id)
save_schedules(@@schedules)
save_overrides(@@overrides)
end
end
# Switch to requested mode
settings.channels.each do |channel, options|
begin
if @@overrides.has_key?(channel)
if not @@overrides[channel][:persistent] and @@overrides[channel][:mode] == get_scheduled_mode(channel)
# Reset temporary override
@@overrides.delete(channel)
save_overrides(@@overrides)
logger.info "Manual override expired for channel #{channel}"
status 200
else
# Ensure requested mode is enabled (in case of external modification)
switch(channel, @@overrides[channel][:mode])
end
else
# Set the scheduled mode only if no override
switch(channel, get_scheduled_mode(channel))
end
rescue Heatman::InternalError => e
logger.error "Failed to switch channel #{channel}: #{e.message}"
status 500
end
end
# Clear any body that may be populated by an exception
body ""
end
# Get the list of available sensors
get '/api/sensors' do
sensors = Hash.new
settings.sensors.each do |sensor, options|
# Filter only wanted keys
sensors[sensor]=options.select do |k,v|
%w[label unit].include? k
end
sensors[sensor][:history]=options.has_key?("rrd")
end
json sensors
end
# Get the current value of the specified sensor
get '/api/sensor/:sensor/?' do |sensor|
sanitize_sensor!(sensor)
json( :name => sensor, :value => get_sensor_value(sensor) )
end
end
# vim: ts=4:sw=4:ai:noet