-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogcards.rb
More file actions
executable file
·361 lines (331 loc) · 10 KB
/
logcards.rb
File metadata and controls
executable file
·361 lines (331 loc) · 10 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
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
356
357
358
359
360
#!/usr/bin/ruby
#nfc-list uses libnfc 1.5.1 (r1175)
#Connected to NFC device: ACS ACR 38U-CCID 00 00 / ACR122U102 - PN532 v1.4 (0x07)
#1 ISO14443A passive target(s) found:
# ATQA (SENS_RES): 00 44
# UID (NFCID1): 04 ed ab d9 a1 25 80
# SAK (SEL_RES): 00
#
require 'rubygems'
require 'bundler/setup'
require 'yaml'
require 'net/http'
require 'json'
require 'uri'
require 'dnsruby'
require 'tzinfo'
#YAML::ENGINE.yamler = 'syck'
class LCConfig
def self.load
@@config = YAML.load_file("#{File.dirname(File.expand_path($0))}/config.yaml")
@@tz = nil
@@users = {}
@@config["users"].each { |k,v|
@@users[String(k).downcase] = v
}
if @@config["settings"] and @@config["settings"]["timezone"]
@@tz = TZInfo::Timezone.get(@@config["settings"]["timezone"])
end
end
def self.door_open_minimum
min = LCConfig.env["door_open_minimum"]
if min.nil? and LCConfig.config["settings"]
min = LCConfig.config["settings"]["door_open_minimum"]
end
min = min.to_f
if min.nil? || min == 0
min = 0.1
end
min
end
def self.config
@@config
end
def self.tz
@@tz
end
def self.env
@@config["environments"][ENV['DOORBOT_ENV']]
end
def self.user(uid)
return @@users[uid.downcase]
end
$connection_error = nil
def self.setup_signal
Signal.trap("HUP") do
begin
LCConfig.load
puts LCConfig.config.inspect
rescue Exception => e
puts "erk: #{e.inspect}"
end
end
Signal.trap("USR2") do
puts GC.stat.inspect
GC.start
end
end
end
puts "Config Pre Load"
LCConfig.load
puts "After load. Config Correct!"
#puts LCConfig.config.inspect
LCConfig.setup_signal
VISITS_YAML = 'visits.yaml'
DAY_VISITS_YAML = 'day_visits.yaml'
UNLOGGED_VISITS_YAML = 'unlogged_visits.yaml'
def setDoorState(state)
if File.exist?('/usr/bin/pinctrl')
# We're on a newer OS, so use "pinctrl" to access GPIO
if state == 1
`pinctrl set 25 dh`
else
`pinctrl set 25 dl`
end
else
File.open('/sys/class/gpio/gpio25/value', 'w') do |out|
out.write(state)
end
end
end
def saveUnloggedVisits
File.open(UNLOGGED_VISITS_YAML, "w") do |out|
YAML.dump($unloggedFile, out)
end
end
def announce(message)
puts message
end
puts "DoorBot: #{ENV["DOORBOT_ENV"]}"
if LCConfig.env.nil?
puts "Error! Must specify a valid doorbot environment."
exit
end
scansFile = nil
hotdesksFile = nil
visitsFile = nil
$testUID = nil
if File.file?(VISITS_YAML)
visits = YAML.load_file(VISITS_YAML, permitted_classes: [Time])
end
if visits.nil? or visits == false
visits = {}
end
if File.file?(DAY_VISITS_YAML)
dayVisits = YAML.load_file(DAY_VISITS_YAML, permitted_classes: [Time])
end
if dayVisits.nil? or dayVisits == false
dayVisits = {}
end
if File.file?(UNLOGGED_VISITS_YAML)
$unloggedFile = YAML.load_file(UNLOGGED_VISITS_YAML, permitted_classes: [Time])
end
if $unloggedFile.nil? or $unloggedFile == false
$unloggedFile = {}
$unloggedFile['user']= {}
end
Signal.trap("SIGUSR1") do
$testUID = ''
end
test_pipe_filename = "/tmp/logcards-test.pipe"
if ENV['DOORBOT_TEST'] == 'test'
`rm -f #{test_pipe_filename}`
`mkfifo #{test_pipe_filename}`
test_input = open(test_pipe_filename, "r")
else
test_input = nil
end
while true
begin
puts "Welcome to Doorbot"
scansFile = File.open("scans.log", "a")
hotdesksFile = File.open("hotdesks.log", "a")
visitsFile = File.open("visits.log", "a")
if test_input and ( test_input.eof? || test_input.closed? )
if ! test_input.closed?
test_input.close
end
test_input = open(test_pipe_filename, "r")
end
while true
if test_input
begin
list = test_input.readline
rescue EOFError
list = ''
end
else
list = `./#{LCConfig.env["rcapp"]}`
end
uid = list.chomp
#uid = matches[1].gsub(/ /,"") if matches
if uid.empty? and $testUID
uid = $testUID
$testUID = nil
end
if ! uid.empty?
time = Time.now.utc
if LCConfig.tz
time = LCConfig.tz.utc_to_local(time)
end
today = Date.today.to_s
scansFile.write("#{uid}\t#{time}\n")
scansFile.flush
user = LCConfig.user(uid)
seen = []
puts "tag #{uid} at #{time}"
while user and user["primary"]
seen << uid
uid = user["primary"]
if seen.index(uid)
puts "OMG RECURSION!!"
blah = `espeak -v en "Recursion error!" --stdout | aplay`
uid = ""
user = nil
break
end
user = LCConfig.config["users"][uid]
puts "- has primary: #{uid}"
end
end
if ! uid.empty?
name = ""
nickname = ""
if user
access = user["access"] || []
announce("Visit from #{user["name"]} (#{access.join(", ")}) from #{ENV['DOORBOT_ENV']}")
name = user["name"]
nickname = user["nickname"]
nickname = name if nickname.nil?
else
announce("#{uid} was unrecognised")
blah = `espeak -v en "Thank you, welcome to duss Liverpool #{nickname}. Please talk to an organiser to be inducted." --stdout | aplay`
next
end
access = user["access"] || []
door_opened_at = nil
access_required = LCConfig.env['access'] || []
if LCConfig.env.has_key?('access') && ( access_required.length == 0 || ( access_required & access ).length > 0 )
setDoorState(1)
door_opened_at = Time.now
end
last_day = dayVisits[uid]
if last_day != today
dayVisits[uid] = today
if LCConfig.env['loghotdesk'] and user and user["hotdesker"] == true and time.hour < 17
days_used = 1
#if time.hour >= 17
# days_used = 0.25
#els
if time.hour >= 13
days_used = 0.5
end
hotdesksFile.write("#{time}\t#{uid}\t#{user["name"]}\t#{days_used}\n")
hotdesksFile.flush
puts "Log hot desk visit by #{user["name"]}!"
# First save it in the unlogged file
if $unloggedFile['user'][user['name']]
days_used = days_used + $unloggedFile['user'][user['name']].to_i
end
$unloggedFile['user'][user['name']] = days_used
saveUnloggedVisits()
end
File.open(DAY_VISITS_YAML, "w") do |out|
YAML.dump(dayVisits,out)
end
end
last_visit = visits[uid]
if last_visit and (last_visit["arrived_at"].yday != time.yday || last_visit["arrived_at"].year != time.year)
visitsFile.write("#{uid}\t#{last_visit["arrived_at"]}\t#{time}\t#{name}\n")
visitsFile.flush
last_visit = nil
end
if last_visit
puts "#{uid} Left"
visitsFile.write("#{uid}\t#{last_visit["arrived_at"]}\t#{time}\t#{name}\n")
visitsFile.flush
visits[uid] = nil
else
puts "#{uid} Arrived"
visits[uid] = { "arrived_at" => time }
end
File.open(VISITS_YAML, "w") do |out|
YAML.dump(visits, out)
end
p = fork do
special_sound = nil
day_sounds = nil
if LCConfig.config["sounds"]
day_sounds = LCConfig.config["sounds"]["#{time.month}-#{time.day}"]
end
if day_sounds
special_sound = day_sounds.sample
end
if special_sound
cmd = "aplay wav/#{special_sound}"
puts "ringtone: #{cmd}"
blah = `#{cmd}`
elsif user and user["ringtone"]
cmd = "aplay wav/#{user['ringtone']}"
puts "ringtone: #{cmd}"
blah = `#{cmd}`
else
blah = `espeak -v en "Thank you, welcome to duss Liverpool #{nickname}" --stdout | aplay`
end
#blah = `aplay thanks-welcome.aiff > /dev/null 2> /dev/null`
end
Process.detach(p) # So we don't leave that process as a zombie
if door_opened_at
opened_for = ( Time.now - door_opened_at )
while opened_for < LCConfig.door_open_minimum
interval = 0.05
puts "Sleeping for an extra #{interval}s"
sleep interval
opened_for += interval
end
setDoorState(0)
end
sleep 2
end
if $unloggedFile['user'].count > 0
puts "Logging unlogged users"
# Using keys so that the fact that we're modifying the hash doesn't matter
$unloggedFile['user'].keys.each do |name|
if name.nil?
puts("Removing nil entry")
$unloggedFile['user'].delete(name)
saveUnloggedVisits()
next
end
user = $unloggedFile['user'][name]
puts("Logging unlogged user: #{name.inspect} (#{user})")
uri = URI.parse("https://docs.google.com/forms/d/1eW3ebkEZcoQ7AvsLoZmL5Ju7eQbw8xABXQm3ggPJ-v4/formResponse?entry.1000001=#{URI.encode_uri_component(name)}&entry.1000002=#{$unloggedFile['user'][name]}&entry.1000002.other_option_response=&submit=Submit")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl =true
un_request = Net::HTTP::Get.new(uri.request_uri)
un_response = http.request(un_request)
if un_response.code.to_i >= 200 or un_response.code.to_i < 500
puts "User #{name} has been logged (#{un_response.code})"
$unloggedFile['user'].delete(name)
saveUnloggedVisits()
else
puts "Logging user #{name} gave: #{un_response.code} #{un_response.message}"
end
end
puts "DONE Logging unlogged users"
end
end
rescue SystemExit
setDoorState(0)
puts 'OOPS'
exit
rescue Exception => e
setDoorState(0)
puts "Oops #{e.inspect}"
puts e.backtrace.join("\n")
end
scansFile.close if scansFile
hotdesksFile.close if hotdesksFile
visitsFile.close if visitsFile
sleep 5
end