Permalink
Browse files

state pre-warden

  • Loading branch information...
1 parent ce03b9b commit 2d0dfe6f94bcb8a869e7b3be277424f2898470b2 @mlm79 committed Mar 9, 2012
Showing with 129 additions and 18 deletions.
  1. +11 −5 config.ru
  2. +20 −3 main.rb
  3. +8 −3 public/css/style.css
  4. +23 −0 public/js/functions.js
  5. +55 −3 src/digiAPI.rb
  6. +4 −3 views/index.haml
  7. +2 −0 views/layout.haml
  8. +6 −1 views/logIn.haml
View
@@ -1,10 +1,16 @@
require 'rubygems'
require 'sinatra'
require 'json'
+require 'warden'
require './main.rb'
-enable :sessions
-set :session_secret, ENV['SESSION_KEY']
-set :session_domain, ENV['DOMAIN'] || 'localhost'
-set :session_expire, 86400
-run Sinatra::Application
+Rack::Builder.new do
+ use Rack::Session::Cookie, :secret => "replace this with some secret key"
+
+ use Warden::Manager do |manager|
+ manager.default_strategies :password, :basic
+ manager.failure_app = Sinatra::Application
+ end
+
+ run Sinatra::Application
+end
View
23 main.rb
@@ -15,7 +15,7 @@
## Handle authencation
def auth (type)
condition do
- redirect "/logIn" unless send("is_#{type}?")
+ redirect '/logIn' unless send("is_#{type}?")
end
end
end
@@ -28,19 +28,36 @@ def is_user?
get '/logIn' do
@server = 'http://'+request.env['HTTP_HOST']
- haml :logIn, :locals => { :title => 'Log In: XBee Remote Control',:msg => params[:msg],:log_state => "Log In",:log_state_url => '/logIn' }
+ ref = "/"
+ if /(logIn|logOut)/.match(params[:referrer]) == nil
+ puts "no match"
+ ref = params[:referrer]
+ end
+ puts ref
+ haml :logIn, :locals => { :title => 'Log In: XBee Remote Control',:msg => params[:msg],:log_state => "Log In",:log_state_url => '/logIn',:ref => ref }
end
post '/logIn' do
session[:user_name] = params[:user_name]
session[:password] = params[:user_password]
+ #return
redirect '/'
end
get '/', :auth => :user do
@server = 'http://'+request.env['HTTP_HOST']
@gateway_ids = getGateways()
- haml :index, :locals => { :title => 'XBee Remote Control',:log_state => "Log Out",:log_state_url => '/logOut' }
+ haml :index, :locals => { :title => 'Configure: XBee Remote Control',:log_state => "Log Out",:log_state_url => '/logOut' }
+end
+
+get '/configureXBee', :auth => :user do
+ @xbee_response = configureXBee(params[:gateway_id],params[:xbee_id])
+ return @xbee_response
+end
+
+get '/toggleXBee', :auth => :user do
+ haml :index, :locals => { :title => 'XBee Remote Control',:log_state => "Log Out",:log_state_url => '/logOut' }
+ puts "toggle xbee"
end
get '/logOut' do
View
@@ -51,7 +51,7 @@ a, a:-webkit-any-link, a:visited {
}
#control_panel {
- width:220px;
+ width:340px;
text-align: center;
padding: 10px;
margin:auto;
@@ -87,17 +87,22 @@ select .prompt {
#button {
float:right;
- width:160px;
+ width:280px;
height:40px;
padding: 2px;
cursor:pointer;
-webkit-border-radius: 4px;
-webkit-box-shadow: 0 2px 6px rgba(0,0,0,0.5), inset 0 1px rgba(255,255,255,0.3), inset 0 10px rgba(255,255,255,0.2), inset 0 10px 20px rgba(255,255,255,0.25), inset 0 -15px 30px rgba(0,0,0,0.3);
color:#efefef;
- font-size: 30px;
+ font-size: 18px;
font-weight: bold;
}
+#button span {
+ position: relative;
+ top: 8px;
+}
+
.selected {
background:#83ad04;
}
View
@@ -12,6 +12,26 @@ function updateXBeeList(){
}
}
+function configureXBee() {
+ //$('#controlPanel').append("div id='test'>QueryingÉ</div>");
+ var params = {
+ gateway_id: $('#gatewaySelect').val()
+ , xbee_id: $('#xbeeSelect').val()
+ }
+
+ $.ajax({
+ url: '/configureXBee'
+ , data: params
+ , success: function(response){
+ //$('#test').remove();
+ $('#button').removeClass('selected').addClass('de-selected');
+ $('#controlPanel').append("div id='test'>Garage Door Opener Configured. Bookmark.</div>");
+ alert("success");
+ }
+
+ });
+}
+
function toggleSensor(){
var gateway_id = $('#gatewaySelect').val();
@@ -23,3 +43,6 @@ function toggleSensor(){
+function getReferrer() {
+ return document.referrer;
+}
View
@@ -54,14 +54,14 @@ def digiRequest (_uri,_type,_msg)
####
#
-# function assertXBee
+# function configureXBee
# @params - gateway_id, xbee_id
# check that all XBee I/Os are configured
# @return - boolean OK
#
####
-def assertXBee(gateway_id,xbee_id)
+def configureXBee(gateway_id,xbee_id)
uri = "http://developer.idigi.com/ws/sci"
msg = '<sci_request version="1.0">
<send_message>
@@ -83,8 +83,38 @@ def assertXBee(gateway_id,xbee_id)
</send_message>
</sci_request>'
xml = REXML::Document.new(digiRequest(uri,'post',msg))
+ puts xml
return xml
+end
+
+####
+#
+# function toggleXBee
+# @params - gateway_id, xbee_id
+# Switch state of XBee
+# @return - boolean OK
+#
+####
+def toggleXBee(gateway_id,xbee_id)
+ uri = "http://developer.idigi.com/ws/sci"
+ msg = '<sci_request version="1.0">
+ <send_message>
+ <targets>
+ <device id="'+gateway_id+'"/>
+ </targets>
+ <rci_request version="1.1">
+ <do_command target="xig">
+ <at hw_address="'+xbee_id+'" command="D1" value="4" apply="True" />
+ <at hw_address="'+xbee_id+'" command="D1" value="5" apply="True" />
+ <at hw_address="'+xbee_id+'" command="D1" value="4" apply="True" />
+ </do_command>
+ </rci_request>
+ </send_message>
+ </sci_request>'
+ xml = REXML::Document.new(digiRequest(uri,'post',msg))
+ puts xml
+ return xml
end
####
@@ -113,13 +143,35 @@ def getXBees (gateway_id)
xml = REXML::Document.new(digiRequest(uri,'post',msg))
xbees = Array.new
- REXML::XPath.match(xml, '//device//ext_addr').each do |x|
+ REXML::XPath.match(xml, '//discover//device//ext_addr').each do |x|
xbees.push(x.text)
end
return xbees
end
+
+def testXBee
+ msg = '<sci_request version="1.0">
+ <send_message>
+ <targets>
+ <device id="00000000-00000000-00409DFF-FF43FA07"/>
+ </targets>
+ <rci_request version="1.1">
+ <do_command target="xig">
+
+ <at hw_address="00:13:a2:00:40:48:5a:23!" command="IS" />
+
+ </do_command>
+ </rci_request>
+ </send_message>
+</sci_request>'
+ uri = "http://developer.idigi.com/ws/sci"
+ xml = REXML::Document.new(digiRequest(uri,'post',msg))
+ puts xml
+
+end
+
####
#
# function getGateways
View
@@ -1,5 +1,5 @@
#inner
- %h3 Control Panel
+ %h3 Configuration
#control_panel.clearfix
- gateway_str = JSON.dump(@gateway_ids)
#data{:data => "#{gateway_str}" }
@@ -11,9 +11,10 @@
%select{:id => "xbeeSelect"}
%option{:class => "prompt", :value => ""} Select a XBee
.control.clearfix
- #button.selected{:onclick => "toggleSensor();"}
+ #button.selected{:onclick => "configureXBee();"}
/ value of xbee state
- Open
+ %span
+ Configure Garage Door Opener
.thumbnail
%a{:href => "", :onclick => "alert('clicked');"}
%img{:src => "/imgs/default.png"}
View
@@ -13,6 +13,8 @@
#nav_div
%nav
%span
+ %a{:href => "/configuration"} Configuration
+ %span
%a{:href => "#{log_state_url}", :log_state => "#{log_state}"} #{log_state}
#logo
%a{:href => "/", :title => "XBee Remote Control"}
View
@@ -8,4 +8,9 @@
%input{:id=>'user_name', :name=>'user_name',:type=>'text'}
%label{:for=>'user_password'} Password:
%input{:id=>'user_password', :name=>'user_password',:type=>'password'}
- %input{:id=>'submit',:type=>'submit',:value=>'Submit'}
+ %input{:id=>'referrer', :name=>'referrer',:type=>'hidden' }
+ %input{:id=>'submit',:type=>'submit',:value=>'Submit'}
+
+:javascript
+ console.log(document.referrer)
+ $('#referrer').attr('value',document.referrer);

0 comments on commit 2d0dfe6

Please sign in to comment.