forked from MetaMeute/metameutestatus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.rb
183 lines (150 loc) · 4.1 KB
/
status.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
# encoding: UTF-8
require 'rubygems'
require 'sqlite3'
require 'sinatra/base'
require 'time_diff'
require 'builder'
require 'json'
require 'sinatra/jsonp'
require 'yaml'
class StatusApp < Sinatra::Base
helpers Sinatra::Jsonp
config = YAML.load_file('config.yml')
helpers do
def h(text)
Rack::Utils.escape_html(text)
end
def getstatus
# max 20 status der letzten Woche
since = Time.now.getutc - 60*60*24*7
status = DB.execute("SELECT * FROM status WHERE timestamp > ? ORDER BY timestamp DESC LIMIT 20", since.to_s)
if status.length == 0
status = DB.execute("SELECT * FROM status ORDER BY timestamp DESC LIMIT 2")
end
return status
end
def getmessages(status)
# und nur Messages, die zu den gefundenen Status passen
messages = DB.execute("SELECT * FROM messages WHERE timestamp > ? ORDER BY timestamp LIMIT 200", status[-1]["timestamp"])
return messages
end
def getdata(status, messages)
if !status
status = getstatus()
end
if !messages
messages = getmessages(status)
end
@data = messages.concat(status)
@data.sort_by! { |k| k["timestamp"] }
return @data
end
end
configure do
DB = SQLite3::Database.new("status.db")
DB.results_as_hash = true
end
get '' do
redirect url('/')
end
get '/' do
@page_title = config["title"]
@door_open = 0
@duration = nil
status = getstatus()
messages = getmessages(status)
@data = getdata(status, messages)
state = nil
@data.each do |d|
if d.has_key? "door_open"
state = d["door_open"]
else
d["door_open"] = state
end
end
@data.reverse!
statusS = Array.new status
statusS.unshift nil
(statusS.zip status).each do |d, dPrev|
if d != nil and dPrev != nil
if d['door_open'] != dPrev['door_open']
start = Time.parse(d['timestamp'] + "UTC")
@duration = Time.diff(Time.now(), start, '%hh %mm')[:diff]
break
end
end
end
begin
@door_open = status[0]['door_open']
rescue
@door_open = 0
end
erb :index
end
post '/' do
message = params[:message]
if message.nil? || message.strip.length == 0 then
redirect '/'
end
message.strip!
# "Spamschutz"
if message.length < 136 and message !~ /.*([a-z0-9]+\.([a-z][a-z]|[a-z][a-z][a-z])($| .*))/i then
DB.execute("INSERT INTO messages (id, timestamp, message) VALUES (NULL,datetime('now'), ?)", message)
end
redirect url('/')
end
post '/door' do
door_open = params[:door_open]
if door_open.nil? then
halt 400
end
DB.execute("INSERT INTO status (id, timestamp, door_open) VALUES (NULL,datetime('now'), ?)", door_open)
redirect url('/')
end
get '/rss' do
@page_title = config["title"]
@data = getdata(false, false)
@data.reverse!
builder :rss
end
get '/json' do
status = getstatus()
messages = getmessages(status)
content_type 'application/json'
{ :status => status, :messages => messages }.to_json
end
get '/spaceapi.json' do
headers['Cache-Control'] = "no-cache"
headers['Access-Control-Allow-Origin'] = "*"
@data = getstatus()
@open = false
@lastchange = nil
dataS = Array.new @data
dataS.unshift nil
(dataS.zip @data).each do |d, dPrev|
if d != nil and dPrev != nil
if d['door_open'] != dPrev['door_open']
start = d['timestamp']
@lastchange = Time.parse(start + "UTC").localtime.strftime("%s").to_i
break
end
end
end
begin
@open = @data[0]['door_open'].to_i == 1
rescue
@open = false
end
json = YAML.load_file('status.yml')
json['state'][:open]= @open
json['state'][:lastchange] = @lastchange
#For compatibility with systems that expect it here...
json[:open]= @open
json[:lastchange] = @lastchange
res = jsonp json
content_type 'application/json', :charset => 'utf-8'
res
end
# start the server if ruby file executed directly
run! if app_file == $0
end