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