luci-app-travelmate: sync with travelmate 0.9.0
[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("/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, "nil", translate("Interface Wizard"))
31 dv.template = "cbi/nullsection"
32
33 o = s:option(Value, "trm_iface", translate("Uplink interface"))
34 o.datatype = "and(uciname,rangelength(3,15))"
35 o.default = "trm_wwan"
36 o.rmempty = false
37
38 function o.validate(self, value)
39 iface = value
40 return iface
41 end
42
43 function o.write(self, section, value)
44 uci:set("travelmate", section, "trm_iface", iface)
45 uci:save("travelmate")
46 uci:commit("travelmate")
47 end
48
49 btn = s:option(Button, "", translate("Create Uplink Interface"),
50 translate("Create a new wireless wan uplink interface, configure it to use dhcp and ")
51 .. translate("add it to the wan zone of the firewall. This step has only to be done once."))
52 btn.inputtitle = translate("Add Interface")
53 btn.inputstyle = "apply"
54 btn.disabled = false
55 function btn.write()
56 local net = nw:add_network(iface, { proto = "dhcp" })
57 if net then
58 nw:save("network")
59 nw:commit("network")
60 local zone = fw:get_zone_by_network("wan")
61 if zone then
62 zone:add_network(iface)
63 fw:save("firewall")
64 fw:commit("firewall")
65 luci.sys.call("env -i /bin/ubus call network reload >/dev/null 2>&1")
66 end
67 end
68 luci.http.redirect(luci.dispatcher.build_url("admin", "services", "travelmate"))
69 end
70 return m
71 end
72
73 -- Main travelmate options
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_automatic", translate("Enable 'automatic' mode"),
80 translate("Keep travelmate in an active state."))
81 o2.default = o2.enabled
82 o2.rmempty = false
83
84 o3 = s:option(Value, "trm_iface", translate("Uplink / Trigger interface"),
85 translate("Name of the uplink interface that triggers travelmate processing."))
86 o3.datatype = "and(uciname,rangelength(3,15))"
87 o3.default = "trm_wwan"
88 o3.rmempty = false
89
90 o4 = s:option(Value, "trm_triggerdelay", translate("Trigger delay"),
91 translate("Additional trigger delay in seconds before travelmate processing begins."))
92 o4.default = 2
93 o4.datatype = "range(1,90)"
94 o4.rmempty = false
95
96 o5 = s:option(Flag, "trm_debug", translate("Enable verbose debug logging"))
97 o5.default = o5.disabled
98 o5.rmempty = false
99
100 -- Runtime information
101
102 ds = s:option(DummyValue, "_dummy", translate("Runtime information"))
103 ds.template = "cbi/nullsection"
104
105 dv1 = s:option(DummyValue, "status", translate("Online Status"))
106 dv1.template = "travelmate/runtime"
107 if parse == nil then
108 dv1.value = translate("n/a")
109 elseif parse.data.station_connection == "true" then
110 dv1.value = translate("connected")
111 else
112 dv1.value = translate("not connected")
113 end
114
115 dv2 = s:option(DummyValue, "travelmate_version", translate("Travelmate version"))
116 dv2.template = "travelmate/runtime"
117 if parse ~= nil then
118 dv2.value = parse.data.travelmate_version or translate("n/a")
119 else
120 dv2.value = translate("n/a")
121 end
122
123 dv3 = s:option(DummyValue, "station_ssid", translate("Station SSID"))
124 dv3.template = "travelmate/runtime"
125 if parse ~= nil then
126 dv3.value = parse.data.station_ssid or translate("n/a")
127 else
128 dv3.value = translate("n/a")
129 end
130
131 dv4 = s:option(DummyValue, "station_interface", translate("Station Interface"))
132 dv4.template = "travelmate/runtime"
133 if parse ~= nil then
134 dv4.value = parse.data.station_interface or translate("n/a")
135 else
136 dv4.value = translate("n/a")
137 end
138
139 dv5 = s:option(DummyValue, "station_radio", translate("Station Radio"))
140 dv5.template = "travelmate/runtime"
141 if parse ~= nil then
142 dv5.value = parse.data.station_radio or translate("n/a")
143 else
144 dv5.value = translate("n/a")
145 end
146
147 dv6 = s:option(DummyValue, "last_rundate", translate("Last rundate"))
148 dv6.template = "travelmate/runtime"
149 if parse ~= nil then
150 dv6.value = parse.data.last_rundate or translate("n/a")
151 else
152 dv6.value = translate("n/a")
153 end
154
155 -- Extra options
156
157 e = m:section(NamedSection, "global", "travelmate", translate("Extra options"),
158 translate("Options for further tweaking in case the defaults are not suitable for you."))
159
160 e1 = e:option(Value, "trm_radio", translate("Radio selection"),
161 translate("Restrict travelmate to a dedicated radio, e.g. 'radio0'"))
162 e1.rmempty = true
163
164 e2 = e:option(Value, "trm_maxretry", translate("Connection Limit"),
165 translate("How many times should travelmate try to connect to an Uplink"))
166 e2.default = 3
167 e2.datatype = "range(1,10)"
168 e2.rmempty = false
169
170 e3 = e:option(Value, "trm_maxwait", translate("Interface Timeout"),
171 translate("How long should travelmate wait for a successful wlan interface reload"))
172 e3.default = 30
173 e3.datatype = "range(5,60)"
174 e3.rmempty = false
175
176 e4 = e:option(Value, "trm_timeout", translate("Overall Timeout"),
177 translate("Timeout in seconds between retries in 'automatic' mode"))
178 e4.default = 60
179 e4.datatype = "range(5,300)"
180 e4.rmempty = false
181
182 return m