modules/admin-core, modules/admin-full: fix switch port status for devices with multiple switches

This commit is contained in:
Jo-Philipp Wich 2012-11-01 20:03:36 +00:00
parent 59af400346
commit 36f88887c1
3 changed files with 61 additions and 56 deletions

View file

@ -153,35 +153,40 @@ function wifi_network(id)
return { } return { }
end end
function switch_status(dev) function switch_status(devs)
local ports = { } local dev
local swc = io.popen("swconfig dev %q show" % dev, "r") local switches = { }
if swc then for dev in devs:gmatch("[^%s,]+") do
local l local ports = { }
repeat local swc = io.popen("swconfig dev %q show" % dev, "r")
l = swc:read("*l") if swc then
if l then local l
local port, up = l:match("port:(%d+) link:(%w+)") repeat
if port then l = swc:read("*l")
local speed = l:match(" speed:(%d+)") if l then
local duplex = l:match(" (%w+)-duplex") local port, up = l:match("port:(%d+) link:(%w+)")
local txflow = l:match(" (txflow)") if port then
local rxflow = l:match(" (rxflow)") local speed = l:match(" speed:(%d+)")
local auto = l:match(" (auto)") local duplex = l:match(" (%w+)-duplex")
local txflow = l:match(" (txflow)")
local rxflow = l:match(" (rxflow)")
local auto = l:match(" (auto)")
ports[#ports+1] = { ports[#ports+1] = {
port = tonumber(port) or 0, port = tonumber(port) or 0,
speed = tonumber(speed) or 0, speed = tonumber(speed) or 0,
link = (up == "up"), link = (up == "up"),
duplex = (duplex == "full"), duplex = (duplex == "full"),
rxflow = (not not rxflow), rxflow = (not not rxflow),
txflow = (not not txflow), txflow = (not not txflow),
auto = (not not auto) auto = (not not auto)
} }
end
end end
end until not l
until not l swc:close()
swc:close() end
switches[dev] = ports
end end
return ports return switches
end end

View file

@ -14,6 +14,8 @@ You may obtain a copy of the License at
m = Map("network", translate("Switch"), translate("The network ports on this device can be combined to several <abbr title=\"Virtual Local Area Network\">VLAN</abbr>s in which computers can communicate directly with each other. <abbr title=\"Virtual Local Area Network\">VLAN</abbr>s are often used to separate different network segments. Often there is by default one Uplink port for a connection to the next greater network like the internet and other ports for a local network.")) m = Map("network", translate("Switch"), translate("The network ports on this device can be combined to several <abbr title=\"Virtual Local Area Network\">VLAN</abbr>s in which computers can communicate directly with each other. <abbr title=\"Virtual Local Area Network\">VLAN</abbr>s are often used to separate different network segments. Often there is by default one Uplink port for a connection to the next greater network like the internet and other ports for a local network."))
local switches = { }
m.uci:foreach("network", "switch", m.uci:foreach("network", "switch",
function(x) function(x)
local sid = x['.name'] local sid = x['.name']
@ -199,7 +201,7 @@ m.uci:foreach("network", "switch",
end end
local vid = s:option(Value, has_vlan4k or "vlan", "VLAN ID") local vid = s:option(Value, has_vlan4k or "vlan", "VLAN ID", "<div id='portstatus-%s'></div>" % switch_name)
vid.rmempty = false vid.rmempty = false
vid.forcewrite = true vid.forcewrite = true
@ -276,12 +278,13 @@ m.uci:foreach("network", "switch",
port_opts[#port_opts+1] = po port_opts[#port_opts+1] = po
end end
switches[#switches+1] = switch_name
-- Switch status template
s = m:section(SimpleSection)
s.template = "admin_network/switch_status"
s.switch = switch_name
end end
) )
-- Switch status template
s = m:section(SimpleSection)
s.template = "admin_network/switch_status"
s.switches = switches
return m return m

View file

@ -1,31 +1,28 @@
<script type="text/javascript">//<![CDATA[ <script type="text/javascript">//<![CDATA[
var tb; var switches = [ '<%=table.concat(self.switches, "', '")%>' ];
var ths = document.getElementsByTagName('th'); XHR.poll(5, '<%=luci.dispatcher.build_url("admin", "network", "switch_status")%>/' + switches.join(','), null,
for (var i = 0; i < ths.length; i++) function(x, st)
if (ths[i].className = 'cbi-section-table-cell' && !ths[i].innerHTML)
{ {
ths[i].innerHTML = '<%:Port status:%>'; for (var i = 0; i < switches.length; i++)
tb = ths[i].parentNode;
break;
}
if (tb)
{
XHR.poll(5, '<%=luci.dispatcher.build_url("admin", "network", "switch_status", self.switch)%>', null,
function(x, st)
{ {
if (st && st.length) var ports = st[switches[i]];
{ var th0 = document.getElementById('portstatus-' + switches[i]);
for (var i = 0; i < st.length; i++)
{
var th = tb.childNodes[i+1];
if (st[i].link) if (th0 && ports && ports.length)
{
if (!th0.innerHTML)
th0.innerHTML = '<%:Port status:%>';
for (var j = 0; j < ports.length; j++)
{
var th = th0.parentNode.parentNode.childNodes[j+1];
if (ports[j].link)
{ {
th.innerHTML = String.format( th.innerHTML = String.format(
'<small><img src="<%=resource%>/icons/port_up.png" />' + '<small><img src="<%=resource%>/icons/port_up.png" />' +
'<br />%d<%:baseT%><br />%s</small>', '<br />%d<%:baseT%><br />%s</small>',
st[i].speed, st[i].duplex ports[j].speed, ports[j].duplex
? '<%:full-duplex%>' : '<%:half-duplex%>' ? '<%:full-duplex%>' : '<%:half-duplex%>'
); );
} }
@ -39,6 +36,6 @@
} }
} }
} }
); }
} );
//]]></script> //]]></script>