From 3adff1c9fe49c137d4fa62a2b78e957b8ea81c3e Mon Sep 17 00:00:00 2001 From: Jandalf81 Date: Sat, 25 Aug 2018 14:51:43 +0200 Subject: [PATCH] added check for available connection 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 --- rclone_script-install.sh | 27 +++++++++++++---- rclone_script-menu.sh | 33 +++++++++++++++++++++ rclone_script.sh | 64 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 6 deletions(-) diff --git a/rclone_script-install.sh b/rclone_script-install.sh index ce33c54..2d51736 100644 --- a/rclone_script-install.sh +++ b/rclone_script-install.sh @@ -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}" } @@ -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}" @@ -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}" @@ -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}" @@ -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}" @@ -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}" diff --git a/rclone_script-menu.sh b/rclone_script-menu.sh index 5ca2b0d..04cc721 100644 --- a/rclone_script-menu.sh +++ b/rclone_script-menu.sh @@ -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 () @@ -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} } @@ -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" ) @@ -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 @@ -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 # diff --git a/rclone_script.sh b/rclone_script.sh index a94c6ad..9d1def3 100644 --- a/rclone_script.sh +++ b/rclone_script.sh @@ -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 # @@ -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=$? @@ -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}.*")