From f92713818d4a0ac425f2b59b774e7ed5ddf31365 Mon Sep 17 00:00:00 2001 From: gSpot Date: Thu, 3 Aug 2023 16:57:06 +0300 Subject: [PATCH] mod_led_control: LED actions --- README.md | 18 ++--- internet-detector/Makefile | 2 +- .../lib/internet-detector/mod_led_control.lua | 74 +++++++++++++++---- luci-app-internet-detector/Makefile | 2 +- .../resources/view/internet-detector.js | 36 +++++++-- .../po/ru/internet-detector.po | 20 ++++- .../po/templates/internet-detector.pot | 18 ++++- 7 files changed, 131 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 6e44c12..0dafd24 100644 --- a/README.md +++ b/README.md @@ -19,15 +19,15 @@ Internet-detector is an application for checking the availability of the Interne **OpenWrt >= 21.02:** opkg update - wget --no-check-certificate -O /tmp/internet-detector_1.0-0_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/internet-detector_1.0-0_all.ipk - opkg install /tmp/internet-detector_1.0-0_all.ipk - rm /tmp/internet-detector_1.0-0_all.ipk + wget --no-check-certificate -O /tmp/internet-detector_1.0-1_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/internet-detector_1.0-1_all.ipk + opkg install /tmp/internet-detector_1.0-1_all.ipk + rm /tmp/internet-detector_1.0-1_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.0-0_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/luci-app-internet-detector_1.0-0_all.ipk - opkg install /tmp/luci-app-internet-detector_1.0-0_all.ipk - rm /tmp/luci-app-internet-detector_1.0-0_all.ipk + wget --no-check-certificate -O /tmp/luci-app-internet-detector_1.0-1_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/luci-app-internet-detector_1.0-1_all.ipk + opkg install /tmp/luci-app-internet-detector_1.0-1_all.ipk + rm /tmp/luci-app-internet-detector_1.0-1_all.ipk /etc/init.d/rpcd restart Email notification: @@ -36,9 +36,9 @@ Email notification: i18n-ru: - wget --no-check-certificate -O /tmp/luci-i18n-internet-detector-ru_1.0-0_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/luci-i18n-internet-detector-ru_1.0-0_all.ipk - opkg install /tmp/luci-i18n-internet-detector-ru_1.0-0_all.ipk - rm /tmp/luci-i18n-internet-detector-ru_1.0-0_all.ipk + wget --no-check-certificate -O /tmp/luci-i18n-internet-detector-ru_1.0-1_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/luci-i18n-internet-detector-ru_1.0-1_all.ipk + opkg install /tmp/luci-i18n-internet-detector-ru_1.0-1_all.ipk + rm /tmp/luci-i18n-internet-detector-ru_1.0-1_all.ipk **[OpenWrt 19.07](https://github.com/gSpotx2f/luci-app-internet-detector/tree/19.07)** diff --git a/internet-detector/Makefile b/internet-detector/Makefile index c893897..aa09e15 100644 --- a/internet-detector/Makefile +++ b/internet-detector/Makefile @@ -6,7 +6,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=internet-detector PKG_VERSION:=1.0 -PKG_RELEASE:=0 +PKG_RELEASE:=1 PKG_MAINTAINER:=gSpot include $(INCLUDE_DIR)/package.mk diff --git a/internet-detector/files/usr/lib/internet-detector/mod_led_control.lua b/internet-detector/files/usr/lib/internet-detector/mod_led_control.lua index a888206..dc547a2 100644 --- a/internet-detector/files/usr/lib/internet-detector/mod_led_control.lua +++ b/internet-detector/files/usr/lib/internet-detector/mod_led_control.lua @@ -11,12 +11,15 @@ local Module = { runInterval = 5, sysLedsDir = "/sys/class/leds", ledName = nil, + ledAction1 = 2, + ledAction2 = 1, status = nil, _enabled = false, _ledDir = nil, _ledMaxBrightnessFile = nil, _ledBrightnessFile = nil, _ledMaxBrightness = nil, + _ledTriggerFile = nil, _counter = 0, } @@ -38,12 +41,17 @@ function Module:init(t) if not self.ledName then return end + self.ledAction1 = tonumber(t.led_action_1) + self.ledAction2 = tonumber(t.led_action_2) self._ledDir = string.format("%s/%s", self.sysLedsDir, self.ledName) self._ledMaxBrightnessFile = string.format("%s/max_brightness", self._ledDir) self._ledBrightnessFile = string.format("%s/brightness", self._ledDir) self._ledMaxBrightness = self.readValue(self._ledMaxBrightnessFile) or 1 + self._ledTriggerFile = string.format("%s/trigger", self._ledDir) + if (not unistd.access(self._ledDir, "r") or - not unistd.access(self._ledBrightnessFile, "rw")) then + not unistd.access(self._ledBrightnessFile, "rw") or + not unistd.access(self._ledTriggerFile, "rw")) then self._enabled = false self.syslog("warning", string.format( "%s: LED '%s' is not available", self.name, self.ledName)) @@ -54,6 +62,31 @@ function Module:init(t) end end +function Module:SetTriggerTimer() + self.writeValue(self._ledTriggerFile, "timer") +end + +function Module:SetTriggerNone() + self.writeValue(self._ledTriggerFile, "none") +end + +function Module:getCurrentTrigger() + local trigger = self.readValue(self._ledTriggerFile) + if trigger and trigger:match("%[timer%]") then + return 1 + end +end + +function Module:on() + self:SetTriggerNone() + self.writeValue(self._ledBrightnessFile, self._ledMaxBrightness) +end + +function Module:off() + self:SetTriggerNone() + self.writeValue(self._ledBrightnessFile, 0) +end + function Module:getCurrentState() local state = self.readValue(self._ledBrightnessFile) if state and tonumber(state) > 0 then @@ -61,33 +94,42 @@ function Module:getCurrentState() end end -function Module:on() - self.writeValue(self._ledBrightnessFile, self._ledMaxBrightness) -end - -function Module:off() - self.writeValue(self._ledBrightnessFile, 0) -end - function Module:run(currentStatus, lastStatus, timeDiff) if not self._enabled then return end if self._counter == 0 or self._counter >= self.runInterval or currentStatus ~= lastStatus then - if currentStatus == 0 then - if not self:getCurrentState() then - self:on() + if self.ledAction1 == 1 then + if self:getCurrentState() or self:getCurrentTrigger() then + self:off() + end + elseif self.ledAction1 == 2 then + if not self:getCurrentState() or self:getCurrentTrigger() then + self:on() + end + elseif self.ledAction1 == 3 then + if not self:getCurrentTrigger() then + self:SetTriggerTimer() + end end else - if self:getCurrentState() then - self:off() + if self.ledAction2 == 1 then + if self:getCurrentState() or self:getCurrentTrigger() then + self:off() + end + elseif self.ledAction2 == 2 then + if not self:getCurrentState() or self:getCurrentTrigger() then + self:on() + end + elseif self.ledAction2 == 3 then + if not self:getCurrentTrigger() then + self:SetTriggerTimer() + end end end - self._counter = 0 end - self._counter = self._counter + timeDiff end diff --git a/luci-app-internet-detector/Makefile b/luci-app-internet-detector/Makefile index 0b3ca63..2a85975 100644 --- a/luci-app-internet-detector/Makefile +++ b/luci-app-internet-detector/Makefile @@ -4,7 +4,7 @@ include $(TOPDIR)/rules.mk -PKG_VERSION:=1.0-0 +PKG_VERSION:=1.0-1 LUCI_TITLE:=LuCI support for internet-detector LUCI_DEPENDS:=+internet-detector LUCI_PKGARCH:=all diff --git a/luci-app-internet-detector/htdocs/luci-static/resources/view/internet-detector.js b/luci-app-internet-detector/htdocs/luci-static/resources/view/internet-detector.js index 5895431..baa549d 100644 --- a/luci-app-internet-detector/htdocs/luci-static/resources/view/internet-detector.js +++ b/luci-app-internet-detector/htdocs/luci-static/resources/view/internet-detector.js @@ -194,7 +194,6 @@ return view.extend({ if(!this.inetStatus || !this.inetStatus.instances || this.inetStatus.instances.length === 0) { let label = E('span', { 'class': 'id-label-status id-undefined' }, _('Undefined')) - if(this.currentAppMode !== '0' && this.appStatus !== 'stoped') { label.classList.add('spinning'); }; @@ -251,9 +250,8 @@ return view.extend({ ]).then(stat => { let curAppStatus = (stat[0].code === 0) ? stat[0].stdout.trim() : null; let inetStatData = this.inetStatusFromJson(stat[1]); - - this.appStatus = curAppStatus; - this.inetStatus = inetStatData; + this.appStatus = curAppStatus; + this.inetStatus = inetStatData; this.setInternetStatus(); }).catch(e => { this.appStatus = 'stoped'; @@ -273,9 +271,11 @@ return view.extend({ return fs.exec(this.execPath, [ 'poll' ]).then(res => { let inetStatData = this.inetStatusFromJson(res); + if(inetStatData.instances[0]) { this.uiPollState = inetStatData.instances[0].inet; }; + this.inetStatus = inetStatData; this.setInternetStatus(); }); @@ -330,6 +330,7 @@ return view.extend({ renderWidget: function(section_id, option_index, cfgvalue) { this.ctx.setInternetStatus(); + return E([ E('label', { 'class': 'cbi-value-title', 'for': 'inetStatusArea' }, _('Internet status') @@ -652,6 +653,7 @@ return view.extend({ 'hosts', _('Hosts'), _('Hosts to check Internet availability. Hosts are polled (in list order) until at least one of them responds.') ); + //o.datatype = 'or(host,hostport)'; o.datatype = 'or(or(host,hostport),ipaddrport(1))'; o.default = this.defaultHosts; o.rmempty = false; @@ -755,11 +757,12 @@ return view.extend({ o = ss.taboption('led_control', form.DummyValue, '_dummy'); o.rawhtml = true; o.default = '
' + - _('LED is on when Internet is available.') + + _('LED indicates the Internet status.') + '
'; o.modalonly = true; if(this.leds.length > 0) { + this.leds.sort((a, b) => a.name > b.name); // enabled o = ss.taboption('led_control', form.Flag, 'mod_led_control_enabled', @@ -772,8 +775,27 @@ return view.extend({ _('LED Name')); o.depends({ mod_led_control_enabled: '1' }); o.modalonly = true; - this.leds.sort((a, b) => a.name > b.name); this.leds.forEach(e => o.value(e.name)); + + // led_action_1 + o = ss.taboption('led_control', form.ListValue, 'mod_led_control_led_action_1', + _('Action when connected')); + o.depends({ mod_led_control_enabled: '1' }); + o.modalonly = true; + o.value(1, _('Off')); + o.value(2, _('On')); + o.value(3, _('Blink')); + o.default = '2'; + + // led_action_2 + o = ss.taboption('led_control', form.ListValue, 'mod_led_control_led_action_2', + _('Action when disconnected')); + o.depends({ mod_led_control_enabled: '1' }); + o.modalonly = true; + o.value(1, _('Off')); + o.value(2, _('On')); + o.value(3, _('Blink')); + o.default = '1'; } else { o = ss.taboption('led_control', form.DummyValue, '_dummy'); o.rawhtml = true; @@ -1176,7 +1198,7 @@ return view.extend({ 'iface', _('Interface'), _('Network interface for Internet access. If not specified, the default interface is used.') ); - o.noaliases = true; + o.noaliases = true; // interval_up o = ss.option(form.ListValue, diff --git a/luci-app-internet-detector/po/ru/internet-detector.po b/luci-app-internet-detector/po/ru/internet-detector.po index c5a24a0..5f94903 100644 --- a/luci-app-internet-detector/po/ru/internet-detector.po +++ b/luci-app-internet-detector/po/ru/internet-detector.po @@ -20,9 +20,14 @@ msgid "LED control" msgstr "Управление LED" msgid "" -"LED is on when Internet is " -"available." -msgstr "LED включен если Интернет доступен." +"LED indicates the Internet status." +msgstr "LED отображает статус Интернет." + +msgid "Action when connected" +msgstr "Действие при подключении" + +msgid "Action when disconnected" +msgstr "Действие при отключении" msgid "Add instance" msgstr "Добавить экземпляр" @@ -44,6 +49,9 @@ msgstr "Произошла ошибка" msgid "Big: 248 bytes" msgstr "Большой: 248 байт" +msgid "Blink" +msgstr "Мигать" + msgid "Check type" msgstr "Тип проверки" @@ -291,6 +299,12 @@ msgstr "Сеть будет перезапущена при отключении msgid "No LEDs available..." msgstr "Нет доступных LED..." +msgid "Off" +msgstr "Выключить" + +msgid "On" +msgstr "Включить" + msgid "One of the following:" msgstr "Одно из следующих значений:" diff --git a/luci-app-internet-detector/po/templates/internet-detector.pot b/luci-app-internet-detector/po/templates/internet-detector.pot index 4be8c77..1f6ffa2 100644 --- a/luci-app-internet-detector/po/templates/internet-detector.pot +++ b/luci-app-internet-detector/po/templates/internet-detector.pot @@ -8,8 +8,13 @@ msgid "LED control" msgstr "" msgid "" -"LED is on when Internet is " -"available." +"LED indicates the Internet status." +msgstr "" + +msgid "Action when connected" +msgstr "" + +msgid "Action when disconnected" msgstr "" msgid "Add instance" @@ -32,6 +37,9 @@ msgstr "" msgid "Big: 248 bytes" msgstr "" +msgid "Blink" +msgstr "" + msgid "Check type" msgstr "" @@ -261,6 +269,12 @@ msgstr "" msgid "No LEDs available..." msgstr "" +msgid "Off" +msgstr "" + +msgid "On" +msgstr "" + msgid "One of the following:" msgstr ""