Class: RealTimeDb

Inherits:
Object
  • Object
show all
Defined in:
lib/publisher/real_time_db.rb

Constant Summary collapse

SITE_URL =
ENV["FIREBASE_SITE"]
SECRET =
ENV["FIREBASE_SECRET"]
WORK_TYPES =
{
  "IMMUNO" => "",
  "BIOCHEM" => "",
  "BIOCHEM-EXL" => "",
  "BIOCHEM-ELECTROLYTE" => "",
  "HEMAT" => "",
  "URINE" => "",
  "OUTSOURCE" => ""
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(work_allotment_hash) ⇒ RealTimeDb

@param work_allotment_hash : key => one of the work types value => name of a worker



25
26
27
28
29
30
# File 'lib/publisher/real_time_db.rb', line 25

def initialize(work_allotment_hash)
  self.connection = RestFirebase.new :site => SITE_URL,
                    :secret => SECRET
       puts "initialized"
       self.work_allotment_hash = work_allotment_hash || WORK_TYPES
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



7
8
9
# File 'lib/publisher/real_time_db.rb', line 7

def connection
  @connection
end

#work_allotment_hashObject

Returns the value of attribute work_allotment_hash.



8
9
10
# File 'lib/publisher/real_time_db.rb', line 8

def work_allotment_hash
  @work_allotment_hash
end

Instance Method Details

#assign_test(barcode, tests, mappings) ⇒ Object

we pass the real_time_data instance into the



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/publisher/real_time_db.rb', line 54

def assign_test(barcode,tests,mappings)
  ## so do we get the name of the worker.
  inverted_mappings = {}
  mappings.keys.each do |machine_code|
    lis_code = mappings[machine_code][LIS_CODE]
    inverted_mappings[lis_code] = mappings[machine_code]
  end
  worker_hash = {}
  tests.each do |lis_code|
    worker_name = "NO_ONE"
    unless inverted_mappings[lis_code].blank?
      test_type = inverted_mappings[lis_code]["TYPE"]
      worker_name = self.work_allotment_hash[test_type]
    end
    worker_hash[worker_name] ||= []
    worker_hash[worker_name] << lis_code
  end
  worker_hash.keys.each do |worker_name|
    #self.connection.post("lab/work/#{worker_name}", :tests => worker_hash[worker_name], :barcode => barcode, :timestamp => Time.now.to_i)
  end
end

#open_event_streamObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/publisher/real_time_db.rb', line 32

def open_event_stream
  es = self.connection.event_source('users/tom')
  es.onopen   { |sock| p sock } # Called when connected
  es.onmessage{ |event, data, sock| p event, data } # Called for each message
  es.onerror  { |error, sock| p error } # Called whenever there's an error
  # Extra: If we return true in onreconnect callback, it would automatically
  #        reconnect the node for us if disconnected.
  @reconnect = true

  es.onreconnect{ |error, sock| p error; @reconnect }

  # Start making the request
  es.start

  self.connection.wait
end