From 5f686cdb57e3d958bb9ba2a972619ee553f9829b Mon Sep 17 00:00:00 2001 From: Paul Spooren Date: Thu, 30 Jan 2025 23:33:46 +0000 Subject: [PATCH 1/2] luci-mod-status: add `oneshot` handling This allows each include to perform an action a single time but don't run it on each poll. The function receives all data loaded via the `load` function. Signed-off-by: Paul Spooren --- .../htdocs/luci-static/resources/view/status/index.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/modules/luci-mod-status/htdocs/luci-static/resources/view/status/index.js b/modules/luci-mod-status/htdocs/luci-static/resources/view/status/index.js index da08ec12c270..911c187584e5 100644 --- a/modules/luci-mod-status/htdocs/luci-static/resources/view/status/index.js +++ b/modules/luci-mod-status/htdocs/luci-static/resources/view/status/index.js @@ -40,6 +40,11 @@ function startPolling(includes, containers) { else if (includes[i].content != null) content = includes[i].content; + if (typeof (includes[i].oneshot) == 'function') { + includes[i].oneshot(results ? results[i] : null); + includes[i].oneshot = null; + } + if (content != null) { containers[i].parentNode.style.display = ''; containers[i].parentNode.classList.add('fade-in'); From 08d26561c6a9801d0a8dd8926b4c36c2518e67c0 Mon Sep 17 00:00:00 2001 From: Paul Spooren Date: Thu, 30 Jan 2025 23:36:34 +0000 Subject: [PATCH 2/2] luci-mod-status: add firmware upgrade check If the UCI luci core section contain the option `check_firmware_version` then a load of the status page checks if the download server offers a new firmware compatible with the currently running device. Signed-off-by: Paul Spooren --- .../view/status/include/10_system.js | 72 ++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/modules/luci-mod-status/htdocs/luci-static/resources/view/status/include/10_system.js b/modules/luci-mod-status/htdocs/luci-static/resources/view/status/include/10_system.js index 45f7b4acae6d..a673a97f9927 100644 --- a/modules/luci-mod-status/htdocs/luci-static/resources/view/status/include/10_system.js +++ b/modules/luci-mod-status/htdocs/luci-static/resources/view/status/include/10_system.js @@ -2,6 +2,8 @@ 'require baseclass'; 'require fs'; 'require rpc'; +'require uci' +'require ui'; var callLuciVersion = rpc.declare({ object: 'luci', @@ -18,6 +20,43 @@ var callSystemInfo = rpc.declare({ method: 'info' }); +function showUpgradeModal(type, current_version, new_version) { + ui.showModal(_('New Firmware Available'), [ + E('p', _('A new %s version of OpenWrt is available: %s').format(type, new_version)), + E('p', _('Your current version is: %s').format(current_version)), + E('p', _('Please check the https://firmware-selector.openwrt.org/ for the firmware upgrade image')), + E('div', { class: 'btn', click: ui.hideModal }, _('Close')), + ]); +}; + +function checkDeviceAvailable(boardinfo, new_version) { + const profile_url = 'https://downloads.openwrt.org/releases/%s/targets/%s/profiles.json'.format(new_version, boardinfo.release.target); + return fetch(profile_url) + .then(response => response.json()) + .then(data => { + // special case for x86 and armsr + if (Object.keys(data.profiles).length == 1 && Object.keys(data.profiles)[0] == "generic") { + return true; + } + + for (var i = 0; i < data.profiles.length; i++) { + if (data.profiles[i].profile == boardinfo.board_name) { + return true; + } + for (var j = 0; j < data.profiles[i].supported_devices.length; j++) { + if (data.profiles[i].supported_devices[j] == boardinfo.board_name) { + return true; + } + } + } + }) + .catch(error => { + console.error('Failed to fetch profile information:', error); + return false; + }); +}; + + return baseclass.extend({ title: _('System'), @@ -25,10 +64,41 @@ return baseclass.extend({ return Promise.all([ L.resolveDefault(callSystemBoard(), {}), L.resolveDefault(callSystemInfo(), {}), - L.resolveDefault(callLuciVersion(), { revision: _('unknown version'), branch: 'LuCI' }) + L.resolveDefault(callLuciVersion(), { revision: _('unknown version'), branch: 'LuCI' }), + uci.load('luci') ]); }, + oneshot: function(data) { + var boardinfo = data[0]; + var check_upgrades = uci.get_first('luci', 'core', 'check_firmware_version') || false; + console.log(check_upgrades) + + if (check_upgrades == '1' || check_upgrades == 'true') { + fetch('https://downloads.openwrt.org/.versions.json') + .then(response => response.json()) + .then(data => { + if (data.oldstable_version + && data.oldstable_version > boardinfo.release.version + && checkDeviceAvailable(boardinfo, data.oldstable_version)) { + showUpgradeModal("oldstable", boardinfo.release.version, data.oldstable_version) + } else if (data.stable_version + && data.stable_version > boardinfo.release.version + && checkDeviceAvailable(boardinfo, data.stable_version)) { + showUpgradeModal("stable", boardinfo.release.version, data.stable_version) + } else if (data.upcoming_version + && boardinfo.release.version > data.stable_version + && data.upcoming_version > boardinfo.release.version + && checkDeviceAvailable(boardinfo, data.upcoming_version)) { + showUpgradeModal("upcomming", boardinfo.release.version, data.upcoming_version) + } + }) + .catch(error => { + console.error('Failed to fetch version information:', error); + }); + } + }, + render: function(data) { var boardinfo = data[0], systeminfo = data[1],