-
Notifications
You must be signed in to change notification settings - Fork 0
/
gps_check.sh
executable file
·127 lines (109 loc) · 3.88 KB
/
gps_check.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/bin/bash
# Verify if this script is running as root and if not exit
if [ "$(id -u)" != "0" ]; then
echo -n "\nERROR: This script must be run as root!\n" 1>&2
exit 1
fi
# Prints usage
usage() {
echo -e "\nUsage:"
echo -e "-h\t\t\tThis help"
echo -e "-s [seconds]\t\tSleep interval between tests (default is 10 seconds)\n"
exit 0
}
# --------------------------------------------------------------------------------
# Set default options
# How many seconds to sleep between tests
update_interval=10
# --------------------------------------------------------------------------------
# Get command line arguments
while getopts ":s:h" opts; do
case "${opts}" in
s)
update_interval=${OPTARG}
;;
h | *)
usage
;;
esac
done
# Look for config file
if [ -e ./config.ini ]; then
source ./config.ini
else
echo -e "\nWARNING: config.ini not found, using defaults!\n";
source ./config.ini.default
fi
# Verify if gpspipe is installed
if [ ! -x $gpspipe_bin ]; then
echo -e "\ngpspipe binary not found or is not executable!\n";
exit 1
fi
# Verify if gpsd is running
gpsd_pid=`ps cax | grep gpsd`
if [ $? -ne 0 ]; then
echo -e "\nERROR: GPSD is not running, please make sure to start it!\n"
exit 1
fi
# Start the loop
while true
do
# Get GPS Data in JSON format from gpsd
echo -ne "Looking for current location..."
while true
do
tpv=$($gpspipe_bin -w -n 5 | grep -m 1 TPV | python -mjson.tool 2>/dev/null)
lon=$(echo "$tpv" | grep "lon" | cut -d: -f2 | cut -d, -f1 | tr -d ' ')
lat=$(echo "$tpv" | grep "lat" | cut -d: -f2 | cut -d, -f1 | tr -d ' ')
if [ ! -z "$lon" -a ! -z "$lat" ]; then
break
else
echo -ne "."
fi
done
echo -e "Ok\n"
gps_date=$(echo "$tpv" | grep "time" | cut -d: -f2 | cut -dT -f1 | cut -d, -f1 | tr -d ' ' | tr -d '"')
gps_time=$(echo "$tpv" | grep "time" | cut -dT -f2 | cut -d. -f1 | cut -d, -f1 | tr -d ' ')
alt=$(echo "$tpv" | grep "alt" | cut -d: -f2 | cut -d, -f1 | tr -d ' ' | awk '{print int($1)}')
spd=$(echo "$tpv" | grep "speed" | cut -d: -f2 | cut -d, -f1 | tr -d ' ')
track=$(echo "$tpv" | grep "track" | cut -d: -f2 | cut -d, -f1 | tr -d ' ' | awk '{print int($1)}')
if [ -z "$alt" ]; then
echo "WARNING: No GPS altitude - setting to 0 Meters"
alt=0
fi
if [ -z "$spd" ]; then
echo "WARNING: No GPS speed - setting to 0 km/h"
spd=0
fi
if [ -z "$track" ]; then
echo "WARNING: No GPS track - setting to 0 Degrees"
track=0
fi
# Convert speed from meters per second to kilometers per hour
spd=`echo $spd | awk '{print int($1 * 3.6)}'`
# Print GPS Results
echo -e "Date:\t\t$gps_date"
echo -e "Time:\t\t$gps_time"
echo -e "Longitude:\t$lon"
echo -e "Latitude:\t$lat"
echo -e "Altitude:\t$alt Meters"
echo -e "Speed:\t\t$spd km/h"
echo -e "Track:\t\t$track Degrees"
echo -e "\nNOTE: Sleeping for $update_interval seconds..."
update_interval_tmp="$update_interval"
while [ $update_interval_tmp -gt 0 ]; do
echo -ne "$update_interval_tmp...\033[0K\r"
sleep 1
: $((update_interval_tmp--))
done
echo "--------------------------------------------------------------------------------"
# Clear all vars
unset tpv
unset gps_date
unset gps_time
unset lat
unset lon
unset alt
unset spd
unset track
done