Created
July 26, 2019 18:36
-
-
Save kmarius/e5150aece1cb63c045d407aebfe11306 to your computer and use it in GitHub Desktop.
This file contains 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/sh | |
APIKEY= | |
LOG=/sdcard/Tasker/log/syncthing-once.txt | |
EVENT_TIMEOUT=15 | |
log() { | |
printf '%s: %s\n' "$(date +'%b %d %H:%M:%S')" "$*" >> "$LOG" | |
} | |
millis() { | |
date +'%s%3N' | |
} | |
mkdir -p "$(dirname "$LOG")" | |
date > /sdcard/Tasker/scripts/.sync_dummy.txt | |
# wait for service to start | |
retries=10 | |
while ! curl -s -L -k -X GET -H "X-API-Key: $APIKEY" "http://localhost:8384/rest/system/ping" >/dev/null | |
do | |
if test $retries -eq 0; then | |
log "Could not connect to REST endpoint" | |
exit 100 | |
fi | |
sleep 0.5 | |
retries=$((retries-1)) | |
done | |
t0=$(millis) | |
# force rescan | |
curl -s -L -k -X POST -H "X-API-Key: $APIKEY" "http://localhost:8384/rest/db/scan" & | |
# if ! curl -s -L -k -X GET -H "X-API-Key: $APIKEY" "http://localhost:8384/rest/events?events=DeviceConnected&timeout=$EVENT_TIMEOUT"; then | |
# log "No devices connected after $EVENT_TIMEOUT seconds" | |
# exit 103 | |
# else | |
# : "device connected after $(($(millis)-t0)) ms" | |
# fi | |
wait | |
id=0 | |
while true; do | |
date > /sdcard/Tasker/scripts/.sync_dummy.txt & | |
response=$(curl -s -L -k -X GET -H "X-API-Key: $APIKEY" \ | |
"http://localhost:8384/rest/events?events=FolderCompletion&limit=1&timeout=$EVENT_TIMEOUT&since=$id") | |
if test $? -eq 0 | |
then | |
if echo "$response" | grep -q '"completion": *100,' | |
then | |
log "Sync completed after $(($(millis)-t0)) ms" | |
break | |
elif test "$response" != '[]'; then | |
id=$(echo "$response" | awk -F: '/"id"/{gsub(/[ ,]/,"",$2); print $2}') | |
fi | |
else | |
if ! curl -s -L -k -X GET -H "X-API-Key: $APIKEY" \ | |
"http://localhost:8384/rest/system/ping" >/dev/null | |
then | |
log "Service stopped unexpectedly after $(($(millis)-t0)) ms" | |
exit 101 | |
fi | |
if ! curl -s -L -k -X GET -H "X-API-Key: $APIKEY" \ | |
"http://localhost:8384/rest/system/connections" \ | |
| grep -q '"connected":.*true' | |
then | |
log "No devices connected after $(($(millis)-t0)) ms" | |
exit 102 | |
fi | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment