class TED::ECC

Public Class Methods

new(host) click to toggle source
# 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

Public Instance Methods

current(source = :net) click to toggle source
# 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) click to toggle source

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

# 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
mtus() click to toggle source

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

# File lib/ted/ecc.rb, line 80
def mtus
  build_system_layout
  @mtus
end
refresh() click to toggle source

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

# File lib/ted/ecc.rb, line 29
def refresh
  @mtus = nil
end
spyders() click to toggle source

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

# File lib/ted/ecc.rb, line 87
def spyders
  build_system_layout
  @spyders
end
system_overview() click to toggle source
# 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