From f0c62fc2a7a12bf004a14765d939155c3862d3b6 Mon Sep 17 00:00:00 2001 From: dman776 Date: Tue, 9 Nov 2021 17:03:20 -0600 Subject: [PATCH] enhancement: migrate location info to uci (#168) * add uci migration script * removed debug lines * added newline to end * include gridsquare values * migrate lat/lon/gridsquare to uci format * remove from sysupgrade kept files list --- files/etc/arednsysupgrade.conf | 2 - .../etc/uci-defaults/50_aredn_migrate_latlon | 25 ++++++++ files/usr/lib/lua/aredn/info.lua | 26 ++------- files/www/cgi-bin/api | 2 +- files/www/cgi-bin/setup | 57 ++++++------------- 5 files changed, 47 insertions(+), 65 deletions(-) create mode 100755 files/etc/uci-defaults/50_aredn_migrate_latlon diff --git a/files/etc/arednsysupgrade.conf b/files/etc/arednsysupgrade.conf index b0b1ecb7..b43a47ef 100644 --- a/files/etc/arednsysupgrade.conf +++ b/files/etc/arednsysupgrade.conf @@ -18,8 +18,6 @@ /etc/firewall.user /etc/group /etc/hosts -/etc/gridsquare -/etc/latlon /etc/local/services /etc/local/uci/hsmmmesh /etc/passwd diff --git a/files/etc/uci-defaults/50_aredn_migrate_latlon b/files/etc/uci-defaults/50_aredn_migrate_latlon new file mode 100755 index 00000000..6b54cbce --- /dev/null +++ b/files/etc/uci-defaults/50_aredn_migrate_latlon @@ -0,0 +1,25 @@ +#! /bin/sh +if [ "$(/sbin/uci -q get aredn.\@location[0])" = "" ]; then + /sbin/uci -q add aredn location +fi + +# read /etc/latlon +if [ -f /etc/latlon ] +then + LAT=$(head -1 /etc/latlon) + LON=$(tail -1 /etc/latlon) +fi + +if [ -f /etc/gridsquare ] +then + GRIDSQUARE=$(head -1 /etc/gridsquare) +fi + +/sbin/uci -q set aredn.@location[0].lat="$LAT" +/sbin/uci -q set aredn.@location[0].lon="$LON" +/sbin/uci -q set aredn.@location[0].gridsquare="$GRIDSQUARE" +/sbin/uci -q commit aredn + +# remove the /etc/latlon and gridsquare files +rm -f /etc/latlon +rm -f /etc/gridsquare diff --git a/files/usr/lib/lua/aredn/info.lua b/files/usr/lib/lua/aredn/info.lua index 7b59dafb..1bc9fb21 100755 --- a/files/usr/lib/lua/aredn/info.lua +++ b/files/usr/lib/lua/aredn/info.lua @@ -109,34 +109,16 @@ end -- Returns array [Latitude, Longitude] ------------------------------------- function model.getLatLon() - local llfname="/etc/latlon" - local lat="" - local lon="" - if file_exists(llfname) then - llfile=io.open(llfname,"r") - if llfile~=nil then - lat=llfile:read() - lon=llfile:read() - llfile:close() - end - end - return lat,lon + loc=aredn_uci.getUciConfType("aredn", "location") + return loc[1]['lat'], loc[1]['lon'] end ------------------------------------- -- Returns Grid Square of Node ------------------------------------- function model.getGridSquare() - local gsfname="/etc/gridsquare" - local grid="" - if file_exists(gsfname) then - gsfile=io.open(gsfname,"r") - if gsfile~=nil then - grid=gsfile:read() - gsfile:close() - end - end - return grid + loc=aredn_uci.getUciConfType("aredn", "location") + return loc[1]['gridsquare'] end ------------------------------------- diff --git a/files/www/cgi-bin/api b/files/www/cgi-bin/api index b75c5794..c29086d7 100755 --- a/files/www/cgi-bin/api +++ b/files/www/cgi-bin/api @@ -44,7 +44,7 @@ require("nixio") local json = require("luci.jsonc") require("iwinfo") -local API_VERSION="1.3" +local API_VERSION="1.4" -- Function extensions os.capture = capture diff --git a/files/www/cgi-bin/setup b/files/www/cgi-bin/setup index d0dbdb4d..4e3b79f4 100755 --- a/files/www/cgi-bin/setup +++ b/files/www/cgi-bin/setup @@ -218,18 +218,17 @@ if($parms{button_updatelocation}) # validate values if($parms{gridsquare} =~ /^[A-Z][A-Z]\d\d[a-z][a-z]$/) { - # delete/define file - unlink("/etc/gridsquare") if(-f "/etc/gridsquare"); - my $rcgood=open(my $gs, ">", "/etc/gridsquare"); - push @errors, "Cannot open gridsquare file" unless $rcgood; - print $gs "$parms{gridsquare}\n"; - close($gs); + # set values/commit + $rc=&uci_set_indexed_option("aredn","location",0,"gridsquare", $parms{gridsquare}); + $rc=&uci_commit("aredn"); + push @errors, "Cannot save gridsquare in uci" if $rc ne "0"; push @output, "Gridsquare updated.\n"; } else { push @errors, "ERROR: Gridsquare format is: 2-uppercase letters, 2-digits, 2-lowercase letters. (AB12cd)\n"; } } else { - unlink("/etc/gridsquare") if(-f "/etc/gridsquare"); + $rc=&uci_set_indexed_option("aredn","location",0,"gridsquare", ""); + $rc=&uci_commit("aredn"); push @output, "Gridsquare purged.\n"; } @@ -239,13 +238,11 @@ if($parms{button_updatelocation}) # validate values if($parms{latitude} =~ /^([-+]?\d{1,2}([.]\d+)?)$/ and $parms{longitude} =~ /^([-+]?\d{1,3}([.]\d+)?)$/) { if($parms{latitude} >= -90 and $parms{latitude} <= 90 and $parms{longitude} >= -180 and $parms{longitude} <= 180) { - # delete/define file - unlink("/etc/latlon") if(-f "/etc/latlon"); - $rcgood=open(my $ll, ">", "/etc/latlon"); - push @errors, "Cannot open lat/lon file" unless $rcgood; - print $ll "$parms{latitude}\n"; - print $ll "$parms{longitude}\n"; - close($ll); + # set values/commit + $rc=&uci_set_indexed_option("aredn","location",0,"lat", $parms{latitude}); + $rc=&uci_set_indexed_option("aredn","location",0,"lon", $parms{longitude}); + $rc=&uci_commit("aredn"); + push @errors, "Cannot save latitude/longitude in uci" if $rc ne "0"; push @output, "Lat/lon updated.\n"; } else { push @errors, "ERROR: Lat/lon values must be between -90/90 and -180/180, respectively.\n"; @@ -254,7 +251,9 @@ if($parms{button_updatelocation}) push @errors, "ERROR: Lat/lon format is decimal: (ex. 30.121456 or -95.911154)\n"; } } else { - unlink("/etc/latlon") if(-f "/etc/latlon"); + $rc=&uci_set_indexed_option("aredn","location",0,"lat", ""); + $rc=&uci_set_indexed_option("aredn","location",0,"lon", ""); + $rc=&uci_commit("aredn"); push @output, "Lat/lon purged.\n"; } } @@ -262,31 +261,9 @@ if($parms{button_updatelocation}) # # retrieve location data # -if(-f "/etc/latlon") -{ - $rcgood=open(FILE, "/etc/latlon"); - push @errors, "ERROR: reading lat/lon data\n" unless $rcgood; - while(){ - chomp; - push @lines,$_; - } - close(FILE); - $lat=$lines[0]; - $lon=$lines[1]; -} - -@lines=(); -if(-f "/etc/gridsquare") -{ - $rcgood=open(FILE, "/etc/gridsquare"); - push @errors, "ERROR: reading gridsquare data\n" unless $rcgood; - while(){ - chomp; - push @lines,$_; - } - close(FILE); - $gridsquare=$lines[0]; -} +$lat=&uci_get_indexed_option("aredn","location",0,"lat"); +$lon=&uci_get_indexed_option("aredn","location",0,"lon"); +$gridsquare=&uci_get_indexed_option("aredn","location",0,"gridsquare"); # validate and save configuration