Got door logs working

This commit is contained in:
Will Bradley 2012-08-24 23:28:00 -07:00
parent b28efe046c
commit 8b775d9172
22 changed files with 324 additions and 241 deletions

View File

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

View File

@ -0,0 +1,3 @@
// Place all the styles related to the DoorLogs controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

View File

@ -0,0 +1,94 @@
class DoorLogsController < ApplicationController
# GET /door_logs
# GET /door_logs.json
def index
@door_logs = DoorLog.all
respond_to do |format|
format.html # index.html.erb
format.json { render :json => @door_logs }
end
end
# GET /door_logs/1
# GET /door_logs/1.json
# def show
# @door_log = DoorLog.find(params[:id])
#
# respond_to do |format|
# format.html # show.html.erb
# format.json { render :json => @door_log }
# end
# end
# GET /door_logs/1
# GET /door_logs/1.json
def download
@results = DoorLog.download_from_door
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @results }
end
end
# # GET /door_logs/new
# # GET /door_logs/new.json
# def new
# @door_log = DoorLog.new
#
# respond_to do |format|
# format.html # new.html.erb
# format.json { render :json => @door_log }
# end
# end
# GET /door_logs/1/edit
# def edit
# @door_log = DoorLog.find(params[:id])
# end
# POST /door_logs
# POST /door_logs.json
# def create
# @door_log = DoorLog.new(params[:door_log])
#
# respond_to do |format|
# if @door_log.save
# format.html { redirect_to @door_log, :notice => 'Door log was successfully created.' }
# format.json { render :json => @door_log, :status => :created, :location => @door_log }
# else
# format.html { render :action => "new" }
# format.json { render :json => @door_log.errors, :status => :unprocessable_entity }
# end
# end
# end
# PUT /door_logs/1
# PUT /door_logs/1.json
# def update
# @door_log = DoorLog.find(params[:id])
#
# respond_to do |format|
# if @door_log.update_attributes(params[:door_log])
# format.html { redirect_to @door_log, :notice => 'Door log was successfully updated.' }
# format.json { head :no_content }
# else
# format.html { render :action => "edit" }
# format.json { render :json => @door_log.errors, :status => :unprocessable_entity }
# end
# end
# end
# DELETE /door_logs/1
# DELETE /door_logs/1.json
# def destroy
# @door_log = DoorLog.find(params[:id])
# @door_log.destroy
#
# respond_to do |format|
# format.html { redirect_to door_logs_url }
# format.json { head :no_content }
# end
# end
end

View File

@ -0,0 +1,2 @@
module DoorLogsHelper
end

40
app/models/door_log.rb Normal file
View File

@ -0,0 +1,40 @@
class DoorLog < ActiveRecord::Base
attr_accessible :data, :key
require 'open-uri'
def self.download_from_door
# do shit here
source = open("http://192.168.1.177?e=1234").read
results = source.scan(/authok/)
if(results.size > 0) then
@end_results = Array.new
#only continue if we've got an OK login
source = open("http://192.168.1.177?z").read
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("http://192.168.1.177?y")
#logout
open("http://192.168.1.177?e=0000")
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
end

View File

@ -0,0 +1,25 @@
<%= form_for(@door_log) do |f| %>
<% if @door_log.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@door_log.errors.count, "error") %> prohibited this door_log from being saved:</h2>
<ul>
<% @door_log.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :key %><br />
<%= f.text_field :key %>
</div>
<div class="field">
<%= f.label :data %><br />
<%= f.number_field :data %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

View File

@ -0,0 +1,8 @@
<p id="notice"><%= notice %></p>
<p>
<b>Download results:</b>
<%= @results.inspect %>
</p>
<%= link_to 'Back', door_logs_path %>

View File

@ -0,0 +1,6 @@
<h1>Editing door_log</h1>
<%= render 'form' %>
<%= link_to 'Show', @door_log %> |
<%= link_to 'Back', door_logs_path %>

View File

@ -0,0 +1,22 @@
<h1>Listing door_logs</h1>
<%= link_to 'Download Door Logs', download_path %>
<table>
<tr>
<th>Key</th>
<th>Data</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @door_logs.each do |door_log| %>
<tr>
<td><%= door_log.key %></td>
<td><%= door_log.data %></td>
</tr>
<% end %>
</table>
<br />

View File

@ -0,0 +1,5 @@
<h1>New door_log</h1>
<%= render 'form' %>
<%= link_to 'Back', door_logs_path %>

View File

@ -0,0 +1,15 @@
<p id="notice"><%= notice %></p>
<p>
<b>Key:</b>
<%= @door_log.key %>
</p>
<p>
<b>Data:</b>
<%= @door_log.data %>
</p>
<%= link_to 'Edit', edit_door_log_path(@door_log) %> |
<%= link_to 'Back', door_logs_path %>

View File

@ -7,7 +7,10 @@
<%= csrf_meta_tags %> <%= csrf_meta_tags %>
</head> </head>
<body> <body>
<div id="header">
<a href="/users">Users</a>
<a href="/door_logs">Logs</a>
</div>
<%= yield %> <%= yield %>
</body> </body>

View File

@ -1,5 +1,7 @@
<h1>Listing users</h1> <h1>Listing users</h1>
<%= link_to 'New User', new_user_path %>
<%= link_to 'Upload all users', upload_all_path %>
<table> <table>
<tr> <tr>
<th>Name</th> <th>Name</th>
@ -26,4 +28,3 @@
<br /> <br />
<%= link_to 'New User', new_user_path %>

View File

@ -20,6 +20,6 @@
<%= @user.card_permissions %> <%= @user.card_permissions %>
</p> </p>
<%= link_to 'Upload to Door', upload_path(user) %>
<%= link_to 'Edit', edit_user_path(@user) %> | <%= link_to 'Edit', edit_user_path(@user) %> |
<%= link_to 'Back', users_path %> <%= link_to 'Back', users_path %>

View File

@ -1,8 +1,12 @@
Dooraccess::Application.routes.draw do Dooraccess::Application.routes.draw do
match 'users/upload_all' => 'users#upload_all', :as => :upload_all match 'users/upload_all' => 'users#upload_all', :as => :upload_all
resources :users resources :users
match 'users/:id/upload' => 'users#upload', :as => :upload match 'users/:id/upload' => 'users#upload', :as => :upload
match 'door_logs/download' => 'door_logs#download', :as => :download
resources :door_logs
# The priority is based upon order of creation: # The priority is based upon order of creation:
# first created -> highest priority. # first created -> highest priority.

View File

@ -0,0 +1,10 @@
class CreateDoorLogs < ActiveRecord::Migration
def change
create_table :door_logs do |t|
t.string :key
t.integer :data
t.timestamps
end
end
end

View File

@ -10,7 +10,14 @@
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20120825014921) do ActiveRecord::Schema.define(:version => 20120825041626) do
create_table "door_logs", :force => true do |t|
t.string "key"
t.integer "data"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "users", :force => true do |t| create_table "users", :force => true do |t|
t.string "name" t.string "name"

View File

@ -1,241 +1,7 @@
<!DOCTYPE html>
<html> <html>
<head> <head></head>
<title>Ruby on Rails: Welcome aboard</title>
<style type="text/css" media="screen">
body {
margin: 0;
margin-bottom: 25px;
padding: 0;
background-color: #f0f0f0;
font-family: "Lucida Grande", "Bitstream Vera Sans", "Verdana";
font-size: 13px;
color: #333;
}
h1 {
font-size: 28px;
color: #000;
}
a {color: #03c}
a:hover {
background-color: #03c;
color: white;
text-decoration: none;
}
#page {
background-color: #f0f0f0;
width: 750px;
margin: 0;
margin-left: auto;
margin-right: auto;
}
#content {
float: left;
background-color: white;
border: 3px solid #aaa;
border-top: none;
padding: 25px;
width: 500px;
}
#sidebar {
float: right;
width: 175px;
}
#footer {
clear: both;
}
#header, #about, #getting-started {
padding-left: 75px;
padding-right: 30px;
}
#header {
background-image: url("assets/rails.png");
background-repeat: no-repeat;
background-position: top left;
height: 64px;
}
#header h1, #header h2 {margin: 0}
#header h2 {
color: #888;
font-weight: normal;
font-size: 16px;
}
#about h3 {
margin: 0;
margin-bottom: 10px;
font-size: 14px;
}
#about-content {
background-color: #ffd;
border: 1px solid #fc0;
margin-left: -55px;
margin-right: -10px;
}
#about-content table {
margin-top: 10px;
margin-bottom: 10px;
font-size: 11px;
border-collapse: collapse;
}
#about-content td {
padding: 10px;
padding-top: 3px;
padding-bottom: 3px;
}
#about-content td.name {color: #555}
#about-content td.value {color: #000}
#about-content ul {
padding: 0;
list-style-type: none;
}
#about-content.failure {
background-color: #fcc;
border: 1px solid #f00;
}
#about-content.failure p {
margin: 0;
padding: 10px;
}
#getting-started {
border-top: 1px solid #ccc;
margin-top: 25px;
padding-top: 15px;
}
#getting-started h1 {
margin: 0;
font-size: 20px;
}
#getting-started h2 {
margin: 0;
font-size: 14px;
font-weight: normal;
color: #333;
margin-bottom: 25px;
}
#getting-started ol {
margin-left: 0;
padding-left: 0;
}
#getting-started li {
font-size: 18px;
color: #888;
margin-bottom: 25px;
}
#getting-started li h2 {
margin: 0;
font-weight: normal;
font-size: 18px;
color: #333;
}
#getting-started li p {
color: #555;
font-size: 13px;
}
#sidebar ul {
margin-left: 0;
padding-left: 0;
}
#sidebar ul h3 {
margin-top: 25px;
font-size: 16px;
padding-bottom: 10px;
border-bottom: 1px solid #ccc;
}
#sidebar li {
list-style-type: none;
}
#sidebar ul.links li {
margin-bottom: 5px;
}
.filename {
font-style: italic;
}
</style>
<script type="text/javascript">
function about() {
info = document.getElementById('about-content');
if (window.XMLHttpRequest)
{ xhr = new XMLHttpRequest(); }
else
{ xhr = new ActiveXObject("Microsoft.XMLHTTP"); }
xhr.open("GET","rails/info/properties",false);
xhr.send("");
info.innerHTML = xhr.responseText;
info.style.display = 'block'
}
</script>
</head>
<body> <body>
<div id="page"> <a href="/users">Users</a>
<div id="sidebar"> <a href="/door_logs">Logs</a>
<ul id="sidebar-items">
<li>
<h3>Browse the documentation</h3>
<ul class="links">
<li><a href="http://guides.rubyonrails.org/">Rails Guides</a></li>
<li><a href="http://api.rubyonrails.org/">Rails API</a></li>
<li><a href="http://www.ruby-doc.org/core/">Ruby core</a></li>
<li><a href="http://www.ruby-doc.org/stdlib/">Ruby standard library</a></li>
</ul>
</li>
</ul>
</div>
<div id="content">
<div id="header">
<h1>Welcome aboard</h1>
<h2>You&rsquo;re riding Ruby on Rails!</h2>
</div>
<div id="about">
<h3><a href="rails/info/properties" onclick="about(); return false">About your application&rsquo;s environment</a></h3>
<div id="about-content" style="display: none"></div>
</div>
<div id="getting-started">
<h1>Getting started</h1>
<h2>Here&rsquo;s how to get rolling:</h2>
<ol>
<li>
<h2>Use <code>rails generate</code> to create your models and controllers</h2>
<p>To see all available options, run it without parameters.</p>
</li>
<li>
<h2>Set up a default route and remove <span class="filename">public/index.html</span></h2>
<p>Routes are set up in <span class="filename">config/routes.rb</span>.</p>
</li>
<li>
<h2>Create your database</h2>
<p>Run <code>rake db:create</code> to create your database. If you're not using SQLite (the default), edit <span class="filename">config/database.yml</span> with your username and password.</p>
</li>
</ol>
</div>
</div>
<div id="footer">&nbsp;</div>
</div>
</body> </body>
</html> </html>

9
test/fixtures/door_logs.yml vendored Normal file
View File

@ -0,0 +1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
one:
key: MyString
data: 1
two:
key: MyString
data: 1

View File

@ -0,0 +1,49 @@
require 'test_helper'
class DoorLogsControllerTest < ActionController::TestCase
setup do
@door_log = door_logs(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:door_logs)
end
test "should get new" do
get :new
assert_response :success
end
test "should create door_log" do
assert_difference('DoorLog.count') do
post :create, :door_log => { :data => @door_log.data, :key => @door_log.key }
end
assert_redirected_to door_log_path(assigns(:door_log))
end
test "should show door_log" do
get :show, :id => @door_log
assert_response :success
end
test "should get edit" do
get :edit, :id => @door_log
assert_response :success
end
test "should update door_log" do
put :update, :id => @door_log, :door_log => { :data => @door_log.data, :key => @door_log.key }
assert_redirected_to door_log_path(assigns(:door_log))
end
test "should destroy door_log" do
assert_difference('DoorLog.count', -1) do
delete :destroy, :id => @door_log
end
assert_redirected_to door_logs_path
end
end

View File

@ -0,0 +1,7 @@
require 'test_helper'
class DoorLogTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View File

@ -0,0 +1,4 @@
require 'test_helper'
class DoorLogsHelperTest < ActionView::TestCase
end