luci-mod-admin-mini: remove uses of luci.sys.sysinfo()
[project/luci.git] / modules / luci-mod-admin-mini / luasrc / model / cbi / mini / system.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 require("luci.sys")
16 require("luci.sys.zoneinfo")
17 require("luci.tools.webadmin")
18 require("luci.util")
19
20
21 m = Map("system", translate("System"), translate("Here you can configure the basic aspects of your device like its hostname or the timezone."))
22
23 s = m:section(TypedSection, "system", "")
24 s.anonymous = true
25 s.addremove = false
26
27
28 local sysinfo = luci.util.ubus("system", "info") or { }
29 local boardinfo = luci.util.ubus("system", "board") or { }
30
31 local uptime = sysinfo.uptime or 0
32 local loads = sysinfo.load or { 0, 0, 0 }
33 local memory = sysinfo.memory or {
34         total = 0,
35         free = 0,
36         buffered = 0,
37         shared = 0
38 }
39
40 s:option(DummyValue, "_system", translate("Model")).value = boardinfo.model or "?"
41 s:option(DummyValue, "_cpu", translate("System")).value = boardinfo.system or "?"
42
43 s:option(DummyValue, "_la", translate("Load")).value =
44  string.format("%.2f, %.2f, %.2f", loads[1] / 65535.0, loads[2] / 65535.0, loads[3] / 65535.0)
45
46 s:option(DummyValue, "_memtotal", translate("Memory")).value =
47  string.format("%.2f MB (%.0f%% %s, %.0f%% %s)",
48   tonumber(memory.total) / 1024 / 1024,
49   100 * memory.buffered / memory.total,
50   tostring(translate("buffered")),
51   100 * memory.free / memory.total,
52   tostring(translate("free"))
53 )
54
55 s:option(DummyValue, "_systime", translate("Local Time")).value =
56  os.date("%c")
57
58 s:option(DummyValue, "_uptime", translate("Uptime")).value =
59  luci.tools.webadmin.date_format(tonumber(uptime))
60
61 hn = s:option(Value, "hostname", translate("Hostname"))
62
63 function hn.write(self, section, value)
64         Value.write(self, section, value)
65         luci.sys.hostname(value)
66 end
67
68
69 tz = s:option(ListValue, "zonename", translate("Timezone"))
70 tz:value("UTC")
71
72 for i, zone in ipairs(luci.sys.zoneinfo.TZ) do
73         tz:value(zone[1])
74 end
75
76 function tz.write(self, section, value)
77         local function lookup_zone(title)
78                 for _, zone in ipairs(luci.sys.zoneinfo.TZ) do
79                         if zone[1] == title then return zone[2] end
80                 end
81         end
82
83         AbstractValue.write(self, section, value)
84         self.map.uci:set("system", section, "timezone", lookup_zone(value) or "GMT0")
85 end
86
87 return m