class SAAL::Envoy::PowerEnergy
Constants
- DEFAULT_CACHE_TIMEOUT
- DEFAULT_HOST
- DEFAULT_PREFIX
- DEFAULT_SOURCES
- DEFAULT_TIMEOUT
- DEFAULT_TYPES
Public Class Methods
new(defs, opts={})
click to toggle source
# File lib/envoy.rb 31 def initialize(defs, opts={}) 32 @host = defs[:host] || defs['host'] || DEFAULT_HOST 33 @timeout = opts[:timeout] || opts['timeout'] || DEFAULT_TIMEOUT 34 @cache_timeout = opts[:cache_timeout] || opts['cache_timeout'] || DEFAULT_CACHE_TIMEOUT 35 @cache = nil 36 @cachetime = nil 37 @sources = defs[:sources] || defs['source'] || DEFAULT_SOURCES 38 @types = defs[:types] || defs['types'] || DEFAULT_TYPES 39 @prefix = defs[:prefix] || defs['prefix'] || DEFAULT_PREFIX 40 end
Public Instance Methods
create_sensors()
click to toggle source
# File lib/envoy.rb 50 def create_sensors 51 sensors = {} 52 @sources.product(@types).each do |source, type| 53 key = "#{@prefix}_#{source}_#{type}" 54 sensors[key] = PowerEnergyUnderlying.new(key, self) 55 end 56 sensors 57 end
read_val(name)
click to toggle source
# File lib/envoy.rb 42 def read_val(name) 43 if !@cachetime or @cachetime < Time.now - @cache_timeout 44 @cache = read_all() 45 @cachetime = Time.now 46 end 47 return @cache ? @cache[name] : nil 48 end
Private Instance Methods
read_all()
click to toggle source
# File lib/envoy.rb 87 def read_all 88 response = SAAL::do_http_get(@host, 80, "/production.json?details=1", nil, nil, @timeout) 89 return nil if !response 90 91 values = JSON.parse(response.body) 92 outputs = {} 93 94 values["production"].each do |source| 95 type = source["type"] 96 case type 97 when "inverters" 98 save_vals(outputs, "production_inverters", source) 99 when "eim" 100 if source["lines"] 101 save_vals(outputs, "production_phase1", source["lines"][0]) 102 save_vals(outputs, "production_phase2", source["lines"][1]) 103 save_vals(outputs, "production_phase3", source["lines"][2]) 104 end 105 save_vals(outputs, "production_total", source) 106 else 107 $stderr.puts "WARNING: ENVOY: don't know source type #{type}" 108 end 109 end 110 111 values["consumption"].each do |source| 112 type = { 113 "total-consumption" => "total", 114 "net-consumption" => "net", 115 }[source["measurementType"]] || "unknown"; 116 117 if source["lines"] 118 save_vals(outputs, "#{type}_consumption_phase1", source["lines"][0]) 119 save_vals(outputs, "#{type}_consumption_phase2", source["lines"][1]) 120 save_vals(outputs, "#{type}_consumption_phase3", source["lines"][2]) 121 end 122 save_vals(outputs, "#{type}_consumption_total", source) 123 end 124 125 outputs 126 end
save_vals(dest, name, source)
click to toggle source
# File lib/envoy.rb 60 def save_vals(dest, name, source) 61 { 62 "wNow" => "w_now", 63 "apprntPwr" => "va_now", 64 "whLifetime" => "wh_lifetime", 65 "vahLifetime" => "vah_lifetime", 66 }.each do |type, label| 67 dest["#{@prefix}_#{name}_#{label}"] = source[type] 68 end 69 70 # Hack around the fact that apprntPwr is broken on the total consumption 71 # calculation for the three-phase sum at least 72 # In those cases it seems to be missing a divide by three, so when the 73 # calculation for voltage and current alone is close do the extra divide 74 va_now = dest["#{@prefix}_#{name}_va_now"] 75 if va_now && !name.include?("phase") 76 voltage = source["rmsVoltage"] 77 current = source["rmsCurrent"] 78 if voltage && current 79 va_alt = voltage * current 80 if ((va_alt / va_now) - 1.0).abs < 0.05 81 dest["#{@prefix}_#{name}_va_now"] = va_now / 3.0 82 end 83 end 84 end 85 end