Sha256: 420181d7863bcb5f172536a73729694e23c0e2cc711362fc78b74f222add4547

Contents?: true

Size: 1.66 KB

Versions: 3

Compression:

Stored size: 1.66 KB

Contents

require_dependency 'terminal_ping'

class Terminal < ActiveRecord::Base
  include Redis::Objects::RMap

  HARDWARE = %w(cash_acceptor modem printer card_reader watchdog)
  ORDERS   = %w(reload reboot disable enable upgrade)

  has_rmap({:id => lambda{|x| x.to_s}}, :keyword)
  has_paper_trail :ignore => [:incomplete_orders_count]

  #
  # RELATIONS
  #
  belongs_to :terminal_profile
  belongs_to :agent
  has_many :collections, :order => 'id DESC'
  has_many :payments, :order => 'id DESC'
  has_many :terminal_orders, :order => 'id DESC'

  list :pings, :marshal => true, :maxlength => 480

  scope :ok,      where(:condition => 'ok')
  scope :warning, where(:condition => 'warning')
  scope :error,   where(:condition => 'error')

  #
  # VALIDATIONS
  #
  validates :terminal_profile, :presence => true
  validates :title, :presence => true
  validates :keyword, :presence => true, :uniqueness => true
  validates :agent, :presence => true

  #
  # METHODS
  #
  def title
    keyword
  end

  def ping!(data)
    raise ActiveRecord::RecordInvalid unless data.valid?
    pings.unshift data

    update = {
      :state       => data.state,
      :condition   => data.condition,
      :notified_at => data.created_at,
      :version     => data.version
    }

    HARDWARE.each do |device|
      update["#{device}_error"] = data.error(device)
    end

    if data.ok?
      update[:issues_started_at] = nil
    else
      update[:issues_started_at] = DateTime.now if issues_started_at.blank?
    end

    self.without_versioning do
      update_attributes update
    end
  end

  def order!(keyword, *args)
    TerminalOrder.create!(:terminal_id => id, :keyword => keyword, :args => args)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
smartkiosk-server-0.10.14 app/models/terminal.rb
smartkiosk-server-0.10.13 app/models/terminal.rb
smartkiosk-server-0.10.12 app/models/terminal.rb