Module: LabInterface

Included in:
AstmServer
Defined in:
lib/ruby_astm/lab_interface.rb

Constant Summary collapse

ACK =
"\x06"
ENQ =
"\x05"
STX =
"\x02"
LF =
"\x10"
CR =
"\x13"
ETX =
"\x03"
EOT =
"\x04"

Instance Method Summary collapse

Instance Method Details

#checksum(input) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ruby_astm/lab_interface.rb', line 49

def checksum(input)
  strString = input
  checksum = strString.sum
  b = checksum.to_s(16)
  strCksm = b[-2..-1]
  if strCksm.length < 2 
    for i in strString.length..1
       strCksm = "0" + strCksm
    end
  end
  strCksm.upcase
end

#data_buffer ⇒ Object

buffer of incoming data.



23
# File 'lib/ruby_astm/lab_interface.rb', line 23

mattr_accessor :data_buffer

#generate_response ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ruby_astm/lab_interface.rb', line 62

def generate_response
  header_responses = self.headers[-1].build_one_response
  header_responses.each_with_index {|response,key|
    message_checksum = checksum(response + terminator + ETX)
    final_resp = STX + response + terminator + ETX + message_checksum + "\r" 
    final_resp_arr = final_resp.bytes.to_a
    final_resp_arr << 10
    if (self.headers[-1].response_sent == false)
      puts "sending the  data as follows----------------------------------------------"
      puts "response sent is:"
      puts self.headers[-1].response_sent
      puts final_resp_arr.to_s
      puts final_resp_arr.pack('c*').gsub(/\r/,'\n')
      send_data(final_resp_arr.pack('c*')) 
      self.headers[-1].response_sent = true if (key == (header_responses.size - 1))
    else
      puts "sending EOT"
      send_data(EOT)
    end
  }
end

#process_text(text) ⇒ Object



189
190
191
192
193
194
# File 'lib/ruby_astm/lab_interface.rb', line 189

def process_text(text)
    text.split("\n").each do |l|
    line = Line.new({:text => l})
      process_type(line)
    end
end

#process_text_file(full_file_path) ⇒ Object

can process a file which contains ASTM output. this method is added so that you can load previously generated ASTM output into your database it also simplifies testing.



34
35
36
37
38
39
40
41
# File 'lib/ruby_astm/lab_interface.rb', line 34

def process_text_file(full_file_path)
  #full_file_path ||= File.join root,'../test','resources','sysmex_550_sample.txt'
  IO.read(full_file_path).each_line do |line|
    line.split('\\r').each do |txt| 
      process_text(txt)
    end
  end
end

#process_type(line) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/ruby_astm/lab_interface.rb', line 196

def process_type(line)
    case line.type
    when "Hl7_Header"
      hl7_header = Hl7Header.new({:line => line})
      self.headers ||= []
      self.headers << hl7_header
    when "Hl7_Observation"
      hl7_observation = Hl7Observation.new({:line => line})
      self.headers[-1].patients[-1].orders[-1].results[hl7_observation.name] = hl7_observation
    when "Hl7_Patient"
      hl7_patient = Hl7Patient.new({:line => line})
      self.headers[-1].patients << hl7_patient
    when "Hl7_Order"
      hl7_order = Hl7Order.new({:line => line, :patient_id => self.headers[-1].patients[-1].patient_id})
      self.headers[-1].patients[-1].orders << hl7_order
    when "Header"
      puts "got header"
      header = Header.new({:line => line})
      self.headers ||= []
      self.headers << header
    when "Query"
      puts "got query"
      query = Query.new({:line => line})
      self.headers[-1].queries << query
    when "Patient"
      puts "got patient."
      patient = Patient.new({:line => line})
      self.headers[-1].patients << patient
    when "Order"
      order = Order.new({:line => line})
      self.headers[-1].patients[-1].orders << order
    when "Result"
      result = Result.new({:line => line})
      self.headers[-1].patients[-1].orders[-1].results[result.name] = result
    when "Terminator"
      ## it didn't terminate so there was no commit being called.
      puts "got terminator."
      self.headers[-1].commit
    end
end

#receive_data(data) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/ruby_astm/lab_interface.rb', line 84

def receive_data(data)
    
  begin

    self.data_buffer ||= ''

    puts "incoming data bytes."

    concat = ""
    
    puts data.bytes.to_a.to_s

    data.bytes.to_a.each do |byte|
      x = [byte].pack('c*').force_encoding('UTF-8')
      if x == "\r"
        concat+="\n"
      elsif x == "\n"
        #puts "new line found --- "
        concat+=x
        #puts "last thing in concat."
        #puts concat[-1].to_s
      else
        concat+=x
      end
    end
    
    #puts "concat is:"
    
    #puts concat.to_s
    
    self.data_buffer << concat

    ## if the last byte is EOT, then call process text.
    ## inside that split by line and process one at a time.
    ##process_text(concat)      
    

    if data.bytes.to_a[-1] == 4
      puts "GOT EOT --- PROCESSING BUFFER, AND CLEARING."
      process_text(self.data_buffer)
      self.data_buffer = ''
      if self.headers[-1].queries.blank?
        puts "no queries in header so sending ack after getting EOT and processing the buffer"
        send_data(ACK)
      else
        puts "sending ENQ"
        send_data(ENQ)
      end
    elsif data.bytes.to_a[0] == 6
      puts "GOT ACK --- GENERATING RESPONSE"
      unless self.headers.blank?
        header_responses = self.headers[-1].build_one_response({machine_name: self.headers[-1].machine_name})
        ## if no queries then, we have to send ack.
        if header_responses.blank?
          puts "sending ACK since there are no queries in the header"
          send_data(ACK)
        end
        header_responses.each_with_index {|response,key|
          message_checksum = checksum(response + terminator + ETX)
          final_resp = STX + response + terminator + ETX + message_checksum + "\r" 
          final_resp_arr = final_resp.bytes.to_a
          final_resp_arr << 10
          if (self.headers[-1].response_sent == false)
            puts "sending the  data as follows----------------------------------------------"
            puts "response sent is:"
            puts self.headers[-1].response_sent
            puts final_resp_arr.pack('c*').gsub(/\r/,'\n')
            send_data(final_resp_arr.pack('c*')) 
            self.headers[-1].response_sent = true if (key == (header_responses.size - 1))
          else
            puts "sending EOT"
            send_data(EOT)
          end
        }
      else
        puts "NO HEADERS PRESENT --- "
      end
    else
      puts self.data_buffer.gsub(/\r/,'\n').to_s
      ## send the header 
      #puts "--------- SENT ACK -----------"
      if self.data_buffer =~ /MSH\|/
        puts " -------------- HEADERS ARE BLANK WITH HL7, sending ack. ------------ "
        process_text(self.data_buffer)
        self.data_buffer = ''
        if self.headers.size > 0
          self.headers[-1].commit
          send_data(self.headers[-1].generate_ack_success_response)
        end
      else
        puts " -------------- HEADERS ARE BLANK NOT HL7, sending ack. ------------ "
        send_data(ACK)
      end
    end
  rescue => e
    AstmServer.log(e.backtrace.to_s)
  end
end

#root ⇒ Object

returns the root directory of the gem.



27
28
29
# File 'lib/ruby_astm/lab_interface.rb', line 27

def root
    File.dirname __dir__
end

#send_enq ⇒ Object



183
184
185
186
187
# File 'lib/ruby_astm/lab_interface.rb', line 183

def send_enq
  puts "enq as bytes is:"
  puts ENQ.unpack('c*')
  send_data(ENQ)
end

#terminator ⇒ Object



44
45
46
# File 'lib/ruby_astm/lab_interface.rb', line 44

def terminator
  "L|1|N\r"
end

#unbind ⇒ Object

1.STX + response + LF + ETX 2.response 3.STX + response + "L|1|N\r" + ETX 4.response + "L|1|N\r"



243
244
245
# File 'lib/ruby_astm/lab_interface.rb', line 243

def unbind
   puts "-- someone disconnected from the echo server!"
end