Merge pull request #2021 from dibdot/travelmate
[oweals/luci.git] / applications / luci-app-travelmate / luasrc / model / cbi / travelmate / overview_tab.lua
1 -- Copyright 2017-2018 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 util     = require("luci.util")
7 local nw       = require("luci.model.network").init()
8 local fw       = require("luci.model.firewall").init()
9 local dump     = util.ubus("network.interface", "dump", {})
10 local trmiface = uci:get("travelmate", "global", "trm_iface") or "trm_wwan"
11 local uplink   = uci:get("network", trmiface) 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 m:chain("network")
19 m:chain("firewall")
20 m.apply_on_parse = true
21
22 function m.on_apply(self)
23         luci.sys.call("/etc/init.d/travelmate restart >/dev/null 2>&1")
24 end
25
26 -- Interface Wizard
27
28 if uplink == "" then
29         ds = m:section(NamedSection, "global", "travelmate", translate("Interface Wizard"))
30         o = ds:option(Value, "trm_iface", translate("Create Uplink interface"),
31                 translate("Create a new wireless wan uplink interface, configure it to use dhcp and ")
32                 .. translate("add it to the wan zone of the firewall. ")
33                 .. translate("This step has only to be done once."))
34         o.datatype = "and(uciname,rangelength(3,15))"
35         o.default = trmiface
36         o.rmempty = false
37
38         function o.validate(self, value)
39                 if value then
40                         local nwnet = nw:get_network(value)
41                         local zone  = fw:get_zone("wan")
42                         local fwnet = fw:get_zone_by_network(value)
43                         if not nwnet then
44                                 nwnet = nw:add_network(value, { proto = "dhcp" })
45                         end
46                         if zone and not fwnet then
47                                 fwnet = zone:add_network(value)
48                         end
49                 end
50                 return value
51         end
52         return m
53 end
54
55 -- Main travelmate options
56
57 s = m:section(NamedSection, "global", "travelmate")
58
59 o1 = s:option(Flag, "trm_enabled", translate("Enable travelmate"))
60 o1.default = o1.disabled
61 o1.rmempty = false
62
63 o2 = s:option(Flag, "trm_captive", translate("Captive Portal Detection"),
64         translate("Check the internet availability, log captive portal redirections and keep the uplink connection 'alive'."))
65 o2.default = o2.enabled
66 o2.rmempty = false
67
68 o3 = s:option(ListValue, "trm_iface", translate("Uplink / Trigger interface"),
69         translate("Name of the used uplink interface."))
70 if dump then
71         local i, v
72         for i, v in ipairs(dump.interface) do
73                 if v.interface ~= "loopback" and v.interface ~= "lan" then
74                         o3:value(v.interface)
75                 end
76         end
77 end
78 o3.default = trmiface
79 o3.rmempty = false
80
81 if fs.access("/usr/bin/qrencode") then
82         btn = s:option(Button, "btn", translate("View AP QR-Codes"),
83                 translate("Connect your Android or iOS devices to your router's WiFi using the shown QR code."))
84         btn.inputtitle = translate("QR-Codes")
85         btn.inputstyle = "apply"
86         btn.disabled = false
87
88         function btn.write()
89                 luci.http.redirect(luci.dispatcher.build_url("admin", "services", "travelmate", "apqr"))
90         end
91 end
92
93 -- Runtime information
94
95 ds = s:option(DummyValue, "_dummy")
96 ds.template = "travelmate/runtime"
97
98 -- Extra options
99
100 e = m:section(NamedSection, "global", "travelmate", translate("Extra options"),
101 translate("Options for further tweaking in case the defaults are not suitable for you."))
102
103 e1 = e:option(Flag, "trm_debug", translate("Enable verbose debug logging"))
104 e1.default = e1.disabled
105 e1.rmempty = false
106
107 e2 = e:option(Value, "trm_radio", translate("Radio selection"),
108         translate("Restrict travelmate to a dedicated radio, e.g. 'radio0'."))
109 e2.datatype = "and(uciname,rangelength(6,6))"
110 e2.rmempty = true
111
112 e3 = e:option(Value, "trm_triggerdelay", translate("Trigger Delay"),
113         translate("Additional trigger delay in seconds before travelmate processing begins."))
114 e3.datatype = "range(1,60)"
115 e3.default = 2
116 e3.rmempty = false
117
118 e4 = e:option(Value, "trm_maxretry", translate("Connection Limit"),
119         translate("Retry limit to connect to an uplink."))
120 e4.default = 3
121 e4.datatype = "range(1,10)"
122 e4.rmempty = false
123
124 e5 = e:option(Value, "trm_minquality", translate("Signal Quality Threshold"),
125         translate("Minimum signal quality threshold as percent for conditional uplink (dis-) connections."))
126 e5.default = 35
127 e5.datatype = "range(20,80)"
128 e5.rmempty = false
129
130 e6 = e:option(Value, "trm_maxwait", translate("Interface Timeout"),
131         translate("How long should travelmate wait for a successful wlan uplink connection."))
132 e6.default = 30
133 e6.datatype = "range(20,40)"
134 e6.rmempty = false
135
136 e7 = e:option(Value, "trm_timeout", translate("Overall Timeout"),
137         translate("Overall retry timeout in seconds."))
138 e7.default = 60
139 e7.datatype = "range(30,300)"
140 e7.rmempty = false
141
142 return m