Merge pull request #1157 from dibdot/app-adblock
[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("uci")
6 local json = require("luci.jsonc")
7 local nw  = require("luci.model.network").init()
8 local fw  = require("luci.model.firewall").init()
9 local uplink = uci.get("network", "trm_wwan") or ""
10 local trminput = uci.get("travelmate", "global", "trm_rtfile") or "/tmp/trm_runtime.json"
11 local parse = json.parse(fs.readfile(trminput) or "")
12
13 m = Map("travelmate", translate("Travelmate"),
14         translate("Configuration of the travelmate package to to enable travel router functionality. ")
15         .. translatef("For further information "
16         .. "<a href=\"%s\" target=\"_blank\">"
17         .. "see online documentation</a>", "https://github.com/openwrt/packages/blob/master/net/travelmate/files/README.md"))
18
19 -- Main travelmate options
20
21 s = m:section(NamedSection, "global", "travelmate")
22
23 o1 = s:option(Flag, "trm_enabled", translate("Enable travelmate"))
24 o1.default = o1.disabled
25 o1.rmempty = false
26
27 o2 = s:option(Flag, "trm_automatic", translate("Enable 'automatic' mode"),
28         translate("Keep travelmate in an active state."))
29 o2.default = o2.enabled
30 o2.rmempty = false
31
32 o3 = s:option(Value, "trm_iface", translate("Restrict interface trigger to certain interface(s)"),
33         translate("Space separated list of interfaces that trigger travelmate processing. "..
34         "To disable event driven (re-)starts remove all entries."))
35 o3.rmempty = true
36
37 o4 = s:option(Value, "trm_triggerdelay", translate("Trigger delay"),
38         translate("Additional trigger delay in seconds before travelmate processing begins."))
39 o4.default = 2
40 o4.datatype = "range(1,90)"
41 o4.rmempty = false
42
43 o5 = s:option(Flag, "trm_debug", translate("Enable verbose debug logging"))
44 o5.default = o5.disabled
45 o5.rmempty = false
46
47 -- Interface setup
48
49 if uplink == "" then
50         dv = s:option(DummyValue, "_dummy", translate("Interface Setup"))
51         dv.template = "cbi/nullsection"
52         btn = s:option(Button, "", translate("Create Uplink Interface"),
53                 translate("Automatically create a new wireless wan uplink interface 'trm_wwan', configure it to use dhcp and ")
54                 .. translate("add it to the wan zone of the firewall. This step has only to be done once."))
55         btn.inputtitle = translate("Add Interface")
56         btn.inputstyle = "apply"
57         btn.disabled = false
58         function btn.write()
59                 local name = "trm_wwan"
60                 local net = nw:add_network(name, { proto = "dhcp" })
61                 if net then
62                         nw:save("network")
63                         nw:commit("network")
64                         local zone = fw:get_zone_by_network("wan")
65                         if zone then
66                                 zone:add_network(name)
67                                 fw:save("firewall")
68                                 fw:commit("firewall")
69                         end
70                         luci.sys.call("env -i /bin/ubus call network reload >/dev/null 2>&1")
71                         luci.http.redirect(luci.dispatcher.build_url("admin", "services", "travelmate"))
72                 end
73         end
74 else
75         dv = s:option(DummyValue, "_dummy", translate("Interface Setup"),
76                 translate("<br />&nbsp;Network Interface 'trm_wwan' created successfully. ")
77                 .. translatef("Scan &amp; Add new wireless stations via standard "
78                 .. "<a href=\"%s\">"
79                 .. "Wireless Setup</a>", luci.dispatcher.build_url("admin/network/wireless")))
80         dv.template = "cbi/nullsection"
81 end
82
83 -- Runtime information
84
85 ds = s:option(DummyValue, "_dummy", translate("Runtime information"))
86 ds.template = "cbi/nullsection"
87
88 dv1 = s:option(DummyValue, "status", translate("Online Status"))
89 dv1.template = "travelmate/runtime"
90 if parse == nil then
91         dv1.value = translate("n/a")
92 elseif parse.data.station_connection == "true" then
93         dv1.value = translate("connected")
94 else
95         dv1.value = translate("not connected")
96 end
97
98 dv2 = s:option(DummyValue, "travelmate_version", translate("Travelmate version"))
99 dv2.template = "travelmate/runtime"
100 if parse ~= nil then
101         dv2.value = parse.data.travelmate_version or translate("n/a")
102 else
103         dv2.value = translate("n/a")
104 end
105
106 dv3 = s:option(DummyValue, "station_ssid", translate("Station SSID"))
107 dv3.template = "travelmate/runtime"
108 if parse ~= nil then
109         dv3.value = parse.data.station_ssid or translate("n/a")
110 else
111         dv3.value = translate("n/a")
112 end
113
114 dv4 = s:option(DummyValue, "station_interface", translate("Station Interface"))
115 dv4.template = "travelmate/runtime"
116 if parse ~= nil then
117         dv4.value = parse.data.station_interface or translate("n/a")
118 else
119         dv4.value = translate("n/a")
120 end
121
122 dv5 = s:option(DummyValue, "station_radio", translate("Station Radio"))
123 dv5.template = "travelmate/runtime"
124 if parse ~= nil then
125         dv5.value = parse.data.station_radio or translate("n/a")
126 else
127         dv5.value = translate("n/a")
128 end
129
130 dv6 = s:option(DummyValue, "last_rundate", translate("Last rundate"))
131 dv6.template = "travelmate/runtime"
132 if parse ~= nil then
133         dv6.value = parse.data.last_rundate or translate("n/a")
134 else
135         dv6.value = translate("n/a")
136 end
137
138 -- Extra options
139
140 e = m:section(NamedSection, "global", "travelmate", translate("Extra options"),
141 translate("Options for further tweaking in case the defaults are not suitable for you."))
142
143 e1 = e:option(Value, "trm_radio", translate("Radio selection"),
144         translate("Restrict travelmate to a dedicated radio, e.g. 'radio0'"))
145 e1.rmempty = true
146
147 e2 = e:option(Value, "trm_maxretry", translate("Connection Limit"),
148         translate("How many times should travelmate try to connect to an Uplink"))
149 e2.default = 3
150 e2.datatype = "range(1,10)"
151 e2.rmempty = false
152
153 e3 = e:option(Value, "trm_maxwait", translate("Interface Timeout"),
154         translate("How long should travelmate wait for a successful wlan interface reload"))
155 e3.default = 30
156 e3.datatype = "range(5,60)"
157 e3.rmempty = false
158
159 e4 = e:option(Value, "trm_timeout", translate("Overall Timeout"),
160         translate("Timeout in seconds between retries in 'automatic' mode"))
161 e4.default = 60
162 e4.datatype = "range(5,300)"
163 e4.rmempty = false
164
165 return m