thingspeak/app/models/feed.rb

138 lines
4.2 KiB
Ruby
Raw Normal View History

# == Schema Information
#
# Table name: feeds
#
# id :integer not null, primary key
# channel_id :integer
# field1 :string(255)
# field2 :string(255)
# field3 :string(255)
# field4 :string(255)
# field5 :string(255)
# field6 :string(255)
# field7 :string(255)
# field8 :string(255)
# created_at :datetime
# updated_at :datetime
# entry_id :integer
# status :string(255)
# latitude :decimal(15, 10)
# longitude :decimal(15, 10)
# elevation :string(255)
# location :string(255)
#
2011-03-27 22:56:15 +02:00
class Feed < ActiveRecord::Base
extend FeedHelper
belongs_to :channel
after_commit :queue_react
delegate :queue_react, :to => :channel
2011-03-27 22:56:15 +02:00
self.include_root_in_json = false
attr_readonly :created_at
2011-03-27 22:56:15 +02:00
# only output these fields for feed
def self.select_options(channel, params)
only = [:created_at]
only += [:entry_id] unless timeparam_valid?(params[:timescale]) or timeparam_valid?(params[:average]) or timeparam_valid?(params[:median]) or timeparam_valid?(params[:sum])
only += [:field1] unless channel.field1.blank? or (params[:field_id] and !params[:field_id].index('1'))
only += [:field2] unless channel.field2.blank? or (params[:field_id] and !params[:field_id].index('2'))
only += [:field3] unless channel.field3.blank? or (params[:field_id] and !params[:field_id].index('3'))
only += [:field4] unless channel.field4.blank? or (params[:field_id] and !params[:field_id].index('4'))
only += [:field5] unless channel.field5.blank? or (params[:field_id] and !params[:field_id].index('5'))
only += [:field6] unless channel.field6.blank? or (params[:field_id] and !params[:field_id].index('6'))
only += [:field7] unless channel.field7.blank? or (params[:field_id] and !params[:field_id].index('7'))
only += [:field8] unless channel.field8.blank? or (params[:field_id] and !params[:field_id].index('8'))
2011-03-27 22:56:15 +02:00
# add geolocation data if necessary
if params[:location] and params[:location].upcase == 'TRUE'
only += [:latitude]
only += [:longitude]
only += [:elevation]
only += [:location]
end
2011-03-27 22:56:15 +02:00
# add status if necessary
only += [:status] if params[:status] and params[:status].upcase == 'TRUE'
return only
end
2011-03-27 22:56:15 +02:00
# outputs feed info correctly, used by daily_feeds
def self.normalize_feeds(daily_feeds)
output = []
hash = {}
2011-03-27 22:56:15 +02:00
# for each daily feed
daily_feeds.each do |daily_feed|
# check if the feed already exists
existing_feed = hash[daily_feed['date']]
# skip blank feeds
next if daily_feed['date'].blank?
# if the feed exists
if existing_feed.present?
# add the new field
existing_feed["field#{daily_feed['field']}"] = daily_feed['result']
# else add a new feed
else
new_feed = Feed.new(:created_at => daily_feed['date'])
# set the field attribute correctly
new_feed["field#{daily_feed['field']}"] = daily_feed['result']
# add the feed
hash[daily_feed['date']] = new_feed
end
end
# turn the hash into an array
output = hash.values
# sort by date
return output
end
# custom json output
def as_json(options = {})
super(options.merge(:except => [:updated_at, :id]))
end
# check if a field value is a number
# usage: Feed.numeric?(field_value)
def self.numeric?(object)
true if Float(object) rescue false
end
def field(number)
self.attributes["field#{number.to_i}"]
end
# make sure any selected fields are greater than a minimum
def greater_than?(minimum)
output = true
self.attributes.each do |attribute|
# if this attribute is a numeric field with a value
if attribute[0].to_s.index('field') == 0 && attribute[1].present? && Feed.numeric?(attribute[1])
output = false if attribute[1].to_f < minimum.to_f
end
end
return output
end
# make sure any selected fields are less than a minimum
def less_than?(maximum)
output = true
self.attributes.each do |attribute|
# if this attribute is a numeric field with a value
if attribute[0].to_s.index('field') == 0 && attribute[1].present? && Feed.numeric?(attribute[1])
output = false if attribute[1].to_f > maximum.to_f
end
end
return output
end
end
2011-03-27 22:56:15 +02:00