acf-openpdu/acf-app/openpdu-model.lua

68 lines
1.7 KiB
Lua
Raw Normal View History

2018-02-05 08:47:07 +00:00
local mymodule = {}
posix = require("posix")
fs = require("acf.fs")
json = require('cjson')
modelfunctions = require("modelfunctions")
2018-02-05 08:47:07 +00:00
local outletsConfigfile = "/etc/openpdu/outlets.conf"
local boardsConfigfile = "/etc/openpdu/boards.conf"
local s = modelfunctions.run_executable({"/usr/bin/openpdu", "outlets", "-j"})
local o = json.decode(s)
local outlets = {}
2018-02-05 08:47:07 +00:00
for Index, Value in pairs( o.outlets ) do
if (Value.powerstatus) then
s = "On"
else
s = "Off"
end
outlets[Value.outlet+1] = {outlet=Value.outlet,status=s,description=Value.description,actions={"on","off"}}
2018-02-05 08:47:07 +00:00
end
mymodule.status = function()
return cfe({ type="structure", value=outlets, label="Outlets" })
end
mymodule.setpower = function(outlet, power, ret)
return modelfunctions.run_executable({"/usr/bin/openpdu", "setpower", outlet, power})
2018-02-05 08:47:07 +00:00
end
mymodule.get_setpower = function(self, clientdata)
local outlet = cfe({label="Outlet", value=clientdata.outlet or ""})
return cfe({ type="group", label="Set Outlet Power state", value={outlet=outlet} })
2018-02-05 08:47:07 +00:00
end
mymodule.setpoweron = function(self, r)
mymodule.setpower(r.value.outlet.value, 1, r)
2018-02-05 08:47:07 +00:00
return r
end
mymodule.setpoweroff = function(self, r)
mymodule.setpower(r.value.outlet.value, 0, r)
2018-02-05 08:47:07 +00:00
return r
end
function mymodule.get_outletsFiledetails()
return modelfunctions.getfiledetails(outletsConfigfile)
end
function mymodule.update_outletsFiledetails(self, filedetails)
return modelfunctions.setfiledetails(self, filedetails, {outletsConfigfile})
end
function mymodule.get_boardsFiledetails()
return modelfunctions.getfiledetails(boardsConfigfile)
end
function mymodule.update_boardsFiledetails(self, filedetails)
return modelfunctions.setfiledetails(self, filedetails, {boardsConfigfile})
end
2018-02-05 08:47:07 +00:00
return mymodule