Merge pull request #1386 from dibdot/dnscrypt-proxy
[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("env -i /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, "", translate("Interface Wizard"))
31         dv.template = "cbi/nullsection"
32
33         o = s:option(Value, "", translate("Uplink interface"))
34         o.datatype = "and(uciname,rangelength(3,15))"
35         o.default = trmiface
36         o.rmempty = false
37
38         btn = s:option(Button, "trm_iface", 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                 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 o1 = s:option(Flag, "trm_enabled", translate("Enable travelmate"))
71 o1.default = o1.disabled
72 o1.rmempty = false
73
74 o2 = s:option(Flag, "trm_automatic", translate("Enable 'automatic' mode"),
75         translate("Keep travelmate in an active state. Check every n seconds the connection status, i.e. the uplink availability."))
76 o2.default = o2.enabled
77 o2.rmempty = false
78
79 btn = s:option(Button, "", translate("Manual Rescan"))
80 btn:depends("trm_automatic", "")
81 btn.inputtitle = translate("Rescan")
82 btn.inputstyle = "find"
83 btn.disabled = false
84 function btn.write()
85         luci.sys.call("env -i /etc/init.d/travelmate start >/dev/null 2>&1")
86         luci.http.redirect(luci.dispatcher.build_url("admin", "services", "travelmate"))
87 end
88
89 o3 = s:option(Value, "trm_iface", translate("Uplink / Trigger interface"),
90         translate("Name of the uplink interface that triggers travelmate processing in 'manual' mode."))
91 o3.datatype = "and(uciname,rangelength(3,15))"
92 o3.default = trmiface
93 o3.rmempty = false
94
95 o4 = s:option(Value, "trm_triggerdelay", translate("Trigger delay"),
96         translate("Additional trigger delay in seconds before travelmate processing begins."))
97 o4.default = 2
98 o4.datatype = "range(1,90)"
99 o4.rmempty = false
100
101 o5 = s:option(Flag, "trm_debug", translate("Enable verbose debug logging"))
102 o5.default = o5.disabled
103 o5.rmempty = false
104
105 -- Runtime information
106
107 ds = s:option(DummyValue, "_dummy", translate("Runtime information"))
108 ds.template = "cbi/nullsection"
109
110 dv1 = s:option(DummyValue, "status", translate("Online Status"))
111 dv1.template = "travelmate/runtime"
112 if parse == nil then
113         dv1.value = translate("n/a")
114 elseif parse.data.station_connection == "true" then
115         dv1.value = translate("connected")
116 else
117         dv1.value = translate("not connected")
118 end
119
120 dv2 = s:option(DummyValue, "travelmate_version", translate("Travelmate version"))
121 dv2.template = "travelmate/runtime"
122 if parse ~= nil then
123         dv2.value = parse.data.travelmate_version or translate("n/a")
124 else
125         dv2.value = translate("n/a")
126 end
127
128 dv3 = s:option(DummyValue, "station_ssid", translate("Station SSID"))
129 dv3.template = "travelmate/runtime"
130 if parse ~= nil then
131         dv3.value = parse.data.station_ssid or translate("n/a")
132 else
133         dv3.value = translate("n/a")
134 end
135
136 dv4 = s:option(DummyValue, "station_interface", translate("Station Interface"))
137 dv4.template = "travelmate/runtime"
138 if parse ~= nil then
139         dv4.value = parse.data.station_interface or translate("n/a")
140 else
141         dv4.value = translate("n/a")
142 end
143
144 dv5 = s:option(DummyValue, "station_radio", translate("Station Radio"))
145 dv5.template = "travelmate/runtime"
146 if parse ~= nil then
147         dv5.value = parse.data.station_radio or translate("n/a")
148 else
149         dv5.value = translate("n/a")
150 end
151
152 dv6 = s:option(DummyValue, "last_rundate", translate("Last rundate"))
153 dv6.template = "travelmate/runtime"
154 if parse ~= nil then
155         dv6.value = parse.data.last_rundate or translate("n/a")
156 else
157         dv6.value = translate("n/a")
158 end
159
160 -- Extra options
161
162 e = m:section(NamedSection, "global", "travelmate", translate("Extra options"),
163 translate("Options for further tweaking in case the defaults are not suitable for you."))
164
165 e1 = e:option(Value, "trm_radio", translate("Radio selection"),
166         translate("Restrict travelmate to a dedicated radio, e.g. 'radio0'."))
167 e1.datatype = "and(uciname,rangelength(6,6))"
168 e1.rmempty = true
169
170 e2 = e:option(Value, "trm_maxretry", translate("Connection Limit"),
171         translate("How many times should travelmate try to connect to an Uplink. ")
172         .. translate("To disable this feature set it to '0' which means unlimited retries."))
173 e2.default = 3
174 e2.datatype = "range(0,30)"
175 e2.rmempty = false
176
177 e3 = e:option(Value, "trm_maxwait", translate("Interface Timeout"),
178         translate("How long should travelmate wait for a successful wlan interface reload."))
179 e3.default = 30
180 e3.datatype = "range(5,60)"
181 e3.rmempty = false
182
183 e4 = e:option(Value, "trm_timeout", translate("Overall Timeout"),
184         translate("Timeout in seconds between retries in 'automatic' mode."))
185 e4.default = 60
186 e4.datatype = "range(60,300)"
187 e4.rmempty = false
188
189 return m