I set up something like that using gpscon
. Unfortunately it seems starting gpscon
is not enough to actually activate the location services.
But for reference, this is what I use to submit location data to PhoneTrack (a Nextcloud app). Itās very crude and does more than @Seven.of.nine wants here, but maybe itās useful.
All the magic is done by gpscon
, so play around with that.
I manually trigger start of this service from Situation when it detects i am āNot At Homeā, i.e. not in range of known Wifi networks:
~/.config/systemd/user/org.nephros.sailfish.phonetrack.service
[Unit]
Description=PhoneTrack Submission
After=lipstick.service
After=user-session.target
[Service]
Type=simple
RestartSec=1800
Restart=always
ExecStart=/bin/bash %h/.local/bin/phonetrack/submit.sh
[Install]
#WantedBy=user-session.target
And the script:
%h/.local/bin/phonetrack/submit.sh
#!/bin/sh
BASEURL=https://nextcloud.example.org/apps/phonetrack/logPost
TOKEN=<<PHONETRACK TOKEN>>
NAME=pgxperiiia10 # device name in phonetrack
POSTURL="${BASEURL}/${TOKEN}/${NAME}"
UA="sfos-track-with-gpscon"
# check whether we're online
if [ $( /usr/bin/connmanctl state | grep -c 'State = online' ) -ne 1 ]; then
echo "$0: Not online, doing nothing" > /dev/stderr
exit 0
fi
BATTINFO=/sys/class/power_supply/bms/uevent
eval $(grep POWER_SUPPLY_CAPACITY= ${BATTINFO})
# [limit=integer]
# sets the limit of the needed horizontal/vertical accuracy value before print out.
# There will be only one print out and after this the app is closing
#POSITION_CLI_CMD="/usr/bin/gpscon --script --fout limit=250 interval=5000 tout=60"
POSITION_CLI_CMD="/usr/bin/gpscon --script --fout --posm=all limit=250 interval=5000 tout=60"
POSITION_CLI_FALLBACK_CMD="/usr/bin/gpscon --script --lknv runs=1"
export QT_LOGGING_RULES="*.debug=false"
DATA=$($POSITION_CLI_CMD | sed 's/; /\n/g' | awk '{print $NF}' | xargs echo )
if [ "$DATA" = "" ]; then
echo "$0: falling back to last known location" > /dev/stderr
DATA=$($POSITION_CLI_FALLBACK_CMD | sed 's/; /\n/g' | awk '{print $NF}' | xargs echo )
fi
# Latitude: 47.xxx
# Longitude: 15.xxx
# Altitude: 439.xxx
# Direction: nan
# Ground Speed: nan
# Vertical Speed: nan
# Horizontal Acc: 45.3756
# Vertical Acc: 35.9893
# run: 46
# --> 47.xxx 15.xxx 439.xxx nan nan nan 45.3756 35.9893 46
# lat lon alt dir spd vspd hacc vacc
# $1 $2 $3 $4 $5 $6 $7 $8
# OK=$(echo $DATA | awk '{print $1}')
#echo dat: lat lon alt dir spd vspd hacc vacc runs
#echo dat: $DATA
LAT=$(echo $DATA | awk '{print $1}')
LON=$(echo $DATA | awk '{print $2}')
#ALT=$(echo $DATA | awk '{print $3}')
ALT=$(echo $DATA | awk '{printf("%d", $3)}')
DIR=$(echo $DATA | awk '{print $4}')
SPD=$(echo $DATA | awk '{print $5}')
ACC=$(echo $DATA | awk '{printf("%d", $7)}')
#if [ "$OK" != "yes" ]; then
# echo "$0: GPS is invalid" > /dev/stderr
# exit 75 # BSD EX_TEMPFAIL # https://freedesktop.org/software/systemd/man/systemd.exec.html#Process%20Exit%20Codes
#fi
if [ "$ACC" != "" ] && [ "$ACC" != "nan" ] && [ "$ACC" -gt 500 ]; then
echo "$0: GPS accuracy bad." > /dev/stderr
exit 0
fi
PAYLOAD="lat=${LAT}&lon=${LON}&bat=${POWER_SUPPLY_CAPACITY}"
if [ "$ALT" != "" ] && [ "$ALT" != "nan" ] ; then
PAYLOAD=$(printf '%s&alt=%d' "${PAYLOAD}" "${ALT}")
fi
if [ "$ACC" != "" ] && [ "$ACC" != "nan" ]; then
PAYLOAD=$(printf '%s&acc=%d' "${PAYLOAD}" "${ACC}")
fi
if [ "$SPD" != "" ] && [ "$SPD" != "nan" ]; then
PAYLOAD=$(printf '%s&speed=%d' "${PAYLOAD}" "${SPD}")
fi
if [ "$DIR" != "" ] && [ "$DIR" != "nan" ]; then
PAYLOAD=$(printf '%s&bearing=%d' "${PAYLOAD}" "${DIR}")
fi
PAYLOAD=$(printf '%s&useragent=%s' "${PAYLOAD}" "${UA}")
TIME=$(date +%s)
PAYLOAD=$(printf '%s×tamp=%d' "${PAYLOAD}" "${TIME}")
#curl -s -L -I -X POST "$PAYLOAD" # HEAD request
echo pld: "${PAYLOAD}"
#echo url: "${POSTURL}"
RES=$(curl -s -k -L -X POST -o /dev/null -w "%{http_code}" -d "$PAYLOAD" "${POSTURL}")
if [ "$RES" = "200" ]; then
echo "success."
exit 0
else
echo "$0: remote host answered $RES" > /dev/stderr
echo "failed."
exit 75 # BSD EX_TEMPFAIL # https://freedesktop.org/software/systemd/man/systemd.exec.html#Process%20Exit%20Codes
fi