freifunk.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. -- Copyright 2008 Steven Barth <steven@midlink.org>
  2. -- Licensed to the public under the Apache License 2.0.
  3. module("luci.controller.freifunk.freifunk", package.seeall)
  4. function index()
  5. local uci = require "luci.model.uci".cursor()
  6. local page
  7. -- Frontend
  8. page = node()
  9. page.lock = true
  10. page.target = alias("freifunk")
  11. page.subindex = true
  12. page.index = false
  13. page = node("freifunk")
  14. page.title = _("Freifunk")
  15. page.target = alias("freifunk", "index")
  16. page.order = 5
  17. page.setuser = "nobody"
  18. page.setgroup = "nogroup"
  19. page.i18n = "freifunk"
  20. page.index = true
  21. page = node("freifunk", "index")
  22. page.target = template("freifunk/index")
  23. page.title = _("Overview")
  24. page.order = 10
  25. page.indexignore = true
  26. page = node("freifunk", "contact")
  27. page.target = template("freifunk/contact")
  28. page.title = _("Contact")
  29. page.order = 15
  30. page = node("freifunk", "status")
  31. page.target = template("freifunk/public_status")
  32. page.title = _("Status")
  33. page.order = 20
  34. page.i18n = "base"
  35. page.setuser = false
  36. page.setgroup = false
  37. entry({"freifunk", "status.json"}, call("jsonstatus"))
  38. entry({"freifunk", "status", "zeroes"}, call("zeroes"), "Testdownload")
  39. if nixio.fs.access("/usr/sbin/luci-splash") then
  40. assign({"freifunk", "status", "splash"}, {"splash", "publicstatus"}, _("Splash"), 40)
  41. end
  42. page = assign({"freifunk", "olsr"}, {"admin", "status", "olsr"}, _("OLSR"), 30)
  43. page.setuser = false
  44. page.setgroup = false
  45. if nixio.fs.access("/etc/config/luci_statistics") then
  46. assign({"freifunk", "graph"}, {"admin", "statistics", "graph"}, _("Statistics"), 40)
  47. end
  48. -- backend
  49. assign({"mini", "freifunk"}, {"admin", "freifunk"}, _("Freifunk"), 5)
  50. entry({"admin", "freifunk"}, alias("admin", "freifunk", "index"), _("Freifunk"), 5)
  51. page = node("admin", "freifunk")
  52. page.target = template("freifunk/adminindex")
  53. page.title = _("Freifunk")
  54. page.order = 5
  55. page = node("admin", "freifunk", "basics")
  56. page.target = cbi("freifunk/basics")
  57. page.title = _("Basic Settings")
  58. page.order = 5
  59. page = node("admin", "freifunk", "basics", "profile")
  60. page.target = cbi("freifunk/profile")
  61. page.title = _("Profile")
  62. page.order = 10
  63. page = node("admin", "freifunk", "basics", "profile_expert")
  64. page.target = cbi("freifunk/profile_expert")
  65. page.title = _("Profile (Expert)")
  66. page.order = 20
  67. page = node("admin", "freifunk", "Index-Page")
  68. page.target = cbi("freifunk/user_index")
  69. page.title = _("Index Page")
  70. page.order = 50
  71. page = node("admin", "freifunk", "contact")
  72. page.target = cbi("freifunk/contact")
  73. page.title = _("Contact")
  74. page.order = 15
  75. entry({"freifunk", "map"}, template("freifunk-map/frame"), _("Map"), 50)
  76. entry({"freifunk", "map", "content"}, template("freifunk-map/map"), nil, 51)
  77. entry({"admin", "freifunk", "profile_error"}, template("freifunk/profile_error"))
  78. end
  79. function zeroes()
  80. local string = require "string"
  81. local http = require "luci.http"
  82. local zeroes = string.rep(string.char(0), 8192)
  83. local cnt = 0
  84. local lim = 1024 * 1024 * 1024
  85. http.prepare_content("application/x-many-zeroes")
  86. while cnt < lim do
  87. http.write(zeroes)
  88. cnt = cnt + #zeroes
  89. end
  90. end
  91. function jsonstatus()
  92. local root = {}
  93. local sys = require "luci.sys"
  94. local uci = require "luci.model.uci"
  95. local util = require "luci.util"
  96. local http = require "luci.http"
  97. local json = require "luci.json"
  98. local ltn12 = require "luci.ltn12"
  99. local version = require "luci.version"
  100. local webadmin = require "luci.tools.webadmin"
  101. local cursor = uci.cursor_state()
  102. local ffzone = webadmin.firewall_find_zone("freifunk")
  103. local ffznet = ffzone and cursor:get("firewall", ffzone, "network")
  104. local ffwifs = ffznet and util.split(ffznet, " ") or {}
  105. local sysinfo = util.ubus("system", "info") or { }
  106. local boardinfo = util.ubus("system", "board") or { }
  107. local loads = sysinfo.load or { 0, 0, 0 }
  108. local memory = sysinfo.memory or {
  109. total = 0,
  110. free = 0,
  111. shared = 0,
  112. buffered = 0
  113. }
  114. local swap = sysinfo.swap or {
  115. total = 0,
  116. free = 0
  117. }
  118. root.protocol = 1
  119. root.system = {
  120. uptime = { sysinfo.uptime or 0 },
  121. loadavg = { loads[1] / 65535.0, loads[2] / 65535.0, loads[3] / 65535.0 },
  122. sysinfo = {
  123. boardinfo.system or "?",
  124. boardinfo.model or "?",
  125. memory.total,
  126. 0, -- former cached memory
  127. memory.buffered,
  128. memory.free,
  129. 0, -- former bogomips
  130. swap.total,
  131. 0, -- former cached swap
  132. swap.free
  133. },
  134. hostname = boardinfo.hostname
  135. }
  136. root.firmware = {
  137. luciname=version.luciname,
  138. luciversion=version.luciversion,
  139. distname=version.distname,
  140. distversion=version.distversion
  141. }
  142. root.freifunk = {}
  143. cursor:foreach("freifunk", "public", function(s)
  144. root.freifunk[s[".name"]] = s
  145. end)
  146. cursor:foreach("system", "system", function(s)
  147. root.geo = {
  148. latitude = s.latitude,
  149. longitude = s.longitude
  150. }
  151. end)
  152. root.network = {}
  153. root.wireless = {devices = {}, interfaces = {}, status = {}}
  154. local wifs = root.wireless.interfaces
  155. local netdata = luci.sys.net.deviceinfo() or {}
  156. for _, vif in ipairs(ffwifs) do
  157. root.network[vif] = cursor:get_all("network", vif)
  158. root.wireless.devices[vif] = cursor:get_all("wireless", vif)
  159. cursor:foreach("wireless", "wifi-iface", function(s)
  160. if s.device == vif and s.network == vif then
  161. wifs[#wifs+1] = s
  162. if s.ifname then
  163. local iwinfo = luci.sys.wifi.getiwinfo(s.ifname)
  164. if iwinfo then
  165. root.wireless.status[s.ifname] = { }
  166. local _, f
  167. for _, f in ipairs({
  168. "channel", "txpower", "bitrate", "signal", "noise",
  169. "quality", "quality_max", "mode", "ssid", "bssid", "encryption", "ifname"
  170. }) do
  171. root.wireless.status[s.ifname][f] = iwinfo[f]
  172. end
  173. end
  174. end
  175. end
  176. end)
  177. end
  178. http.prepare_content("application/json")
  179. ltn12.pump.all(json.Encoder(root):source(), http.write)
  180. end