Rework LuCI build system
[project/luci.git] / applications / luci-app-ahcp / luasrc / controller / ahcp.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2011 Jo-Philipp Wich <xm@subsignal.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: init.lua 6731 2011-01-14 19:44:03Z soma $
13 ]]--
14
15 module("luci.controller.ahcp", package.seeall)
16
17 function index()
18         if not nixio.fs.access("/etc/config/ahcpd") then
19                 return
20         end
21
22         entry({"admin", "network", "ahcpd"}, cbi("ahcp"), _("AHCP Server"), 90)
23         entry({"admin", "network", "ahcpd", "status"}, call("ahcp_status"))
24 end
25
26 function ahcp_status()
27         local nfs = require "nixio.fs"
28         local uci = require "luci.model.uci".cursor()
29         local lsd = uci:get_first("ahcpd", "ahcpd", "lease_dir") or "/var/lib/leases"
30         local idf = uci:get_first("ahcpd", "ahcpd", "id_file")   or "/var/lib/ahcpd-unique-id"
31
32         local rv = {
33                 uid    = "00:00:00:00:00:00:00:00",
34                 leases = { }
35         }
36
37         idf = nfs.readfile(idf)
38         if idf and #idf == 8 then
39                 rv.uid = "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X" %{ idf:byte(1, 8) }
40         end
41
42         local itr = nfs.dir(lsd)
43         if itr then
44                 local addr
45                 for addr in itr do
46                         if addr:match("^%d+%.%d+%.%d+%.%d+$") then
47                                 local s = nfs.stat(lsd .. "/" .. addr)
48                                 rv.leases[#rv.leases+1] = {
49                                         addr = addr,
50                                         age  = s and (os.time() - s.mtime) or 0
51                                 }
52                         end
53                 end
54         end
55
56         table.sort(rv.leases, function(a, b) return a.age < b.age end)
57
58         luci.http.prepare_content("application/json")
59         luci.http.write_json(rv)
60 end