Skip to content

Commit

Permalink
added check for available connection
Browse files Browse the repository at this point in the history
rclone_script-install.sh
* added new configuration setting for needed connection
* changed the way commands are added to other files (from ECHO to PRINTF)

rclone_script.sh
* added new function getAvailableConnection
* changed both downloadSaves and uploadSaves so they react on that new function (check if the available connection is greater than the needed connection, else error)

rclone_script-menu.sh
* added new menu item to re-set the needed connection
  • Loading branch information
Jandalf81 committed Aug 25, 2018
1 parent d59cf2d commit 3adff1c
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 6 deletions.
27 changes: 21 additions & 6 deletions rclone_script-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,22 @@ function 4cConfigureRCLONE_SCRIPT ()
*) shownotifications="FALSE" ;;
esac

choice=$(dialog \
--stdout \
--colors \
--no-collapse \
--cr-wrap \
--backtitle "${backtitle}" \
--title "Needed connection" \
--ok-label "Select" \
--no-cancel \
--menu "\nPlease select which type of connection will be needed for your configured remote" 20 50 5 \
0 "Internet access" \
1 "LAN / WLAN connection only"
)

neededConnection=${choice}

printf "$(date +%FT%T%:z):\t4cConfigureRCLONE_SCRIPT\tDONE\n" >> "${logfile}"
}

Expand Down Expand Up @@ -779,7 +795,7 @@ function 5aRUNCOMMAND-ONSTART ()
printf "$(date +%FT%T%:z):\t5aRUNCOMMAND-ONSTART\tCALL NOT FOUND\n" >> "${logfile}"

# add call
echo "~/scripts/rclone_script/rclone_script.sh \"down\" \"\$1\" \"\$2\" \"\$3\" \"\$4\"" >> /opt/retropie/configs/all/runcommand-onstart.sh
printf "\n~/scripts/rclone_script/rclone_script.sh \"down\" \"\$1\" \"\$2\" \"\$3\" \"\$4\"\n" >> /opt/retropie/configs/all/runcommand-onstart.sh

printf "$(date +%FT%T%:z):\t5aRUNCOMMAND-ONSTART\tCALL CREATED\n" >> "${logfile}"

Expand All @@ -788,8 +804,7 @@ function 5aRUNCOMMAND-ONSTART ()
else
printf "$(date +%FT%T%:z):\t5aRUNCOMMAND-ONSTART\tFILE NOT FOUND\n" >> "${logfile}"

echo "#!/bin/bash" > /opt/retropie/configs/all/runcommand-onstart.sh
echo "~/scripts/rclone_script/rclone_script.sh \"down\" \"\$1\" \"\$2\" \"\$3\" \"\$4\"" >> /opt/retropie/configs/all/runcommand-onstart.sh
printf "#!/bin/bash\n~/scripts/rclone_script/rclone_script.sh \"down\" \"\$1\" \"\$2\" \"\$3\" \"\$4\"\n" > /opt/retropie/configs/all/runcommand-onstart.sh

printf "$(date +%FT%T%:z):\t5aRUNCOMMAND-ONSTART\tFILE CREATED\n" >> "${logfile}"

Expand Down Expand Up @@ -820,7 +835,7 @@ function 5aRUNCOMMAND-ONEND ()
printf "$(date +%FT%T%:z):\t5aRUNCOMMAND-ONEND\tCALL NOT FOUND\n" >> "${logfile}"

# add call
echo "~/scripts/rclone_script/rclone_script.sh \"up\" \"\$1\" \"\$2\" \"\$3\" \"\$4\"" >> /opt/retropie/configs/all/runcommand-onend.sh
printf "\n~/scripts/rclone_script/rclone_script.sh \"up\" \"\$1\" \"\$2\" \"\$3\" \"\$4\"\n" >> /opt/retropie/configs/all/runcommand-onend.sh

printf "$(date +%FT%T%:z):\t5aRUNCOMMAND-ONEND\tCALL CREATED\n" >> "${logfile}"

Expand All @@ -829,8 +844,7 @@ function 5aRUNCOMMAND-ONEND ()
else
printf "$(date +%FT%T%:z):\t5aRUNCOMMAND-ONEND\tFILE NOT FOUND\n" >> "${logfile}"

echo "#!/bin/bash" > /opt/retropie/configs/all/runcommand-onend.sh
echo "~/scripts/rclone_script/rclone_script.sh \"up\" \"\$1\" \"\$2\" \"\$3\" \"\$4\"" >> /opt/retropie/configs/all/runcommand-onend.sh
printf "#!/bin/bash\n~/scripts/rclone_script/rclone_script.sh \"up\" \"\$1\" \"\$2\" \"\$3\" \"\$4\"\n" >> /opt/retropie/configs/all/runcommand-onend.sh

printf "$(date +%FT%T%:z):\t5aRUNCOMMAND-ONEND\tFILE CREATED\n" >> "${logfile}"

Expand Down Expand Up @@ -1140,6 +1154,7 @@ function 9aSaveConfiguration ()
echo "showNotifications=${shownotifications}" >> ~/scripts/rclone_script/rclone_script.ini
echo "syncOnStartStop=\"TRUE\"" >> ~/scripts/rclone_script/rclone_script.ini
echo "logfile=~/scripts/rclone_script/rclone_script.log" >> ~/scripts/rclone_script/rclone_script.ini
echo "neededConnection=${neededConnection}" >> ~/scripts/rclone_script/rclone_script.ini
echo "debug=0" >> ~/scripts/rclone_script/rclone_script.ini

printf "$(date +%FT%T%:z):\t9aSaveConfiguration\tDONE\n" >> "${logfile}"
Expand Down
33 changes: 33 additions & 0 deletions rclone_script-menu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ function getStatusOfParameters ()
else
statusShowNotifications="${RED}DISABLED${NORMAL}"
fi

case ${neededConnection} in
0) statusNeededConnection="Internet access" ;;
1) statusNeededConnection="LAN / WLAN" ;;
esac
}

function saveConfig ()
Expand All @@ -68,6 +73,7 @@ function saveConfig ()
echo "showNotifications=${showNotifications}" >> ${config}
echo "syncOnStartStop=${syncOnStartStop}" >> ${config}
echo "logfile=~/scripts/rclone_script/rclone_script.log" >> ${config}
echo "neededConnection=${neededConnection}" >> ${config}
echo "debug=0" >> ${config}
}

Expand Down Expand Up @@ -95,6 +101,7 @@ function main_menu ()
1 "Full synchronization of all savefiles and statefiles" \
2 "Toggle \"Synchronize saves on start / stop\" (currently ${statusSyncOnStartStop})" \
3 "Toggle \"Show notifications on sync\" (currently ${statusShowNotifications})" \
4 "Set needed Connection (currently \"${statusNeededConnection}\")" \
"" ""\
9 "uninstall RCLONE_SCRIPT"
)
Expand All @@ -103,6 +110,7 @@ function main_menu ()
1) doFullSync ;;
2) toggleSyncOnStartStop ;;
3) toggleShowNotifications ;;
4) setNeededConnection ;;
9) ~/scripts/rclone_script/rclone_script-uninstall.sh ;;
*) break ;;
esac
Expand Down Expand Up @@ -171,6 +179,31 @@ function toggleShowNotifications ()
saveConfig
}

function setNeededConnection ()
{
choice=$(dialog \
--stdout \
--colors \
--no-collapse \
--cr-wrap \
--backtitle "${backtitle}" \
--title "Needed connection" \
--default-item "${neededConnection}" \
--ok-label "Select" \
--menu "\nPlease select which type of connection will be needed for your configured remote" 20 50 5 \
0 "Internet access" \
1 "LAN / WLAN connection only"
)

case ${choice} in
0) neededConnection=0 ;;
1) neededConnection=1 ;;
*) return ;;
esac

saveConfig
}


########
# MAIN #
Expand Down
64 changes: 64 additions & 0 deletions rclone_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,42 @@ function getTypeOfRemote ()
remoteType=$(echo ${remoteType} | xargs)
}

function getAvailableConnection ()
# checks if the device is connected to a LAN / WLAN and the Internet
# RETURN
# 0 > device seems to be connected to the Internet
# 1 > device seems to be connected to a LAN / WLAN without internet access
# 2 > device doesn't seem to be connected at all
{
gatewayIP=$(ip r | grep default | cut -d " " -f 3)
if [ "${gatewayIP}" == "" ]
then
log "INFO" "Gateway could not be detected\n"
return 2
else
log "INFO" "Gateway IP: ${gatewayIP}\n"
fi

ping -q -w 1 -c 1 ${gatewayIP} > /dev/null
if [[ $? -eq 0 ]]
then
log "INFO" "Gateway PING successful\n"
else
log "INFO" "Gateway could not be PINGed\n"
return 2
fi

ping -q -w 1 -c 1 "www.google.com" > /dev/null
if [[ $? -eq 0 ]]
then
log "INFO" "www.google.com PING successful\n"
return 0
else
log "INFO" "www.google.com could not be PINGed\n"
return 1
fi
}


##################
# SYNC FUNCTIONS #
Expand All @@ -155,6 +191,20 @@ function downloadSaves ()
log "INFO" "Downloading saves and states for ${system}/${romfilename} from ${remoteType}..."
showNotification "Downloading saves and states from ${remoteType}..."

getAvailableConnection
availableConnection=$?
if [[ ${availableConnection} -gt ${neededConnection} ]]
then
log "ERROR" "Needed Connection not available. Needed ${neededConnection}, available ${availableConnection}"

case ${neededConnection} in
0) showNotification "Downloading saves and states from ${remoteType}... No Internet connection available" "red" "" "" "" "forced" ;;
1) showNotification "Downloading saves and states from ${remoteType}... No LAN / WLAN connection available" "red" "" "" "" "forced" ;;
esac

return
fi

# test for remote files
remotefiles=$(rclone lsf retropie:${remotebasedir}/${system} --include "${filter}.*")
retval=$?
Expand Down Expand Up @@ -199,6 +249,20 @@ function uploadSaves ()
log "INFO" "Stopped ${system}/${romfilename} "
log "INFO" "Uploading saves and states for ${system}/${romfilename} to ${remoteType}..."
showNotification "Uploading saves and states to ${remoteType}..."

getAvailableConnection
availableConnection=$?
if [[ ${availableConnection} -gt ${neededConnection} ]]
then
log "ERROR" "Needed Connection not available. Needed ${neededConnection}, available ${availableConnection}"

case ${neededConnection} in
0) showNotification "Uploading saves and states to ${remoteType}... No Internet connection available" "red" "" "" "" "forced" ;;
1) showNotification "Uploading saves and states to ${remoteType}... No LAN / WLAN connection available" "red" "" "" "" "forced" ;;
esac

return
fi

localfiles=$(find ~/RetroPie/saves/${system} -type f -iname "${filter}.*")

Expand Down

0 comments on commit 3adff1c

Please sign in to comment.