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