Sha256: 1767d132a780ee5074fa8d1888fd41428c057572aada57d85294ed25e1e28a96

Contents?: true

Size: 1.49 KB

Versions: 3

Compression:

Stored size: 1.49 KB

Contents

require'timeout'
module Extface
  class DriverBase < ActiveRecord::Base
    self.abstract_class = true
    
    has_one :device, :as => :driveable
    
    [:raw?, :print?, :fiscal?, :cdr?].each do |driver_type|
      define_method driver_type do
        false
      end
    end

    class << self
      def has_serial_config
        has_one :serial_config, as: :s_configureable
        accepts_nested_attributes_for :serial_config
        define_method :serial? do
          true
        end
      end
    end
    
    def handle(buffer) # handle push messages from device outside active session
      return false
    end
    
    def push(buffer)
      
        if @job
          Timeout.timeout(Extface.device_timeout) do
            Extface.redis_block do |r|
              r.subscribe(@job.id) do |on| #blocking until delivered
                on.subscribe do |channel, subscriptions|
                  @job.rpush buffer
                end
                on.message do |event, data|
                  r.unsubscribe
                  @job.connected!
                end
              end
            end
          end
        else
          raise "No job given"
        end
      
    end
    
    def pull(timeout = nil)
      Extface.redis_block do |r|
        list, element = r.blpop(device.uuid, :timeout => timeout)
      end
      element
    end
    
    def notify(message)
      raise "No job given" unless @job
      @job.notify(message)
    end
    
    def set_job(job)
      @job = job
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
extface-0.1.0 app/models/extface/driver_base.rb
extface-0.0.8 app/models/extface/driver_base.rb
extface-0.0.7 app/models/extface/driver_base.rb