0400-geo-location.lua 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. local cbi = require "luci.cbi"
  2. local i18n = require "luci.i18n"
  3. local uci = luci.model.uci.cursor()
  4. local M = {}
  5. function M.section(form)
  6. local s = form:section(cbi.SimpleSection, nil, i18n.translate(
  7. 'If you want the location of your node to be displayed on the map, '
  8. .. 'you can enter its coordinates here. Specifying the altitude '
  9. .. 'is optional and should only be done if a proper value is known.'))
  10. local o
  11. o = s:option(cbi.Flag, "_location", i18n.translate("Show node on the map"))
  12. o.default = uci:get_first("gluon-node-info", "location", "share_location", o.disabled)
  13. o.rmempty = false
  14. o = s:option(cbi.Value, "_latitude", i18n.translate("Latitude"))
  15. o.default = uci:get_first("gluon-node-info", "location", "latitude")
  16. o:depends("_location", "1")
  17. o.rmempty = false
  18. o.datatype = "float"
  19. o.description = i18n.translatef("e.g. %s", "53.873621")
  20. o = s:option(cbi.Value, "_longitude", i18n.translate("Longitude"))
  21. o.default = uci:get_first("gluon-node-info", "location", "longitude")
  22. o:depends("_location", "1")
  23. o.rmempty = false
  24. o.datatype = "float"
  25. o.description = i18n.translatef("e.g. %s", "10.689901")
  26. o = s:option(cbi.Value, "_altitude", i18n.translate("Altitude"))
  27. o.default = uci:get_first("gluon-node-info", "location", "altitude")
  28. o:depends("_location", "1")
  29. o.rmempty = true
  30. o.datatype = "float"
  31. o.description = i18n.translatef("e.g. %s", "11.51")
  32. end
  33. function M.handle(data)
  34. local sname = uci:get_first("gluon-node-info", "location")
  35. uci:set("gluon-node-info", sname, "share_location", data._location)
  36. if data._location and data._latitude ~= nil and data._longitude ~= nil then
  37. uci:set("gluon-node-info", sname, "latitude", data._latitude:trim())
  38. uci:set("gluon-node-info", sname, "longitude", data._longitude:trim())
  39. if data._altitude ~= nil then
  40. uci:set("gluon-node-info", sname, "altitude", data._altitude:trim())
  41. else
  42. uci:delete("gluon-node-info", sname, "altitude")
  43. end
  44. end
  45. uci:save("gluon-node-info")
  46. uci:commit("gluon-node-info")
  47. end
  48. return M