Sha256: 33df8773a389a718c3b6c3d8ecc51a8e4e39e5607d72f54d053952b828966b9f

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

#!/usr/bin/env ruby

require 'rexml/document'
require 'net/http'
require 'pvoutput/client'
require 'yaml'
require 'ipaddress'

# Load the configuration from the yaml file
sajcollector_config = YAML.load_file('saj_collector.yaml')

# Create a pvoutput with the configured system_id and api_key which both
# can be obtained from the pvoutput website
pvoutput = PVOutput::Client.new(sajcollector_config[:system_id], sajcollector_config[:api_key])

unless IPAddress.valid?(sajcollector_config[:saj])
  raise ("[#{sajcollector_config[:saj]}] is not a valid IP address")
end

# Get the current day trend data from the SAJ device
c = Net::HTTP.get(sajcollector_config[:saj], '/day_trend.xml')

# Let REXML parse the XML file
doc = REXML::Document.new c

# Get the current day number, we can't post data for future dates
time = Time.now
days = time.day
m = time.strftime('%Y%m')

puts "Number of days until now for this month #{days}"

options = {
}

counter = 0
current_day = '00'
day_trend_multiplication = 100

# Some SAJ Inverters provide they energy_generated in granularity of 0.1kWh and
# others in 0.01kWh.
unless sajcollector_config[:day_trend_multiplication_factor].nil?
  day_trend_multiplication = sajcollector_config[:day_trend_multiplication_factor].to_i
end

while counter < days
  value = {
    energy_generated: 0
  }
  # Get the power output of the day,
  doc.elements.each("day_trend/d#{counter}") do |e|
    value[:energy_generated] = (e.text.to_i * day_trend_multiplication).to_s
  end
  options[:"#{m}#{current_day.next!}"] = value

  counter += 1
end

options.each do |date, values|
  puts "Energy generated #{date}: #{values[:energy_generated]} Wh"
end

pvoutput.add_batch_output(options)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
saj_collector-0.5.6 exe/saj_output_collector