Class: CurrentCost::Reading

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#channelsObject

Returns the value of attribute channels.



61
62
63
# File 'lib/currentcost/reading.rb', line 61

def channels
  @channels
end

#days_since_birthObject

Returns the value of attribute days_since_birth.



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

def days_since_birth
  @days_since_birth
end

#historyObject

Returns the value of attribute history.



63
64
65
# File 'lib/currentcost/reading.rb', line 63

def history
  @history
end

#hourObject

Returns the value of attribute hour.



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

def hour
  @hour
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#minuteObject

Returns the value of attribute minute.



55
56
57
# File 'lib/currentcost/reading.rb', line 55

def minute
  @minute
end

#nameObject

Returns the value of attribute name.



57
58
59
# File 'lib/currentcost/reading.rb', line 57

def name
  @name
end

#secondObject

Returns the value of attribute second.



56
57
58
# File 'lib/currentcost/reading.rb', line 56

def second
  @second
end

#software_versionObject

Returns the value of attribute software_version.



60
61
62
# File 'lib/currentcost/reading.rb', line 60

def software_version
  @software_version
end

#temperatureObject

Returns the value of attribute temperature.



62
63
64
# File 'lib/currentcost/reading.rb', line 62

def temperature
  @temperature
end

#typeObject

Returns the value of attribute type.



59
60
61
# File 'lib/currentcost/reading.rb', line 59

def type
  @type
end

Class Method Details

.from_xml(xml) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/currentcost/reading.rb', line 8

def self.from_xml(xml)
  # Parse XML
  doc = REXML::Document.new(xml)
  # Create reading object
  r = Reading.new
  # Extract basic data
  r.days_since_birth = REXML::XPath.first(doc, "/msg/date/dsb").text.to_i
  r.hour = REXML::XPath.first(doc, "/msg/date/hr").text.to_i
  r.minute = REXML::XPath.first(doc, "/msg/date/min").text.to_i
  r.second = REXML::XPath.first(doc, "/msg/date/sec").text.to_i
  r.name = REXML::XPath.first(doc, "/msg/src/name").text
  r.id = REXML::XPath.first(doc, "/msg/src/id").text
  r.type = REXML::XPath.first(doc, "/msg/src/type").text
  r.software_version = REXML::XPath.first(doc, "/msg/src/sver").text
  r.temperature = REXML::XPath.first(doc, "/msg/tmpr").text.to_f
  r.channels = []
  REXML::XPath.each(doc, "/msg/*/watts") do |node|
    r.channels << { :watts => node.text.to_i }
  end
  # Extract history data
  if REXML::XPath.first(doc, "/msg/hist")
    r.history = {}
    r.history[:hours] = []
    REXML::XPath.each(doc, "/msg/hist/hrs/*") do |node|
      r.history[:hours][node.name.slice(1,2).to_i] = node.text.to_f
    end
    r.history[:days] = []
    REXML::XPath.each(doc, "/msg/hist/days/*") do |node|
      r.history[:days][node.name.slice(1,2).to_i] = node.text.to_i
    end
    r.history[:months] = []
    REXML::XPath.each(doc, "/msg/hist/mths/*") do |node|
      r.history[:months][node.name.slice(1,2).to_i] = node.text.to_i
    end
    r.history[:years] = []
    REXML::XPath.each(doc, "/msg/hist/yrs/*") do |node|
      r.history[:years][node.name.slice(1,2).to_i] = node.text.to_i
    end
  end
  # Done
  return r
rescue 
  raise CurrentCost::ParseError.new("Couldn't parse XML data.")
end