v1.4. New module: mod_regular_script

This commit is contained in:
gSpot
2025-02-16 16:26:43 +03:00
parent 90b711f55e
commit dd46273f24
20 changed files with 210 additions and 40 deletions

View File

@@ -390,7 +390,7 @@ function InternetDetector:mainLoop()
mTimeDiff = 1
end
mLastTime = mTimeNow
e:run(currentStatus, lastStatus, mTimeDiff)
e:run(currentStatus, lastStatus, mTimeDiff, mTimeNow)
end
local modulesStatus = {}

View File

@@ -100,7 +100,7 @@ function Module:getCurrentState()
end
end
function Module:run(currentStatus, lastStatus, timeDiff)
function Module:run(currentStatus, lastStatus, timeDiff, timeNow)
if not self._enabled then
return
end

View File

@@ -76,7 +76,7 @@ function Module:init(t)
end
end
function Module:run(currentStatus, lastStatus, timeDiff)
function Module:run(currentStatus, lastStatus, timeDiff, timeNow)
if currentStatus == 1 then
if self.attempts == 0 or self._attemptsCounter < self.attempts then
if self._deadCounter >= self.deadPeriod then

View File

@@ -359,7 +359,7 @@ function Module:init(t)
self._enabled = true
end
function Module:run(currentStatus, lastStatus, timeDiff)
function Module:run(currentStatus, lastStatus, timeDiff, timeNow)
if not self._enabled then
return
end

View File

@@ -34,7 +34,7 @@ function Module:init(t)
end
end
function Module:run(currentStatus, lastStatus, timeDiff)
function Module:run(currentStatus, lastStatus, timeDiff, timeNow)
if currentStatus == 1 then
if self._deadCounter >= self.deadPeriod then
self:rebootDevice()

View File

@@ -0,0 +1,55 @@
local stdlib = require("posix.stdlib")
local unistd = require("posix.unistd")
local Module = {
name = "mod_regular_script",
runPrio = 90,
config = {},
syslog = function(level, msg) return true end,
writeValue = function(filePath, str) return false end,
readValue = function(filePath) return nil end,
inetState = 2, -- 0: connected, 1: disconnected, 2: both
interval = 3600,
script = "",
status = nil,
_nextTime = nil,
}
function Module:runExternalScript(scriptPath, currentStatus)
if unistd.access(scriptPath, "r") then
stdlib.setenv("INET_STATE", currentStatus)
os.execute(string.format('/bin/sh "%s" &', scriptPath))
end
end
function Module:init(t)
if t.inet_state ~= nil then
self.inetState = tonumber(t.inet_state)
end
if t.interval ~= nil then
self.interval = tonumber(t.interval)
end
if self.config.configDir then
self.script = string.format(
"%s/regular-script.%s", self.config.configDir, self.config.serviceConfig.instance)
end
end
function Module:run(currentStatus, lastStatus, timeDiff, timeNow)
if not self._nextTime then
if timeNow < self.interval then
self._nextTime = self.interval
else
self._nextTime = timeNow - (timeNow % self.interval) + self.interval
end
end
if timeNow >= self._nextTime then
if self.inetState == 2 or (self.inetState == 0 and currentStatus == 0) or (self.inetState == 1 and currentStatus == 1) then
self:runExternalScript(self.script, currentStatus)
end
self._nextTime = self._nextTime + self.interval
end
end
return Module

View File

@@ -40,7 +40,7 @@ function Module:init(t)
end
end
function Module:run(currentStatus, lastStatus, timeDiff)
function Module:run(currentStatus, lastStatus, timeDiff, timeNow)
if currentStatus == 1 then
self._aliveCounter = 0
self._downScriptExecuted = false