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



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ruby_astm/lab_interface.rb', line 66

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_bufferObject

buffer of incoming data.



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

mattr_accessor :data_buffer

#generate_responseObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ruby_astm/lab_interface.rb', line 79

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

#is_mid_frame_end?(bytes_array) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/ruby_astm/lab_interface.rb', line 101

def is_mid_frame_end?(bytes_array)

  ## if you get 13,10,2 anywhere, ignore that and the subsequent digit.
  bytes_indices_to_delete = []
  unless bytes_array.blank?
    bytes_array.each_with_index{|val,key|
      if bytes_array.size >= (key + 2)
        if bytes_array[key..(key+2)] == [13,10,2]
          bytes_indices_to_delete.push(key - 3)
          bytes_indices_to_delete.push(key - 2)
          bytes_indices_to_delete.push(key - 1)
          bytes_indices_to_delete.push(key)
          bytes_indices_to_delete.push(key + 1)
          bytes_indices_to_delete.push(key + 2)
        end
      end
    }
  end
  bytes_indices_to_delete
end

#pre_process_bytes(byte_arr, concat) ⇒ Object



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
# File 'lib/ruby_astm/lab_interface.rb', line 122

def pre_process_bytes(byte_arr,concat)

    puts byte_arr.to_s
    indices_to_delete = is_mid_frame_end?(byte_arr)
    #puts "indices to delete"
    #puts indices_to_delete.to_s

    if self.mid_frame_end_detected == true
      #puts "deletected mid fram is true, so deleting first byte before delete"
      #puts byte_arr.to_s
      byte_arr = byte_arr[1..-1]
      #puts "after deleteing"
      #puts byte_arr.to_s
      self.mid_frame_end_detected = false
    end

    unless indices_to_delete.blank?
      if byte_arr[(indices_to_delete[-1] + 1)]
        #puts "before deleting frame number "
        #puts byte_arr.to_s
        byte_arr.delete_at((indices_to_delete[-1] + 1))
        #puts "after deleting"
        #puts byte_arr.to_s
      else
        self.mid_frame_end_detected = true
      end
    end
    #puts "byte arr before reject"
    byte_arr = byte_arr.reject.with_index{|c,i|  indices_to_delete.include? i}
    

    byte_arr.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

    concat

end

#process_text(text) ⇒ Object



291
292
293
294
295
296
297
298
299
# File 'lib/ruby_astm/lab_interface.rb', line 291

def process_text(text)
    puts "text is:"
    puts text
    text.split("\n").each do |l|
      puts "doing line:#{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.



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

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



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/ruby_astm/lab_interface.rb', line 301

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"
      unless self.headers.blank?
        unless self.headers[-1].patients.blank?
          unless self.headers[-1].patients[-1].orders[-1].blank?
            hl7_observation = Hl7Observation.new({:line => line})
            self.headers[-1].patients[-1].orders[-1].results[hl7_observation.name] ||= hl7_observation
          end
        end
      end
    when "Hl7_Patient"
      hl7_patient = Hl7Patient.new({:line => line})
      self.headers[-1].patients << hl7_patient
    when "Hl7_Order"
      unless self.headers[-1].patients.blank?
        hl7_order = Hl7Order.new({:line => line, :patient_id => self.headers[-1].patients[-1].patient_id})
        self.headers[-1].patients[-1].orders << hl7_order
      end
    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})
      unless self.headers.blank?
        self.headers[-1].queries << query
      end
    when "Patient"
      puts "got patient."
      patient = Patient.new({:line => line})
      unless self.headers.blank?
        self.headers[-1].patients << patient
      end
    when "Order"
      order = Order.new({:line => line})
      unless self.headers.blank?
        unless self.headers[-1].patients.blank?
          self.headers[-1].patients[-1].orders << order
        end
      end
    when "Result"
      result = Result.new({:line => line})
      unless self.headers.blank?
        unless self.headers[-1].patients.blank?
          unless self.headers[-1].patients[-1].orders[-1].blank?
            self.headers[-1].patients[-1].orders[-1].results[result.name] ||= result
          end
        end
      end
    when "Terminator"
      ## it didn't terminate so there was no commit being called.
      unless self.headers.blank?
        puts "got terminator."
        self.headers[-1].commit
      end
    end
end

#receive_data(data) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/ruby_astm/lab_interface.rb', line 171

def receive_data(data)
    

  begin


    self.data_buffer ||= ''

    #puts "incoming data bytes."

    concat = ""
    
 
    byte_arr = data.bytes.to_a
    self.test_data_bytes ||= []
    self.test_data_bytes.push(byte_arr)
  
    concat = pre_process_bytes(byte_arr,concat)

   
    
    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)
      #root_path = File.dirname __dir
      #puts "root path #{root_path}"
      #IO.write((File.join root_path,'test','resources','roche_multi_frame_bytes.txt'),self.test_data_bytes.to_s)
      #puts self.test_data_bytes.flatten.to_s
      self.data_buffer = ''
      unless self.headers.blank?
        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
      else
        puts "sending catch all --------------- ACK --------------"
        send_data(ACK)
      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
    elsif data.bytes.to_a[0] == 255
      puts  " ----------- got 255 data -----------, not sending anything back. "
    else
      #unless self.data_buffer.blank?
      #  puts self.data_buffer.gsub(/\r/,'\n').to_s
      #end
      ## send the header 
      #puts "--------- SENT ACK -----------"
      ## strip non utf 8 characters from it.
      self.data_buffer.encode!('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
      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
    
    #self.headers = []
    AstmServer.log("data was: " + self.data_buffer + "error is:" + e.backtrace.to_s)
    #send_data(EOT)
  end

end

#rootObject

returns the root directory of the gem.



42
43
44
# File 'lib/ruby_astm/lab_interface.rb', line 42

def root
    File.dirname __dir__
end

#send_enqObject



285
286
287
288
289
# File 'lib/ruby_astm/lab_interface.rb', line 285

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

#terminatorObject



61
62
63
# File 'lib/ruby_astm/lab_interface.rb', line 61

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

#test_data_bytesObject

gather bytes to store for us to test.



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

mattr_accessor :test_data_bytes

#unbindObject

1.STX + response + LF + ETX 2.response 3.STX + response + “L|1|Nr” + ETX 4.response + “L|1|Nr”



372
373
374
# File 'lib/ruby_astm/lab_interface.rb', line 372

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