-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtracker
More file actions
271 lines (246 loc) · 10.1 KB
/
tracker
File metadata and controls
271 lines (246 loc) · 10.1 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/bin/bash
##############################################
# Author: Kamil BuriXon Burek (BuriXon-code) #
# Name: Termux-GPS-Tracker (c) 2025 #
# Description: Calculating the distance to #
# the designated target and #
# saving location data. #
# Version: v 2.1 #
# Changelog: + save data to KML file #
# + timeout on location retrieval #
# + verbose errors #
# + prettier outputs formatting #
# + more output informations #
# ++ handle API fails #
# Todo: - - - #
##############################################
BEEP_VOLUME="40"
SAVING=false
DELAY="15"
PROVIDER="gps"
GREEN="\e[38;5;46m"
YELLOW="\e[38;5;226m"
RED="\e[38;5;196m"
ORANGE="\e[38;5;214m"
TEXT_COLOR="\e[38;5;45m"
RESET_COLOR="\e[0m"
abort() {
if $SAVING; then
finish_kml
fi
echo -e "\n${YELLOW}---${ORANGE}\n\nAborting\n\e[?25h"
termux-volume music 0
exit 1
}
print_usage() {
echo -e "${TEXT_COLOR}Usage:${RESET_COLOR} $(basename $0) -lt <latitude> -lg <longitude> -r <radius> -d <delay> -p <provider>"
echo " -lt, --latitude Latitude target (e.g., 51.5074)."
echo " -lg, --longitude Longitude target (e.g., -0.1278)."
echo " -r, --radius Radius in meters (only digits, e.g., 1000)."
echo " -d, --delay Delay between checks in seconds (1-3600) (default 15)."
echo " -p, --provider Location provider (gps, network, or passive) (default gps)."
echo " -v, --volume Volume for beeping soumd when reaching target (0-150) (default=40)."
echo " -s, --save Save step-to-step locations to specified KML file. (Using the same file name will overwrite it!)"
exit 1
}
calculate_distance() {
lat1=$1
lon1=$2
lat2=$3
lon2=$4
awk -v lat1="$lat1" -v lon1="$lon1" -v lat2="$lat2" -v lon2="$lon2" '
BEGIN {
pi = 3.141592653589793;
R = 6371000; # Promień Ziemi w metrach
dlat = (lat2 - lat1) * pi / 180;
dlon = (lon2 - lon1) * pi / 180;
lat1 = lat1 * pi / 180;
lat2 = lat2 * pi / 180;
a = sin(dlat / 2) ^ 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ^ 2;
c = 2 * atan2(sqrt(a), sqrt(1 - a));
print R * c;
}'
}
vibrate_intense() {
while true; do
termux-vibrate -f -d 1000
sleep 1
done
}
beeping() {
while true; do
play -q -n synth 0.1 sine 400 &>/dev/null
play -q -n synth 0.1 sine 430 &>/dev/null
play -q -n synth 0.1 sine 400 &>/dev/null
play -q -n synth 0.1 sine 430 &>/dev/null
play -q -n synth 0.1 sine 400 &>/dev/null
play -q -n synth 0.1 sine 450 &>/dev/null
play -q -n synth 0.1 sine 360 &>/dev/null
sleep 1.5
done
}
target_reached() {
echo -en "\e[?25h"
termux-volume music $BEEP_VOLUME
vibrate_intense &
beeping
}
error_vib() {
termux-vibrate -f -d 50
termux-vibrate -f -d 80
termux-vibrate -f -d 80
}
save_location_kml() {
local timestamp=$(date "+%m.%d %H:%M:%S")
local lat=$1
local lon=$2
local alt=$3
if [[ $(wc -c < "$SAVE_FILE") -le 1 ]]; then
cat <<EOF > "$SAVE_FILE"
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>Termux-GPS-tracker v2.0 (c) by BuriXon-code</name>
EOF
fi
cat <<EOF >> "$SAVE_FILE"
<Placemark>
<TimeStamp><when>$timestamp</when></TimeStamp>
<title>$timestamp</title>
<name>$timestamp</name>
<description>$timestamp</description>
<Point>
<coordinates>$lon,$lat,$alt</coordinates>
</Point>
</Placemark>
EOF
}
finish_kml() {
if [[ $(wc -c < "$SAVE_FILE") -gt 300 && -f "$SAVE_FILE" ]]; then
echo "</Document>
</kml>" >> "$SAVE_FILE"
echo -e "${GREEN}Saved in:${RESET_COLOR} "$SAVE_FILE"\n"
else
echo -e "${RED}Error:${RESET_COLOR} ${ORANGE}Some error occured while saving file....${RESET_COLOR}\n" >&2
exit 1
fi
}
main() {
echo -e "\e[?25l"
echo -e "${GREEN}Target:${RESET_COLOR} latitude $TARGET_LAT, longitude $TARGET_LON"
echo -e "${GREEN}Radius:${RESET_COLOR} ${RADIUS}m"
echo -e "${GREEN}Provider:${RESET_COLOR} $PROVIDER"
echo -e "${GREEN}Measurement Interval:${RESET_COLOR} ${DELAY}s"
echo -e "${GREEN}Beeping volume:${RESET_COLOR} ${BEEP_VOLUME}/150\n"
echo -e "${YELLOW}--- waiting...${RESET_COLOR}\n"
while true; do
location=$(timeout 15 termux-location -p "$PROVIDER" 2>/dev/null ) 2>/dev/null
curr_lat=$(echo "$location" | jq .latitude)
curr_lon=$(echo "$location" | jq .longitude)
curr_alt=$(echo "$location" | jq .altitude)
if [[ -z $curr_lat || -z $curr_lon ]]; then
echo -e "\e[2A\r${YELLOW}---\e[K\n\n${RED}Error:${RESET_COLOR} ${ORANGE}Failed to get location data.${RESET_COLOR}\n" >&2
echo -e "${YELLOW}--- waiting ...${RESET_COLOR}\n"
error_vib &
termux-api-stop &>/dev/null
if ! termux-api-start &>/dev/null; then
echo -e "\e[2A\r${YELLOW}---\e[K\n\n${RED}Error:${RESET_COLOR} ${ORANGE}API failed... aborting.${RESET_COLOR}\n" >&2
exit 1
fi
sleep "$DELAY"
continue
fi
echo -e "\e[2A\r${YELLOW}---${RESET_COLOR}\e[K\n"
echo -e "${TEXT_COLOR}Target Location:${RESET_COLOR} Lat: $TARGET_LAT, Lon: $TARGET_LON"
echo -e "${TEXT_COLOR}Current Location:${RESET_COLOR} Lat: $curr_lat, Lon: $curr_lon"
echo -e "${TEXT_COLOR}Current Time:${RESET_COLOR} $(date '+%d·%m — %A — %H:%M:%S')"
distance=$(calculate_distance "$curr_lat" "$curr_lon" "$TARGET_LAT" "$TARGET_LON")
distance=$(printf "%.2f" "$distance")
if $SAVING; then
save_location_kml "$curr_lat" "$curr_lon" "$curr_alt"
echo -e "${TEXT_COLOR}Saved in:${RESET_COLOR} "$SAVE_FILE""
else
echo
fi
echo -e "${TEXT_COLOR}Distance to Target:${RESET_COLOR} ${distance}m\n"
echo -e "${YELLOW}--- waiting ...${RESET_COLOR}\n"
if (( $(echo "$distance <= $RADIUS" | bc -l) )); then
echo -e "${GREEN}Within radius! Target reached!${RESET_COLOR}\n"
target_reached
fi
sleep "$DELAY"
done
}
while [[ $# -gt 0 ]]; do
case $1 in
-lt|--latitude)
TARGET_LAT=$2
shift 2
;;
-lg|--longitude)
TARGET_LON=$2
shift 2
;;
-r|--radius)
if [[ $2 =~ ^[0-9]+$ ]]; then
RADIUS=$2
else
echo -e "${RED}Error:${RESET_COLOR} ${ORANGE}Radius must be a positive integer.${RESET_COLOR}" >&2
exit 1
fi
shift 2
;;
-d|--delay)
DELAY=$2
if ! [[ $DELAY =~ ^[0-9]+$ && $DELAY -ge 1 && $DELAY -le 3600 ]]; then
echo -e "${RED}Error:${RESET_COLOR} ${ORANGE}Delay must be between 1 and 3600 seconds.${RESET_COLOR}" >&2
exit 1
fi
shift 2
;;
-p|--provider)
PROVIDER=$2
if [[ ! $PROVIDER =~ ^(gps|network|passive)$ ]]; then
echo -e "${RED}Error:${RESET_COLOR} ${ORANGE}Provider must be one of: gps, network, passive.${RESET_COLOR}" >&2
exit 1
fi
shift 2
;;
-v|--volume)
BEEP_VOLUME=$2
if ! [[ $BEEP_VOLUME =~ ^[0-9]+$ && $BEEP_VOLUME -ge 0 && $BEEP_VOLUME -le 150 ]]; then
echo -e "${RED}Error:${RESET_COLOR} ${ORANGE}Volume must be between 0 and 150.${RESET_COLOR}" >&2
exit 1
fi
shift 2
;;
-s|--save)
if [[ -z $2 ]]; then
echo -e "${RED}Error:${RESET_COLOR} ${ORANGE}Must specify output file!.\n${RESET_COLOR}" >&2
exit 1
fi
SAVE_FILE="$2"
mkdir -p "$(dirname "$SAVE_FILE")"
touch "$SAVE_FILE"
echo > "$SAVE_FILE"
SAVING=true
shift 2
;;
*)
echo -e "${RED}Error:${RESET_COLOR} ${ORANGE}Unknown option $1.\n${RESET_COLOR}" >&2
print_usage
;;
esac
done
if [[ -z $DELAY || -z $BEEP_VOLUME || -z $PROVIDER || -z $TARGET_LAT || -z $TARGET_LON || -z $RADIUS ]]; then
print_usage
fi
required_commands=("termux-location" "jq" "play" "awk" "termux-volume")
for cmd in "${required_commands[@]}"; do
if ! command -v "$cmd" &>/dev/null; then
echo -e "\n${RED}Error:${RESET_COLOR} ${ORANGE}Command '$cmd' is not installed.${RESET_COLOR}" >&2
abort
fi
done
trap abort SIGINT
main