Moving spaceapi endpoint and OAC door status checking here

This commit is contained in:
Will Bradley 2013-12-01 19:32:08 -07:00
parent 9dc8645c32
commit d2434be109
7 changed files with 121 additions and 1 deletions

View File

@ -26,6 +26,7 @@ class DoorLogsController < ApplicationController
# GET /door_logs/auto_download
def auto_download
@results = DoorLog.download_from_door
@status = DoorLog.download_status # for space_api
respond_to do |format|
format.html # show.html.erb

View File

@ -0,0 +1,31 @@
class SpaceApiController < ApplicationController
authorize_resource :except => :index
before_filter :authenticate_user!, :except => :index
def index
@json = JSON.parse(Setting.space_api_json_template)
door_status = DoorLog.show_status # Expect {:unlocked => boolean, :door_1_locked => boolean, :door_2_locked => boolean}
@json["open"] = door_status[:unlocked]
if( door_status[:unlocked] )
@json["status"] = "doors_open=both"
elsif( !door_status[:door_1_locked] )
@json["status"] = "doors_open=door1"
elsif( !door_status[:door_2_locked] )
@json["status"] = "doors_open=door2"
else
@json["status"] = "doors_open=none"
end
respond_to do |format|
format.html
format.json {
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
render :json => @json
}
end
end
end

View File

@ -2,6 +2,42 @@ class DoorLog < ActiveRecord::Base
attr_accessible :data, :key
require 'open-uri'
def self.show_status
door_logs = DoorLog.where(key: ["door_1_locked","door_2_locked"]).order('created_at DESC').take(2)
door_1_locked = parse_locked_status(door_logs, "door_1_locked")
door_2_locked = parse_locked_status(door_logs, "door_2_locked")
# Doors are unlocked if 1 AND 2 are NOT locked
status = {:unlocked => (!door_1_locked && !door_2_locked), :door_1_locked => door_1_locked, :door_2_locked => door_2_locked }
end
def self.parse_locked_status(door_logs, door_key)
door_logs_selected = door_logs.select{|s| s.key == door_key }
if door_logs_selected.present?
door_data = door_logs_selected.first.data
if door_data == 0 # 0 = unlocked
return false
else
return true # 1 = locked
end
end
end
def self.download_status
# load config values
door_access_url = APP_CONFIG['door_access_url']
door_access_password = APP_CONFIG['door_access_password']
# query for status
source = open("#{door_access_url}?9").read
# expect {"armed"=>255, "activated"=>255, "alarm_3"=>1, "alarm_2"=>1, "door_1_locked"=>1, "door_2_locked"=>1}
# See https://github.com/heatsynclabs/Open_Access_Control_Ethernet for more info
@status = JSON.parse(source)
@status.each do |key,value|
DoorLog.create!({:key => key, :data => value})
end
end
def self.download_from_door
# load config values
door_access_url = APP_CONFIG['door_access_url']

View File

@ -4,3 +4,8 @@
<%= !@results.nil? %>
</p>
<p>
<b>Door status:</b>
<%= @status %>
</p>

View File

@ -0,0 +1,11 @@
<h2>Space Status API
<%= link_to 'JSON', space_api_path( :format => :json ), :class => 'btn' %>
</h2>
<dl>
<% @json.each do |key,value| %>
<dt><%= key.humanize.capitalize %>:</dt>
<dd><%= value %></dd>
<% end %>
</dl>

View File

@ -3,7 +3,41 @@
:welcome_body => "<p>We are a member-driven community workshop where you can learn, make cool stuff, meet other cool people, and make your city a better place to live!</p><p>You don't have to be a member to come visit, but if you're interested in volunteering or being a member, feel free to sign up here! For more information, <a href=\"/more_info\">Click Here</a>.</p>",
:more_info_page => "No info here yet, bug a member about filling this part out!",
:member_resources_inset => "No info here yet, bug a member about filling this part out!",
:analytics_code => "<!-- insert analytics code here -->"
:analytics_code => "<!-- insert analytics code here -->",
:space_api_json_template => '{
"api" : "0.12",
"space" : "Your Hackerspace Name Here",
"logo" : "http://example.com/logo.png",
"lat": 0,
"lon": -0,
"icon":{
"open": "http://example.com/open.png",
"closed":"http://example.com/closed.png"
},
"url" : "http://example.com",
"address" : "123 main st, city, state, country",
"contact" : {
"phone" : "",
"irc" : "",
"twitter" : "",
"ml" : ""
},
"cam" : [""],
"feeds" : [{"name" : "",
"url" : ""},
{"name" : "",
"url" : ""}],
"apis" : {
"oac" : {
"url" : "http://this-apps-url.example.com/door_access",
"description" : "https://github.com/heatsynclabs/Open-Source-Access-Control-Web-Interface"
},
"pamela" : {
"url" : "http://this-apps-url.example.com/macs.json",
"description" : "https://github.com/heatsynclabs/Open-Source-Access-Control-Web-Interface"
}
}
}'
}
if ActiveRecord::Base.connection.tables.include?('settings') and !defined?(::Rake)

View File

@ -45,6 +45,8 @@ Dooraccess::Application.routes.draw do
resources :cards
match 'cards/:id/upload' => 'cards#upload', :as => :upload
match 'space_api' => 'space_api#index', :as => :space_api
match 'door_logs' => 'door_logs#index', :as => :door_logs
match 'door_logs/download' => 'door_logs#download', :as => :download
match 'door_logs/auto_download' => 'door_logs#auto_download', :as => :auto_download