Module: UsbModule

Defined in:
lib/ruby_astm/usb_module.rb

Constant Summary collapse

ESR_RESULTS_HASH =
"ESR_RESULTS_HASH"
ESR_SORTED_SET =
"ESR_SORTED_SET"
MAX_REPEAT_RESULT_COUNT =

a particular barcode will be allowed to have 3 different results, after which the next time it won’t be uploaded.

3

Instance Method Summary collapse

Instance Method Details

#add_result?(barcode, result, existing_barcodes) ⇒ Boolean

total times repeated, we can have that as max 3 times. so after that it wont add. so we keep a sorted set with the barcode and the count like key -> value, score key(barcode) -> value(time) -> score(count) so we can zincrby, each time. @param barcode : the barcode of the result @param

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ruby_astm/usb_module.rb', line 55

def add_result?(barcode,result,existing_barcodes)
  begin
    Integer(result)
    #puts "result is: #{result}"
    return false if existing_barcodes.include? barcode
    return false if result.to_i == 0
    #puts "the result is not zero"
    existing_result = $redis.hget(ESR_RESULTS_HASH,barcode)
    #puts "existing result is: #{existing_result}"
    if (existing_result.blank?)
      true
    else
      if existing_result != result
        true
      end
    end 
  rescue => e
    puts e.to_s
    false
  end
  
end

#add_result_by_count?(barcode, result) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
# File 'lib/ruby_astm/usb_module.rb', line 38

def add_result_by_count?(barcode,result)
  begin

    
  rescue => e
    puts e.to_s
    return true
  end
end

#begin_patient_resultsObject



12
13
14
# File 'lib/ruby_astm/usb_module.rb', line 12

def begin_patient_results
  ">00A00013"
end

#interpret?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/ruby_astm/usb_module.rb', line 32

def interpret?
  ## if the third last is a carriage return, then interpret.l

  self.usb_response_bytes[-3] == 13
end

#parse_usb_response(string_data) ⇒ Object



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
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ruby_astm/usb_module.rb', line 78

def parse_usb_response(string_data)
  string_data.bytes.to_a.each do |byte|
    self.usb_response_bytes.push(byte)
  end
  #puts "self usb response bytes:"
  #puts self.usb_response_bytes.to_s
  if interpret?
    #puts "interpret"
    barcodes = []
    if kk = self.usb_response_bytes[13..-4]
      ## its going fresh -> old
      ## so if the barcode has already come
      ## we do not push it.
      kk.each_slice(32) do |patient_record|
        unless patient_record.blank?
          unless patient_record.size < 24
            bar_code = nil
            bar_code = patient_record[11..23].pack('c*').gsub(/\./,'')
            #puts "bar code: #{bar_code}"
            unless bar_code.strip.blank?
              esr = patient_record[26]
              patient = Patient.new(:orders => [Order.new(:results => [Result.new(:value => esr, :name => "ESR", :report_name => "ESR")])])
              patient = Patient.new({})
              patient.patient_id = bar_code
              patient.orders = []
              order = Order.new({})
              result = Result.new({})
              result.value = esr.to_s
              result.name = "ESR"
              result.report_name = "ESR"
              order.id = bar_code
              order.results = []
              order.results << result
              #puts "barcode: #{bar_code}, result : #{result.value}"
              patient.orders << order
              if add_result?(bar_code,result.value,existing_barcodes)
                #puts patient.to_json
                $redis.lpush("patients",patient.to_json)
                $redis.hset(ESR_RESULTS_HASH,bar_code,result.value.to_i)
                existing_barcodes << bar_code
              end
            end
          end 
        end
      end
    end
    self.usb_response_bytes = []  
  else
    #puts "dont interpret"    
  end

end

#request_resultsObject

requests the last 5 tests performed. last two digits before the carriage return are 00 if digits are 01 : will take the 5 penultimate tests if digits are 13 : will take the first 5 tests from memory.



28
29
30
# File 'lib/ruby_astm/usb_module.rb', line 28

def request_results
  ">0002009300\r00"
end

#request_statusObject



20
21
22
# File 'lib/ruby_astm/usb_module.rb', line 20

def request_status
  ">00000082\r00"
end

#request_versionObject



16
17
18
# File 'lib/ruby_astm/usb_module.rb', line 16

def request_version
  ">00000080\r00"
end