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



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ruby_astm/lab_interface.rb', line 44

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

#process_text(text) ⇒ Object



125
126
127
128
129
130
131
132
# File 'lib/ruby_astm/lab_interface.rb', line 125

def process_text(text)
    ## should replace carriage returns with new lines, before processing any file.
    ## or 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.



29
30
31
32
33
34
35
36
# File 'lib/ruby_astm/lab_interface.rb', line 29

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



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 134

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"
      header = Header.new({:line => line})
      self.headers ||= []
      self.headers << header
    when "Query"
      query = Query.new({:line => line})
      self.headers[-1].queries << query
    when "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.
      self.headers[-1].commit
    end
end

#receive_data(data) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
# File 'lib/ruby_astm/lab_interface.rb', line 57

def receive_data(data)
    
    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
    process_text(concat)      
    
    if data.bytes.to_a[0] == 4
      send_data(ENQ)
    elsif data.bytes.to_a[0] == 6
      self.headers[-1].build_responses.each do |response|
        message_checksum = checksum(response + terminator + ETX)
        #puts "Calculated checksum is: #{message_checksum}"
        final_resp = STX + response + terminator + ETX + message_checksum + "\r" 
        final_resp_arr = final_resp.bytes.to_a
        final_resp_arr << 10
        #puts final_resp_arr.to_s
        if (self.headers[-1].response_sent == false)
          send_data(final_resp_arr.pack('c*')) 
          self.headers[-1].response_sent = true
        else
          send_data(EOT)
        end
      end
    else
      ## send the header 
      puts "--------- SENT ACK -----------"
      if self.headers.blank?
        send_data(ACK)
      else
        if self.headers[-1].is_astm?
          send_data(ACK)
        elsif self.headers[-1].is_hl7?
          if self.headers.size > 0
            ## commit should return the jsonified thing, if possible.
            self.headers[-1].commit
            send_data(self.headers[-1].generate_ack_success_response)
          end
        end
      end
    end
end

#root ⇒ Object

returns the root directory of the gem.



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

def root
    File.dirname __dir__
end

#send_enq ⇒ Object



119
120
121
122
123
# File 'lib/ruby_astm/lab_interface.rb', line 119

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

#terminator ⇒ Object



39
40
41
# File 'lib/ruby_astm/lab_interface.rb', line 39

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"



177
178
179
# File 'lib/ruby_astm/lab_interface.rb', line 177

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