Merge pull request #1264 from kuoruan/zh-cn-tr
[project/luci.git] / applications / luci-app-travelmate / luasrc / model / cbi / travelmate / overview_tab.lua
1 -- Copyright 2017 Dirk Brenken (dev@brenken.org)
2 -- This is free software, licensed under the Apache License, Version 2.0
3
4 local fs = require("nixio.fs")
5 local uci = require("luci.model.uci").cursor()
6 local json = require("luci.jsonc")
7 local nw  = require("luci.model.network").init()
8 local fw  = require("luci.model.firewall").init()
9 local trmiface = uci.get("travelmate", "global", "trm_iface") or "trm_wwan"
10 local trminput = uci.get("travelmate", "global", "trm_rtfile") or "/tmp/trm_runtime.json"
11 local uplink = uci.get("network", trmiface) or ""
12 local parse = json.parse(fs.readfile(trminput) or "")
13
14 m = Map("travelmate", translate("Travelmate"),
15         translate("Configuration of the travelmate package to to enable travel router functionality. ")
16         .. translatef("For further information "
17         .. "<a href=\"%s\" target=\"_blank\">"
18         .. "see online documentation</a>", "https://github.com/openwrt/packages/blob/master/net/travelmate/files/README.md"))
19
20 function m.on_after_commit(self)
21         luci.sys.call("/etc/init.d/travelmate restart >/dev/null 2>&1")
22         luci.http.redirect(luci.dispatcher.build_url("admin", "services", "travelmate"))
23 end
24
25 s = m:section(NamedSection, "global", "travelmate")
26
27 -- Interface Wizard
28
29 if uplink == "" then
30         dv = s:option(DummyValue, "nil", translate("Interface Wizard"))
31         dv.template = "cbi/nullsection"
32
33         o = s:option(Value, "trm_iface", translate("Uplink interface"))
34         o.datatype = "and(uciname,rangelength(3,15))"
35         o.default = "trm_wwan"
36         o.rmempty = false
37
38         btn = s:option(Button, "", translate("Create Uplink Interface"),
39                 translate("Create a new wireless wan uplink interface, configure it to use dhcp and ")
40                 .. translate("add it to the wan zone of the firewall. This step has only to be done once."))
41         btn.inputtitle = translate("Add Interface")
42         btn.inputstyle = "apply"
43         btn.disabled = false
44         function btn.write(self, section, value)
45                 iface = o:formvalue(section)
46                 uci:set("travelmate", section, "trm_iface", iface)
47                 uci:save("travelmate")
48                 uci:commit("travelmate")
49                 local net = nw:add_network(iface, { proto = "dhcp" })
50                 if net then
51                         nw:save("network")
52                         nw:commit("network")
53                         local zone = fw:get_zone_by_network("wan")
54                         if zone then
55                                 zone:add_network(iface)
56                                 fw:save("firewall")
57                                 fw:commit("firewall")
58                                 luci.sys.call("env -i /bin/ubus call network reload >/dev/null 2>&1")
59                         end
60                 end
61                 luci.http.redirect(luci.dispatcher.build_url("admin", "services", "travelmate"))
62         end
63         return m
64 end
65
66 -- Main travelmate options
67
68 o1 = s:option(Flag, "trm_enabled", translate("Enable travelmate"))
69 o1.default = o1.disabled
70 o1.rmempty = false
71
72 o2 = s:option(Flag, "trm_automatic", translate("Enable 'automatic' mode"),
73         translate("Keep travelmate in an active state. Check every n seconds the connection status, i.e. the uplink availability."))
74 o2.default = o2.enabled
75 o2.rmempty = false
76
77 btn = s:option(Button, "", translate("Manual Rescan"))
78 btn:depends("trm_automatic", "")
79 btn.inputtitle = translate("Rescan")
80 btn.inputstyle = "find"
81 btn.disabled = false
82 function btn.write()
83         luci.sys.call("/etc/init.d/travelmate start >/dev/null 2>&1")
84         luci.http.redirect(luci.dispatcher.build_url("admin", "services", "travelmate"))
85 end
86
87 o3 = s:option(Value, "trm_iface", translate("Uplink / Trigger interface"),
88         translate("Name of the uplink interface that triggers travelmate processing in 'manual' mode."))
89 o3.datatype = "and(uciname,rangelength(3,15))"
90 o3.default = trmiface
91 o3.rmempty = false
92
93 o4 = s:option(Value, "trm_triggerdelay", translate("Trigger delay"),
94         translate("Additional trigger delay in seconds before travelmate processing begins."))
95 o4.default = 2
96 o4.datatype = "range(1,90)"
97 o4.rmempty = false
98
99 o5 = s:option(Flag, "trm_debug", translate("Enable verbose debug logging"))
100 o5.default = o5.disabled
101 o5.rmempty = false
102
103 -- Runtime information
104
105 ds = s:option(DummyValue, "_dummy", translate("Runtime information"))
106 ds.template = "cbi/nullsection"
107
108 dv1 = s:option(DummyValue, "status", translate("Online Status"))
109 dv1.template = "travelmate/runtime"
110 if parse == nil then
111         dv1.value = translate("n/a")
112 elseif parse.data.station_connection == "true" then
113         dv1.value = translate("connected")
114 else
115         dv1.value = translate("not connected")
116 end
117
118 dv2 = s:option(DummyValue, "travelmate_version", translate("Travelmate version"))
119 dv2.template = "travelmate/runtime"
120 if parse ~= nil then
121         dv2.value = parse.data.travelmate_version or translate("n/a")
122 else
123         dv2.value = translate("n/a")
124 end
125
126 dv3 = s:option(DummyValue, "station_ssid", translate("Station SSID"))
127 dv3.template = "travelmate/runtime"
128 if parse ~= nil then
129         dv3.value = parse.data.station_ssid or translate("n/a")
130 else
131         dv3.value = translate("n/a")
132 end
133
134 dv4 = s:option(DummyValue, "station_interface", translate("Station Interface"))
135 dv4.template = "travelmate/runtime"
136 if parse ~= nil then
137         dv4.value = parse.data.station_interface or translate("n/a")
138 else
139         dv4.value = translate("n/a")
140 end
141
142 dv5 = s:option(DummyValue, "station_radio", translate("Station Radio"))
143 dv5.template = "travelmate/runtime"
144 if parse ~= nil then
145         dv5.value = parse.data.station_radio or translate("n/a")
146 else
147         dv5.value = translate("n/a")
148 end
149
150 dv6 = s:option(DummyValue, "last_rundate", translate("Last rundate"))
151 dv6.template = "travelmate/runtime"
152 if parse ~= nil then
153         dv6.value = parse.data.last_rundate or translate("n/a")
154 else
155         dv6.value = translate("n/a")
156 end
157
158 -- Extra options
159
160 e = m:section(NamedSection, "global", "travelmate", translate("Extra options"),
161 translate("Options for further tweaking in case the defaults are not suitable for you."))
162
163 e1 = e:option(Value, "trm_radio", translate("Radio selection"),
164         translate("Restrict travelmate to a dedicated radio, e.g. 'radio0'"))
165 e1.rmempty = true
166
167 e2 = e:option(Value, "trm_maxretry", translate("Connection Limit"),
168         translate("How many times should travelmate try to connect to an Uplink"))
169 e2.default = 3
170 e2.datatype = "range(1,10)"
171 e2.rmempty = false
172
173 e3 = e:option(Value, "trm_maxwait", translate("Interface Timeout"),
174         translate("How long should travelmate wait for a successful wlan interface reload"))
175 e3.default = 30
176 e3.datatype = "range(5,60)"
177 e3.rmempty = false
178
179 e4 = e:option(Value, "trm_timeout", translate("Overall Timeout"),
180         translate("Timeout in seconds between retries in 'automatic' mode"))
181 e4.default = 60
182 e4.datatype = "range(5,300)"
183 e4.rmempty = false
184
185 return m