overview_tab.lua 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. -- Copyright 2017 Dirk Brenken (dev@brenken.org)
  2. -- This is free software, licensed under the Apache License, Version 2.0
  3. local fs = require("nixio.fs")
  4. local uci = require("luci.model.uci").cursor()
  5. local json = require("luci.jsonc")
  6. local nw = require("luci.model.network").init()
  7. local fw = require("luci.model.firewall").init()
  8. local trmiface = uci.get("travelmate", "global", "trm_iface") or "trm_wwan"
  9. local trminput = uci.get("travelmate", "global", "trm_rtfile") or "/tmp/trm_runtime.json"
  10. local uplink = uci.get("network", trmiface) or ""
  11. local parse = json.parse(fs.readfile(trminput) or "")
  12. m = Map("travelmate", translate("Travelmate"),
  13. translate("Configuration of the travelmate package to to enable travel router functionality. ")
  14. .. translatef("For further information "
  15. .. "<a href=\"%s\" target=\"_blank\">"
  16. .. "see online documentation</a>", "https://github.com/openwrt/packages/blob/master/net/travelmate/files/README.md"))
  17. function m.on_after_commit(self)
  18. luci.sys.call("env -i /etc/init.d/travelmate restart >/dev/null 2>&1")
  19. luci.http.redirect(luci.dispatcher.build_url("admin", "services", "travelmate"))
  20. end
  21. s = m:section(NamedSection, "global", "travelmate")
  22. -- Interface Wizard
  23. if uplink == "" then
  24. dv = s:option(DummyValue, "", translate("Interface Wizard"))
  25. dv.template = "cbi/nullsection"
  26. o = s:option(Value, "", translate("Uplink interface"))
  27. o.datatype = "and(uciname,rangelength(3,15))"
  28. o.default = trmiface
  29. o.rmempty = false
  30. btn = s:option(Button, "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. This step has only to be done once."))
  33. btn.inputtitle = translate("Add Interface")
  34. btn.inputstyle = "apply"
  35. btn.disabled = false
  36. function btn.write(self, section, value)
  37. local iface = o:formvalue(section)
  38. if iface then
  39. uci:set("travelmate", section, "trm_iface", iface)
  40. uci:save("travelmate")
  41. uci:commit("travelmate")
  42. local net = nw:add_network(iface, { proto = "dhcp" })
  43. if net then
  44. nw:save("network")
  45. nw:commit("network")
  46. local zone = fw:get_zone_by_network("wan")
  47. if zone then
  48. zone:add_network(iface)
  49. fw:save("firewall")
  50. fw:commit("firewall")
  51. end
  52. end
  53. luci.sys.call("env -i /bin/ubus call network reload >/dev/null 2>&1")
  54. end
  55. luci.http.redirect(luci.dispatcher.build_url("admin", "services", "travelmate"))
  56. end
  57. return m
  58. end
  59. -- Main travelmate options
  60. o1 = s:option(Flag, "trm_enabled", translate("Enable travelmate"))
  61. o1.default = o1.disabled
  62. o1.rmempty = false
  63. o2 = s:option(Flag, "trm_automatic", translate("Enable 'automatic' mode"),
  64. translate("Keep travelmate in an active state. Check every n seconds the connection status, i.e. the uplink availability."))
  65. o2.default = o2.enabled
  66. o2.rmempty = false
  67. btn = s:option(Button, "", translate("Manual Rescan"))
  68. btn:depends("trm_automatic", "")
  69. btn.inputtitle = translate("Rescan")
  70. btn.inputstyle = "find"
  71. btn.disabled = false
  72. function btn.write()
  73. luci.sys.call("env -i /etc/init.d/travelmate start >/dev/null 2>&1")
  74. luci.http.redirect(luci.dispatcher.build_url("admin", "services", "travelmate"))
  75. end
  76. o3 = s:option(Value, "trm_iface", translate("Uplink / Trigger interface"),
  77. translate("Name of the uplink interface that triggers travelmate processing in 'manual' mode."))
  78. o3.datatype = "and(uciname,rangelength(3,15))"
  79. o3.default = trmiface
  80. o3.rmempty = false
  81. o4 = s:option(Value, "trm_triggerdelay", translate("Trigger delay"),
  82. translate("Additional trigger delay in seconds before travelmate processing begins."))
  83. o4.default = 2
  84. o4.datatype = "range(1,90)"
  85. o4.rmempty = false
  86. o5 = s:option(Flag, "trm_debug", translate("Enable verbose debug logging"))
  87. o5.default = o5.disabled
  88. o5.rmempty = false
  89. -- Runtime information
  90. ds = s:option(DummyValue, "_dummy", translate("Runtime information"))
  91. ds.template = "cbi/nullsection"
  92. dv1 = s:option(DummyValue, "status", translate("Online Status"))
  93. dv1.template = "travelmate/runtime"
  94. if parse == nil then
  95. dv1.value = translate("n/a")
  96. elseif parse.data.station_connection == "true" then
  97. dv1.value = translate("connected")
  98. else
  99. dv1.value = translate("not connected")
  100. end
  101. dv2 = s:option(DummyValue, "travelmate_version", translate("Travelmate version"))
  102. dv2.template = "travelmate/runtime"
  103. if parse ~= nil then
  104. dv2.value = parse.data.travelmate_version or translate("n/a")
  105. else
  106. dv2.value = translate("n/a")
  107. end
  108. dv3 = s:option(DummyValue, "station_ssid", translate("Station SSID"))
  109. dv3.template = "travelmate/runtime"
  110. if parse ~= nil then
  111. dv3.value = parse.data.station_ssid or translate("n/a")
  112. else
  113. dv3.value = translate("n/a")
  114. end
  115. dv4 = s:option(DummyValue, "station_interface", translate("Station Interface"))
  116. dv4.template = "travelmate/runtime"
  117. if parse ~= nil then
  118. dv4.value = parse.data.station_interface or translate("n/a")
  119. else
  120. dv4.value = translate("n/a")
  121. end
  122. dv5 = s:option(DummyValue, "station_radio", translate("Station Radio"))
  123. dv5.template = "travelmate/runtime"
  124. if parse ~= nil then
  125. dv5.value = parse.data.station_radio or translate("n/a")
  126. else
  127. dv5.value = translate("n/a")
  128. end
  129. dv6 = s:option(DummyValue, "last_rundate", translate("Last rundate"))
  130. dv6.template = "travelmate/runtime"
  131. if parse ~= nil then
  132. dv6.value = parse.data.last_rundate or translate("n/a")
  133. else
  134. dv6.value = translate("n/a")
  135. end
  136. -- Extra options
  137. e = m:section(NamedSection, "global", "travelmate", translate("Extra options"),
  138. translate("Options for further tweaking in case the defaults are not suitable for you."))
  139. e1 = e:option(Value, "trm_radio", translate("Radio selection"),
  140. translate("Restrict travelmate to a dedicated radio, e.g. 'radio0'."))
  141. e1.datatype = "and(uciname,rangelength(6,6))"
  142. e1.rmempty = true
  143. e2 = e:option(Value, "trm_maxretry", translate("Connection Limit"),
  144. translate("How many times should travelmate try to connect to an Uplink. ")
  145. .. translate("To disable this feature set it to '0' which means unlimited retries."))
  146. e2.default = 3
  147. e2.datatype = "range(0,30)"
  148. e2.rmempty = false
  149. e3 = e:option(Value, "trm_maxwait", translate("Interface Timeout"),
  150. translate("How long should travelmate wait for a successful wlan interface reload."))
  151. e3.default = 30
  152. e3.datatype = "range(5,60)"
  153. e3.rmempty = false
  154. e4 = e:option(Value, "trm_timeout", translate("Overall Timeout"),
  155. translate("Timeout in seconds between retries in 'automatic' mode."))
  156. e4.default = 60
  157. e4.datatype = "range(60,300)"
  158. e4.rmempty = false
  159. return m