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



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ruby_astm/lab_interface.rb', line 40

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



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

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.



29
30
31
32
33
34
# 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|
    process_text(line)
  end
end

#process_type(line) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/ruby_astm/lab_interface.rb', line 127

def process_type(line)
    case line.type
    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"
      self.headers[-1].commit
    end
end

#receive_data(data) ⇒ Object



53
54
55
56
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
# File 'lib/ruby_astm/lab_interface.rb', line 53

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
      puts "sent ENQ"
      send_data(ENQ)
    elsif data.bytes.to_a[0] == 6
      ## so now the middle part has to be 
=begin
      response = STX + "1H|\`^&||||||||||P|E 1394-97|#{Time.now.strftime("%Y%m%d%H%M%S")}\r" + terminator + ETX + checksum("1H|\`^&||||||||||P|E 1394-97|#{Time.now.strftime("%Y%m%d%H%M%S")}\r" + terminator) + "\r"
      response = response.bytes.to_a
      response << 10
      send_data(response.pack('c*'))
=end
      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
          puts "response was already sent."
          send_data(EOT)
        end

      end
    else
      ## send the header 
      puts "--------- SENT ACK -----------"
      send_data(ACK)
    end
end

#rootObject

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_enqObject



114
115
116
117
118
# File 'lib/ruby_astm/lab_interface.rb', line 114

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

#terminatorObject



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

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

#unbindObject

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



156
157
158
# File 'lib/ruby_astm/lab_interface.rb', line 156

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