sunnycarcharger-agent 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 300f7e97fe554caf8db90f6c9cb4f428ffbff5648b5cd5b0dfee1da5fcd1bff6
4
+ data.tar.gz: 6ccfa0cbf5bd490af1905231fe0ccbdd3fa127a0a8053d6a6b93752478edf36c
5
+ SHA512:
6
+ metadata.gz: 1a308ac0eb698c3f9bf2453ca259045816a9583303e2693a68f1972a615174ef1a1b46d3717442b1f17a631724e6441166870dc2882e632f29367de89ae691e0
7
+ data.tar.gz: ace0fe2c2323c3fa8b5030924b09041ce2d32e8ef2e12041e0649fff781aaf66513af66c27e515e1ed8a3f2457f37e502660b613b34352a72313b790798cc522
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Dr Nic Williams
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Sunnycarcharger::Agent
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/sunnycarcharger/agent`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/sunnycarcharger-agent. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/sunnycarcharger-agent/blob/develop/CODE_OF_CONDUCT.md).
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
36
+
37
+ ## Code of Conduct
38
+
39
+ Everyone interacting in the Sunnycarcharger::Agent project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/sunnycarcharger-agent/blob/develop/CODE_OF_CONDUCT.md).
data/bin/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "sunnycarcharger/agent"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ require "irb"
11
+ IRB.start(__FILE__)
data/bin/docker-buildx ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+
3
+ ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
4
+ cd $ROOT
5
+
6
+ # On MacOS Docker Desktop turn on "Use containerd for pulling and storing images" experimental feature
7
+ docker buildx build --platform linux/arm/v7,linux/arm64/v8,linux/amd64 -t drnic/sunnycarcharger-agent .
data/bin/docker-push ADDED
@@ -0,0 +1,6 @@
1
+ #!/bin/bash
2
+
3
+ ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
4
+ cd $ROOT
5
+
6
+ docker push drnic/sunnycarcharger-agent
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "sunnycarcharger/agent"
4
+
5
+ puts "Fronius IP address: #{Config.fronius_ip}"
6
+ puts "Web URL: #{Config.web_url}"
7
+
8
+ startup_errors = false
9
+
10
+ fronius_client = Sunnycarcharger::Agent::Fronius.new
11
+ unless fronius_client.connected?
12
+ warn "ERROR: Fronius #{Config.fronius_ip} could not be connected"
13
+ startup_errors = true
14
+ end
15
+ web_client = Sunnycarcharger::Agent::Web.new
16
+ unless web_client.connected?
17
+ warn "ERROR: Web #{Config.web_url} could not be connected"
18
+ startup_errors = true
19
+ end
20
+ exit 1 if startup_errors
21
+
22
+ # Loop and sleep for Config.refresh_interval minutes
23
+ loop do
24
+ begin
25
+ invertor_data = fronius_client.fetch
26
+ rescue Faraday::ConnectionFailed, Errno::ECONNREFUSED
27
+ warn "ERROR: Fronius #{Config.fronius_ip} could not be connected"
28
+ end
29
+
30
+ begin
31
+ web_client.publish(invertor_data) if invertor_data
32
+ rescue Faraday::ConnectionFailed, Errno::ECONNREFUSED
33
+ warn "ERROR: Web #{Config.web_url} could not be connected"
34
+ end
35
+
36
+ sleep Config.refresh_interval.to_i * 60
37
+ end
@@ -0,0 +1,19 @@
1
+ require "anyway_config"
2
+
3
+ class AgentConfig < Anyway::Config
4
+ config_name ""
5
+ attr_config web_url: "https://sunnycarcharger.com/"
6
+ attr_config :web_token
7
+
8
+ attr_config refresh_interval: 1 # minutes
9
+
10
+ attr_config :fronius_ip
11
+
12
+ extend_options do |parser, config|
13
+ parser.on_tail "-h", "--help" do
14
+ puts parser
15
+ end
16
+ end
17
+ end
18
+
19
+ Config = AgentConfig.new
@@ -0,0 +1,39 @@
1
+ require "faraday"
2
+
3
+ class Sunnycarcharger::Agent::Fronius
4
+ def initialize(ip: Config.fronius_ip)
5
+ raise ArgumentError, "Missing Fronius IP address" unless ip
6
+ @ip = ip
7
+ end
8
+
9
+ def client
10
+ @client ||= Faraday.new(
11
+ "http://" + @ip,
12
+ headers: {"User-Agent" => "sunnycarcharger-agent"}
13
+ ) do |conn|
14
+ conn.request :json
15
+ conn.response :json
16
+ conn.response :raise_error
17
+ # conn.request :retry, retry_options if retry_options # Must be registered after :raise_error
18
+ conn.adapter Faraday.default_adapter
19
+ conn.options.timeout = 1
20
+ end
21
+ end
22
+
23
+ def fetch
24
+ path = "/solar_api/v1/GetPowerFlowRealtimeData.fcgi"
25
+ response = client.get(path).body
26
+ data = Sunnycarcharger::Agent::InvertorData.new
27
+ data.power_from_grid = response.dig("Body", "Data", "Site", "P_Grid").to_i
28
+ data.power_to_load = -1 * response.dig("Body", "Data", "Site", "P_Load").to_i
29
+ data.power_from_solar = response.dig("Body", "Data", "Site", "P_PV").to_i
30
+ data
31
+ end
32
+
33
+ def connected?
34
+ fetch
35
+ true
36
+ rescue Faraday::ConnectionFailed, Errno::ECONNREFUSED
37
+ false
38
+ end
39
+ end
@@ -0,0 +1,15 @@
1
+ class Sunnycarcharger::Agent::InvertorData
2
+ attr_accessor :power_from_grid, :power_to_load, :power_from_solar
3
+
4
+ def power_to_grid
5
+ -power_from_grid
6
+ end
7
+
8
+ def as_json
9
+ {
10
+ power_from_grid: power_from_grid,
11
+ power_to_load: power_to_load,
12
+ power_from_solar: power_from_solar
13
+ }
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sunnycarcharger
4
+ module Agent
5
+ VERSION = "1.0.0"
6
+ end
7
+ end
@@ -0,0 +1,41 @@
1
+ require "faraday"
2
+
3
+ class Sunnycarcharger::Agent::Web
4
+ def initialize(url: Config.web_url, token: Config.web_token)
5
+ raise ArgumentError, "Missing web token" unless token
6
+ @url = url
7
+ @token = token
8
+ end
9
+
10
+ def client
11
+ @client ||= Faraday.new(
12
+ @url,
13
+ headers: {"User-Agent" => "sunnycarcharger-agent"}
14
+ ) do |conn|
15
+ conn.request :json
16
+ conn.response :json
17
+ conn.response :raise_error
18
+ # conn.request :retry, retry_options if retry_options # Must be registered after :raise_error
19
+ conn.adapter Faraday.default_adapter
20
+ conn.options.timeout = 2
21
+ end
22
+ end
23
+
24
+ def publish(invertor_data)
25
+ url = File.join(@url, "/api/v1/solar_charge_events")
26
+ data = {solar_charge_event: invertor_data}
27
+ client.post do |req|
28
+ req.url url
29
+ req.headers["Authorization"] = "token #{@token}"
30
+ req.body = data.to_json
31
+ end
32
+ end
33
+
34
+ def connected?
35
+ url = File.join(@url, "/api/v1/ping")
36
+ response = client.get(url)
37
+ response.status == 200
38
+ rescue Faraday::ConnectionFailed, Errno::ECONNREFUSED
39
+ false
40
+ end
41
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sunnycarcharger
4
+ module Agent
5
+ end
6
+ end
7
+
8
+ require_relative "agent/version"
9
+ require_relative "agent/config"
10
+ require_relative "agent/fronius"
11
+ require_relative "agent/web"
12
+ require_relative "agent/invertor_data"
@@ -0,0 +1,80 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: http://192.168.86.26/solar_api/v1/GetPowerFlowRealtimeData.fcgi
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ User-Agent:
11
+ - sunnycarcharger-agent
12
+ response:
13
+ status:
14
+ code: 200
15
+ message: OK
16
+ headers:
17
+ content-type:
18
+ - application/json
19
+ cache-control:
20
+ - no-cache, no-store, must-revalidate
21
+ pragma:
22
+ - no-cache
23
+ expires:
24
+ - '0'
25
+ content-length:
26
+ - '1124'
27
+ date:
28
+ - Sun, 09 Jul 2023 04:03:52 GMT
29
+ server:
30
+ - webserver
31
+ body:
32
+ encoding: UTF-8
33
+ string: |
34
+ {
35
+ "Body" : {
36
+ "Data" : {
37
+ "Inverters" : {
38
+ "1" : {
39
+ "DT" : 102,
40
+ "E_Day" : 20905,
41
+ "E_Total" : 39760000,
42
+ "E_Year" : 6499775.5,
43
+ "P" : 2798
44
+ },
45
+ "2" : {
46
+ "DT" : 102,
47
+ "E_Day" : 17696,
48
+ "E_Total" : 40577100,
49
+ "E_Year" : 6406383.5,
50
+ "P" : 3646
51
+ }
52
+ },
53
+ "Site" : {
54
+ "E_Day" : 38601,
55
+ "E_Total" : 80337100,
56
+ "E_Year" : 12906159,
57
+ "Meter_Location" : "grid",
58
+ "Mode" : "meter",
59
+ "P_Akku" : null,
60
+ "P_Grid" : -3599.85595703125,
61
+ "P_Load" : -2844.14404296875,
62
+ "P_PV" : 6444,
63
+ "rel_Autonomy" : 100,
64
+ "rel_SelfConsumption" : 44.136313515964467
65
+ },
66
+ "Version" : "12"
67
+ }
68
+ },
69
+ "Head" : {
70
+ "RequestArguments" : {},
71
+ "Status" : {
72
+ "Code" : 0,
73
+ "Reason" : "",
74
+ "UserMessage" : ""
75
+ },
76
+ "Timestamp" : "2023-07-09T14:03:52+10:00"
77
+ }
78
+ }
79
+ recorded_at: Sun, 09 Jul 2023 04:03:53 GMT
80
+ recorded_with: VCR 6.2.0
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe Sunnycarcharger::Agent::Fronius do
6
+ it "fetches from fronius" do
7
+ fronius_ip = "192.168.86.26"
8
+ agent = Sunnycarcharger::Agent::Fronius.new(ip: fronius_ip)
9
+ VCR.use_cassette("fronius-midday") do
10
+ data = agent.fetch
11
+
12
+ assert_equal(-3599, data.power_from_grid.to_i)
13
+ assert_equal(3599, data.power_to_grid.to_i)
14
+ assert_equal(6444, data.power_from_solar.to_i)
15
+ assert_equal(2844, data.power_to_load.to_i)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
4
+ require "sunnycarcharger/agent"
5
+
6
+ require "minitest/autorun"
7
+ require "minitest/spec"
8
+ require "vcr"
9
+
10
+ VCR.configure do |config|
11
+ config.cassette_library_dir = "test/fixtures/vcr_cassettes"
12
+ config.hook_into :faraday
13
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sunnycarcharger-agent
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Dr Nic Williams
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-07-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: anyway_config
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.4.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.4.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: standardrb
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: vcr
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Run agent within home network to get solar invertor data and upload to
84
+ Sunny Car Charger.
85
+ email:
86
+ - drnicwilliams@gmail.com
87
+ executables:
88
+ - sunnycarcharger-agent
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - LICENSE.txt
93
+ - README.md
94
+ - bin/console
95
+ - bin/docker-buildx
96
+ - bin/docker-push
97
+ - bin/setup
98
+ - exe/sunnycarcharger-agent
99
+ - lib/sunnycarcharger/agent.rb
100
+ - lib/sunnycarcharger/agent/config.rb
101
+ - lib/sunnycarcharger/agent/fronius.rb
102
+ - lib/sunnycarcharger/agent/invertor_data.rb
103
+ - lib/sunnycarcharger/agent/version.rb
104
+ - lib/sunnycarcharger/agent/web.rb
105
+ - test/fixtures/vcr_cassettes/fronius-midday.yml
106
+ - test/sunnycarcharger/agent/fronius_test.rb
107
+ - test/test_helper.rb
108
+ homepage: https://sunnycarcharger.com
109
+ licenses:
110
+ - MIT
111
+ metadata:
112
+ allowed_push_host: https://rubygems.org
113
+ homepage_uri: https://sunnycarcharger.com
114
+ source_code_uri: https://sunnycarcharger.com
115
+ changelog_uri: https://sunnycarcharger.com
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: 2.6.0
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubygems_version: 3.4.17
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Run agent within home network to get solar invertor data and upload to Sunny
135
+ Car Charger.
136
+ test_files: []