modules/freifunk: implement map update for GlobalMap
authorJo-Philipp Wich <jow@openwrt.org>
Sun, 24 May 2009 19:28:04 +0000 (19:28 +0000)
committerJo-Philipp Wich <jow@openwrt.org>
Sun, 24 May 2009 19:28:04 +0000 (19:28 +0000)
applications/luci-ffwizard-leipzig: add lat and lon fields

applications/luci-ffwizard-leipzig/luasrc/model/cbi/ffwizard.lua
modules/freifunk/root/etc/init.d/freifunk
modules/freifunk/root/usr/sbin/ff_mapupdate [new file with mode: 0755]

index fa88f015070d90f385611e8f7a157ff1055634fe..e82353031215309b4735d426b334bfaca915456c 100644 (file)
@@ -80,10 +80,15 @@ client = f:field(Flag, "client", "WLAN-DHCP anbieten")
 client:depends("wifi", "1")
 client.rmempty = true
 
-
 olsr = f:field(Flag, "olsr", "OLSR einrichten")
 olsr.rmempty = true
 
+lat = f:field(Value, "lat", "Latitude")
+lat:depends("olsr", "1")
+
+lon = f:field(Value, "lon", "Longitude")
+lon:depends("olsr", "1")
+
 share = f:field(Flag, "sharenet", "Eigenen Internetzugang freigeben")
 share.rmempty = true
 
@@ -265,6 +270,10 @@ function olsr.write(self, section, value)
        local community = net:formvalue(section)
        local external  = community and uci:get("freifunk", community, "external") or ""
 
+       local latval = tonumber(lat:formvalue(section))
+       local lonval = tonumber(lon:formvalue(section))
+
+
        -- Delete old interface
        uci:delete_all("olsrd", "Interface", {interface=device})
 
@@ -285,6 +294,29 @@ function olsr.write(self, section, value)
                interval = "30"
        })
 
+       -- Delete old nameservice settings
+       uci:delete_all("olsrd", "LoadPlugin", {library="olsrd_nameservice.so.0.3"})
+
+       -- Write new nameservice settings
+       uci:section("olsrd", "LoadPlugin", nil, {
+               library     = "olsrd_nameservice.so.0.3",
+               latlon_file = "/var/run/latlon.js",
+               lat         = latval and string.format("%.15f", latval) or "",
+               lon         = lonval and string.format("%.15f", lonval) or ""
+       })
+
+       -- Save latlon to system too
+       if latval and lonval then
+               uci:foreach("system", "system", function(s)
+                       uci:set("system", s[".name"], "latlon",
+                               string.format("%.15f %.15f", latval, lonval))
+               end)
+       else
+               uci:foreach("system", "system", function(s)
+                       uci:delete("system", s[".name"], "latlon")
+               end)
+       end
+
        -- Import hosts
        uci:foreach("dhcp", "dnsmasq", function(s)
                uci:set("dhcp", s[".name"], "addnhosts", "/var/etc/hosts.olsr")
@@ -305,6 +337,7 @@ function share.write(self, section, value)
        end
        uci:save("firewall")
        uci:save("olsrd")
+       uci:save("system")
 end
 
 
index d90e3ff5e907dc8ed4070431be47d0535fe6831a..3a668b11f5dc9f931838ca2a7bb6616753edc97c 100755 (executable)
@@ -23,6 +23,10 @@ boot() {
                echo "0 */4 * * *       /usr/sbin/ff_rdate" >> /etc/crontabs/root
        }
 
+       grep -q '/usr/sbin/ff_mapupdate' /etc/crontabs/root || {
+               echo "17 * * * *        /usr/sbin/ff_mapupdate >> /etc/crontabs/root
+       }
+
        [ -f /etc/rc.local ] && . /etc/rc.local
        [ -d /etc/rc.local.d ] && {
                for file in /etc/rc.local.d/*; do
diff --git a/modules/freifunk/root/usr/sbin/ff_mapupdate b/modules/freifunk/root/usr/sbin/ff_mapupdate
new file mode 100755 (executable)
index 0000000..132546c
--- /dev/null
@@ -0,0 +1,49 @@
+#!/usr/bin/lua
+
+local uci = require "luci.model.uci"
+local x = uci.cursor()
+
+local update_url = "http://www.layereight.de/freifunkmap.php?update=%.15f,%.15f&olsrip=%s&note=%s&robot=%s"
+local update_all = ( arg[1] and arg[1] == "all" ) and true or false
+
+local file
+x:foreach("olsrd", "LoadPlugin", function(s)
+       if s.library == "olsrd_nameservice.so.0.3" then
+               file = io.open(s.latlon_file)
+       end
+end)
+
+if file then
+       local ln
+       local count = 0
+       while true do
+               ln = file:read("*l")
+               if not ln then break end
+               if update_all and ln:match("^Node%(") then
+                       local ip, lat, lon, note = ln:match("Node%('(%S+)',([%d%.]+),([%d%.]+),%d+,'%S+','(%S+)'%)")
+                       lat = tonumber(lat)
+                       lon = tonumber(lon)
+               
+                       if ip and lat ~= 0.0 and lon ~= 0.0 and note then
+                               note = note:gsub("[^%w%-%.]+", "_")
+                               os.execute("wget -qO/dev/null %q" % string.format(update_url, lat, lon, ip, note, "luci-massupdate"))
+                               count = count + 1
+                       end
+
+               elseif ln:match("^Self%(") then
+                       local ip, lat, lon, note = ln:match("Self%('(%S+)',([%d%.]+),([%d%.]+),%d+,'%S+','(%S+)'%)")
+                       lat = tonumber(lat)
+                       lot = tonumber(lon)
+                       
+                       if ip and lat ~= 0.0 and lon ~= 0.0 and note then
+                               note = note:gsub("[^%w%-%.]+", "_")
+                               os.execute("wget -qO/dev/null %q" % string.format(update_url, lat, lon, ip, note, "luci-selfupdate"))
+                               count = count + 1
+                       end
+               end
+       end
+
+       os.execute("logger -t 'mapupdate' 'Updated %d entries in freifunk map'" % count)
+
+       file:close()
+end