Merge pull request #1689 from aparcar/asu-fixup
[project/luci.git] / applications / luci-app-travelmate / luasrc / model / cbi / travelmate / overview_tab.lua
1 -- Copyright 2017-2018 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 util     = require("luci.util")
8 local nw       = require("luci.model.network").init()
9 local fw       = require("luci.model.firewall").init()
10 local dump     = util.ubus("network.interface", "dump", {})
11 local trmiface = uci:get("travelmate", "global", "trm_iface") or "trm_wwan"
12 local trminput = uci:get("travelmate", "global", "trm_rtfile") or "/tmp/trm_runtime.json"
13 local uplink   = uci:get("network", trmiface) or ""
14 local parse    = json.parse(fs.readfile(trminput) or "")
15
16 m = Map("travelmate", translate("Travelmate"),
17         translate("Configuration of the travelmate package to to enable travel router functionality. ")
18         .. translatef("For further information "
19         .. "<a href=\"%s\" target=\"_blank\">"
20         .. "see online documentation</a>", "https://github.com/openwrt/packages/blob/master/net/travelmate/files/README.md"))
21
22 function m.on_after_commit(self)
23         luci.sys.call("env -i /etc/init.d/travelmate restart >/dev/null 2>&1")
24         luci.http.redirect(luci.dispatcher.build_url("admin", "services", "travelmate"))
25 end
26
27 -- Interface Wizard
28
29 if uplink == "" then
30         ds = m:section(NamedSection, "global", "travelmate", translate("Interface Wizard"))
31
32         o = ds:option(Value, "", translate("Uplink interface"))
33         o.datatype = "and(uciname,rangelength(3,15))"
34         o.default = trmiface
35         o.rmempty = false
36
37         btn = ds:option(Button, "trm_iface", translate("Create Uplink Interface"),
38                 translate("Create a new wireless wan uplink interface, configure it to use dhcp and ")
39                 .. translate("add it to the wan zone of the firewall. This step has only to be done once."))
40         btn.inputtitle = translate("Add Interface")
41         btn.inputstyle = "apply"
42         btn.disabled = false
43
44         function btn.write(self, section)
45                 local iface = o:formvalue(section)
46                 if iface then
47                         uci:set("travelmate", section, "trm_iface", iface)
48                         uci:save("travelmate")
49                         uci:commit("travelmate")
50                         local net = nw:add_network(iface, { proto = "dhcp" })
51                         if net then
52                                 nw:save("network")
53                                 nw:commit("network")
54                                 local zone = fw:get_zone_by_network("wan")
55                                 if zone then
56                                         zone:add_network(iface)
57                                         fw:save("firewall")
58                                         fw:commit("firewall")
59                                 end
60                         end
61                         luci.sys.call("env -i /bin/ubus call network reload >/dev/null 2>&1")
62                 end
63                 luci.http.redirect(luci.dispatcher.build_url("admin", "services", "travelmate"))
64         end
65         return m
66 end
67
68 -- Main travelmate options
69
70 s = m:section(NamedSection, "global", "travelmate")
71
72 o1 = s:option(Flag, "trm_enabled", translate("Enable travelmate"))
73 o1.default = o1.disabled
74 o1.rmempty = false
75
76 o2 = s:option(Flag, "trm_automatic", translate("Enable 'automatic' mode"),
77         translate("Keep travelmate in an active state. Check every n seconds the connection status, i.e. the uplink availability."))
78 o2.default = o2.enabled
79 o2.rmempty = false
80
81 o3 = s:option(Flag, "trm_captive", translate("Captive Portal Detection"),
82         translate("Check the internet availability, log captive portal redirections and keep the uplink connection 'alive'."))
83 o3.default = o3.enabled
84 o3.rmempty = false
85
86 o4 = s:option(ListValue, "trm_iface", translate("Uplink / Trigger interface"),
87         translate("Name of the used uplink interface."))
88 if dump then
89         local i, v
90         for i, v in ipairs(dump.interface) do
91                 if v.interface ~= "loopback" and v.interface ~= "lan" then
92                         o4:value(v.interface)
93                 end
94         end
95 end
96 o4.default = trmiface
97 o4.rmempty = false
98
99 if fs.access("/usr/bin/qrencode") then
100         btn1 = s:option(Button, "btn1", translate("View AP QR-Codes"),
101                 translate("Connect your Android or iOS devices to your router's WiFi using the shown QR code."))
102         btn1.inputtitle = translate("QR-Codes")
103         btn1.inputstyle = "apply"
104         btn1.disabled = false
105
106         function btn1.write()
107                 luci.http.redirect(luci.dispatcher.build_url("admin", "services", "travelmate", "apqr"))
108         end
109 end
110
111 btn2 = s:option(Button, "btn2", translate("Manual Rescan"),
112         translate("Force a manual uplink rescan / reconnect in 'trigger' mode."))
113 btn2:depends("trm_automatic", "")
114 btn2.inputtitle = translate("Rescan")
115 btn2.inputstyle = "find"
116 btn2.disabled = false
117
118 function btn2.write()
119         luci.sys.call("env -i /etc/init.d/travelmate start >/dev/null 2>&1")
120         luci.http.redirect(luci.dispatcher.build_url("admin", "services", "travelmate"))
121 end
122
123 -- Runtime information
124
125 ds = m:section(NamedSection, "global", "travelmate", translate("Runtime Information"))
126
127 dv1 = ds:option(DummyValue, "status", translate("Travelmate Status (Quality)"))
128 dv1.template = "travelmate/runtime"
129 if parse ~= nil then
130         dv1.value = parse.data.travelmate_status or translate("n/a")
131 else
132         dv1.value = translate("n/a")
133 end
134
135 dv2 = ds:option(DummyValue, "travelmate_version", translate("Travelmate Version"))
136 dv2.template = "travelmate/runtime"
137 if parse ~= nil then
138         dv2.value = parse.data.travelmate_version or translate("n/a")
139 else
140         dv2.value = translate("n/a")
141 end
142
143 dv3 = ds:option(DummyValue, "station_id", translate("Station ID (SSID/BSSID)"))
144 dv3.template = "travelmate/runtime"
145 if parse ~= nil then
146         dv3.value = parse.data.station_id or translate("n/a")
147 else
148         dv3.value = translate("n/a")
149 end
150
151 dv4 = ds:option(DummyValue, "station_interface", translate("Station Interface"))
152 dv4.template = "travelmate/runtime"
153 if parse ~= nil then
154         dv4.value = parse.data.station_interface or translate("n/a")
155 else
156         dv4.value = translate("n/a")
157 end
158
159 dv5 = ds:option(DummyValue, "station_radio", translate("Station Radio"))
160 dv5.template = "travelmate/runtime"
161 if parse ~= nil then
162         dv5.value = parse.data.station_radio or translate("n/a")
163 else
164         dv5.value = translate("n/a")
165 end
166
167 dv6 = ds:option(DummyValue, "last_rundate", translate("Last rundate"))
168 dv6.template = "travelmate/runtime"
169 if parse ~= nil then
170         dv6.value = parse.data.last_rundate or translate("n/a")
171 else
172         dv6.value = translate("n/a")
173 end
174
175 -- Extra options
176
177 e = m:section(NamedSection, "global", "travelmate", translate("Extra options"),
178 translate("Options for further tweaking in case the defaults are not suitable for you."))
179
180 e1 = e:option(Flag, "trm_debug", translate("Enable verbose debug logging"))
181 e1.default = e1.disabled
182 e1.rmempty = false
183
184 e2 = e:option(Value, "trm_radio", translate("Radio selection"),
185         translate("Restrict travelmate to a dedicated radio, e.g. 'radio0'."))
186 e2.datatype = "and(uciname,rangelength(6,6))"
187 e2.rmempty = true
188
189 e3 = e:option(Value, "trm_triggerdelay", translate("Trigger Delay"),
190         translate("Additional trigger delay in seconds before travelmate processing begins."))
191 e3.datatype = "range(1,60)"
192 e3.default = 2
193 e3.rmempty = false
194
195 e4 = e:option(Value, "trm_maxretry", translate("Connection Limit"),
196         translate("Retry limit to connect to an uplink."))
197 e4.default = 3
198 e4.datatype = "range(1,10)"
199 e4.rmempty = false
200
201 e5 = e:option(Value, "trm_minquality", translate("Signal Quality Threshold"),
202         translate("Minimum signal quality threshold as percent for conditional uplink (dis-) connections."))
203 e5.default = 35
204 e5.datatype = "range(20,80)"
205 e5.rmempty = false
206
207 e6 = e:option(Value, "trm_maxwait", translate("Interface Timeout"),
208         translate("How long should travelmate wait for a successful wlan uplink connection."))
209 e6.default = 30
210 e6.datatype = "range(20,40)"
211 e6.rmempty = false
212
213 e7 = e:option(Value, "trm_timeout", translate("Overall Timeout"),
214         translate("Timeout in seconds between retries in 'automatic' mode."))
215 e7.default = 60
216 e7.datatype = "range(30,300)"
217 e7.rmempty = false
218
219 return m