luci-mod-status: 29_ports.js: attempt to use getBuiltinEthernetPorts
authorJo-Philipp Wich <jo@mein.io>
Thu, 24 Aug 2023 14:46:02 +0000 (16:46 +0200)
committerJo-Philipp Wich <jo@mein.io>
Thu, 24 Aug 2023 14:50:05 +0000 (16:50 +0200)
Try to use the new luci/getBuiltinEthernetPorts RPC call to enumerate known
ports and fall back to manual board.json parsing if the call is unavailable
yet. The fallback code will be dropped in a while when everything settled.

Ref: #6534, #6538
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
(cherry picked from commit ed059e4cd3ee9b15aabcc974b4e66b0090b920ae)

modules/luci-mod-status/htdocs/luci-static/resources/view/status/include/29_ports.js

index b226e4f3c60cef8ba4e1d0ff2d7bef515aaf8310..1351ab935a55c206d75f0927ed7e154987cf3d48 100644 (file)
@@ -3,9 +3,16 @@
 'require fs';
 'require ui';
 'require uci';
+'require rpc';
 'require network';
 'require firewall';
 
+var callGetBuiltinEthernetPorts = rpc.declare({
+       object: 'luci',
+       method: 'getBuiltinEthernetPorts',
+       expect: { result: [] }
+});
+
 function isString(v)
 {
        return typeof(v) === 'string' && v !== '';
@@ -284,6 +291,7 @@ return baseclass.extend({
 
        load: function() {
                return Promise.all([
+                       L.resolveDefault(callGetBuiltinEthernetPorts(), []),
                        L.resolveDefault(fs.read('/etc/board.json'), '{}'),
                        firewall.getZones(),
                        network.getNetworks(),
@@ -295,28 +303,36 @@ return baseclass.extend({
                if (L.hasSystemFeature('swconfig'))
                        return null;
 
-               var board = JSON.parse(data[0]),
+               var board = JSON.parse(data[1]),
                    known_ports = [],
-                   port_map = buildInterfaceMapping(data[1], data[2]);
-
-               if (L.isObject(board) && L.isObject(board.network)) {
-                       for (var k = 'lan'; k != null; k = (k == 'lan') ? 'wan' : null) {
-                               if (!L.isObject(board.network[k]))
-                                       continue;
+                   port_map = buildInterfaceMapping(data[2], data[3]);
 
-                               if (Array.isArray(board.network[k].ports))
-                                       for (let i = 0; i < board.network[k].ports.length; i++)
+               if (Array.isArray(data[0]) && data[0].length > 0) {
+                       known_ports = data[0].map(port => ({
+                               ...port,
+                               netdev: network.instantiateDevice(port.device)
+                       }));
+               }
+               else {
+                       if (L.isObject(board) && L.isObject(board.network)) {
+                               for (var k = 'lan'; k != null; k = (k == 'lan') ? 'wan' : null) {
+                                       if (!L.isObject(board.network[k]))
+                                               continue;
+
+                                       if (Array.isArray(board.network[k].ports))
+                                               for (let i = 0; i < board.network[k].ports.length; i++)
+                                                       known_ports.push({
+                                                               role: k,
+                                                               device: board.network[k].ports[i],
+                                                               netdev: network.instantiateDevice(board.network[k].ports[i])
+                                                       });
+                                       else if (typeof(board.network[k].device) == 'string')
                                                known_ports.push({
                                                        role: k,
-                                                       device: board.network[k].ports[i],
-                                                       netdev: network.instantiateDevice(board.network[k].ports[i])
+                                                       device: board.network[k].device,
+                                                       netdev: network.instantiateDevice(board.network[k].device)
                                                });
-                               else if (typeof(board.network[k].device) == 'string')
-                                       known_ports.push({
-                                               role: k,
-                                               device: board.network[k].device,
-                                               netdev: network.instantiateDevice(board.network[k].device)
-                                       });
+                               }
                        }
                }