added support for gelocation in feeds

This commit is contained in:
ioBridge 2011-03-27 19:27:48 -04:00
parent 93a51bdc93
commit 7e964afcc6
3 changed files with 75 additions and 23 deletions

View File

@ -9,6 +9,7 @@ class ChannelsController < ApplicationController
def show def show
@channel = Channel.find(params[:id]) if params[:id] @channel = Channel.find(params[:id]) if params[:id]
@domain = domain
# if owner of channel # if owner of channel
get_channel_data if current_user and @channel.user_id == current_user.id get_channel_data if current_user and @channel.user_id == current_user.id
@ -28,6 +29,10 @@ class ChannelsController < ApplicationController
@channel.update_attributes(params[:channel]) @channel.update_attributes(params[:channel])
@channel.name = "#{t(:channel_default_name)} #{@channel.id}" if params[:channel][:name].empty? @channel.name = "#{t(:channel_default_name)} #{@channel.id}" if params[:channel][:name].empty?
@channel.save @channel.save
# save tags
@channel.save_tags(params[:tags][:name])
redirect_to channel_path(@channel.id) and return redirect_to channel_path(@channel.id) and return
end end
@ -59,6 +64,20 @@ class ChannelsController < ApplicationController
redirect_to edit_channel_path(@channel.id) redirect_to edit_channel_path(@channel.id)
end end
# clear all data from a channel
def clear
channel = Channel.find(params[:id])
# make sure channel belongs to current user
check_permissions(channel)
# do the delete
channel.feeds.each do |f|
f.delete
end
redirect_to channels_path
end
def destroy def destroy
@channel = Channel.find(params[:id]) @channel = Channel.find(params[:id])
# make sure channel belongs to current user # make sure channel belongs to current user
@ -73,13 +92,16 @@ class ChannelsController < ApplicationController
def post_data def post_data
status = '0' status = '0'
feed = Feed.new feed = Feed.new
api_key = ApiKey.find_by_api_key(get_userkey) api_key = ApiKey.find_by_api_key(get_userkey)
# if write persmission, allow post # if write persmission, allow post
if (api_key && api_key.write_flag) if (api_key && api_key.write_flag)
channel = Channel.find(api_key.channel_id) channel = Channel.find(api_key.channel_id)
# rate limit posts
render :text => '0' and return if (Time.now < channel.updated_at + 14.seconds)
# update entry_id for channel and feed # update entry_id for channel and feed
entry_id = channel.last_entry_id.nil? ? 1 : channel.last_entry_id + 1 entry_id = channel.last_entry_id.nil? ? 1 : channel.last_entry_id + 1
channel.last_entry_id = entry_id channel.last_entry_id = entry_id
@ -88,15 +110,18 @@ class ChannelsController < ApplicationController
# try to get created_at datetime if appropriate # try to get created_at datetime if appropriate
if params[:created_at] if params[:created_at]
begin begin
@feed.created_at = DateTime.parse(params[:created_at]) feed.created_at = DateTime.parse(params[:created_at])
# if invalid datetime, don't do anything--rails will set created_at # if invalid datetime, don't do anything--rails will set created_at
rescue rescue
end end
end end
# strip line feeds from end of parameters # modify parameters
params.each do |key, value| params.each do |key, value|
params[key] = value.sub(/\\n$/, '').sub(/\\r$/, '') # strip line feeds from end of parameters
params[key] = value.sub(/\\n$/, '').sub(/\\r$/, '') if value
# use ip address if found
params[key] = request.remote_addr if value.upcase == 'IP_ADDRESS'
end end
# set feed details # set feed details
@ -111,6 +136,11 @@ class ChannelsController < ApplicationController
feed.field7 = params[:field7] if params[:field7] feed.field7 = params[:field7] if params[:field7]
feed.field8 = params[:field8] if params[:field8] feed.field8 = params[:field8] if params[:field8]
feed.status = params[:status] if params[:status] feed.status = params[:status] if params[:status]
feed.latitude = params[:lat] if params[:lat]
feed.latitude = params[:latitude] if params[:latitude]
feed.longitude = params[:long] if params[:long]
feed.longitude = params[:longitude] if params[:longitude]
feed.elevation = params[:elevation] if params[:elevation]
if channel.save && feed.save if channel.save && feed.save
status = entry_id status = entry_id
@ -122,4 +152,4 @@ class ChannelsController < ApplicationController
render :text => status render :text => status
end end
end end

View File

@ -1,5 +1,6 @@
class FeedController < ApplicationController class FeedController < ApplicationController
require 'csv' require 'csv'
layout 'application', :except => :index
def index def index
channel = Channel.find(params[:channel_id]) channel = Channel.find(params[:channel_id])
@ -8,6 +9,7 @@ class FeedController < ApplicationController
# check for access # check for access
if @success if @success
# create options hash # create options hash
channel_options = { :only => channel_select_data(channel) } channel_options = { :only => channel_select_data(channel) }
select_options = feed_select_data(channel) select_options = feed_select_data(channel)
@ -17,7 +19,7 @@ class FeedController < ApplicationController
:all, :all,
:conditions => { :channel_id => channel.id, :created_at => get_date_range(params) }, :conditions => { :channel_id => channel.id, :created_at => get_date_range(params) },
:select => select_options, :select => select_options,
:order => 'created_at' :order => 'created_at desc'
) )
# if a feed has data # if a feed has data
@ -159,6 +161,13 @@ class FeedController < ApplicationController
only += [:field8] unless channel.field8.blank? or (params[:field_id] and params[:field_id] != '8') only += [:field8] unless channel.field8.blank? or (params[:field_id] and params[:field_id] != '8')
only += [:status] if params[:status] and params[:status].upcase == 'TRUE' only += [:status] if params[:status] and params[:status].upcase == 'TRUE'
# add geolocation data if necessary
if params[:location] and params[:location].upcase == 'TRUE'
only += [:latitude]
only += [:longitude]
only += [:elevation]
end
return only return only
end end
@ -171,6 +180,7 @@ class FeedController < ApplicationController
return false return false
end end
end end
# slice feed into timescales # slice feed into timescales
def feeds_into_timescales(feeds) def feeds_into_timescales(feeds)
# convert timescale (minutes) into seconds # convert timescale (minutes) into seconds

View File

@ -31,14 +31,14 @@ ActiveRecord::Schema.define(:version => 20101210151518) do
t.string "description" t.string "description"
t.decimal "latitude", :precision => 15, :scale => 10 t.decimal "latitude", :precision => 15, :scale => 10
t.decimal "longitude", :precision => 15, :scale => 10 t.decimal "longitude", :precision => 15, :scale => 10
t.text "field1" t.string "field1"
t.text "field2" t.string "field2"
t.text "field3" t.string "field3"
t.text "field4" t.string "field4"
t.text "field5" t.string "field5"
t.text "field6" t.string "field6"
t.text "field7" t.string "field7"
t.text "field8" t.string "field8"
t.integer "scale1" t.integer "scale1"
t.integer "scale2" t.integer "scale2"
t.integer "scale3" t.integer "scale3"
@ -52,26 +52,38 @@ ActiveRecord::Schema.define(:version => 20101210151518) do
t.string "elevation" t.string "elevation"
t.integer "last_entry_id" t.integer "last_entry_id"
t.boolean "public_flag", :default => false t.boolean "public_flag", :default => false
t.string "options1"
t.string "options2"
t.string "options3"
t.string "options4"
t.string "options5"
t.string "options6"
t.string "options7"
t.string "options8"
end end
create_table "feeds", :force => true do |t| create_table "feeds", :force => true do |t|
t.integer "channel_id" t.integer "channel_id"
t.text "raw_data" t.text "raw_data"
t.text "field1" t.string "field1"
t.text "field2" t.string "field2"
t.text "field3" t.string "field3"
t.text "field4" t.string "field4"
t.text "field5" t.string "field5"
t.text "field6" t.string "field6"
t.text "field7" t.string "field7"
t.text "field8" t.string "field8"
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.integer "entry_id" t.integer "entry_id"
t.string "status" t.string "status"
t.decimal "latitude", :precision => 15, :scale => 10
t.decimal "longitude", :precision => 15, :scale => 10
t.string "elevation"
end end
add_index "feeds", ["channel_id"], :name => "index_feeds_on_channel_id" add_index "feeds", ["channel_id", "created_at"], :name => "index_feeds_on_channel_id_and_created_at"
create_table "users", :force => true do |t| create_table "users", :force => true do |t|
t.string "login", :null => false t.string "login", :null => false