Class: PVOutput::Client
- Inherits:
-
Object
- Object
- PVOutput::Client
- Includes:
- HTTParty
- Defined in:
- lib/pvoutput/client.rb
Instance Method Summary collapse
-
#add_batch_output(options) ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
-
#add_output(options) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
-
#add_status(options) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
-
#initialize(system_id, api_key, donation_mode = false) ⇒ Client
constructor
A new instance of Client.
-
#post_request(path, options = {}, &block) ⇒ Object
Helper method to post batch request to pvoutput that retries at the moment we get a 400 error back with body containing ‘Load in progress’.
Constructor Details
#initialize(system_id, api_key, donation_mode = false) ⇒ Client
Returns a new instance of Client.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/pvoutput/client.rb', line 11 def initialize(system_id, api_key, donation_mode = false) @system_id = system_id.to_s @api_key = api_key.to_s # The batch operations have default limit of 30 sets of data in one request, when you # are using the donation mode the limit is 100 sets @batch_size = 30 @batch_size = 100 if donation_mode self.class.headers 'X-Pvoutput-Apikey' => @api_key, 'X-Pvoutput-SystemId' => @system_id end |
Instance Method Details
#add_batch_output(options) ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/pvoutput/client.rb', line 89 def add_batch_output() keys = %i[energy_generated energy_export energy_used] keys += %i[peak_power peak_time condition min_temp] keys += %i[max_temp comments import_peak import_off_peak] keys += %i[import_shoulder] .to_a.each_slice(@batch_size) do |slice| data = '' slice.each do |entry| date, values = entry data += "#{date},#{keys.map { |key| values[key] }.join(',')};" end params = { :data => data.chop, } post_request('/service/r2/addbatchoutput.jsp', :body => params) end end |
#add_output(options) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/pvoutput/client.rb', line 65 def add_output() params = { } params[:d] = [:output_date] params[:g] = [:energy_generated] if [:energy_generated] params[:pp] = [:peak_power] if [:peak_power] params[:pt] = [:peak_time] if [:peak_time] params[:cd] = [:condition] if [:condition] params[:tm] = [:min_temp] if [:min_temp] params[:tx] = [:max_temp] if [:max_temp] params[:cm] = [:comments] if [:comments] params[:ip] = [:import_peak] if [:import_peak] params[:io] = [:import_off_peak] if [:import_off_peak] params[:is] = [:import_shoulder] if [:import_shoulder] params[:ih] = [:import_high_shoulder] if [:import_high_shoulder] params[:c] = [:consumption] if [:consumption] response = self.class.post('/service/r2/addoutput.jsp', :body => params) raise('Bad Post') unless response.code == 200 end |
#add_status(options) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/pvoutput/client.rb', line 41 def add_status() time = [:when] || Time.now params = { 'd' => time.strftime('%Y%m%d'), 't' => time.strftime('%H:%M'), } params[:v1] = [:energy_generated] if [:energy_generated] params[:v2] = [:power_generated] if [:power_generated] params[:v3] = [:energy_consumed] if [:energy_consumed] params[:v4] = [:power_consumed] if [:power_consumed] params[:v5] = [:temperature] if [:temperature] params[:v6] = [:voltage] if [:voltage] params[:c1] = 1 if [:cumulative] == true params[:n] = 1 if [:net] == true response = self.class.post('/service/r2/addstatus.jsp', :body => params) raise('Bad Post') unless response.code == 200 end |
#post_request(path, options = {}, &block) ⇒ Object
Helper method to post batch request to pvoutput that retries at the moment we get a 400 error back with body containing ‘Load in progress’
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/pvoutput/client.rb', line 24 def post_request(path, = {}, &block) loop do response = self.class.post(path, , &block) if response.code == 400 && response.body =~ /Load in progress/ # We can't send data too fast, when the previous request is still loaded we # have to wait so sleep 10 seconds and try again sleep(10) elsif response.code == 200 return else raise("Bad Post: #{response.body}") end end end |