Last active
May 20, 2024 06:42
-
-
Save krohn/25c5ef46460d6eb50a99d3649baea2b7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
ADDR="http://traccar.example.com:5055" # Replace with your traccar server address | |
ID="12345" # Replace this with your chosen traccar id | |
LOGFILE="/var/log/gps-traccar.log" | |
function execute_push { | |
DATA="${ADDR}/?id=${ID}&lat=${LAT}&lon=${LON}×tamp="${TIME}"&hdop=${HDOP}&altitude=${ALT}&speed=${SPEED}&climb=${CLIMB}" | |
# echo "[$(date +%s)] $DATA" >> LOGFILE | |
curl -m 10 -X POST "$DATA" >> LOGFILE 2>&1 | |
echo "[$(date +%s)] Pushed position" >> LOGFILE | |
} | |
function get_position { | |
gpspipe --json --count 5 2> /dev/null 1> gps-traccar.dmp | |
MODE=$( cat gps-traccar.dmp | jq '. | select( .class == "TPV" ).mode' | sed 's/null/0/g') | |
LAT=$( cat gps-traccar.dmp | jq '. | select( .class == "TPV" ).lat' | sed 's/null/0/g') | |
LON=$( cat gps-traccar.dmp | jq '. | select( .class == "TPV" ).lon' | sed 's/null/0/g') | |
ALT=$( cat gps-traccar.dmp | jq '. | select( .class == "TPV" ).altMSL' | sed 's/null/0/g') | |
TIME=$( cat gps-traccar.dmp | jq '. | select( .class == "TPV" ).time' | sed 's/null/0/g' | sed 's/"//g' ) | |
HDOP=$( cat gps-traccar.dmp | jq '. | select( .class == "SKY" ).hdop' | sed 's/null/0/g') | |
SPEED=$( cat gps-traccar.dmp | jq '. | select( .class == "SKY" ).speed' | sed 's/null/0/g') | |
CLIMB=$( cat gps-traccar.dmp | jq '. | select( .class == "SKY" ).climb' | sed 's/null/0/g') | |
HEADING=$( cat gps-traccar.dmp | jq '. | select( .class == "ATT" ).heading'| sed 's/null/0/g' ) | |
echo "[$(date +%s)] LAT:${LAT} LON:${LON} HDOP:${HDOP} SPEED:${SPEED} ALT:${ALT} DIR:${HEADING}" >> LOGFILE | |
} | |
SPEED=0 | |
PREV_SPEED=${SPEED} | |
CURRENT_TIME=$(date +%s) | |
echo "[${CURRENT_TIME}] Starting..." >> LOGFILE | |
echo "[${CURRENT_TIME}] Device-ID: ${ID}, Traccar-Server: ${ADDR}" >> LOGFILE | |
while true; do | |
get_position | |
CURRENT_TIME=$(date +%s) | |
if (( SPEED > 60 )); then | |
# Execute once, if SPEED is > 60, update and wait 10 sec | |
execute_push | |
echo "[$(date +%s)] Speed > 60, wait 10s" >> LOGFILE | |
sleep 10 | |
elif (( SPEED > 0 )); then | |
# Execute once, if SPEED is between 1 and 60, update and wait 30 sec | |
execute_push | |
echo "[$(date +%s)] Speed > 0, wait 30s" >> LOGFILE | |
sleep 30 | |
else | |
if (( PREV_SPEED > 0 )); then | |
# Execute once, if SPEED is reduced from >0 to 0 | |
execute_push | |
echo "[$(date +%s)] Device stopped moving" >> LOGFILE | |
fi | |
# Check whether an hour has passed | |
if (( CURRENT_TIME - LAST_EXECUTION_TIME >= 3600 )); then | |
execute_push | |
echo "[$(date +%s)] Hourly update" >> LOGFILE | |
LAST_EXECUTION_TIME=$CURRENT_TIME | |
fi | |
sleep 10 | |
fi | |
# Update previous SPEED | |
PREV_SPEED=$SPEED | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment