Sha256: 2c871d5f253268b1e64dfd805e452d14b295305dfaea1271b7bdbbbd42cc634a

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

require 'httparty'

module PVOutput
  class Client
    include HTTParty
    base_uri 'pvoutput.org'
    # debug_output $stdout

    def initialize(system_id, api_key)
      @system_id = system_id
      @api_key = api_key

      self.class.headers 'X-Pvoutput-Apikey' => @api_key, 'X-Pvoutput-SystemId' => @system_id
    end

    # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
    def add_status(options)
      time = options[:when] || Time.now

      params = {
        'd' => time.strftime('%Y%m%d'),
        't' => time.strftime('%H:%M'),
      }

      params[:v1] = options[:energy_generated] if options[:energy_generated]
      params[:v2] = options[:power_generated] if options[:power_generated]
      params[:v3] = options[:energy_consumed] if options[:energy_consumed]
      params[:v4] = options[:power_consumed] if options[:power_consumed]
      params[:v5] = options[:temperature] if options[:temparature]
      params[:v6] = options[:voltage] if options[:voltage]
      params[:c1] = 1 if options[:cumulative] == true
      params[:n]  = 1 if options[:net] == true

      response = self.class.post('/service/r2/addstatus.jsp', :body => params)

      fail('Bad Post') unless response.code == 200
    end
    # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pvoutput-0.1.0 lib/pvoutput/client.rb