stations 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/lua
  2. util = require 'luci.util'
  3. json = require 'luci.jsonc'
  4. nixio = require 'nixio'
  5. iwinfo = require 'iwinfo'
  6. function badrequest()
  7. io.write("Status: 400 Bad Request\n\n")
  8. os.exit(1)
  9. end
  10. function get_stations(iw, ifname)
  11. local stations = {}
  12. for k, v in pairs(iw.assoclist(ifname)) do
  13. stations[k:lower()] = {signal = v.signal, noise = v.noise, inactive = v.inactive}
  14. end
  15. return stations
  16. end
  17. local ifname = os.getenv("QUERY_STRING")
  18. if ifname == nil then badrequest() end
  19. local list = util.exec('batctl if')
  20. local found = false
  21. for _, line in ipairs(util.split(list)) do
  22. if ifname == line:match('^(.-):') then
  23. found = true
  24. break
  25. end
  26. end
  27. if found == false then badrequest() end
  28. local wifitype = iwinfo.type(ifname)
  29. if wifitype == nil then badrequest() end
  30. local iw = iwinfo[wifitype]
  31. io.write("Access-Control-Allow-Origin: *\n")
  32. io.write("Content-type: text/event-stream\n\n")
  33. while true do
  34. local stations = json.stringify(get_stations(iw, ifname))
  35. io.write("data: " .. stations .. "\n\n")
  36. io.flush()
  37. nixio.nanosleep(0, 150e6)
  38. end