Permalink
Browse files

Merge branch 'master' of git@github.com:aaronpk/Geolocation-Examples

  • Loading branch information...
2 parents 18e6620 + a2055c8 commit 0bc972500155e1c4d80d35ac6ce2d5c844be8858 @aaronpk committed Jan 17, 2012
Showing with 37 additions and 63 deletions.
  1. +37 −15 Web/home-automation/{home-automation.rb → controller.rb}
  2. +0 −48 Web/home-automation/geoloqi.rb
@@ -1,6 +1,6 @@
# This is an example of the glue between Geoloqi triggers and an X10 controller.
# Set up a Sinatra project, and use this class as the main class.
-# See https://github.com/aaronpk/MapAttack for an example of using Sinatra and the Geoloqi class.
+# See https://github.com/geoloqi/MapAttack for an example of using Sinatra and the Geoloqi class.
# Please note, this is mostly untested code put together from a few different sources.
# You may have trouble running it as is, so please feel free to contribute back if you
@@ -9,24 +9,46 @@
# Download the cm17a library from http://x10-cm17a.rubyforge.org/
require 'x10/cm17a'
-class HomeAutomation < Sinatra::Base
+class Controller < Sinatra::Base
+ before do
+ @redirect_uri = request.scheme + '://' + request.host_with_port + '/oauth'
+ @trigger_uri = request.scheme + '://' + request.host_with_port + '/trigger'
+ redirect geoloqi.authorize_url(@redirect_uri) unless geoloqi.access_token?
+ end
+
+ after do
+ session[:geoloqi_auth] = geoloqi.auth
+ end
+
get '/?' do
erb :'index'
end
+ # OAuth endpoint, see https://developers.geoloqi.com/api/Authentication for more information
+ get '/oauth' do
+ # If we're coming back from the OAuth authorization, there will be a 'code' parameter in the query string
+ if params[:code]
+ # Take the code and use it to get an access token, store it in the session
+ session[:geoloqi_auth] = geoloqi.get_auth(params[:code], @redirect_uri)
+ redirect '/'
+ else
+ # If no code was in the query string, redirect to the authorization endpoint
+ redirect geoloqi.authorize_url(@redirect_uri)
+ end
+ end
+
# Run this once to create the layer in Geoloqi, create a "Home" and a "Work" place, and set up the triggers
get '/setup' do
- @oauth_token = "" # Put your permanent access token here for now. http://beta.geoloqi.com/settings/connections
# Create the layer and store the response which has the layer ID
- layer = Geoloqi.post @oauth_token, 'layer/create', {
+ layer = geoloqi.post 'layer/create', {
:name => "Home Automation",
:description => "My Home Automation Layer"
}
# Create a place called "Home"
- Geoloqi.post @oauth_token, 'place/create', {
+ geoloqi.post 'place/create', {
:layer_id => layer.layer_id,
:name => "Home",
:latitude => 47.6170, # Replace with your lat/lng
@@ -35,7 +57,7 @@ class HomeAutomation < Sinatra::Base
}
# Create a place called "Work"
- Geoloqi.post @oauth_token, 'place/create', {
+ geoloqi.post 'place/create', {
:layer_id => layer.layer_id,
:name => "Work",
:latitude => 47.613, # Replace with your lat/lng
@@ -45,45 +67,45 @@ class HomeAutomation < Sinatra::Base
# Create a trigger for the layer which will be called
# when you enter any of the places
- Geoloqi.post @oauth_token, 'trigger/create', {
+ geoloqi.post 'trigger/create', {
:layer_id => layer.layer_id,
:type => 'callback',
- :callback => 'http://example.com:9292/trigger',
+ :callback => @trigger_uri,
:trigger_on => 'enter'
}
# Create a trigger for the layer which will be called
# when you leave any of the places
- Geoloqi.post @oauth_token, 'trigger/create', {
+ geoloqi.post 'trigger/create', {
:layer_id => layer.layer_id,
:type => 'callback',
- :callback => 'http://example.com:9292/trigger',
+ :callback => @trigger_uri,
:trigger_on => 'exit'
}
end
- # Create a trigger in Geoloqi pointing back to this URL. This will be called by Geoloqi
- # whenever you enter or leave a place on the layer
+ # This will be called by Geoloqi whenever you enter or leave
+ # a place on the layer
post '/trigger' do
- body = SymbolTable.new JSON.parse(request.body)
+ body = Hashie::Mash.new JSON.parse(request.body)
lamp = X10.device('a1') # Create an X10 device at address 'a1'
case body.place.name
-
when "Home"
if(body.triggered_on == "enter")
# Turn the device on when getting home
lamp.on
+ SMSified.send "+13605551212", "Honey, I'm home!"
else
# Turn the device off when leaving
lamp.off
end
when "Work"
if(body.triggered_on == "enter")
-
+ SMSified.send "+13605551212", "I'm at work!"
else
# Message somebody when you are leaving work
SMSified.send "+13605551212", "I'm on my way home!"
@@ -1,48 +0,0 @@
-require "base64"
-
-RestClient.log = STDOUT
-module Geoloqi
- class Error < StandardError
- def initialize(type, message=nil)
- type += " - #{message}" if message
- super type
- end
- end
-
- API_URL = 'https://api.geoloqi.com/1/'
- def self.headers(oauth_token)
- {'Authorization' => "OAuth #{oauth_token}", 'Content-Type' => 'application/json'}
- end
-
- def self.run(meth, oauth_token, url, body=nil)
- args = {:head => headers(oauth_token)}
- args[:body] = body.to_json if body
- response = JSON.parse EM::Synchrony.sync(EventMachine::HttpRequest.new(API_URL+url).send(meth.to_sym, args)).response
-
- raise Error.new(response['error'], response['error_description']) if response.is_a?(Hash) && response['error']
-
- case response
- when Array
- response.map! {|e| SymbolTable.new e}
- when Hash
- SymbolTable.new response
- end
- end
-
- def self.post(oauth_token, url, body)
- run :post, oauth_token, url, body
- end
-
- def self.get(oauth_token, url)
- run :get, oauth_token, url
- end
-
- def self.get_token(auth_code, redirect_uri)
- args = {:body => {:client_id => Geoloqi::CLIENT_ID,
- :client_secret => Geoloqi::CLIENT_SECRET,
- :code => auth_code,
- :grant_type => "authorization_code",
- :redirect_uri => redirect_uri}}
- JSON.parse EM::Synchrony.sync(EventMachine::HttpRequest.new(API_URL+"oauth/token").post(args)).response
- end
-end

0 comments on commit 0bc9725

Please sign in to comment.