Sha256: bafdd93e1b7760292e78256945aa92a7ec17bf3b6a0989f11d8aa825e3b5765b

Contents?: true

Size: 1.83 KB

Versions: 2

Compression:

Stored size: 1.83 KB

Contents

#!/usr/bin/env ruby
# frozen_string_literal: true

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

yaml_file = 'saj_collector.yaml'

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

# 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, please correct your #{yaml_file} file")
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 = 1
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.to_s.rjust(2, '0')}"] = value
  counter += 1
  current_day += 1
end

options.each do |date, values|
  energy_generated = values[:energy_generated].to_i / 1000.0
  puts "Energy generated #{date}: #{energy_generated} kWh"
end

pvoutput.add_batch_output(options)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
saj_collector-1.0.3 exe/saj_output_collector
saj_collector-1.0.2 exe/saj_output_collector