Class: TED::ECC

Inherits:
Object
  • Object
show all
Defined in:
lib/ted/ecc.rb

Instance Method Summary collapse

Constructor Details

#initialize(host) ⇒ ECC

Returns a new instance of ECC.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ted/ecc.rb', line 13

def initialize(host)
  if host.is_a?(String)
    @host = URI.parse(host)
  else
    @host = host.dup
  end
  @user = @host.user
  @password = @host.password
  @host.user = nil
  @http = Net::HTTP.new(@host.host, @host.port)
  @http.use_ssl = (@host.scheme == 'https')
  @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end

Instance Method Details

#current(source = :net) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ted/ecc.rb', line 33

def current(source = :net)
  params = {}
  params[:T] = 0 # Power

  params[:D] = case source
                 when :net
                   0
                 when :load
                   1
                 when :generation
                   2
                 when MTU
                   params[:M] = source.index
                   255
                 when :spyders
                   return spyders_current
                 else
                   raise ArgumentError, 'source must be :net, :load, :generation, or :spyders'
               end

  dashboard_data(Nokogiri::XML(query("api/DashData.xml", params)))
end

#history(interval: :seconds) ⇒ Object

Returns history for all connected MTUs and Spyders The return value is a hash indexed by the MTU or Spyder::Group, and a hash of timestamp, energy or power, and cost

Raises:

  • (ArgumentError)


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
# File 'lib/ted/ecc.rb', line 95

def history(interval: :seconds)
  raise ArgumentError, "invalid interval" unless INTERVALS.include?(interval)

  params = {}

  params[:T] = INTERVALS.index(interval) + 1

  response = query("history/exportAll.csv", params)
  result = {}
  response.strip!
  CSV.parse(response) do |(channel_name, timestamp, kwh, cost)|
    channel = mtus[channel_name] || spyders[channel_name]
    result[channel] ||= []
    timestamp = case interval
                  when :seconds, :minutes, :hours
                    DateTime.strptime(timestamp, "%m/%d/%Y %H:%M:%S").to_time
                  when :days, :months
                    month, day, year = timestamp.split('/').map(&:to_i)
                    Date.new(year, month, day)
                end
    energy_key = [:seconds, :minutes].include?(interval) ? :power : :energy
    result[channel] << {
        timestamp: timestamp,
        energy_key => (kwh.to_f * 1000).to_i,
        cost: cost.to_f
    }
  end
  result
end

#inspectObject

:nodoc:



126
127
128
# File 'lib/ted/ecc.rb', line 126

def inspect
  "#<TED::ECC:#{@host}>"
end

#mtusObject

A hash of the MTUs connected to this ECC. It is indexed by both description and numerical index



80
81
82
83
# File 'lib/ted/ecc.rb', line 80

def mtus
  build_system_layout
  @mtus
end

#refreshObject

Removes the cached system layout, allowing access to newly defined MTUs and Spyders



29
30
31
# File 'lib/ted/ecc.rb', line 29

def refresh
  @mtus = nil
end

#spydersObject

A hash of the Spyders connected to this ECC. It is index by both description and numerical index



87
88
89
90
# File 'lib/ted/ecc.rb', line 87

def spyders
  build_system_layout
  @spyders
end

#system_overviewObject



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

def system_overview
  xml = Nokogiri::XML(query("api/SystemOverview.xml", T: 0))
  result = ObjectHash.new
  (1..4).each do |i|
    mtu = {}
    mtu_xml = xml.at_css("MTU#{i}")
    mtu[:power] = mtu_xml.at_css("Value").text.to_i
    mtu[:apparent_power] = mtu_xml.at_css("KVA").text.to_i
    mtu[:power_factor] = mtu_xml.at_css("PF").text.to_i / 100.0
    voltage_xml = mtu_xml.at_css("PhaseVoltage")
    current_xml = mtu_xml.at_css("PhaseCurrent")
    mtu[:voltage] = {}
    mtu[:current] = {}
    %w{A B C}.each do |phase|
      mtu[:voltage][phase.to_sym] = voltage_xml.at_css(phase).text.to_i / 10.0
      mtu[:current][phase.to_sym] = current_xml.at_css(phase).text.to_i / 10.0
    end
    result[i - 1] = result[mtus[i - 1].description] = mtu
  end
  result
end