modules: bug fixes, refactoring

This commit is contained in:
gSpot
2025-02-21 17:36:43 +03:00
parent 6c19812db5
commit 8ad9c5a086
13 changed files with 166 additions and 132 deletions

View File

@@ -15,22 +15,22 @@ Internet-detector is an application for checking the availability of the Interne
## Installation notes (OpenWrt >= 21.02) ## Installation notes (OpenWrt >= 21.02)
opkg update opkg update
wget --no-check-certificate -O /tmp/internet-detector_1.4.1-r1_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/internet-detector_1.4.1-r1_all.ipk wget --no-check-certificate -O /tmp/internet-detector_1.4.2-r1_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/internet-detector_1.4.2-r1_all.ipk
opkg install /tmp/internet-detector_1.4.1-r1_all.ipk opkg install /tmp/internet-detector_1.4.2-r1_all.ipk
rm /tmp/internet-detector_1.4.1-r1_all.ipk rm /tmp/internet-detector_1.4.2-r1_all.ipk
/etc/init.d/internet-detector start /etc/init.d/internet-detector start
/etc/init.d/internet-detector enable /etc/init.d/internet-detector enable
wget --no-check-certificate -O /tmp/luci-app-internet-detector_1.4.1-r1_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/luci-app-internet-detector_1.4.1-r1_all.ipk wget --no-check-certificate -O /tmp/luci-app-internet-detector_1.4.2-r1_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/luci-app-internet-detector_1.4.2-r1_all.ipk
opkg install /tmp/luci-app-internet-detector_1.4.1-r1_all.ipk opkg install /tmp/luci-app-internet-detector_1.4.2-r1_all.ipk
rm /tmp/luci-app-internet-detector_1.4.1-r1_all.ipk rm /tmp/luci-app-internet-detector_1.4.2-r1_all.ipk
/etc/init.d/rpcd restart /etc/init.d/rpcd restart
i18n-ru: i18n-ru:
wget --no-check-certificate -O /tmp/luci-i18n-internet-detector-ru_1.4.1-r1_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/luci-i18n-internet-detector-ru_1.4.1-r1_all.ipk wget --no-check-certificate -O /tmp/luci-i18n-internet-detector-ru_1.4.2-r1_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/luci-i18n-internet-detector-ru_1.4.2-r1_all.ipk
opkg install /tmp/luci-i18n-internet-detector-ru_1.4.1-r1_all.ipk opkg install /tmp/luci-i18n-internet-detector-ru_1.4.2-r1_all.ipk
rm /tmp/luci-i18n-internet-detector-ru_1.4.1-r1_all.ipk rm /tmp/luci-i18n-internet-detector-ru_1.4.2-r1_all.ipk
## Screenshots: ## Screenshots:
@@ -42,9 +42,9 @@ i18n-ru:
**Dependences:** modemmanager. **Dependences:** modemmanager.
wget --no-check-certificate -O /tmp/internet-detector-mod-modem-restart_1.4.1-r1_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/internet-detector-mod-modem-restart_1.4.1-r1_all.ipk wget --no-check-certificate -O /tmp/internet-detector-mod-modem-restart_1.4.2-r1_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/internet-detector-mod-modem-restart_1.4.2-r1_all.ipk
opkg install /tmp/internet-detector-mod-modem-restart_1.4.1-r1_all.ipk opkg install /tmp/internet-detector-mod-modem-restart_1.4.2-r1_all.ipk
rm /tmp/internet-detector-mod-modem-restart_1.4.1-r1_all.ipk rm /tmp/internet-detector-mod-modem-restart_1.4.2-r1_all.ipk
/etc/init.d/internet-detector restart /etc/init.d/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)
@@ -53,9 +53,9 @@ i18n-ru:
**Dependences:** mailsend. **Dependences:** mailsend.
wget --no-check-certificate -O /tmp/internet-detector-mod-email_1.4.1-r1_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/internet-detector-mod-email_1.4.1-r1_all.ipk wget --no-check-certificate -O /tmp/internet-detector-mod-email_1.4.2-r1_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/internet-detector-mod-email_1.4.2-r1_all.ipk
opkg install /tmp/internet-detector-mod-email_1.4.1-r1_all.ipk opkg install /tmp/internet-detector-mod-email_1.4.2-r1_all.ipk
rm /tmp/internet-detector-mod-email_1.4.1-r1_all.ipk rm /tmp/internet-detector-mod-email_1.4.2-r1_all.ipk
/etc/init.d/internet-detector restart /etc/init.d/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)

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.4.1 PKG_VERSION:=1.4.2
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

@@ -18,8 +18,8 @@ local Module = {
mode = 0, -- 0: connected, 1: disconnected, 2: both mode = 0, -- 0: connected, 1: disconnected, 2: both
hostAlias = "OpenWrt", hostAlias = "OpenWrt",
mta = "/usr/bin/mailsend", mta = "/usr/bin/mailsend",
mtaConnectTimeout = 5, -- default = 5 mtaConnectTimeout = 5,
mtaReadTimeout = 5, -- default = 5 mtaReadTimeout = 5,
mailRecipient = nil, mailRecipient = nil,
mailSender = nil, mailSender = nil,
mailUser = nil, mailUser = nil,
@@ -27,19 +27,21 @@ local Module = {
mailSmtp = nil, mailSmtp = nil,
mailSmtpPort = nil, mailSmtpPort = nil,
mailSecurity = "tls", mailSecurity = "tls",
msgTextPattern1 = "[%s]: %s: %s", -- Connected (host, instance, message) msgTextPattern = "[%s] (%s) | %s", -- Message (host, instance, message)
msgTextPattern2 = "[%s]: %s: %s", -- Disconnected (host, instance, message)
msgSubPattern = "%s notification", -- Subject (host) msgSubPattern = "%s notification", -- Subject (host)
msgConnectPattern = "Internet connected: %s",
msgDisconnectPattern = "Internet disconnected: %s",
msgSeparator = "; ",
msgMaxItems = 50,
status = nil, status = nil,
_enabled = false, _enabled = false,
_deadCounter = 0, _deadCounter = 0,
_aliveCounter = 0, _aliveCounter = 0,
_msgSentDisconnect = true, _msgSentDisconnect = true,
_msgSentConnect = true,
_disconnected = true, _disconnected = true,
_lastDisconnection = nil, _msgSentConnect = true,
_lastConnection = nil, _connected = true,
_message = {}, _msgBuffer = {},
} }
function Module:init(t) function Module:init(t)
@@ -88,6 +90,17 @@ function Module:init(t)
end end
end end
function Module:appendNotice(str)
self._msgBuffer[#self._msgBuffer + 1] = str
if #self._msgBuffer > self.msgMaxItems then
local t = {}
for i = #self._msgBuffer - self.msgMaxItems + 1, #self._msgBuffer do
t[#t + 1] = self._msgBuffer[i]
end
self._msgBuffer = t
end
end
function Module:sendMessage(msg, textPattern) function Module:sendMessage(msg, textPattern)
local verboseArg = "" local verboseArg = ""
local emailMsg = string.format( local emailMsg = string.format(
@@ -136,37 +149,37 @@ function Module:run(currentStatus, lastStatus, timeDiff, timeNow)
if currentStatus == 1 then if currentStatus == 1 then
self._aliveCounter = 0 self._aliveCounter = 0
self._msgSentConnect = false self._msgSentConnect = false
self._lastConnection = nil
if not self._disconnected then if not self._disconnected then
self._disconnected = true self._disconnected = true
if not self._lastDisconnection then self:appendNotice(string.format(
self._lastDisconnection = os.date("%Y.%m.%d %H:%M:%S", os.time()) self.msgDisconnectPattern, os.date("%Y.%m.%d %H:%M:%S", os.time())))
end
self._message[#self._message + 1] = string.format(
"Internet disconnected: %s", self._lastDisconnection)
end end
if not self._msgSentDisconnect and (self.mode == 1 or self.mode == 2) then if not self._msgSentDisconnect and (self.mode == 1 or self.mode == 2) then
if self._deadCounter >= self.deadPeriod then if self._deadCounter >= self.deadPeriod then
self._lastDisconnection = nil self:sendMessage(table.concat(self._msgBuffer, self.msgSeparator), self.msgTextPattern)
self:sendMessage(table.concat(self._message, "; "), self.msgTextPattern2) self._msgBuffer = {}
self._message = {}
self._msgSentDisconnect = true self._msgSentDisconnect = true
else else
self._deadCounter = self._deadCounter + timeDiff self._deadCounter = self._deadCounter + timeDiff
end end
end end
self._connected = false
else else
self._deadCounter = 0 self._deadCounter = 0
self._msgSentDisconnect = false self._msgSentDisconnect = false
if not self._msgSentConnect and (self.mode == 0 or self.mode == 2) then
if not self._lastConnection then if not self._connected then
self._lastConnection = os.date("%Y.%m.%d %H:%M:%S", os.time()) self._connected = true
self:appendNotice(string.format(
self.msgConnectPattern, os.date("%Y.%m.%d %H:%M:%S", os.time())))
end end
if not self._msgSentConnect and (self.mode == 0 or self.mode == 2) then
if self._aliveCounter >= self.alivePeriod then if self._aliveCounter >= self.alivePeriod then
self._message[#self._message + 1] = string.format( self:sendMessage(table.concat(self._msgBuffer, self.msgSeparator), self.msgTextPattern)
"Internet connected: %s", self._lastConnection) self._msgBuffer = {}
self:sendMessage(table.concat(self._message, "; "), self.msgTextPattern1)
self._message = {}
self._msgSentConnect = true self._msgSentConnect = true
else else
self._aliveCounter = self._aliveCounter + timeDiff self._aliveCounter = self._aliveCounter + timeDiff

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.4.1 PKG_VERSION:=1.4.2
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

@@ -19,7 +19,7 @@ local Module = {
status = nil, status = nil,
_enabled = false, _enabled = false,
_deadCounter = 0, _deadCounter = 0,
_restarted = false, _restarted = true,
} }
function Module:toggleIface(flag) function Module:toggleIface(flag)

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.4.1 PKG_VERSION:=1.4.2
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

@@ -15,6 +15,8 @@ local Module = {
status = nil, status = nil,
_attemptsCounter = 0, _attemptsCounter = 0,
_deadCounter = 0, _deadCounter = 0,
_ifaceRestarting = false,
_ifaceRestartCounter = 0,
} }
function Module:toggleFunc(flag) function Module:toggleFunc(flag)
@@ -74,18 +76,31 @@ function Module:init(t)
if t.restart_timeout ~= nil then if t.restart_timeout ~= nil then
self.restartTimeout = tonumber(t.restart_timeout) self.restartTimeout = tonumber(t.restart_timeout)
end end
self._attemptsCounter = self.attempts
end end
function Module:run(currentStatus, lastStatus, timeDiff, timeNow) function Module:run(currentStatus, lastStatus, timeDiff, timeNow)
if self._ifaceRestarting then
if self._ifaceRestartCounter >= self.restartTimeout then
self:ifaceUp()
self._ifaceRestarting = false
self._ifaceRestartCounter = 0
else
self._ifaceRestartCounter = self._ifaceRestartCounter + timeDiff
end
else
if currentStatus == 1 then if currentStatus == 1 then
if self.attempts == 0 or self._attemptsCounter < self.attempts then if self._attemptsCounter < self.attempts then
if self._deadCounter >= self.deadPeriod then if self._deadCounter >= self.deadPeriod then
if self.iface then if self.iface then
self.syslog("info", string.format( self.syslog("info", string.format(
"%s: restarting network interface '%s'", self.name, self.iface)) "%s: restarting network interface '%s'", self.name, self.iface))
self:ifaceDown() self:ifaceDown()
unistd.sleep(self.restartTimeout) if self.restartTimeout < 1 then
self:ifaceUp() self:ifaceUp()
else
self._ifaceRestarting = true
end
else else
self.syslog("info", string.format( self.syslog("info", string.format(
"%s: restarting network", self.name)) "%s: restarting network", self.name))
@@ -101,6 +116,8 @@ function Module:run(currentStatus, lastStatus, timeDiff, timeNow)
self._attemptsCounter = 0 self._attemptsCounter = 0
self._deadCounter = 0 self._deadCounter = 0
end end
self._ifaceRestartCounter = 0
end
end end
return Module return Module

View File

@@ -10,8 +10,10 @@ local Module = {
readValue = function(filePath) return nil end, readValue = function(filePath) return nil end,
deadPeriod = 3600, deadPeriod = 3600,
forceRebootDelay = 300, forceRebootDelay = 300,
antiBootloopDelay = 300,
status = nil, status = nil,
_deadCounter = 0, _deadCounter = 0,
_rebooted = true,
} }
function Module:rebootDevice() function Module:rebootDevice()
@@ -36,15 +38,17 @@ end
function Module:run(currentStatus, lastStatus, timeDiff, timeNow) function Module:run(currentStatus, lastStatus, timeDiff, timeNow)
if currentStatus == 1 then if currentStatus == 1 then
if self._deadCounter >= self.deadPeriod then if not self._rebooted then
if timeNow >= self.antiBootloopDelay and self._deadCounter >= self.deadPeriod then
self:rebootDevice() self:rebootDevice()
self._deadCounter = 0 self._rebooted = true
else else
self._deadCounter = self._deadCounter + timeDiff self._deadCounter = self._deadCounter + timeDiff
end end
end
else else
self._deadCounter = 0 self._deadCounter = 0
self._rebooted = false
end end
end end

View File

@@ -15,8 +15,8 @@ local Module = {
status = nil, status = nil,
_deadCounter = 0, _deadCounter = 0,
_aliveCounter = 0, _aliveCounter = 0,
_upScriptExecuted = true,
_downScriptExecuted = true, _downScriptExecuted = true,
_upScriptExecuted = true,
} }
function Module:runExternalScript(scriptPath) function Module:runExternalScript(scriptPath)
@@ -43,22 +43,22 @@ end
function Module:run(currentStatus, lastStatus, timeDiff, timeNow) function Module:run(currentStatus, lastStatus, timeDiff, timeNow)
if currentStatus == 1 then if currentStatus == 1 then
self._aliveCounter = 0 self._aliveCounter = 0
self._downScriptExecuted = false self._upScriptExecuted = false
if not self._upScriptExecuted then if not self._downScriptExecuted then
if self._deadCounter >= self.deadPeriod then if self._deadCounter >= self.deadPeriod then
self:runExternalScript(self.downScript) self:runExternalScript(self.downScript)
self._upScriptExecuted = true self._downScriptExecuted = true
else else
self._deadCounter = self._deadCounter + timeDiff self._deadCounter = self._deadCounter + timeDiff
end end
end end
else else
self._deadCounter = 0 self._deadCounter = 0
self._upScriptExecuted = false self._downScriptExecuted = false
if not self._downScriptExecuted then if not self._upScriptExecuted then
if self._aliveCounter >= self.alivePeriod then if self._aliveCounter >= self.alivePeriod then
self:runExternalScript(self.upScript) self:runExternalScript(self.upScript)
self._downScriptExecuted = true self._upScriptExecuted = true
else else
self._aliveCounter = self._aliveCounter + timeDiff self._aliveCounter = self._aliveCounter + timeDiff
end end

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.4.1 PKG_VERSION:=1.4.2
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

@@ -873,7 +873,7 @@ return view.extend({
// dead_period // dead_period
o = s.taboption('restart_network', this.CBITimeInput, o = s.taboption('restart_network', this.CBITimeInput,
'mod_network_restart_dead_period', _('Dead period'), 'mod_network_restart_dead_period', _('Dead period'),
_('Period of time without Internet access before network restart.') _('Period of time without Internet access before network restart or between restart attempts.')
); );
o.default = '900'; o.default = '900';
o.rmempty = false; o.rmempty = false;

View File

@@ -309,8 +309,8 @@ msgstr "Период времени отсутствия доступа в Ин
msgid "Period of time without Internet access before modem restart." msgid "Period of time without Internet access before modem restart."
msgstr "Период времени отсутствия доступа в Интренет перед перезапуском модема." msgstr "Период времени отсутствия доступа в Интренет перед перезапуском модема."
msgid "Period of time without Internet access before network restart." msgid "Period of time without Internet access before network restart or between restart attempts."
msgstr "Период времени отсутствия доступа в Интренет перед перезапуском сети." msgstr "Период времени отсутствия доступа в Интренет перед перезапуском сети или между попытками перезапуска."
msgid "" msgid ""
"Period of time without Internet access until the device is rebooted." "Period of time without Internet access until the device is rebooted."

View File

@@ -285,7 +285,7 @@ msgstr ""
msgid "Period of time without Internet access before modem restart." msgid "Period of time without Internet access before modem restart."
msgstr "" msgstr ""
msgid "Period of time without Internet access before network restart." msgid "Period of time without Internet access before network restart or between restart attempts."
msgstr "" msgstr ""
msgid "" msgid ""