alpha_ess 1.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: 68832aefca51b56b3377029413e5e45c0becbb9cc013c4f4bfe26fc0d34f4b72
4
+ data.tar.gz: '032388588e713ebaf563c051cf3908e43e8590466b090d8bb28e7ad3a68dcbb3'
5
+ SHA512:
6
+ metadata.gz: cc3b2139df295df17edb54869d87bf8ff60c7c9b3cb03811ecc8af7ac1d9b87af748ba5d1608b62555eb3b5cac445e7d5e3a448d2ec07970295bfc30e30f7c30
7
+ data.tar.gz: 8abb15e9dfe30b362b8870c55032171b2fac00a87ff14fe7d25d465aa29d6f17c3923bfcab3f1ccd5c70efb350019fea41299e3a9e9a040f3730fce16c474bd4
data/LICENSE.md ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2022 Oliver Gaida
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,144 @@
1
+ # Was haben wir hier?
2
+
3
+ API-Calls für Alpha Ess
4
+
5
+ Es gibt keine öffentliche Dokumentation zur aktuellen API von Alpha Ess. Ich habe den Netzwerkverkehr meines Browsers etwas unter die Lupe genommen und hier die entscheidenen Request nachgebaut.
6
+
7
+ # Abgrenzung
8
+
9
+ Ich übernehme keinerlei Haftung für die Verwendung dieses Ruby-Modules. Das Modul ist selbstgebaut und ist nur rudimentär getestet. Also alles komplett auf eigenes Risiko.
10
+
11
+ # Installation
12
+
13
+ ```bash
14
+ gem install alpha_ess
15
+ ```
16
+
17
+ # Verwendung
18
+
19
+ ## Umgebunsvariablen
20
+
21
+ Die verwendeten Umgebungsvariablen am besten in ein Textfile schreiben, wodrin die Variablen exportiert werden und dann das Textfile in der bash sourcen.
22
+
23
+ Vor der Ausführung des Ruby-Scriptes sollten folgende Umgebungsvariablen gesetzt werden:
24
+
25
+ ### für Alpha Ess Api
26
+
27
+ ess_serial, ess_username und ess_password
28
+
29
+ Das Textfile mit dem Namen `ess_env` könnte also wie folgt aussehen:
30
+
31
+ ```
32
+ export ess_serial=ABC000123
33
+ export ess_username=meine@email.de
34
+ export ess_password=mein_password
35
+ ```
36
+
37
+ dann source mit
38
+
39
+ ```
40
+ . ./ess_env
41
+ ```
42
+
43
+ ### für optionale pushover notifications
44
+
45
+ po_user und po_token , beides über pushover Website einstellen. Pushover App gibt es für Smartphones und Tablets für einmalig 5 €.
46
+
47
+ Das Textfile mit dem Namen `po_env` könnte also wie folgt aussehen:
48
+
49
+ ```
50
+ export po_token=sdfisdfoiodisosdifs
51
+ export po_user=dflgkdlkjneofifslkdnv
52
+ ```
53
+
54
+ dann source mit
55
+
56
+ ```
57
+ . ./po_env
58
+ ```
59
+
60
+ ### Ablauf
61
+
62
+ Vor dem Start des Scripts beide Env-Files sourcen und dann das Ruby-Script mit dem gewünschten Code ausführen.
63
+
64
+ ```bash
65
+ . ./ess_env
66
+ . ./po_env
67
+ ./a_ess_script.rb
68
+ ```
69
+
70
+ Alternativ kann man die Variablen auch direkt im Rubyscript setzen und die Funktionen damit aufrufen:
71
+
72
+ ```ruby
73
+ ae = AlphaEss.new(ess_serial, ess_username, ess_password)
74
+ ...
75
+ ae.send_pushover_alarm_by_soc("Nachrichtentext ...", po_user, po_token)
76
+ ```
77
+
78
+ ## Datenstruktur selbst erkunden
79
+
80
+ Wenn ihr wissen wollt, was die einzelnen Funktionen zurückgeben und wo ihr welche Daten findet, dann probiert es einfach aus. Ich werde sicherlich bald ein youtube Video dazu machen, wo ich das vorführe.
81
+
82
+ Folgende 3 Funktionen für das Abholen von Daten gibt es derzeit:
83
+
84
+ - get_stics_by_day, bringt statische Daten des Tages
85
+ - get_last_power_data, bringt aktuelle Betriebsdaten (momentaner Verbrauch / Erzeugung etc.)
86
+ - get_custom_use_ess_setting, holt Daten zur aktuellen Systemeinstellung
87
+
88
+ ## aktuellen Status abfragen
89
+
90
+ Die Daten über `/api/ESS/GetLastPowerDataBySN` werden alle 10 Sekunden von der API neu bereitgestellt.
91
+
92
+ ```ruby
93
+ load "alpha_ess.rb" # require "alpha_ess"
94
+ ae = AlphaEss.new
95
+ data = ae.get_last_power_data()
96
+ netz = data["pmeter_l1"].to_f + data["pmeter_l2"].to_f + data["pmeter_l3"].to_f
97
+
98
+ puts "
99
+
100
+ Batteriestatus: #{data["soc"]}
101
+ PV-Leistung: #{data["ppv1"]}
102
+ Netzbezug / Einspeisung: #{netz}
103
+ Batteriebezug / Ladung: #{data["pbat"]}
104
+ Verbrauch: #{data["pbat"] + data["ppv1"].to_f + netz}
105
+
106
+
107
+ "
108
+ ```
109
+
110
+ Hier die Ausgabe und der Vergleich zur Website:
111
+
112
+ ![picture 1](images/b965404834760626afe73785811995faa1629d2ab66a53f966443fbf22463a67.png)
113
+
114
+
115
+ ## pushover Alarm auslösen, wenn die PV-Anlage einen speziellen Status hat
116
+
117
+ ```ruby
118
+ load "alpha_ess.rb"
119
+ ae = AlphaEss.new
120
+ ae_data = ae.get_last_power_data()
121
+ batterie_ladung_erwartet = 40
122
+ if (ae_data["soc"].to_f < soc_expexcted)
123
+ ae.send_pushover_alarm_by_soc "Die Batterie hat aktuell weniger als #{soc_expexcted} % Ladung!"
124
+ end
125
+ # ae.set_min_soc(((ae_data["soc"].to_f)+0.9).to_i)
126
+ # sleep 10
127
+ min_netzbezug_erwartet = 50 # Watt
128
+ aktueller_netzbezug = ae_data["pmeter_l1"].to_f + ae_data["pmeter_l2"].to_f + ae_data["pmeter_l3"].to_f
129
+ if (aktueller_netzbezug < min_netzbezug_erwartet)
130
+ ae.send_pushover_alarm_by_soc "Stromausfall ???"
131
+ end
132
+ ```
133
+
134
+ ## update ESS Settings
135
+
136
+ ```ruby
137
+ load "alpha_ess.rb"
138
+ ae = AlphaEss.new
139
+ bat_min = ae.get_custom_use_ess_setting()["bat_use_cap"]
140
+ puts "Batterie-Minimum: #{bat_min}"
141
+ ae.set_custom_use_ess_setting({"bat_use_cap" => 15})
142
+ bat_min = ae.get_custom_use_ess_setting()["bat_use_cap"]
143
+ puts "Batterie-Minimum neu: #{bat_min}"
144
+ ```
data/alpha_ess.gemspec ADDED
@@ -0,0 +1,15 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = 'alpha_ess'
3
+ spec.version = '1.0'
4
+ spec.date = '2022-10-02'
5
+ spec.summary = "Alpha Ess API Calls"
6
+ spec.description = "a Ruby class for Alpha-Ess-API-Calls"
7
+ spec.authors = ["Oliver Gaida"]
8
+ spec.email = 'ogaida@t-online.de'
9
+ spec.files = `git ls-files`.split($/)
10
+ spec.homepage = 'https://github.com/ogaida/alpha_ess'
11
+ #spec.executables = %w(rusdc)
12
+ spec.add_runtime_dependency 'httparty'
13
+ #spec.add_runtime_dependency 'json', '~> 2.1', '>= 2.1.0'
14
+ spec.license = 'MIT'
15
+ end
data/lib/alpha_ess.rb ADDED
@@ -0,0 +1,107 @@
1
+ require "httparty"
2
+
3
+ class AlphaEss
4
+
5
+ def initialize(sn = ENV["ess_serial"], u = ENV["ess_username"], p = ENV["ess_password"])
6
+ @serial, @username, @password = sn, u, p
7
+ get_token()
8
+ end
9
+
10
+ def get_stics_by_day()
11
+ today = Time.now.strftime("%Y-%m-%d")
12
+ body = {
13
+ "sn" => @serial,
14
+ "userId" => @serial,
15
+ "szDay" => today,
16
+ "isOEM" => 0,
17
+ "sDate" => today
18
+ }
19
+ url = "https://cloud.alphaess.com/api/Power/SticsByDay"
20
+ res = HTTParty.post(url, headers: header(), body: body.to_json)
21
+ res.parsed_response["data"]
22
+ end
23
+
24
+ def get_last_power_data()
25
+ body = {
26
+ "sys_sn" => @serial,
27
+ "noLoading" => true
28
+ }
29
+ url = "https://cloud.alphaess.com/api/ESS/GetLastPowerDataBySN"
30
+ res = HTTParty.post(url, headers: header(), body: body.to_json)
31
+ res.parsed_response["data"]
32
+ end
33
+
34
+ def get_custom_use_ess_setting()
35
+ url = "https://cloud.alphaess.com/api/Account/GetCustomUseESSSetting"
36
+ res = HTTParty.get(url, headers: header())
37
+ res.parsed_response["data"]
38
+ end
39
+
40
+ def set_custom_use_ess_setting(hash={})
41
+ data = self.get_custom_use_ess_setting
42
+ data.update(hash)
43
+ url = "https://cloud.alphaess.com/api/Account/CustomUseESSSetting"
44
+ res = HTTParty.post(url, headers: header(), body: data.to_json)
45
+ end
46
+
47
+ def send_pushover_alarm_by_soc(message, po_user = ENV["po_user"], po_token = ENV["po_token"])
48
+ # po_user: pushover username
49
+ # po_token: pushover token
50
+ body = {
51
+ "user" => po_user,
52
+ "token" => po_token,
53
+ "message" => message
54
+ }
55
+ url = "https://api.pushover.net/1/messages.json"
56
+ res = HTTParty.post(url, headers: {
57
+ "Content-Type" => "application/json;charset=UTF-8"
58
+ }, body: body.to_json)
59
+ end
60
+
61
+ private
62
+
63
+ def get_token()
64
+ Dir.mkdir "#{ENV["HOME"]}/.alpha_ess" if ! (Dir.exists?"#{ENV["HOME"]}/.alpha_ess")
65
+ read_token() if File.exists?"#{ENV["HOME"]}/.alpha_ess/token"
66
+ if !(defined?@token_valid_to) or @token_valid_to.to_i < (Time.now.to_i+10)
67
+ create_token()
68
+ end
69
+ end
70
+
71
+ def save_token()
72
+ cmd = %(echo "#{@token_valid_to}:#{@token}" > #{ENV["HOME"]}/.alpha_ess/token)
73
+ `#{cmd}`
74
+ end
75
+
76
+ def read_token()
77
+ token_valid_to, @token = File.open("#{ENV["HOME"]}/.alpha_ess/token").readline.chop.split(/:/)
78
+ @token_valid_to = token_valid_to.to_i
79
+ end
80
+
81
+ def create_token()
82
+ url = "https://cloud.alphaess.com/api/Account/Login"
83
+ body = {
84
+ "username" => @username,
85
+ "password" => @password
86
+ }
87
+ res = HTTParty.post(url, headers: {
88
+ "Accept" => "application/json",
89
+ "Content-Type" => "application/json;charset=UTF-8"
90
+ }, body: body.to_json)
91
+ @token = res.parsed_response["data"]["AccessToken"]
92
+ @token_valid_to = Time.now.to_i+36000
93
+ save_token()
94
+ end
95
+
96
+ def header(hash={})
97
+ get_token()
98
+ hash = {
99
+ "Accept" => "application/json",
100
+ "Content-Type" => "application/json;charset=UTF-8",
101
+ "Authorization" => "Bearer #{@token}"
102
+ }.update hash
103
+ hash
104
+ end
105
+ end
106
+
107
+
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alpha_ess
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Oliver Gaida
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-10-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: a Ruby class for Alpha-Ess-API-Calls
28
+ email: ogaida@t-online.de
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - LICENSE.md
34
+ - README.md
35
+ - alpha_ess.gemspec
36
+ - images/b965404834760626afe73785811995faa1629d2ab66a53f966443fbf22463a67.png
37
+ - lib/alpha_ess.rb
38
+ homepage: https://github.com/ogaida/alpha_ess
39
+ licenses:
40
+ - MIT
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubygems_version: 3.0.3.1
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Alpha Ess API Calls
61
+ test_files: []