This repository has been archived by the owner on Dec 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebservice.rb
executable file
·355 lines (289 loc) · 7.48 KB
/
webservice.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/env ruby
require 'sinatra'
require 'thin'
require './lib/lotomation'
include Lotomation
#set :server, :puma
set :bind, '0.0.0.0'
set :port, Configs['webserver']['backendport']
set :protection, :except => [:http_origin]
use Rack::Protection::HttpOrigin, :origin_whitelist => Configs['webserver']['originwhitelist']
use Rack::Auth::Basic, "please log in" do |user, pass|
(user == Configs['webserver']['user'] and pass == Configs['webserver']['pass']) || (user == Configs['webserver']['demouser'] and pass == Configs['webserver']['demopass'])
end
get '/' do
erb :index
end
get '/full' do
erb :bigremote
end
get '/snap' do
erb :snapshot
end
get '/ghetto-nest' do
erb :ghetto_nest
end
get '/la' do
erb :losangeles_snap
end
get '/tiny-nest' do
erb :tiny_nest, :layout => false
end
get '/snap/enforce' do
check_value('snap')
end
get '/buttoncontrol/hvac' do
erb :buttoncontrol_hvac, :layout => false
end
get '/buttoncontrol/index' do
erb :buttoncontrol_index, :layout => false
end
get '/buttoncontrol/tempknob' do
erb :temp_knob, :layout => false
end
get '/panel/index' do
@steps_alert = steps_alert
@punishments = steps_punishments
erb :panel_index, :layout => false
end
post '/switch/:device/:state' do |device, state|
device == 'all' ? actuate_all(state) : actuate(device, state)
redirect request.referrer
end
# start noodling on remembering state
post '/lights/:state' do |state|
['aquariums', 'farside-light', 'nearside-light', 'chaise'].each do |device|
if check_value(device) != state
actuate(device, state)
end
end
['chair-light', 'garden', 'bedside-light'].each do |wemodevice|
if check_value(wemodevice) != state
write_value(wemodevice, state)
end
end
end
post '/lights/radio/:state' do |state|
['aquariums', 'farside-light', 'nearside-light', 'chaise'].each do |device|
if check_value(device) != state
actuate(device, state)
end
end
end
post '/lights/wemo/:state' do |state|
['chair-light', 'garden', 'bedside-light'].each do |wemodevice|
if check_value(wemodevice) != state
write_value(wemodevice, state)
end
end
end
post '/rememberstate' do
rf_list = Array.new
wemo_list = Array.new
['aquariums', 'farside-light', 'nearside-light', 'chaise'].each do |device|
if check_value(device) == 'on'
rf_list << device
end
end
['chair-light', 'garden', 'bedside-light'].each do |wemodevice|
if check_value(wemodevice) == 'on'
wemo_list << wemodevice
end
end
unless radio_list.empty?
write_value('rf_laststate', rf_list.join(' '))
end
unless wemo_list.empty?
write_value('wemo_laststate', wemo_list.join(' '))
end
end
post '/restorestate' do
radioarray = check_value('rf_laststate').split(' ')
wemoarray = check_value('wemo_laststate').split(' ')
radioarray.each do |device|
if check_value(device) != 'on'
actuate(device, 'on')
end
end
wemoarray.each do |device|
if check_value(device) != 'on'
write_value(device, 'on')
end
end
end
post '/restorestate/radio' do
radioarray = check_value('rf_laststate').split(' ')
radioarray.each do |device|
if check_value(device) != 'on'
actuate(device, 'on')
end
end
end
post '/restorestate/wemo' do
wemoarray = check_value('wemo_laststate').split(' ')
wemoarray.each do |device|
if check_value(device) != 'on'
write_value(device, 'on')
end
end
end
# /noodle
post '/flip/:name' do |name|
flip(name)
redirect request.referrer
end
post '/steps/alert' do
steps_alert
end
post '/tracker/:checkpoint' do |checkpoint|
rssi = params[:rssi]
location = checkpoint_interpret_rawlocation(rssi)
write_value(checkpoint,location)
"got you at #{location}"
end
get '/lo/home' do
lo_home? ? "yes" : "no"
end
get '/lo/location' do
lo_location
end
post '/lo/work' do
write_value('lowork', 'yes')
end
get '/jam/:location/played' do |location|
check_value(location + '-jam')
end
post '/jam/:location/played' do
write_value(location + '-jam', 'true')
"played #{location} jam"
end
post '/jam/:location/force' do
write_value(location + '-jam', 'false')
redirect request.referrer
end
get '/stereo/input' do
check_value('stereo-input')
end
get '/stereo/input/:number' do |input|
write_value('stereo-input', input)
redirect request.referrer
end
# FIXME
post '/locator/enforce' do
if check_value('locator') == 'on'
if lo_home?
checkpoint_get_devices('hi-pi').each { |d| actuate(d, 'on') }
else
Configs['devices']['433Mhz'].each do |device|
device =~ /aquarium|stereo/ ? next : actuate(device,'off')
end
write_value('jam', 'false')
end
end
"k"
end
post '/locator/:state' do |state|
write_value('locator', state)
redirect request.referrer
end
get '/punishments/enforce' do
check_value('punishments')
end
post '/punishments/enforce' do
if check_value('punishments') == 'on'
steps_punishments_enforce
"okay"
else
"not enforced"
end
end
get '/hvac' do
check_value('hvac')
end
get '/hvac/historical' do
read_historical_www('hvac', 12)
end
get '/hvac/:state' do |state|
if check_value('maintain') == 'on'
log_historical('hvac', "no longer maintaining #{check_value('maintain-temp')}")
write_value('maintain', 'off')
end
log_historical('hvac', "switching to #{state}")
write_value('hvac', state)
redirect request.referrer
end
get '/temperature/:tracker' do |tracker|
check_value('temperature-' + tracker)
end
post '/temperature/:tracker' do |tracker|
degrees_f = degrees_convert_to_f(params[:rawtemp])
write_value('temperature-' + tracker, degrees_f)
log_historical("trackertemp", "#{tracker} measured #{degrees_f}F")
"cool"
end
get '/temperature/bed-pi/historical' do
read_historical_www('hvac', 10)
end
get '/maintain/temp' do
check_value('maintain-temp')
end
post '/maintain/temp' do
write_value('maintain-temp', params[:temp])
log_historical('hvac', "maintain temperature changed to #{params[:temp]}")
redirect request.referrer
end
get '/maintain/enforce' do
check_value('maintain')
end
post '/maintain/enforce' do
check_value('maintain') == 'on' ? maintain_temp : "not maintaining temp"
end
get '/maintain/enforce/:state/:tracker' do |state,tracker|
log_historical('hvac', "setting maintaining #{check_value('maintain-temp')} to #{state}")
write_value('maintain', state)
write_value('maintain-tracker', tracker)
if request.referrer
redirect request.referrer
else
"OK"
end
end
get '/nightlight' do
check_value('nightlight')
end
post '/nightlight/:state' do |state|
write_value('nightlight', state)
redirect request.referrer
end
get '/traffic/:direction' do |direction|
traffic(direction)
end
post '/door' do
write_value('frontdoor', "open")
sms_out("#{Time.now.ctime} - front door opened") unless lo_home?
end
get '/vpn/response/time' do
vpn_response_time
end
get '/vpn/server/:command' do |command|
openvpn_server(command)
end
post '/twilio/sms' do
if check_value('sms-in') == 'on'
bodyfull=params[:Body]
@reply = sms_in(bodyfull)
else
@reply = "sorry, sms is off. click #{Configs['webserver']['protocol']}://#{Configs['webserver']['host']}:#{Configs['webserver']['port']}/sms-in/enforce/on to enable"
end
erb :twilio_response, :layout => false
end
post '/:configuration/enforce/:state' do |configuration,state|
write_value(configuration, state)
redirect request.referrer
end
get '/:configuration/state' do |configuration|
check_value(configuration)
end
get '/updatetime/human/:file' do |file|
check_update_humantime(file)
end