Class: LIS::Transfer::ApplicationProtocol

Inherits:
PacketIO::Base
  • Object
show all
Defined in:
lib/lis/transfer/application_protocol.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ ApplicationProtocol

Returns a new instance of ApplicationProtocol.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/lis/transfer/application_protocol.rb', line 64

def initialize(*args)
  super

  @patient_information_requests = {}
  @last_patient = nil
  @last_order = nil
  @handlers = {
    LIS::Message::Header => :received_header,
    LIS::Message::Patient => :received_patient_information,
    LIS::Message::Order => :received_order_record,
    LIS::Message::Result => :received_result,
    LIS::Message::Query => :received_request_for_information
  }
end

Instance Attribute Details

#device_nameObject (readonly)

Returns the value of attribute device_name.



5
6
7
# File 'lib/lis/transfer/application_protocol.rb', line 5

def device_name
  @device_name
end

Instance Method Details

#on_request(&block) ⇒ Object



11
12
13
# File 'lib/lis/transfer/application_protocol.rb', line 11

def on_request(&block)
  @on_request_callback = block
end

#on_request_sent(&block) ⇒ Object



15
16
17
# File 'lib/lis/transfer/application_protocol.rb', line 15

def on_request_sent(&block)
  @on_request_sent_callback = block
end

#on_result(&block) ⇒ Object



7
8
9
# File 'lib/lis/transfer/application_protocol.rb', line 7

def on_result(&block)
  @on_result_callback = block
end

#receive(type, message = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/lis/transfer/application_protocol.rb', line 79

def receive(type, message = nil)
  case type
    when :begin
      @last_patient = nil
      @last_order = nil
    when :idle
      send_pending_requests
    when :message
      @message = LIS::Message::Base.from_string(message)
      handler = @handlers[@message.class]
      send(handler, @message) if handler
  end
end

#received_header(message) ⇒ Object



19
20
21
22
# File 'lib/lis/transfer/application_protocol.rb', line 19

def received_header(message)
  @patient_information_requests ||= {} # delete the list of patients
  @device_name = message.sender_name
end

#received_order_record(message) ⇒ Object



33
34
35
# File 'lib/lis/transfer/application_protocol.rb', line 33

def received_order_record(message)
  @last_order = message
end

#received_patient_information(message) ⇒ Object



28
29
30
31
# File 'lib/lis/transfer/application_protocol.rb', line 28

def received_patient_information(message)
  @last_order = nil
  @last_patient = message
end

#received_request_for_information(message) ⇒ Object



41
42
43
44
45
# File 'lib/lis/transfer/application_protocol.rb', line 41

def received_request_for_information(message)
  @patient_information_requests ||= {}
  requests = @on_request_callback.call(@device_name, message.starting_range_id)
  @patient_information_requests[message.sequence_number] = requests if requests
end

#received_result(message) ⇒ Object



37
38
39
# File 'lib/lis/transfer/application_protocol.rb', line 37

def received_result(message)
  result_for(@last_patient, @last_order, message)
end

#result_for(patient, order, result) ⇒ Object



24
25
26
# File 'lib/lis/transfer/application_protocol.rb', line 24

def result_for(patient, order, result)
  @on_result_callback.call(@device_name, patient, order, result)
end

#send_pending_requestsObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/lis/transfer/application_protocol.rb', line 47

def send_pending_requests
  sending_session(@patient_information_requests) do |patient_information|
    patient_information.each do |sequence_nr, request_data|
      write :message, LIS::Message::Patient.new(sequence_nr,
                                                request_data.patient_id,
                                                request_data.patient_last_name,
                                                request_data.patient_first_name).to_message
      request_data.each_type do |id, request|
        write :message, LIS::Message::Order.new(sequence_nr, id, request).to_message
      end

      @on_request_sent_callback.call(@device_name, request_data) if @on_request_sent_callback
    end
  end
  @patient_information_requests = {}
end

#sending_session(data) { ... } ⇒ Object

Yields:

  • data



94
95
96
97
98
99
100
101
102
103
# File 'lib/lis/transfer/application_protocol.rb', line 94

def sending_session(data)
  # don't send anything if there are no pending requests
  return if data.nil? or data.empty?

  write :begin
  write :message, LIS::Message::Header.new("LIS", @device_name).to_message
  yield data
  write :message, LIS::Message::Terminator.new.to_message
  write :idle
end