-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwlmonitor.rb
More file actions
228 lines (178 loc) · 4.48 KB
/
wlmonitor.rb
File metadata and controls
228 lines (178 loc) · 4.48 KB
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
require 'csv'
require 'active_support/all'
require 'sinatra/base'
require 'json'
require 'net/http'
require 'logger'
require "rack/cache"
require 'digest/sha1'
require './WLDataLoader.rb'
require './Haltestelle.rb'
require './Linie.rb'
require './Steig.rb'
class WLData
attr_accessor :haltestellen, :linien, :steige, :types
def initialize
@haltestellen = Hash.new
@linien = Hash.new
@steige = Hash.new
@types = Hash.new
end
end
class App < Sinatra::Base
attr_reader :logger
use Rack::Cache
def self.data
@@data
end
configure do
enable :logging
logger = Logger.new STDOUT
@logger = logger
logger.level = Logger::INFO
logger.datetime_format = '%a %d-%m-%Y %H:%M:%S '
set :logger, logger
set :logging, logger
set :google_maps_api_key, ENV['GOOGLE_MAPS_API_KEY']
set :wlsender, ENV['WLSENDER']
WLDataLoader.update_csv_files
end
@logger.info "Lese Haltestellen..."
@@data = WLData.new
CSV.foreach("./wl-data/haltestellen.csv", col_sep: ';', headers: true) do |row|
h = Haltestelle.new row
@@data.haltestellen[h.id] = h
end
@logger.info "Fertig, insgesamt #{@@data.haltestellen.size} Haltestellen gelesen."
@logger.info "Lese Linien..."
CSV.foreach("./wl-data/linien.csv", col_sep: ';', headers: true) do |row|
l = Linie.new row
@@data.linien[l.id] = l
end
linien_types = @@data.linien.map {|id, l| l.verkehrsmittel }.uniq
linien_types.each do |t|
@@data.types[t] = @@data.linien.select do |id,l|
l.is_a? Linie and l.verkehrsmittel == t
end
end
@logger.info "Fertig, insgesamt #{@@data.linien.size} Linien gelesen."
@logger.info "Lese Steige..."
CSV.foreach("./wl-data/steige.csv", col_sep: ';', headers: true) do |row|
s = Steig.new row
h = @@data.haltestellen[s.haltestelle]
@@data.linien[s.linie].haltestellen[s.reihenfolge.to_i] = h.id
@@data.steige[s.id] = s
h.steige << s.id
h.linien << s.linie
end
@logger.info "Fertig, insgesamt #{@@data.steige.size} Steige gelesen."
def not_found
send_file 'public/404.html', status: 404
end
def json_ids(objects)
content_type :json
id_hash = { anzahl: objects.size,
ids: [] }
objects.each_key do |id|
id_hash[:ids].push id
end
id_hash.to_json
end
before do
expires 500, :public, :must_revalidate
end
get '/' do
erb :index, :layout => :application
end
get '/linien/?' do
@linien = @@data.linien
@types = @@data.types
erb :linien, :layout => :application
end
get '/haltestellen/?' do
@haltestellen = @@data.haltestellen
erb :haltestellen, :layout => :application
end
get '/haltestellen.json' do
content_type :json
headers 'Access-Control-Allow-Origin' => '*'
json = @@data.haltestellen.to_json
etag Digest::SHA1.base64digest json
json
end
get '/linien.json' do
content_type :json
json = @@data.linien.to_json
etag Digest::SHA1.base64digest json
json
end
get '/steige.json' do
content_type :json
json = @@data.steige.to_json
etag Digest::SHA1.base64digest json
json
end
get '/linien/:id.json' do
@linie = @@data.linien[params[:id].to_i]
if @linie
content_type :json
json = @linie.to_json
etag Digest::SHA1.base64digest json
json
else
not_found
end
end
get '/linien/:id' do
@haltestellen = @@data.haltestellen
@l = @@data.linien[params[:id].to_i]
if @l
etag Digest::SHA1.base64digest @l.to_s
erb :linie, :layout => :application
else
not_found
end
end
get '/steige/:id.json' do
@steig = @@data.steige[params[:id].to_i]
if @steig
content_type :json
json = @steig.to_json
etag Digest::SHA1.base64digest json
json
else
not_found
end
end
get '/haltestellen/:id.json' do
@h = @@data.haltestellen[params[:id].to_i]
if @h
@h.refresh_monitors
expires 5
headers 'Access-Control-Allow-Origin' => '*'
content_type :json
json = @h.to_json
etag Digest::SHA1.base64digest json
json
else
not_found
end
end
get '/haltestellen/:id' do
@h = @@data.haltestellen[params[:id].to_i]
@steige = @@data.steige
@linien = @@data.linien
if @h
@h.refresh_monitors
expires 5
erb :haltestelle, :layout => :application
else
not_found
end
end
get '/karte/?' do
@map = Hash.new
erb :karte, :layout => :application
end
run! if app_file == $0
end