Class: AtreaControl::SensorParser

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/atrea_control/sensor_parser.rb

Overview

Call RD5 unit ang get current sensors values

parse it and return Hash

Instance Method Summary collapse

Methods included from Logger

#logger

Constructor Details

#initialize(user_ctrl) ⇒ SensorParser

Returns a new instance of SensorParser.

Parameters:



13
14
15
# File 'lib/atrea_control/sensor_parser.rb', line 13

def initialize(user_ctrl)
  @user_ctrl = user_ctrl
end

Instance Method Details

#format_data(values) ⇒ Hash

Parameters:

  • values (Hash)

Returns:

  • (Hash)


55
56
57
58
59
60
61
62
63
64
65
# File 'lib/atrea_control/sensor_parser.rb', line 55

def format_data(values)
  {
    "current_mode" => parse_current_mode(values),
    "current_power" => values["current_power"].to_f,
    "outdoor_temperature" => values["outdoor_temperature"].to_f / 10.0,
    "preheat_temperature" => values["preheat_temperature"].to_f / 10.0,
    "input_temperature" => values["input_temperature"].to_f / 10.0,
    "preheating" => values["preheating"],
    "timestamp" => Time.strptime(values["timestamp"], "%Y-%m-%d %H:%M:%S"),
  }
end

#input(sensor, value) ⇒ Object



21
22
23
# File 'lib/atrea_control/sensor_parser.rb', line 21

def input(sensor, value)
  sensor + value.to_s.rjust(5, "0")
end

#parse(xml) ⇒ Object

Note:

if(values>32767) values-=65536; if(params && params.offset) values=values-params.offset; if(params && params.coef) values=values/params.coef;

See Also:

  • -> loadRD5Values(node, init)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/atrea_control/sensor_parser.rb', line 32

def parse(xml)
  xml = Nokogiri::XML xml
  parsed = @user_ctrl.sensors.transform_values do |id|
    # node = xml.xpath("//O[@I=\"#{id}\"]/@V").last
    node = xml.xpath("//O[@I=\"#{id}\"]").last
    # logger.debug node.to_s
    value = node.attribute("V").value.to_i
    value -= 65_536 if value > 32_767
    # value -= 0 if "offset"
    # value = value / coef if "coef"
    value
  end
  preheating = %w[C10200 C10202 C10215 C10217].any? do |i|
    xml.xpath("//O[@I=\"#{i}\"]/@V").last&.value == "1"
  end
  # @note timestamp seems to be localtime of creation of xml
  parsed["timestamp"] = xml.xpath("//RD5WEB").attribute("t").to_s
  parsed["preheating"] = preheating
  parsed
end

#values(xml) ⇒ Object



17
18
19
# File 'lib/atrea_control/sensor_parser.rb', line 17

def values(xml)
  format_data(parse(xml))
end