From d2434be10916dfa8465ec0a441adba89f880ca06 Mon Sep 17 00:00:00 2001
From: Will Bradley
Date: Sun, 1 Dec 2013 19:32:08 -0700
Subject: [PATCH] Moving spaceapi endpoint and OAC door status checking here
---
app/controllers/door_logs_controller.rb | 1 +
app/controllers/space_api_controller.rb | 31 +++++++++++++++++++
app/models/door_log.rb | 36 ++++++++++++++++++++++
app/views/door_logs/auto_download.html.erb | 5 +++
app/views/space_api/index.html.erb | 11 +++++++
config/initializers/default_settings.rb | 36 +++++++++++++++++++++-
config/routes.rb | 2 ++
7 files changed, 121 insertions(+), 1 deletion(-)
create mode 100644 app/controllers/space_api_controller.rb
create mode 100644 app/views/space_api/index.html.erb
diff --git a/app/controllers/door_logs_controller.rb b/app/controllers/door_logs_controller.rb
index 345fa72..2f7df9d 100644
--- a/app/controllers/door_logs_controller.rb
+++ b/app/controllers/door_logs_controller.rb
@@ -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
diff --git a/app/controllers/space_api_controller.rb b/app/controllers/space_api_controller.rb
new file mode 100644
index 0000000..919de65
--- /dev/null
+++ b/app/controllers/space_api_controller.rb
@@ -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
diff --git a/app/models/door_log.rb b/app/models/door_log.rb
index 098bf0e..353b043 100644
--- a/app/models/door_log.rb
+++ b/app/models/door_log.rb
@@ -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']
diff --git a/app/views/door_logs/auto_download.html.erb b/app/views/door_logs/auto_download.html.erb
index e1af088..7e92e89 100644
--- a/app/views/door_logs/auto_download.html.erb
+++ b/app/views/door_logs/auto_download.html.erb
@@ -4,3 +4,8 @@
<%= !@results.nil? %>
+
+ Door status:
+ <%= @status %>
+
+
diff --git a/app/views/space_api/index.html.erb b/app/views/space_api/index.html.erb
new file mode 100644
index 0000000..d93a749
--- /dev/null
+++ b/app/views/space_api/index.html.erb
@@ -0,0 +1,11 @@
+Space Status API
+<%= link_to 'JSON', space_api_path( :format => :json ), :class => 'btn' %>
+
+
+
+<% @json.each do |key,value| %>
+ - <%= key.humanize.capitalize %>:
+ - <%= value %>
+<% end %>
+
+
diff --git a/config/initializers/default_settings.rb b/config/initializers/default_settings.rb
index 27f1bf9..f3a01e7 100644
--- a/config/initializers/default_settings.rb
+++ b/config/initializers/default_settings.rb
@@ -3,7 +3,41 @@
:welcome_body => "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!
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, Click Here.
",
: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 => ""
+ :analytics_code => "",
+ :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)
diff --git a/config/routes.rb b/config/routes.rb
index 861d171..22050cd 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -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