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