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

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

View File

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

View File

@@ -129,7 +129,7 @@ function Module:sendMessage(msg, textPattern)
end
end
function Module:run(currentStatus, lastStatus, timeDiff)
function Module:run(currentStatus, lastStatus, timeDiff, timeNow)
if not self._enabled then
return
end

View File

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

View File

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

View File

@@ -5,7 +5,7 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=internet-detector
PKG_VERSION:=1.3.3
PKG_VERSION:=1.4.0
PKG_RELEASE:=1
PKG_MAINTAINER:=gSpot <https://github.com/gSpotx2f/luci-app-internet-detector>
@@ -31,6 +31,7 @@ define Package/$(PKG_NAME)/conffiles
/etc/internet-detector/down-script.internet
/etc/internet-detector/up-script.internet
/etc/internet-detector/public-ip-script.internet
/etc/internet-detector/regular-script.internet
endef
define Build/Configure
@@ -46,6 +47,7 @@ define Package/$(PKG_NAME)/install
$(INSTALL_DATA) ./files/etc/internet-detector/down-script.internet $(1)/etc/internet-detector/down-script.internet
$(INSTALL_DATA) ./files/etc/internet-detector/up-script.internet $(1)/etc/internet-detector/up-script.internet
$(INSTALL_DATA) ./files/etc/internet-detector/public-ip-script.internet $(1)/etc/internet-detector/public-ip-script.internet
$(INSTALL_DATA) ./files/etc/internet-detector/regular-script.internet $(1)/etc/internet-detector/regular-script.internet
$(INSTALL_DIR) $(1)/etc/init.d
$(INSTALL_BIN) ./files/etc/init.d/internet-detector $(1)/etc/init.d/internet-detector
$(INSTALL_DIR) $(1)/usr/bin
@@ -57,6 +59,7 @@ define Package/$(PKG_NAME)/install
$(INSTALL_DATA) ./files/usr/lib/lua/internet-detector/mod_network_restart.lua $(1)/usr/lib/lua/internet-detector/mod_network_restart.lua
$(INSTALL_DATA) ./files/usr/lib/lua/internet-detector/mod_public_ip.lua $(1)/usr/lib/lua/internet-detector/mod_public_ip.lua
$(INSTALL_DATA) ./files/usr/lib/lua/internet-detector/mod_user_scripts.lua $(1)/usr/lib/lua/internet-detector/mod_user_scripts.lua
$(INSTALL_DATA) ./files/usr/lib/lua/internet-detector/mod_regular_script.lua $(1)/usr/lib/lua/internet-detector/mod_regular_script.lua
endef
$(eval $(call BuildPackage,$(PKG_NAME)))

View File

@@ -36,3 +36,6 @@ config instance 'internet'
option mod_user_scripts_enabled '0'
option mod_user_scripts_alive_period '0'
option mod_user_scripts_dead_period '0'
option mod_regular_script_enabled '0'
option mod_regular_script_inet_state '2'
option mod_regular_script_interval '3600'

View File

@@ -0,0 +1 @@
# Shell commands that are run regularly

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

View File

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

View File

@@ -68,7 +68,10 @@ var Timefield = ui.Textfield.extend({
let string = '0';
if(/^\d+$/.test(value)) {
value = Number(value);
if(value >= 3600 && (value % 3600) === 0) {
if(value >= 86400 && (value % 86400) === 0) {
string = String(value / 86400) + 'd';
}
else if(value >= 3600 && (value % 3600) === 0) {
string = String(value / 3600) + 'h';
}
else if(value >= 60 && (value % 60) === 0) {
@@ -101,9 +104,12 @@ var Timefield = ui.Textfield.extend({
getValue() {
let rawValue = this.node.querySelector('input').value,
value = 0,
res = rawValue.match(/^(\d+)([hms]?)$/);
res = rawValue.match(/^(\d+)([dhms]?)$/);
if(res) {
if(res[2] === 'h') {
if(res[2] === 'd') {
value = Number(res[1]) * 86400;
}
else if(res[2] === 'h') {
value = Number(res[1]) * 3600;
}
else if(res[2] === 'm') {
@@ -321,8 +327,8 @@ return view.extend({
placeholder: _('Type a time string'),
validate : L.bind(
function(section, value) {
return (/^$|^\d+[hms]?$/.test(value)) ? true : _('Expecting:') +
` ${_('One of the following:')}\n - ${_('hours')}: 2h\n - ${_('minutes')}: 10m\n - ${_('seconds')}: 30s\n`;
return (/^$|^\d+[dhms]?$/.test(value)) ? true : _('Expecting:') +
` ${_('One of the following:')}\n - ${_('days')}: 1d\n - ${_('hours')}: 2h\n - ${_('minutes')}: 10m\n - ${_('seconds')}: 30s\n`;
},
this,
section_id
@@ -560,6 +566,7 @@ return view.extend({
/* Main settings */
// mode
let mode = s.option(form.ListValue, 'mode',
_('Internet detector mode'));
mode.value('0', _('Disabled'));
@@ -572,7 +579,6 @@ return view.extend({
);
mode.default = '0';
/* Service instances configuration */
if(this.currentAppMode !== '2') {
@@ -736,6 +742,7 @@ return view.extend({
s.tab('email', _('Email notification'));
};
s.tab('user_scripts', _('User scripts'));
s.tab('regular_script', _('Regular script'));
};
s.addModalOptions = (s, section_id, ev) => {
@@ -1160,7 +1167,7 @@ return view.extend({
o.rmempty = false;
o.modalonly = true;
// up_script edit dialog
// up_script edit
o = s.taboption('user_scripts', this.CBIBlockFileEdit, this,
'up_script',
this.configDir + '/up-script.' + s.section,
@@ -1178,7 +1185,7 @@ return view.extend({
o.rmempty = false;
o.modalonly = true;
// down_script edit dialog
// down_script edit
o = s.taboption('user_scripts', this.CBIBlockFileEdit, this,
'down_script',
this.configDir + '/down-script.' + s.section,
@@ -1195,6 +1202,48 @@ return view.extend({
o.default = '0';
o.rmempty = false;
o.modalonly = true;
// Regular script
o = s.taboption('regular_script', form.DummyValue, '_dummy');
o.rawhtml = true;
o.default = '<div class="cbi-section-descr">' +
_('Shell commands that are run regularly.') +
'</div>';
o.modalonly = true;
// enabled
o = s.taboption('regular_script', form.Flag, 'mod_regular_script_enabled',
_('Enabled'));
o.rmempty = false;
o.modalonly = true;
// inet_state
o = s.taboption('regular_script', form.ListValue,
'mod_regular_script_inet_state', _('Run if Internet state is')
);
o.modalonly = true;
o.value(0, _('connected'));
o.value(1, _('disconnected'));
o.value(2, _('connected or disconnected'));
o.default = '2';
// regular_script edit
o = s.taboption('regular_script', this.CBIBlockFileEdit, this,
'regular_script',
this.configDir + '/regular-script.' + s.section,
_('Edit regular-script'),
_('Shell commands that run regularly at a specified interval. Current state of the Internet is available as value of the <code>$INET_STATE</code> variable (<code>0</code> - connected, <code>1</code> - disconnected).')
);
o.modalonly = true;
// interval
o = s.taboption('regular_script', this.CBITimeInput,
'mod_regular_script_interval', _('Run interval')
);
o.default = '3600';
o.rmempty = false;
o.modalonly = true;
};
};

View File

@@ -113,6 +113,9 @@ msgstr "Изменить"
msgid "Edit down-script"
msgstr "Изменить down-script"
msgid "Edit regular-script"
msgstr "Изменить regular-script"
msgid "Edit public-ip-script"
msgstr "Изменить public-ip-script"
@@ -314,6 +317,9 @@ msgid ""
msgstr ""
"Период времени отсутствия доступа в Интренет перед перезагрузкой устройства."
msgid "Regular script"
msgstr "Регулярный скрипт"
msgid "Polling interval"
msgstr "Интервал опроса"
@@ -341,6 +347,12 @@ msgstr "Перезапуск службы"
msgid "Restart timeout"
msgstr "Таймаут перезапуска"
msgid "Run interval"
msgstr "Интервал запуска"
msgid "Run if Internet state is"
msgstr "Выполнять если статус Интернет"
msgid "Run service at startup"
msgstr "Запуск службы при старте"
@@ -389,9 +401,15 @@ msgstr "Экземпляры службы"
msgid "Set the modem to be allowed to use any band."
msgstr "Разрешить модему использование любой частоты."
msgid "Shell commands that are run regularly."
msgstr "Команды shell выполняемые регулярно."
msgid "Shell commands that run when connected to the Internet."
msgstr "Команды shell выполняемые при подключении к Интернет."
msgid "Shell commands that run regularly at a specified interval. Current state of the Internet is available as value of the <code>$INET_STATE</code> variable (<code>0</code> - connected, <code>1</code> - disconnected)."
msgstr "Команды shell выполняемые регулярно с заданным интервалом. Текущее состояние Интернет доступно как значение переменной <code>$INET_STATE</code> (<code>0</code> - подключен, <code>1</code> - отключен)."
msgid "Shell commands that run when the public IP address changes. New IP is available as value of the <code>$PUBLIC_IP</code> variable (empty string if undefined)."
msgstr "Команды shell выполняемые при изменении публичного IP адреса. Новый IP доступен как значение переменной <code>$PUBLIC_IP</code> (пустая строка если не определён)."
@@ -474,11 +492,23 @@ msgstr "Записывать сообщения в системный журна
msgid "after connection"
msgstr "после подключения"
msgid "after connection or disconnection"
msgstr "после подключения или отключения"
msgid "after disconnection"
msgstr "после отключения"
msgid "after connection or disconnection"
msgstr "после подключения или отключения"
msgid "connected"
msgstr "подключен"
msgid "connected or disconnected"
msgstr "подключен или отключен"
msgid "days"
msgstr "дни"
msgid "disconnected"
msgstr "отключен"
msgid "down-script"
msgstr "down-script"

View File

@@ -101,6 +101,9 @@ msgstr ""
msgid "Edit down-script"
msgstr ""
msgid "Edit regular-script"
msgstr ""
msgid "Edit public-ip-script"
msgstr ""
@@ -289,6 +292,9 @@ msgid ""
"Period of time without Internet access until the device is rebooted."
msgstr ""
msgid "Regular script"
msgstr ""
msgid "Polling interval"
msgstr ""
@@ -316,6 +322,12 @@ msgstr ""
msgid "Restart timeout"
msgstr ""
msgid "Run interval"
msgstr ""
msgid "Run if Internet state is"
msgstr ""
msgid "Run service at startup"
msgstr ""
@@ -364,9 +376,15 @@ msgstr ""
msgid "Set the modem to be allowed to use any band."
msgstr ""
msgid "Shell commands that are run regularly."
msgstr ""
msgid "Shell commands that run when connected to the Internet."
msgstr ""
msgid "Shell commands that run regularly at a specified interval. Current state of the Internet is available as value of the <code>$INET_STATE</code> variable (<code>0</code> - connected, <code>1</code> - disconnected)."
msgstr ""
msgid "Shell commands that run when the public IP address changes. New IP is available as value of the <code>$PUBLIC_IP</code> variable (empty string if undefined)."
msgstr ""
@@ -448,7 +466,16 @@ msgstr ""
msgid "after disconnection"
msgstr ""
msgid "both"
msgid "connected"
msgstr ""
msgid "connected or disconnected"
msgstr ""
msgid "days"
msgstr ""
msgid "disconnected"
msgstr ""
msgid "down-script"

View File

@@ -6,7 +6,8 @@
"/sys/class/leds": [ "list" ],
"/etc/internet-detector/up-script*": [ "read" ],
"/etc/internet-detector/down-script*": [ "read" ],
"/etc/internet-detector/public-ip-script*": [ "read" ]
"/etc/internet-detector/public-ip-script*": [ "read" ],
"/etc/internet-detector/regular-script*": [ "read" ]
},
"uci": [ "internet-detector" ],
"ubus": {
@@ -18,7 +19,8 @@
"file": {
"/etc/internet-detector/up-script*": [ "write" ],
"/etc/internet-detector/down-script*": [ "write" ],
"/etc/internet-detector/public-ip-script*": [ "write" ]
"/etc/internet-detector/public-ip-script*": [ "write" ],
"/etc/internet-detector/regular-script*": [ "write" ]
},
"uci": [ "internet-detector" ]
}