Class: DmmUtil::Reading

Inherits:
Object
  • Object
show all
Defined in:
lib/dmm_util/reading.rb

Constant Summary collapse

MULTIPLIER_MAP =
{
-12 => "p",
 -9 => "n",
 -6 => "u",
 -3 => "m",
 -2 => "c",
 -1 => "d",
  0 => "",
  1 => "D",
  2 => "h",
  3 => "k",
  6 => "M",
  9 => "G",
 12 => "T"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Reading

Returns a new instance of Reading.



21
22
23
# File 'lib/dmm_util/reading.rb', line 21

def initialize(attrs)
  @raw = attrs
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



3
4
5
# File 'lib/dmm_util/reading.rb', line 3

def raw
  @raw
end

Instance Method Details

#==(other) ⇒ Object



58
59
60
# File 'lib/dmm_util/reading.rb', line 58

def ==(other)
  value == other.value && unit == other.unit
end

#scaled_valueObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dmm_util/reading.rb', line 37

def scaled_value
  decimals = @raw[:decimals]
  multiplier = @raw[:unit_multiplier]
  state = @raw[:state]
  
  if state == "NORMAL"
    val = "%.#{decimals}f" % (value / (10 ** multiplier))
  elsif state == "OL_MINUS"
    val = "-OL"
  else
    val = state
  end
  
  [val, "#{MULTIPLIER_MAP[multiplier]}#{unit}"]
end

#to_sObject



53
54
55
56
# File 'lib/dmm_util/reading.rb', line 53

def to_s
  sv = scaled_value
  "#{sv.first} #{sv.last}"
end

#tsObject



25
26
27
# File 'lib/dmm_util/reading.rb', line 25

def ts 
  raw[:ts]
end

#unitObject



33
34
35
# File 'lib/dmm_util/reading.rb', line 33

def unit
  raw[:unit]
end

#valueObject



29
30
31
# File 'lib/dmm_util/reading.rb', line 29

def value
  raw[:value]
end