Merge pull request #1769 from jow-/master
[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 m:chain("network")
22 m:chain("firewall")
23 m.apply_on_parse = true
24
25 function m.on_apply(self)
26         luci.sys.call("env -i /etc/init.d/travelmate restart >/dev/null 2>&1")
27         luci.http.redirect(luci.dispatcher.build_url("admin", "services", "travelmate"))
28 end
29
30 -- Interface Wizard
31
32 if uplink == "" then
33         ds = m:section(NamedSection, "global", "travelmate", translate("Interface Wizard"))
34         o = ds:option(Value, "trm_iface", translate("Create Uplink interface"),
35                 translate("Create a new wireless wan uplink interface, configure it to use dhcp and ")
36                 .. translate("add it to the wan zone of the firewall.<br />")
37                 .. translate("This step has only to be done once."))
38         o.datatype = "and(uciname,rangelength(3,15))"
39         o.default = trmiface
40         o.rmempty = false
41
42         function o.validate(self, value)
43                 if value then
44                         local nwnet = nw:get_network(value)
45                         local zone  = fw:get_zone("wan")
46                         local fwnet = fw:get_zone_by_network(value)
47                         if not nwnet then
48                                 nwnet = nw:add_network(value, { proto = "dhcp" })
49                         end
50                         if zone and not fwnet then
51                                 fwnet = zone:add_network(value)
52                         end
53                 end
54                 return value
55         end
56         return m
57 end
58
59 -- Main travelmate options
60
61 s = m:section(NamedSection, "global", "travelmate")
62
63 o1 = s:option(Flag, "trm_enabled", translate("Enable travelmate"))
64 o1.default = o1.disabled
65 o1.rmempty = false
66
67 o2 = s:option(Flag, "trm_captive", translate("Captive Portal Detection"),
68         translate("Check the internet availability, log captive portal redirections and keep the uplink connection 'alive'."))
69 o2.default = o2.enabled
70 o2.rmempty = false
71
72 o3 = s:option(ListValue, "trm_iface", translate("Uplink / Trigger interface"),
73         translate("Name of the used uplink interface."))
74 if dump then
75         local i, v
76         for i, v in ipairs(dump.interface) do
77                 if v.interface ~= "loopback" and v.interface ~= "lan" then
78                         o3:value(v.interface)
79                 end
80         end
81 end
82 o3.default = trmiface
83 o3.rmempty = false
84
85 if fs.access("/usr/bin/qrencode") then
86         btn = s:option(Button, "btn", translate("View AP QR-Codes"),
87                 translate("Connect your Android or iOS devices to your router's WiFi using the shown QR code."))
88         btn.inputtitle = translate("QR-Codes")
89         btn.inputstyle = "apply"
90         btn.disabled = false
91
92         function btn.write()
93                 luci.http.redirect(luci.dispatcher.build_url("admin", "services", "travelmate", "apqr"))
94         end
95 end
96
97 -- Runtime information
98
99 ds = m:section(NamedSection, "global", "travelmate", translate("Runtime Information"))
100
101 dv1 = ds:option(DummyValue, "status", translate("Travelmate Status (Quality)"))
102 dv1.template = "travelmate/runtime"
103 if parse ~= nil then
104         dv1.value = parse.data.travelmate_status or translate("n/a")
105 else
106         dv1.value = translate("n/a")
107 end
108
109 dv2 = ds:option(DummyValue, "travelmate_version", translate("Travelmate Version"))
110 dv2.template = "travelmate/runtime"
111 if parse ~= nil then
112         dv2.value = parse.data.travelmate_version or translate("n/a")
113 else
114         dv2.value = translate("n/a")
115 end
116
117 dv3 = ds:option(DummyValue, "station_id", translate("Station ID (SSID/BSSID)"))
118 dv3.template = "travelmate/runtime"
119 if parse ~= nil then
120         dv3.value = parse.data.station_id or translate("n/a")
121 else
122         dv3.value = translate("n/a")
123 end
124
125 dv4 = ds:option(DummyValue, "station_interface", translate("Station Interface"))
126 dv4.template = "travelmate/runtime"
127 if parse ~= nil then
128         dv4.value = parse.data.station_interface or translate("n/a")
129 else
130         dv4.value = translate("n/a")
131 end
132
133 dv5 = ds:option(DummyValue, "station_radio", translate("Station Radio"))
134 dv5.template = "travelmate/runtime"
135 if parse ~= nil then
136         dv5.value = parse.data.station_radio or translate("n/a")
137 else
138         dv5.value = translate("n/a")
139 end
140
141 dv6 = ds:option(DummyValue, "last_rundate", translate("Last rundate"))
142 dv6.template = "travelmate/runtime"
143 if parse ~= nil then
144         dv6.value = parse.data.last_rundate or translate("n/a")
145 else
146         dv6.value = translate("n/a")
147 end
148
149 -- Extra options
150
151 e = m:section(NamedSection, "global", "travelmate", translate("Extra options"),
152 translate("Options for further tweaking in case the defaults are not suitable for you."))
153
154 e1 = e:option(Flag, "trm_debug", translate("Enable verbose debug logging"))
155 e1.default = e1.disabled
156 e1.rmempty = false
157
158 e2 = e:option(Value, "trm_radio", translate("Radio selection"),
159         translate("Restrict travelmate to a dedicated radio, e.g. 'radio0'."))
160 e2.datatype = "and(uciname,rangelength(6,6))"
161 e2.rmempty = true
162
163 e3 = e:option(Value, "trm_triggerdelay", translate("Trigger Delay"),
164         translate("Additional trigger delay in seconds before travelmate processing begins."))
165 e3.datatype = "range(1,60)"
166 e3.default = 2
167 e3.rmempty = false
168
169 e4 = e:option(Value, "trm_maxretry", translate("Connection Limit"),
170         translate("Retry limit to connect to an uplink."))
171 e4.default = 3
172 e4.datatype = "range(1,10)"
173 e4.rmempty = false
174
175 e5 = e:option(Value, "trm_minquality", translate("Signal Quality Threshold"),
176         translate("Minimum signal quality threshold as percent for conditional uplink (dis-) connections."))
177 e5.default = 35
178 e5.datatype = "range(20,80)"
179 e5.rmempty = false
180
181 e6 = e:option(Value, "trm_maxwait", translate("Interface Timeout"),
182         translate("How long should travelmate wait for a successful wlan uplink connection."))
183 e6.default = 30
184 e6.datatype = "range(20,40)"
185 e6.rmempty = false
186
187 e7 = e:option(Value, "trm_timeout", translate("Overall Timeout"),
188         translate("Overall retry timeout in seconds."))
189 e7.default = 60
190 e7.datatype = "range(30,300)"
191 e7.rmempty = false
192
193 return m