luci-app-travelmate: bugfixes
[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.write(self, section, value)
39                 iface = o:formvalue(section)
40                 uci:set("travelmate", section, "trm_iface", iface)
41                 uci:save("travelmate")
42                 uci:commit("travelmate")
43         end
44
45         btn = s:option(Button, "", translate("Create Uplink Interface"),
46                 translate("Create a new wireless wan uplink interface, configure it to use dhcp and ")
47                 .. translate("add it to the wan zone of the firewall. This step has only to be done once."))
48         btn.inputtitle = translate("Add Interface")
49         btn.inputstyle = "apply"
50         btn.disabled = false
51         function btn.write()
52                 local net = nw:add_network(iface, { proto = "dhcp" })
53                 if net then
54                         nw:save("network")
55                         nw:commit("network")
56                         local zone = fw:get_zone_by_network("wan")
57                         if zone then
58                                 zone:add_network(iface)
59                                 fw:save("firewall")
60                                 fw:commit("firewall")
61                                 luci.sys.call("env -i /bin/ubus call network reload >/dev/null 2>&1")
62                         end
63                 end
64                 luci.http.redirect(luci.dispatcher.build_url("admin", "services", "travelmate"))
65         end
66         return m
67 end
68
69 -- Main travelmate options
70
71 o1 = s:option(Flag, "trm_enabled", translate("Enable travelmate"))
72 o1.default = o1.disabled
73 o1.rmempty = false
74
75 o2 = s:option(Flag, "trm_automatic", translate("Enable 'automatic' mode"),
76         translate("Keep travelmate in an active state. Check every n seconds the connection status, i.e. the uplink availability."))
77 o2.default = o2.enabled
78 o2.rmempty = false
79
80 btn = s:option(Button, "", translate("Manual Rescan"))
81 btn:depends("trm_automatic", "")
82 btn.inputtitle = translate("Rescan")
83 btn.inputstyle = "find"
84 btn.disabled = false
85 function btn.write()
86         luci.sys.call("/etc/init.d/travelmate start >/dev/null 2>&1")
87         luci.http.redirect(luci.dispatcher.build_url("admin", "services", "travelmate"))
88 end
89
90 o3 = s:option(Value, "trm_iface", translate("Uplink / Trigger interface"),
91         translate("Name of the uplink interface that triggers travelmate processing in 'manual' mode."))
92 o3.datatype = "and(uciname,rangelength(3,15))"
93 o3.default = trmiface
94 o3.rmempty = false
95
96 o4 = s:option(Value, "trm_triggerdelay", translate("Trigger delay"),
97         translate("Additional trigger delay in seconds before travelmate processing begins."))
98 o4.default = 2
99 o4.datatype = "range(1,90)"
100 o4.rmempty = false
101
102 o5 = s:option(Flag, "trm_debug", translate("Enable verbose debug logging"))
103 o5.default = o5.disabled
104 o5.rmempty = false
105
106 -- Runtime information
107
108 ds = s:option(DummyValue, "_dummy", translate("Runtime information"))
109 ds.template = "cbi/nullsection"
110
111 dv1 = s:option(DummyValue, "status", translate("Online Status"))
112 dv1.template = "travelmate/runtime"
113 if parse == nil then
114         dv1.value = translate("n/a")
115 elseif parse.data.station_connection == "true" then
116         dv1.value = translate("connected")
117 else
118         dv1.value = translate("not connected")
119 end
120
121 dv2 = s: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 = s:option(DummyValue, "station_ssid", translate("Station SSID"))
130 dv3.template = "travelmate/runtime"
131 if parse ~= nil then
132         dv3.value = parse.data.station_ssid or translate("n/a")
133 else
134         dv3.value = translate("n/a")
135 end
136
137 dv4 = s: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 = s: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 = s: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(Value, "trm_radio", translate("Radio selection"),
167         translate("Restrict travelmate to a dedicated radio, e.g. 'radio0'"))
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 e2.default = 3
173 e2.datatype = "range(1,10)"
174 e2.rmempty = false
175
176 e3 = e:option(Value, "trm_maxwait", translate("Interface Timeout"),
177         translate("How long should travelmate wait for a successful wlan interface reload"))
178 e3.default = 30
179 e3.datatype = "range(5,60)"
180 e3.rmempty = false
181
182 e4 = e:option(Value, "trm_timeout", translate("Overall Timeout"),
183         translate("Timeout in seconds between retries in 'automatic' mode"))
184 e4.default = 60
185 e4.datatype = "range(5,300)"
186 e4.rmempty = false
187
188 return m