egauge-rb 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 38d34c0ab7c558f82841f348e0135d3ba9120678
4
- data.tar.gz: 528d77d916ffffe031a2be9b02157e893d70786a
2
+ SHA256:
3
+ metadata.gz: 62c4a2fc2d4b598158af977fef23b0358f8b3aef93c8ff67c244fc4496a5273f
4
+ data.tar.gz: 64de5a4c83da20b04103b9b1dd6c81892d34947b7f43740fd6b7b2e6760aaaa4
5
5
  SHA512:
6
- metadata.gz: 62964f6afeec1cfb09c298c50f468ffeaf96d2bbce43ecd39f4a0a56a693b04ef3d705db0cb236f5b0718a4aa3d393bdea6257beac4d9ebcf39aa3c5eeff43f6
7
- data.tar.gz: f49fdd271da951848b7190aca2497d097b9b7d1cb54d5055d2d0580604735132624a819a2f931b3f2479667d35e66ac0a6ea722957a95e8a0958a180267373e7
6
+ metadata.gz: 849514cd5fd5d144d472f961b5467cd7abfa62f483f2d702e6b7366afaf4bc492e423d007f2b2f9c54b9acafc7dc929ff314c40fe090c8eb8381432571dcc919
7
+ data.tar.gz: 9180bb1e396cc44b844d3703ad3e79bdfb9dcebd80cd38ed67915615858d3059ae287903f43a0ef509b10704d28361393f54520d67426f5e3980e707396f2071
data/README.md CHANGED
@@ -22,29 +22,28 @@ Or install it yourself as:
22
22
  Using this gem requires some understanding of the [Egauge API](https://www.egauge.net/docs/egauge-xml-api.pdf).
23
23
 
24
24
  ### Basic Query
25
-
26
-
27
-
25
+ Queries return response objects that can be interacted with. More details on the response object are found below.
28
26
  ```ruby
27
+ require 'egauge'
29
28
  client = Egauge::Client.new('http://egaugeurl.egaug.es/12345')
30
29
  # This nil is necessary because the query structure is 'h&n=24'
31
- client.query('cgi-bin/egauge-show', :h => nil, :n => 24, :f => 1522800000)
30
+ client.query(:h => nil, :n => 24, :f => 1522800000)
32
31
  ```
33
32
  ### Helper query
34
- There are helpers to query data for you without having to craft queries
35
-
33
+ There are helpers to query data for you without having to craft queries. These also return response objects.
36
34
  ```ruby
35
+ require 'egauge'
37
36
  client = Egauge::Client.new('http://egaugeurl.egaug.es/12345')
38
37
  client.full_day_kwh
39
- {:solar => 432, :solar2 => 486}
40
38
  ```
41
39
 
42
40
  ### Egauge response object
43
- Basic queries will return a response object. That object will have reader methods for each header that will return the rows of that header.
41
+ Queries will return a response object. That object will have reader methods for each header that will return the rows of that header.
44
42
 
45
43
  If you're unsure what your headers are, there is a method you can use to find out!
46
44
 
47
45
  ```ruby
46
+ require 'egauge'
48
47
  client = Egauge::Client.new('http://egaugeurl.egaug.es/12345')
49
48
  response = client.query('cgi-bin/egauge-show', :h => nil, :n => 24, :f => 1522800000)
50
49
 
data/lib/egauge/client.rb CHANGED
@@ -1,19 +1,28 @@
1
1
  require 'httpclient'
2
- require 'uri'
3
2
 
4
3
  module Egauge
5
4
  class Client
6
5
  include Egauge::PowerHelpers
7
6
 
7
+ EGAUGE_PATH = '/cgi-bin/egauge-show?'.freeze
8
+
8
9
  def initialize(base_url)
9
10
  @client = HTTPClient.new(base_url: base_url)
10
11
  end
11
12
 
12
- def query(endpoint, options = {})
13
- Egauge::Response.new(client.get(endpoint, options).body)
13
+ def query(options = {})
14
+ Egauge::Response.new(client.get(EGAUGE_PATH + query_string(options)).body)
14
15
  end
15
16
 
16
17
  private
17
18
  attr_reader :client
19
+
20
+ def query_string(options)
21
+ options.stringify_keys.map do |key, value|
22
+ query_parameter = key.dup
23
+ query_parameter << "=#{value.to_s}" unless value.nil?
24
+ query_parameter
25
+ end.join('&')
26
+ end
18
27
  end
19
28
  end
@@ -2,23 +2,20 @@ module Egauge
2
2
  module PowerHelpers
3
3
  KWH_DIVIDER = 3600000
4
4
 
5
- def full_day_kwh
6
- full_day_kwh = {}
7
- response = query("/cgi-bin/egauge-show?#{yesterday_query}")
8
- response.headers.each do |header|
9
- full_day_kwh[header] = calculate_kwh(response.send(header))
10
- end
11
- full_day_kwh
5
+ def full_day_kwh(day = Time.zone.now.end_of_day.to_i)
6
+ query(:C => nil, :h => nil, :n => 24, :f => day)
12
7
  end
13
8
 
14
- private
9
+ def monthly_kwh(number_of_months = 12)
10
+ query(:C => nil, :T => months(number_of_months))
11
+ end
15
12
 
16
- def calculate_kwh(power_output)
17
- (power_output.first - power_output.last) / KWH_DIVIDER
18
- end
13
+ private
19
14
 
20
- def yesterday_query
21
- "h&n=24&f=#{Date.yesterday.end_of_day.to_i}"
15
+ def months(number_of_months)
16
+ number_of_months.times.map do |number|
17
+ number.months.ago.beginning_of_month.to_i
18
+ end.unshift(Time.current.to_i).join(',')
22
19
  end
23
20
  end
24
21
  end
@@ -1,8 +1,9 @@
1
1
  require 'nokogiri'
2
- require 'ostruct'
3
2
 
4
3
  module Egauge
5
4
  class Response
5
+ KWH_DIVIDER = 3600000
6
+
6
7
  def initialize(body)
7
8
  @body = Nokogiri.parse(body)
8
9
  load_power_output
@@ -17,7 +18,7 @@ module Egauge
17
18
  attr_reader :body
18
19
 
19
20
  def column_content(header_index)
20
- rows.map { |row| row.css("c").children[header_index].text.to_i }
21
+ rows.map { |row| (row.css("c").children[header_index].text.to_i / KWH_DIVIDER).abs }
21
22
  end
22
23
 
23
24
  def rows
@@ -1,3 +1,3 @@
1
1
  module Egauge
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: egauge-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joey Ferguson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-04-11 00:00:00.000000000 Z
11
+ date: 2018-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httpclient
@@ -152,7 +152,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
152
152
  version: '0'
153
153
  requirements: []
154
154
  rubyforge_project:
155
- rubygems_version: 2.6.12
155
+ rubygems_version: 2.7.6
156
156
  signing_key:
157
157
  specification_version: 4
158
158
  summary: Ruby client for egauge inverter