Refactoring. Added respawn option to init-script.

This commit is contained in:
gSpot
2025-09-25 22:38:53 +03:00
parent cb7dbba054
commit 1720662f73
12 changed files with 198 additions and 152 deletions

View File

@@ -20,22 +20,22 @@ Internet-detector is an application for checking the availability of the Interne
## Installation notes: ## Installation notes:
opkg update opkg update
wget --no-check-certificate -O /tmp/internet-detector_1.6.4-r1_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/internet-detector_1.6.4-r1_all.ipk wget --no-check-certificate -O /tmp/internet-detector_1.6.5-r1_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/internet-detector_1.6.5-r1_all.ipk
opkg install /tmp/internet-detector_1.6.4-r1_all.ipk opkg install /tmp/internet-detector_1.6.5-r1_all.ipk
rm /tmp/internet-detector_1.6.4-r1_all.ipk rm /tmp/internet-detector_1.6.5-r1_all.ipk
service internet-detector start service internet-detector start
service internet-detector enable service internet-detector enable
wget --no-check-certificate -O /tmp/luci-app-internet-detector_1.6.4-r1_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/luci-app-internet-detector_1.6.4-r1_all.ipk wget --no-check-certificate -O /tmp/luci-app-internet-detector_1.6.5-r1_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/luci-app-internet-detector_1.6.5-r1_all.ipk
opkg install /tmp/luci-app-internet-detector_1.6.4-r1_all.ipk opkg install /tmp/luci-app-internet-detector_1.6.5-r1_all.ipk
rm /tmp/luci-app-internet-detector_1.6.4-r1_all.ipk rm /tmp/luci-app-internet-detector_1.6.5-r1_all.ipk
service rpcd restart service rpcd restart
i18n-ru: i18n-ru:
wget --no-check-certificate -O /tmp/luci-i18n-internet-detector-ru_1.6.4-r1_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/luci-i18n-internet-detector-ru_1.6.4-r1_all.ipk wget --no-check-certificate -O /tmp/luci-i18n-internet-detector-ru_1.6.5-r1_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/luci-i18n-internet-detector-ru_1.6.5-r1_all.ipk
opkg install /tmp/luci-i18n-internet-detector-ru_1.6.4-r1_all.ipk opkg install /tmp/luci-i18n-internet-detector-ru_1.6.5-r1_all.ipk
rm /tmp/luci-i18n-internet-detector-ru_1.6.4-r1_all.ipk rm /tmp/luci-i18n-internet-detector-ru_1.6.5-r1_all.ipk
## Screenshots: ## Screenshots:
@@ -47,9 +47,9 @@ i18n-ru:
**Dependences:** modemmanager. **Dependences:** modemmanager.
wget --no-check-certificate -O /tmp/internet-detector-mod-modem-restart_1.6.4-r1_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/internet-detector-mod-modem-restart_1.6.4-r1_all.ipk wget --no-check-certificate -O /tmp/internet-detector-mod-modem-restart_1.6.5-r1_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/internet-detector-mod-modem-restart_1.6.5-r1_all.ipk
opkg install /tmp/internet-detector-mod-modem-restart_1.6.4-r1_all.ipk opkg install /tmp/internet-detector-mod-modem-restart_1.6.5-r1_all.ipk
rm /tmp/internet-detector-mod-modem-restart_1.6.4-r1_all.ipk rm /tmp/internet-detector-mod-modem-restart_1.6.5-r1_all.ipk
service internet-detector restart service internet-detector restart
![](https://github.com/gSpotx2f/luci-app-internet-detector/blob/master/screenshots/04.jpg) ![](https://github.com/gSpotx2f/luci-app-internet-detector/blob/master/screenshots/04.jpg)
@@ -58,9 +58,9 @@ i18n-ru:
**Dependences:** mailsend. **Dependences:** mailsend.
wget --no-check-certificate -O /tmp/internet-detector-mod-email_1.6.4-r1_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/internet-detector-mod-email_1.6.4-r1_all.ipk wget --no-check-certificate -O /tmp/internet-detector-mod-email_1.6.5-r1_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/internet-detector-mod-email_1.6.5-r1_all.ipk
opkg install /tmp/internet-detector-mod-email_1.6.4-r1_all.ipk opkg install /tmp/internet-detector-mod-email_1.6.5-r1_all.ipk
rm /tmp/internet-detector-mod-email_1.6.4-r1_all.ipk rm /tmp/internet-detector-mod-email_1.6.5-r1_all.ipk
service internet-detector restart service internet-detector restart
![](https://github.com/gSpotx2f/luci-app-internet-detector/blob/master/screenshots/05.jpg) ![](https://github.com/gSpotx2f/luci-app-internet-detector/blob/master/screenshots/05.jpg)
@@ -69,9 +69,9 @@ i18n-ru:
**Dependences:** curl. **Dependences:** curl.
wget --no-check-certificate -O /tmp/internet-detector-mod-telegram_1.6.4-r1_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/internet-detector-mod-telegram_1.6.4-r1_all.ipk wget --no-check-certificate -O /tmp/internet-detector-mod-telegram_1.6.5-r1_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/internet-detector-mod-telegram_1.6.5-r1_all.ipk
opkg install /tmp/internet-detector-mod-telegram_1.6.4-r1_all.ipk opkg install /tmp/internet-detector-mod-telegram_1.6.5-r1_all.ipk
rm /tmp/internet-detector-mod-telegram_1.6.4-r1_all.ipk rm /tmp/internet-detector-mod-telegram_1.6.5-r1_all.ipk
service internet-detector restart service internet-detector restart
![](https://github.com/gSpotx2f/luci-app-internet-detector/blob/master/screenshots/06.jpg) ![](https://github.com/gSpotx2f/luci-app-internet-detector/blob/master/screenshots/06.jpg)

View File

@@ -5,7 +5,7 @@
include $(TOPDIR)/rules.mk include $(TOPDIR)/rules.mk
PKG_NAME:=internet-detector-mod-email PKG_NAME:=internet-detector-mod-email
PKG_VERSION:=1.6.4 PKG_VERSION:=1.6.5
PKG_RELEASE:=1 PKG_RELEASE:=1
PKG_MAINTAINER:=gSpot <https://github.com/gSpotx2f/luci-app-internet-detector> PKG_MAINTAINER:=gSpot <https://github.com/gSpotx2f/luci-app-internet-detector>

View File

@@ -5,7 +5,7 @@
include $(TOPDIR)/rules.mk include $(TOPDIR)/rules.mk
PKG_NAME:=internet-detector-mod-modem-restart PKG_NAME:=internet-detector-mod-modem-restart
PKG_VERSION:=1.6.4 PKG_VERSION:=1.6.5
PKG_RELEASE:=1 PKG_RELEASE:=1
PKG_MAINTAINER:=gSpot <https://github.com/gSpotx2f/luci-app-internet-detector> PKG_MAINTAINER:=gSpot <https://github.com/gSpotx2f/luci-app-internet-detector>

View File

@@ -5,7 +5,7 @@
include $(TOPDIR)/rules.mk include $(TOPDIR)/rules.mk
PKG_NAME:=internet-detector-mod-telegram PKG_NAME:=internet-detector-mod-telegram
PKG_VERSION:=1.6.4 PKG_VERSION:=1.6.5
PKG_RELEASE:=1 PKG_RELEASE:=1
PKG_MAINTAINER:=gSpot <https://github.com/gSpotx2f/luci-app-internet-detector> PKG_MAINTAINER:=gSpot <https://github.com/gSpotx2f/luci-app-internet-detector>

View File

@@ -120,15 +120,22 @@ end
function Module:httpRequest(url) function Module:httpRequest(url)
local retCode = 1, data local retCode = 1, data
local fh = io.popen(string.format( local fh = io.popen(string.format(
'%s --connect-timeout %s %s "%s"; printf "\n$?";', self.curlExec, self.connectTimeout, self.curlParams, url), "r") '%s --connect-timeout %s %s "%s"; printf "\n$?";',
self.curlExec,
self.connectTimeout,
self.curlParams,
url
), "r")
if fh then if fh then
data = fh:read("*a") data = fh:read("*a")
fh:close() fh:close()
local s, e = data:find("[0-9]+\n?$") if data ~= nil then
retCode = tonumber(data:sub(s)) local s, e = data:find("[0-9]+\n?$")
data = data:sub(0, s - 2) retCode = tonumber(data:sub(s))
if not data or data == "" then data = data:sub(0, s - 2)
data = nil if not data or data == "" then
data = nil
end
end end
else else
retCode = 1 retCode = 1

View File

@@ -5,7 +5,7 @@
include $(TOPDIR)/rules.mk include $(TOPDIR)/rules.mk
PKG_NAME:=internet-detector PKG_NAME:=internet-detector
PKG_VERSION:=1.6.4 PKG_VERSION:=1.6.5
PKG_RELEASE:=1 PKG_RELEASE:=1
PKG_MAINTAINER:=gSpot <https://github.com/gSpotx2f/luci-app-internet-detector> PKG_MAINTAINER:=gSpot <https://github.com/gSpotx2f/luci-app-internet-detector>

View File

@@ -11,6 +11,8 @@ run_instance() {
if [ $enabled = "1" ]; then if [ $enabled = "1" ]; then
procd_open_instance "$1" procd_open_instance "$1"
procd_set_param command "$PROG" "-a" "nodaemon" "-i" "$1" procd_set_param command "$PROG" "-a" "nodaemon" "-i" "$1"
procd_set_param respawn
procd_set_param term_timeout 60
procd_close_instance procd_close_instance
fi fi
} }

View File

@@ -51,11 +51,15 @@ local InternetDetector = {
modules = {}, modules = {},
parsedHosts = {}, parsedHosts = {},
uiCounter = 0, uiCounter = 0,
pidFile = nil,
statusFile = nil,
} }
InternetDetector.configDir = string.format("/etc/%s", InternetDetector.appName) InternetDetector.configDir = string.format("/etc/%s", InternetDetector.appName)
InternetDetector.modulesDir = string.format( InternetDetector.modulesDir = string.format(
"%s/%s/modules", InternetDetector.libDir, InternetDetector.appName) "%s/%s/modules", InternetDetector.libDir, InternetDetector.appName)
InternetDetector.commonDir = string.format("/tmp/run/%s", InternetDetector.appName) InternetDetector.commonDir = string.format("/tmp/run/%s", InternetDetector.appName)
InternetDetector.appNamePattern = InternetDetector.appName:gsub("-", "%%-")
InternetDetector.pidFilePattern = "^" .. InternetDetector.appNamePattern .. ".-%.pid$"
-- Loading settings from UCI -- Loading settings from UCI
@@ -79,11 +83,9 @@ elseif err then
io.stderr:write(string.format("Error: %s\n", err)) io.stderr:write(string.format("Error: %s\n", err))
end end
local _RUNNING
function InternetDetector:prequire(package) function InternetDetector:prequire(package)
local retVal, pkg = pcall(require, package) local ok, pkg = pcall(require, package)
return retVal and pkg return ok and pkg
end end
function InternetDetector:loadUCIConfig(sType, instance) function InternetDetector:loadUCIConfig(sType, instance)
@@ -107,10 +109,6 @@ function InternetDetector:loadUCIConfig(sType, instance)
end end
) )
self.serviceConfig.instance = instance self.serviceConfig.instance = instance
self.pidFile = string.format(
"%s/%s.%s.pid", self.commonDir, self.appName, instance)
self.statusFile = string.format(
"%s/%s.%s.status", self.commonDir, self.appName, instance)
return success return success
end end
@@ -317,25 +315,16 @@ function InternetDetector:TCPConnectionToHost(host, port)
return retCode return retCode
end end
function InternetDetector:checkHosts() function InternetDetector:exit()
local checkFunc = (self.serviceConfig.check_type == 1) and self.pingHost or self.TCPConnectionToHost for _, e in ipairs(self.modules) do
local retCode = 1 e:onExit()
for k, v in ipairs(self.parsedHosts) do
for i = 1, self.serviceConfig.connection_attempts do
if checkFunc(self, v.addr, v.port) == 0 then
retCode = 0
break
end
end
if retCode == 0 then
break
end
end end
return retCode self:removeProcessFiles()
end if self.loggingLevel > 0 then
self:writeLogMessage("info", "stoped")
function InternetDetector:breakMainLoop(signo) syslog.closelog()
_RUNNING = false end
os.exit(0)
end end
function InternetDetector:resetUiCounter(signo) function InternetDetector:resetUiCounter(signo)
@@ -343,9 +332,9 @@ function InternetDetector:resetUiCounter(signo)
end end
function InternetDetector:mainLoop() function InternetDetector:mainLoop()
signal.signal(signal.SIGTERM, function(signo) self:breakMainLoop(signo) end) signal.signal(signal.SIGTERM, function(signo) self:exit(signo) end)
signal.signal(signal.SIGINT, function(signo) self:breakMainLoop(signo) end) signal.signal(signal.SIGINT, function(signo) self:exit(signo) end)
signal.signal(signal.SIGQUIT, function(signo) self:breakMainLoop(signo) end) signal.signal(signal.SIGQUIT, function(signo) self:exit(signo) end)
signal.signal(signal.SIGUSR1, function(signo) self:resetUiCounter(signo) end) signal.signal(signal.SIGUSR1, function(signo) self:resetUiCounter(signo) end)
local lastStatus, currentStatus, mTimeNow, mTimeDiff, mLastTime, uiTimeNow, uiLastTime local lastStatus, currentStatus, mTimeNow, mTimeDiff, mLastTime, uiTimeNow, uiLastTime
@@ -353,40 +342,91 @@ function InternetDetector:mainLoop()
local modulesStatus = {} local modulesStatus = {}
local counter = 0 local counter = 0
local inetChecked = false local inetChecked = false
_RUNNING = true local checking = false
while _RUNNING do local hostNum = 1
local attempt = 1
local checkFunc = (self.serviceConfig.check_type == 1) and self.pingHost or self.TCPConnectionToHost
while true do
if counter == 0 or counter >= interval then if counter == 0 or counter >= interval then
if self.debug then checking = true
currentStatus = self:checkHosts() end
else
local ret, status = pcall(self.checkHosts, self, currentStatus) if checking then
if ret then local newStatus = 1
currentStatus = status local checkCompleted = false
if hostNum <= #self.parsedHosts then
if attempt <= self.serviceConfig.connection_attempts then
local addr = self.parsedHosts[hostNum].addr
local port = self.parsedHosts[hostNum].port
local retCode = 1
if self.debug then
retCode = checkFunc(self, addr, port)
else
local ok, status = pcall(checkFunc, self, addr, port)
if ok then
retCode = status
else
self:writeLogMessage("err", string.format(
"An error occurred while checking the host %s: %s",
tostring(addr),
tostring(status))
)
end
end
if retCode == 0 then
attempt = 1
hostNum = 1
checking = false
newStatus = 0
counter = 0
checkCompleted = true
else
attempt = attempt + 1
if attempt > self.serviceConfig.connection_attempts then
attempt = 1
hostNum = hostNum + 1
end
end
else else
self:writeLogMessage("err", "Unknown error while checking host!") attempt = 1
hostNum = hostNum + 1
end end
end if hostNum > #self.parsedHosts then
if not stat.stat(self.statusFile) then hostNum = 1
self:writeValueToFile(self.statusFile, self:statusJson( checking = false
currentStatus, self.serviceConfig.instance)) counter = 0
checkCompleted = true
end
else
hostNum = 1
checking = false
counter = 0
checkCompleted = true
end end
if currentStatus == 0 then if checkCompleted then
interval = self.serviceConfig.interval_up currentStatus = newStatus
if currentStatus ~= lastStatus then if not stat.stat(self.statusFile) then
self:writeValueToFile(self.statusFile, self:statusJson( self:writeValueToFile(self.statusFile, self:statusJson(
currentStatus, self.serviceConfig.instance)) currentStatus, self.serviceConfig.instance))
self:writeLogMessage("notice", "Connected")
end end
else if currentStatus == 0 then
interval = self.serviceConfig.interval_down interval = self.serviceConfig.interval_up
if currentStatus ~= lastStatus then if currentStatus ~= lastStatus then
self:writeValueToFile(self.statusFile, self:statusJson( self:writeValueToFile(self.statusFile, self:statusJson(
currentStatus, self.serviceConfig.instance)) currentStatus, self.serviceConfig.instance))
self:writeLogMessage("notice", "Disconnected") self:writeLogMessage("notice", "Connected")
end
else
interval = self.serviceConfig.interval_down
if currentStatus ~= lastStatus then
self:writeValueToFile(self.statusFile, self:statusJson(
currentStatus, self.serviceConfig.instance))
self:writeLogMessage("notice", "Disconnected")
end
end end
end end
counter = 0
end end
mTimeDiff = 0 mTimeDiff = 0
@@ -402,9 +442,10 @@ function InternetDetector:mainLoop()
if self.debug then if self.debug then
e:run(currentStatus, lastStatus, mTimeDiff, mTimeNow, inetChecked) e:run(currentStatus, lastStatus, mTimeDiff, mTimeNow, inetChecked)
else else
local ret = pcall(e.run, e, currentStatus, lastStatus, mTimeDiff, mTimeNow, inetChecked) local ok, err = pcall(e.run, e, currentStatus, lastStatus, mTimeDiff, mTimeNow, inetChecked)
if not ret then if not ok then
self:writeLogMessage("err", string.format("%s: Unknown error!", e.name)) self:writeLogMessage("err", string.format(
"%s: Module error: %s", e.name, tostring(err)))
end end
end end
end end
@@ -421,9 +462,12 @@ function InternetDetector:mainLoop()
currentStatus, self.serviceConfig.instance, modulesStatus)) currentStatus, self.serviceConfig.instance, modulesStatus))
end end
lastStatus = currentStatus
unistd.sleep(1) unistd.sleep(1)
counter = counter + 1
if not checking then
lastStatus = currentStatus
counter = counter + 1
end
if self.mode == 2 then if self.mode == 2 then
uiTimeNow = time.clock_gettime(time.CLOCK_MONOTONIC).tv_sec uiTimeNow = time.clock_gettime(time.CLOCK_MONOTONIC).tv_sec
@@ -434,25 +478,22 @@ function InternetDetector:mainLoop()
end end
uiLastTime = uiTimeNow uiLastTime = uiTimeNow
if self.uiCounter >= self.uiRunTime then if self.uiCounter >= self.uiRunTime then
self:breakMainLoop(signal.SIGTERM) self:exit(signal.SIGTERM)
end end
end end
end end
end end
function InternetDetector:removeProcessFiles() function InternetDetector:removeProcessFiles()
os.remove(string.format( os.remove(self.statusFile)
"%s/%s.%s.pid", self.commonDir, self.appName, self.serviceConfig.instance)) os.remove(self.pidFile)
os.remove(string.format(
"%s/%s.%s.status", self.commonDir, self.appName, self.serviceConfig.instance))
end end
function InternetDetector:status() function InternetDetector:status()
local ok, commonDir = pcall(dirent.files, self.commonDir) local ok, commonDir = pcall(dirent.files, self.commonDir)
if ok then if ok then
local appName = self.appName:gsub("-", "%%-")
for item in commonDir do for item in commonDir do
if item:match("^" .. appName .. ".-%.pid$") then if item:match(self.pidFilePattern) then
return "running" return "running"
end end
end end
@@ -464,10 +505,10 @@ function InternetDetector:inetStatus()
local inetStat = '{"instances":[]}' local inetStat = '{"instances":[]}'
local ok, commonDir = pcall(dirent.files, self.commonDir) local ok, commonDir = pcall(dirent.files, self.commonDir)
if ok then if ok then
local appName = self.appName:gsub("-", "%%-") local statusFilePattern = "^" .. self.appNamePattern .. ".-%.status$"
local lines = {} local lines = {}
for item in commonDir do for item in commonDir do
if item:match("^" .. appName .. ".-%.status$") then if item:match(statusFilePattern) then
lines[#lines + 1] = self:readValueFromFile( lines[#lines + 1] = self:readValueFromFile(
string.format("%s/%s", self.commonDir, item)) string.format("%s/%s", self.commonDir, item))
end end
@@ -478,7 +519,7 @@ function InternetDetector:inetStatus()
end end
function InternetDetector:stopInstance(pidFile) function InternetDetector:stopInstance(pidFile)
local retVal local retVal = false, pidValue
if stat.stat(pidFile) then if stat.stat(pidFile) then
pidValue = self:readValueFromFile(pidFile) pidValue = self:readValueFromFile(pidFile)
if pidValue then if pidValue then
@@ -497,6 +538,8 @@ function InternetDetector:stopInstance(pidFile)
os.remove(pidFile) os.remove(pidFile)
end end
retVal = true retVal = true
else
os.remove(pidFile)
end end
end end
if not pidValue then if not pidValue then
@@ -508,22 +551,22 @@ function InternetDetector:stopInstance(pidFile)
end end
function InternetDetector:stop() function InternetDetector:stop()
local appName = self.appName:gsub("-", "%%-") local nopids = false
local success for i = 0, 100 do
for i = 0, 10 do nopids = true
success = true
local ok, commonDir = pcall(dirent.files, self.commonDir) local ok, commonDir = pcall(dirent.files, self.commonDir)
if ok then if ok then
for item in commonDir do for item in commonDir do
if item:match("^" .. appName .. ".-%.pid$") then if item:match(self.pidFilePattern) then
self:stopInstance(string.format("%s/%s", self.commonDir, item)) if self:stopInstance(string.format("%s/%s", self.commonDir, item)) then
success = false nopids = false
end
end end
end end
if success then if nopids then
break break
end end
unistd.sleep(1) time.nanosleep({ tv_sec = 0, tv_nsec = 10000000 })
else else
break break
end end
@@ -531,11 +574,10 @@ function InternetDetector:stop()
end end
function InternetDetector:setSIGUSR() function InternetDetector:setSIGUSR()
local appName = self.appName:gsub("-", "%%-")
local ok, commonDir = pcall(dirent.files, self.commonDir) local ok, commonDir = pcall(dirent.files, self.commonDir)
if ok then if ok then
for item in commonDir do for item in commonDir do
if item:match("^" .. appName .. ".-%.pid$") then if item:match(self.pidFilePattern) then
pidValue = self:readValueFromFile(string.format("%s/%s", self.commonDir, item)) pidValue = self:readValueFromFile(string.format("%s/%s", self.commonDir, item))
if pidValue then if pidValue then
signal.kill(tonumber(pidValue), signal.SIGUSR1) signal.kill(tonumber(pidValue), signal.SIGUSR1)
@@ -559,18 +601,25 @@ function InternetDetector:preRun()
os.exit(1) os.exit(1)
end end
end end
if stat.stat(self.pidFile) then local ok, commonDir = pcall(dirent.files, self.commonDir)
io.stderr:write( if ok then
string.format('PID file "%s" exists. Is the %s already running?\n', local instancePattern = "^" .. self.appNamePattern .. "%." .. self.serviceConfig.instance .. "%.[%d]+%.pid$"
self.pidFile, self.appName)) for item in commonDir do
return false if item:match(instancePattern) then
self:stopInstance(string.format("%s/%s", self.commonDir, item))
end
end
end end
return true
end end
function InternetDetector:run() function InternetDetector:run()
local pidValue = unistd.getpid() local pidValue = unistd.getpid()
self.pidFile = string.format(
"%s/%s.%s.%s.pid", self.commonDir, self.appName, self.serviceConfig.instance, pidValue)
self.statusFile = string.format(
"%s/%s.%s.status", self.commonDir, self.appName, self.serviceConfig.instance)
self:writeValueToFile(self.pidFile, pidValue) self:writeValueToFile(self.pidFile, pidValue)
if self.loggingLevel > 0 then if self.loggingLevel > 0 then
syslog.openlog(self.appName, syslog.LOG_PID, syslog.LOG_DAEMON) syslog.openlog(self.appName, syslog.LOG_PID, syslog.LOG_DAEMON)
end end
@@ -613,29 +662,16 @@ function InternetDetector:run()
self.statusFile, self:statusJson(-1, self.serviceConfig.instance)) self.statusFile, self:statusJson(-1, self.serviceConfig.instance))
self:mainLoop() self:mainLoop()
self:exit()
for _, e in ipairs(self.modules) do
e:onExit()
end
self:removeProcessFiles()
if self.loggingLevel > 0 then
self:writeLogMessage("info", "stoped")
syslog.closelog()
end
end end
function InternetDetector:noDaemon() function InternetDetector:noDaemon()
if not self:preRun() then self:preRun()
return
end
self:run() self:run()
end end
function InternetDetector:daemon() function InternetDetector:daemon()
if not self:preRun() then self:preRun()
return
end
-- UNIX double fork -- UNIX double fork
if unistd.fork() == 0 then if unistd.fork() == 0 then
unistd.setpid("s") unistd.setpid("s")

View File

@@ -25,6 +25,7 @@ local Module = {
_enabled = false, _enabled = false,
_leds = {}, _leds = {},
_counter = 0, _counter = 0,
_exit = false,
} }
function Module:setLedAttrs(t) function Module:setLedAttrs(t)
@@ -114,6 +115,7 @@ function Module:init(t)
self.syslog("err", string.format( self.syslog("err", string.format(
"%s: module disabled. LED '%s' is not available", self.name, l.ledName)) "%s: module disabled. LED '%s' is not available", self.name, l.ledName))
end end
self._exit = false
end end
end end
@@ -303,6 +305,9 @@ function Module:run(currentStatus, lastStatus, timeDiff, timeNow, inetChecked)
end end
if self._counter == 0 or self._counter >= self.runInterval or currentStatus ~= lastStatus then if self._counter == 0 or self._counter >= self.runInterval or currentStatus ~= lastStatus then
for _, t in ipairs(self._leds) do for _, t in ipairs(self._leds) do
if self._exit then
break
end
if t.enabled then if t.enabled then
self:ledRunFunc(t, currentStatus) self:ledRunFunc(t, currentStatus)
end end
@@ -313,6 +318,7 @@ function Module:run(currentStatus, lastStatus, timeDiff, timeNow, inetChecked)
end end
function Module:onExit() function Module:onExit()
self._exit = true
for _, l in ipairs(self._leds) do for _, l in ipairs(self._leds) do
if l.ledPrevState then if l.ledPrevState then
if l.ledPrevState.brightness then if l.ledPrevState.brightness then

View File

@@ -350,7 +350,7 @@ end
function Module:httpRequest(url) function Module:httpRequest(url)
local retCode = 1, data local retCode = 1, data
local curl = string.format( local curl = string.format(
'%s%s --connect-timeout %s %s "%s"; printf "\n$?";', '%s%s --connect-timeout %s %s "%s"; printf "\n$?";',
self.curlExec, self.curlExec,
self.config.serviceConfig.iface and (" --interface " .. self.config.serviceConfig.iface) or "", self.config.serviceConfig.iface and (" --interface " .. self.config.serviceConfig.iface) or "",
@@ -358,15 +358,17 @@ function Module:httpRequest(url)
self.curlParams, self.curlParams,
url url
) )
local fh = io.popen(curl, "r") local fh = io.popen(curl, "r")
if fh then if fh then
data = fh:read("*a") data = fh:read("*a")
fh:close() fh:close()
local s, e = data:find("[0-9]+\n?$") if data ~= nil then
retCode = tonumber(data:sub(s)) local s, e = data:find("[0-9]+\n?$")
data = data:sub(0, s - 2) retCode = tonumber(data:sub(s))
if not data or data == "" then data = data:sub(0, s - 2)
data = nil if not data or data == "" then
data = nil
end
end end
else else
retCode = 1 retCode = 1

View File

@@ -5,7 +5,7 @@
include $(TOPDIR)/rules.mk include $(TOPDIR)/rules.mk
PKG_NAME:=luci-app-internet-detector PKG_NAME:=luci-app-internet-detector
PKG_VERSION:=1.6.4 PKG_VERSION:=1.6.5
PKG_RELEASE:=1 PKG_RELEASE:=1
LUCI_TITLE:=LuCI support for internet-detector LUCI_TITLE:=LuCI support for internet-detector
LUCI_DEPENDS:=+internet-detector LUCI_DEPENDS:=+internet-detector

View File

@@ -844,8 +844,6 @@ return view.extend({
o.value(1); o.value(1);
o.value(2); o.value(2);
o.value(3); o.value(3);
o.value(4);
o.value(5);
o.default = '2'; o.default = '2';
// connection_timeout // connection_timeout
@@ -859,11 +857,6 @@ return view.extend({
o.value(3, '3 ' + _('sec')); o.value(3, '3 ' + _('sec'));
o.value(4, '4 ' + _('sec')); o.value(4, '4 ' + _('sec'));
o.value(5, '5 ' + _('sec')); o.value(5, '5 ' + _('sec'));
o.value(6, '6 ' + _('sec'));
o.value(7, '7 ' + _('sec'));
o.value(8, '8 ' + _('sec'));
o.value(9, '9 ' + _('sec'));
o.value(10, '10 ' + _('sec'));
o.default = '2'; o.default = '2';
// enabled // enabled
@@ -1569,7 +1562,7 @@ return view.extend({
// enabled // enabled
o = s.taboption('telegram', form.Flag, o = s.taboption('telegram', form.Flag,
'mod_telegram_enabled', 'mod_telegram_enabled',
_('Enable')); _('Enabled'));
o.rmempty = false; o.rmempty = false;
o.modalonly = true; o.modalonly = true;