Open-Source-Access-Control-.../app/models/door_log.rb

49 lines
1.2 KiB
Ruby
Raw Normal View History

2012-08-25 06:28:00 +00:00
class DoorLog < ActiveRecord::Base
attr_accessible :data, :key
require 'open-uri'
def self.download_from_door
# load config values
door_access_url = APP_CONFIG['door_access_url']
door_access_password = APP_CONFIG['door_access_password']
# connect to door access system
source = open("#{door_access_url}?e=#{door_access_password}").read
results = source.scan(/ok/)
2012-08-25 06:28:00 +00:00
if(results.size > 0) then
@end_results = Array.new
#only continue if we've got an OK login
source = open("#{door_access_url}?z").read
2012-08-25 06:28:00 +00:00
results = source.scan(/(.*): (.*)\r\n/)
results.each do |r|
if(r[0] != "\000") then
DoorLog.create!({:key => r[0], :data => r[1]})
end
end
#clear log
open("#{door_access_url}?y")
2012-08-25 06:28:00 +00:00
#logout
open("#{door_access_url}?e=0000")
2012-08-25 06:28:00 +00:00
if(results.size > 0) then
#only return true if we got some kind of decent response
return results
else
# We didn't get a decent response.
return false
end
else
# We didn't get an OK login.
return false
end
end
def self.key_legend
{'G' => "Granted", "R" => "Read", "D" => "Denied",
'g' => "granted", "r" => "read", "d" => "denied"}
end
2012-08-25 06:28:00 +00:00
end