0400-geo-location.lua 2.4 KB

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