Questions or tips to improve the script, I'd love to hear them
Code: Select all
-- Created by Raymond Wiertz
-- DateTime: 2020-02-13
-- Tracking your token
-- To find out your token, you must execute the CURL command below from the command line.
-- curl -d 'account=[LOGINNAME]&pwd=[PASSWORD]&code=' https://www.semsportal.com/Home/Login;
local UserName = ""
local Password = ""
local token = ""
local refresh_time = 180 -- secondens
local sensorSolarPanels = "solar panels" -- name of your
-- get login token
local loginCommand = string.format("curl --silent --output /dev/null --cookie-jar - -c cookie.txt -d 'account=%s&pwd=%s&code=' https://www.semsportal.com/Home/Login",UserName,Password)
-- get data from site
local getDataCommand = string.format("curl -b cookie.txt -d 'str={\"api\":\"v1/PowerStation/GetMonitorDetailByPowerstationId\",\"param\":{\"powerStationId\":\"%s\"}}&s=v1/PowerStation/GetMonitorDetailByPowerstationId' 'https://www.semsportal.com//GopsApi/Post?s=v1/PowerStation/GetMonitorDetailByPowerstationId'",token)
commandArray = {}
i=1
function isempty(s)
return s == nil or s == ''
end
function getExecuteCommand(command)
local f = assert(io.popen(command, 'r'))
s = assert(f:read('*a'))
f:close()
return s
end
function stripchars(str, chrs)
local s = str:gsub("["..chrs:gsub("%W","%%%1").."]", '')
return s
end
function getValue(value, searchString)
local response = ""
searchString = string.format("%q", searchString)
dummy, startPos = string.find(value, searchString )
if not(startPos == nil) then
dummy, startPos = string.find(value, searchString )
dummy, endPos = string.find(value, ",", startPos)
response = string.sub(value, startPos, endPos-1)
response = string.gsub(response, ":", "")
response = string.gsub(response, "\"", "")
response = string.gsub(response, " ", "")
end
return response
end
function timedifference(s)
year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)
t1 = os.time()
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes,sec=seconds}
difference = os.difftime (t1, t2)
return difference
end
function UpdateDevice(device, data)
idx = otherdevices_idx[device]
if (idx == nil) then
print('** Unknown device'..device)
else
commandArray[i] = {['UpdateDevice'] = idx..'|0|'..data}
i = i+1
end
end
difference = timedifference(otherdevices_lastupdate[sensorSolarPanels])
if (difference > refresh_time) then
getExecuteCommand(loginCommand)
local data = getExecuteCommand(getDataCommand)
--print(data)
if (isempty(data) == false) then
output_power = getValue(data, "output_power")
last_refresh_time = getValue(data, "last_refresh_time")
eDay = getValue(data, "eDay")
print("last_refresh_time :" .. last_refresh_time)
print("output_power :" .. output_power)
print("eDay :" .. eDay)
current = tonumber(stripchars(output_power, "kwhKWH"))
dayTotal = tonumber(eDay)*1000
UpdateDevice(sensorSolarPanels, current..";"..dayTotal)
end
end
return commandArray
hopefully the script will continue to work in the near future.
hopefully the script will continue to work in the near future.
Have fun with it.