NIU:
[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 fs  = require "nixio.fs"
18
19
20 local function date_format(secs)
21         local suff = {"min", "h", "d"}
22         local mins = 0
23         local hour = 0
24         local days = 0
25         
26         secs = math.floor(secs)
27         if secs > 60 then
28                 mins = math.floor(secs / 60)
29                 secs = secs % 60
30         end
31         
32         if mins > 60 then
33                 hour = math.floor(mins / 60)
34                 mins = mins % 60
35         end
36         
37         if hour > 24 then
38                 days = math.floor(hour / 24)
39                 hour = hour % 24
40         end
41         
42         if days > 0 then
43                 return string.format("%.0fd %02.0fh %02.0fmin %02.0fs", days, hour, mins, secs)
44         else
45                 return string.format("%02.0fh %02.0fmin %02.0fs", hour, mins, secs)
46         end
47 end
48
49 m2 = Map("dhcp", "Display and Customize Address Assignment")
50
51 local leasefn, leasefp, leases
52 uci:foreach("dhcp", "dnsmasq",
53  function(section)
54         leasefn = section.leasefile
55  end
56
57 local leasefp = leasefn and fs.access(leasefn) and io.lines(leasefn)
58 if leasefp then
59         leases = {}
60         for lease in leasefp do
61                 table.insert(leases, luci.util.split(lease, " "))
62         end
63 end
64
65 if leases then
66         v = m2:section(Table, leases, translate("Active Leases"))
67         ip = v:option(DummyValue, 3, translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
68         
69         mac  = v:option(DummyValue, 2, translate("<abbr title=\"Media Access Control\">MAC</abbr>-Address"))
70         
71         ltime = v:option(DummyValue, 1, translate("Leasetime remaining"))
72         function ltime.cfgvalue(self, ...)
73                 local value = DummyValue.cfgvalue(self, ...)
74                 return date_format(os.difftime(tonumber(value), os.time()))
75         end
76 end
77
78 s = m2:section(TypedSection, "host", "Static Assignment",
79 "You can assign fixed addresses and DNS names to devices in you local network to make reaching them more easy.")
80 s.addremove = true
81 s.anonymous = true
82 s.template = "cbi/tblsection"
83
84 hn = s:option(Value, "name", translate("Hostname"))
85 mac = s:option(Value, "mac", translate("<abbr title=\"Media Access Control\">MAC</abbr>-Address"))
86 ip = s:option(Value, "ip", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
87 sys.net.arptable(function(entry)
88         ip:value(entry["IP address"])
89         mac:value(
90                 entry["HW address"],
91                 entry["HW address"] .. " (" .. entry["IP address"] .. ")"
92         )
93 end)
94
95         
96 return m2