Trying to access data from a server that holds my solar inverter information. The login part goes fine, but my credentials are not saved when polling the server for the data, I output the headers to console and apparently the page redirects to a “not logged in”-page. I assume it has something to do with the session but I have no idea how to fix it.
local crypto = require("crypto")
local json = require("json")
local GrowattApi = {
server_url = "https://server.growatt.com/",
agent_identifier = "Dalvik/2.1.0 (Linux; U; Android 12; https://github.com/indykoning/PyPi_GrowattServer)",
}
local function hash_password(password)
local password_md5 = crypto.digest(crypto.md5, password)
for i=1, string.len(password_md5), 2 do
if string.sub(password_md5, i, i) == '0' then
password_md5 = string.sub(password_md5, 1, i-1) .. 'c' .. string.sub(password_md5, i+1)
end
end
return password_md5
end
function GrowattApi:new(add_random_user_id, agent_identifier)
local instance = {}
local add_random_user_id = true
if agent_identifier ~= nil then
instance.agent_identifier = agent_identifier
print "agent_identifier is nil"
else
instance.agent_identifier = self.agent_identifier
print "agent_identifier is not nil"
end
if add_random_user_id then
print "adding random user_id"
local random_number = ""
for i=1, 5 do
random_number = random_number .. tostring(math.random(0, 9))
end
instance.agent_identifier = instance.agent_identifier .. " - " .. random_number
end
instance.request_headers = {
["User-Agent"] = instance.agent_identifier,
}
setmetatable(instance, { __index = self })
return instance
end
function GrowattApi:get_url(endpoint)
local url = self.server_url .. endpoint
return url
end
function GrowattApi:login(username, password, is_password_hashed)
is_password_hashed = is_password_hashed or false
if not is_password_hashed then
password = hash_password(password)
end
local headers = {}
headers["Content-Type"] = "application/x-www-form-urlencoded"
local params = {
headers = headers,
body = "userName=" .. username .. "&password=" .. password,
}
network.request(
self:get_url('newTwoLoginAPI.do'),
'POST',
function(response)
if response.phase == "ended" and response.status == 200 then
local data = json.decode(response.response)["back"]
if data["success"] then
data["userId"] = data["user"]["id"]
data["userLevel"] = data["user"]["rightlevel"]
end
return data
else
print "there was no response to my login attempt :("
end
end,
params
)
end
local function networkListener(event)
if (event.isError) then
print("Network error:", event.response)
else
print("Response headers:", json.encode(event.responseHeaders))
local contentType = event.responseHeaders["Content-Type"]
if (contentType == "application/json") then
local response = json.decode(event.response)
-- handle JSON response here
print("JSON response:", response)
else
-- handle non-JSON response here
print("Unknown response type:", contentType)
end
end
end
function GrowattApi:tlx_detail(tlx_id)
print("tlx_id is", tlx_id)
local url = self:get_url('newTlxApi.do')
local params = {
op = 'getTlxDetailData',
id = tlx_id
}
local response = network.request(url, 'GET', networkListener, params)
local data = json.decode(response)
return data
end
return GrowattApi