Last active
September 17, 2022 06:09
-
-
Save ogaida/61316e9e1dbb869e111b3c7ba4d87ef4 to your computer and use it in GitHub Desktop.
Alpha Ess Monitoring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require "json"; | |
class Ress | |
attr_reader :eload, :feedin, :gridcharge, :ppv, :soc, :userpower, :measure_time, :token_valid_to, :token, :now_s, :now_str | |
def initialize(sn, u, p) | |
p = get_json(sn, u, p) | |
i = p["LasIdx"] | |
@measure_time = p["Time"][i] | |
@now_str = Time.now.strftime("%Y-%m-%d %T") | |
@now_s = Time.now.to_i | |
@eload = p["ELoad"] | |
@feedin = p["FeedIn"][i] # aktuell eingespeist | |
@gridcharge = p["GridCharge"][i] # aktuell vom E-Werk | |
@ppv = p["Ppv"][i] # aktuell Erzeugung | |
@soc = p["Soc"] # akt. Ladestand der Batt. | |
@userpower = p["UsePower"][i] # aktueller Stromverbrauch | |
end | |
private | |
attr_writer :token_valid_to, :token | |
def get_token(u, p) | |
Dir.mkdir "#{ENV["HOME"]}/.ress" if ! (Dir.exists?"#{ENV["HOME"]}/.ress") | |
read_token() if File.exists?"#{ENV["HOME"]}/.ress/token" | |
if !(defined?@token_valid_to) or @token_valid_to.to_i < (Time.now.to_i+10) | |
create_token(u, p) | |
end | |
end | |
def save_token() | |
cmd = %(echo "#{@token_valid_to}:#{@token}" > #{ENV["HOME"]}/.ress/token) | |
`#{cmd}` | |
end | |
def read_token() | |
token_valid_to, @token = File.open("#{ENV["HOME"]}/.ress/token").readline.chop.split(/:/) | |
@token_valid_to = token_valid_to.to_i | |
end | |
def create_token(u, p) | |
cmd = %(curl -s 'https://cloud.alphaess.com/api/Account/Login' ) + | |
%(-X POST -H 'User-Agent: Mozilla/5.0' ) + | |
%(-H 'Accept: application/json' ) + | |
%(-H 'Accept-Language: de,en-US;q=0.7,en;q=0.3' ) + | |
%(-H 'Accept-Encoding: gzip, deflate, br' ) + | |
%(-H 'Content-Type: application/json;charset=UTF-8' ) + | |
%(-H 'Origin: https://cloud.alphaess.com' ) + | |
%(-H 'Referer: https://cloud.alphaess.com/login?redirect=%2Fdashboard' ) + | |
%(-H 'Sec-Fetch-Dest: empty' ) + | |
%(-H 'Sec-Fetch-Mode: cors' ) + | |
%(-H 'Sec-Fetch-Site: same-origin' ) + | |
%(-H 'Pragma: no-cache' ) + | |
%(-H 'Cache-Control: no-cache' ) + | |
%(-H 'TE: trailers' ) + | |
%(--data-raw '{"username":"#{u}","password":"#{p}"}') | |
@token = JSON.parse(`#{cmd}`)["data"]["AccessToken"] | |
@token_valid_to = Time.now.to_i+36000 | |
save_token() | |
end | |
def get_json(sn, u, p) | |
get_token(u, p) | |
today = Time.now.strftime("%Y-%m-%d") | |
cmd = %(curl -s 'https://cloud.alphaess.com/api/Power/SticsByDay' ) + | |
%(-X POST ) + | |
%(-H 'User-Agent: Mozilla/5.0' ) + | |
%(-H 'Accept: application/json' ) + | |
%(-H 'Accept-Encoding: gzip, deflate, br' ) + | |
%(-H 'Content-Type: application/json;charset=UTF-8' ) + | |
%(-H 'Authorization: Bearer #{@token}' ) + | |
%(-H 'Origin: https://cloud.alphaess.com' ) + | |
%(-H 'Referer: https://cloud.alphaess.com/dashboard' ) + | |
%(-H 'Sec-Fetch-Dest: empty' ) + | |
%(-H 'Sec-Fetch-Mode: cors' ) + | |
%(-H 'Sec-Fetch-Site: same-origin' ) + | |
%(-H 'TE: trailers' ) + | |
%(--data-raw '{"sn":"#{sn}","userId":"#{sn}","szDay":"#{today}","isOEM":0,"sDate":"#{today}"}') | |
json = `#{cmd}` | |
JSON.parse(json)["data"] | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
load "/home/ogaida/pv/ress.rb" | |
csvfile = ARGV[0] | |
if defined?csvfile | |
sn = "ALB12345678" | |
username = "xy" | |
password = "z" | |
e = Ress.new(username, password) | |
cmd = %(echo measure_time,time,epoche,feedin,gridcharge,ppv,soc,userpower > #{csvfile}) | |
`#{cmd}` if ! File.exists?csvfile | |
cmd = %(echo #{e.measure_time},#{e.now_str},#{e.now_s},#{e.feedin},#{e.gridcharge},#{e.ppv},#{e.soc},#{e.userpower} >> #{csvfile}) | |
`#{cmd}` | |
else | |
puts "specify a csvfile please" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Beispiel: