JSON: Add encode / decode shortcut
[project/luci.git] / modules / niu / luasrc / model / cbi / niu / network / assign1.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14
15 local uci = require "luci.model.uci".cursor()
16 local sys = require "luci.sys"
17 local wa  = require "luci.tools.webadmin"
18 local fs  = require "nixio.fs"
19
20 m2 = Map("dhcp", "Address Assignment")
21
22 local leasefn, leasefp, leases
23 uci:foreach("dhcp", "dnsmasq",
24  function(section)
25         leasefn = section.leasefile
26  end
27
28 local leasefp = leasefn and fs.access(leasefn) and io.lines(leasefn)
29 if leasefp then
30         leases = {}
31         for lease in leasefp do
32                 table.insert(leases, luci.util.split(lease, " "))
33         end
34 end
35
36 if leases then
37         v = m2:section(Table, leases, translate("Active Leases"))
38         ip = v:option(DummyValue, 3, translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
39         
40         mac  = v:option(DummyValue, 2, translate("<abbr title=\"Media Access Control\">MAC</abbr>-Address"))
41         
42         ltime = v:option(DummyValue, 1, translate("Leasetime remaining"))
43         function ltime.cfgvalue(self, ...)
44                 local value = DummyValue.cfgvalue(self, ...)
45                 return wa.date_format(os.difftime(tonumber(value), os.time()))
46         end
47 end
48
49 s = m2:section(TypedSection, "host")
50 s.addremove = true
51 s.anonymous = true
52 s.template = "cbi/tblsection"
53
54 hn = s:option(Value, "name", translate("Hostname"))
55 mac = s:option(Value, "mac", translate("<abbr title=\"Media Access Control\">MAC</abbr>-Address"))
56 ip = s:option(Value, "ip", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
57 sys.net.arptable(function(entry)
58         ip:value(entry["IP address"])
59         mac:value(
60                 entry["HW address"],
61                 entry["HW address"] .. " (" .. entry["IP address"] .. ")"
62         )
63 end)
64
65         
66 return m2