class Senec::Value

Public Class Methods

new(data) click to toggle source
# File lib/senec/value.rb, line 3
def initialize(data)
  @data = data
end

Public Instance Methods

to_f() click to toggle source
# File lib/senec/value.rb, line 7
def to_f
  decoded_value
end
to_i() click to toggle source
# File lib/senec/value.rb, line 11
def to_i
  decoded_value.round
end

Private Instance Methods

decoded_value() click to toggle source
# File lib/senec/value.rb, line 17
def decoded_value
  parts  = @data.split('_')
  prefix = parts[0]
  value  = parts[1]

  case prefix
  when 'fl'
    hex2float(value)
  when 'i3', 'u1', 'u3'
    hex2int(value)
  # TODO: There are some more prefixes to handle
  else
    raise ArgumentError, "#{@data} cannot be decoded!"
  end
end
hex2float(hex) click to toggle source
# File lib/senec/value.rb, line 33
def hex2float(hex)
  [ "0x#{hex}".to_i(16) ].
    pack('L').
    unpack1('F').
    round(1)
end
hex2int(hex) click to toggle source
# File lib/senec/value.rb, line 40
def hex2int(hex)
  "0x#{hex}".to_i(16)
end