refactor: rework moonraker telegram bot functions

Signed-off-by: Dominik Willner th33xitus@gmail.com
This commit is contained in:
th33xitus
2022-04-17 19:44:02 +02:00
parent e968ed1d6d
commit 6463f5007d
4 changed files with 45 additions and 46 deletions

View File

@@ -60,7 +60,7 @@ function set_globals(){
#========== MOONRAKER-TELEGRAM-BOT ============# #========== MOONRAKER-TELEGRAM-BOT ============#
MOONRAKER_TELEGRAM_BOT_ENV_DIR=${HOME}/moonraker-telegram-bot-env MOONRAKER_TELEGRAM_BOT_ENV_DIR=${HOME}/moonraker-telegram-bot-env
MOONRAKER_TELEGRAM_BOT_DIR=${HOME}/moonraker-telegram-bot MOONRAKER_TELEGRAM_BOT_DIR=${HOME}/moonraker-telegram-bot
NLEF_REPO=https://github.com/nlef/moonraker-telegram-bot.git MOONRAKER_TELEGRAM_BOT_REPO=https://github.com/nlef/moonraker-telegram-bot.git
#=============== PRETTY-GCODE =================# #=============== PRETTY-GCODE =================#
PGC_DIR="${HOME}/pgcode" PGC_DIR="${HOME}/pgcode"

View File

@@ -12,24 +12,31 @@
set -e set -e
#===================================================# #===================================================#
#=========== REMOVE MOONRAKERTELEGRAMBOT ===========# #========== INSTALL MOONRAKERTELEGRAMBOT ===========#
#===================================================# #===================================================#
function telegram_bot_systemd() {
local services
services=$(find "${SYSTEMD}" -maxdepth 1 -regextype posix-extended -regex "${SYSTEMD}/moonraker-telegram-bot(-[^0])?[0-9]*.service")
echo "${services}"
}
function install_MoonrakerTelegramBot(){ function install_MoonrakerTelegramBot(){
MoonrakerTelegramBot_setup MoonrakerTelegramBot_setup
restart_MoonrakerTelegramBot do_action_service "restart" "moonraker-telegram-bot"
} }
function MoonrakerTelegramBot_setup(){ function MoonrakerTelegramBot_setup(){
source_kiauh_ini local klipper_cfg_loc
klipper_cfg_loc="$(get_klipper_cfg_dir)"
export klipper_cfg_loc export klipper_cfg_loc
dep=(virtualenv) local dep=(virtualenv)
dependency_check dependency_check "${dep[@]}"
status_msg "Downloading MoonrakerTelegramBot ..." status_msg "Downloading MoonrakerTelegramBot ..."
#force remove existing MoonrakerTelegramBot dir #force remove existing MoonrakerTelegramBot dir
[ -d "${MOONRAKER_TELEGRAM_BOT_DIR}" ] && rm -rf "${MOONRAKER_TELEGRAM_BOT_DIR}" [ -d "${MOONRAKER_TELEGRAM_BOT_DIR}" ] && rm -rf "${MOONRAKER_TELEGRAM_BOT_DIR}"
#clone into fresh MoonrakerTelegramBot dir #clone into fresh MoonrakerTelegramBot dir
cd "${HOME}" && git clone "${NLEF_REPO}" cd "${HOME}" && git clone "${MOONRAKER_TELEGRAM_BOT_REPO}"
ok_msg "Download complete!" ok_msg "Download complete!"
status_msg "Installing MoonrakerTelegramBot ..." status_msg "Installing MoonrakerTelegramBot ..."
/bin/bash "${MOONRAKER_TELEGRAM_BOT_DIR}/scripts/install.sh" /bin/bash "${MOONRAKER_TELEGRAM_BOT_DIR}/scripts/install.sh"
@@ -57,8 +64,8 @@ function remove_MoonrakerTelegramBot(){
### remove MoonrakerTelegramBot service ### remove MoonrakerTelegramBot service
if [ -e "${SYSTEMD}/moonraker-telegram-bot.service" ]; then if [ -e "${SYSTEMD}/moonraker-telegram-bot.service" ]; then
status_msg "Removing MoonrakerTelegramBot service ..." status_msg "Removing MoonrakerTelegramBot service ..."
sudo systemctl stop moonraker-telegram-bot do_action_service "stop" "moonraker-telegram-bot"
sudo systemctl disable moonraker-telegram-bot do_action_service "disable" "moonraker-telegram-bot"
sudo rm -f "${SYSTEMD}/moonraker-telegram-bot.service" sudo rm -f "${SYSTEMD}/moonraker-telegram-bot.service"
###reloading units ###reloading units
sudo systemctl daemon-reload sudo systemctl daemon-reload
@@ -73,61 +80,54 @@ function remove_MoonrakerTelegramBot(){
fi fi
### remove MoonrakerTelegramBot log symlink in config dir ### remove MoonrakerTelegramBot log symlink in config dir
if [ -e "${KLIPPER_CONFIG}/telegram.log" ]; then if [ -e "${KLIPPER_CONFIG}/telegram.log" ]; then
status_msg "Removing MoonrakerTelegramBot log symlink ..." status_msg "Removing MoonrakerTelegramBot log symlink ..."
rm -f "${KLIPPER_CONFIG}/telegram.log" && ok_msg "File removed!" rm -f "${KLIPPER_CONFIG}/telegram.log" && ok_msg "File removed!"
fi fi
CONFIRM_MSG="MoonrakerTelegramBot successfully removed!" print_confirm "MoonrakerTelegramBot successfully removed!"
} }
#===================================================# #===================================================#
#=========== UPDATE MOONRAKERTELEGRAMBOT ===========# #=========== UPDATE MOONRAKERTELEGRAMBOT ===========#
#===================================================# #===================================================#
function update_MoonrakerTelegramBot(){ function update_telegram_bot(){
export KLIPPER_CONFIG local klipper_cfg_loc
stop_MoonrakerTelegramBot klipper_cfg_loc="$(get_klipper_cfg_dir)"
cd "${MOONRAKER_TELEGRAM_BOT_DIR}" do_action_service "stop" "moonraker-telegram-bot"
git pull cd "${MOONRAKER_TELEGRAM_BOT_DIR}" && git pull
/bin/bash "./scripts/install.sh" /bin/bash "./scripts/install.sh"
do_action_service "start" "moonraker-telegram-bot"
ok_msg "Update complete!" ok_msg "Update complete!"
start_MoonrakerTelegramBot
} }
#===================================================# #===================================================#
#=========== MOONRAKERTELEGRAMBOT STATUS ===========# #=========== MOONRAKERTELEGRAMBOT STATUS ===========#
#===================================================# #===================================================#
function MoonrakerTelegramBot_status(){ function get_telegram_bot_status(){
mtbcount=0 local sf_count status
MoonrakerTelegramBot_data=( sf_count="$(telegram_bot_systemd | wc -w)"
SERVICE
"${MOONRAKER_TELEGRAM_BOT_DIR}"
"${MOONRAKER_TELEGRAM_BOT_ENV_DIR}"
)
### count amount of MoonrakerTelegramBot_data service files in /etc/systemd/system ### remove the "SERVICE" entry from the data array if a moonraker service is installed
SERVICE_FILE_COUNT=$(ls /etc/systemd/system | grep -E "moonraker-telegram-bot" | wc -l) local data_arr=(SERVICE "${MOONRAKER_TELEGRAM_BOT_DIR}" "${MOONRAKER_TELEGRAM_BOT_ENV_DIR}")
[ "${sf_count}" -gt 0 ] && unset "data_arr[0]"
### remove the "SERVICE" entry from the MoonrakerTelegramBot_data array if a MoonrakerTelegramBot service is installed ### count+1 for each found data-item from array
[ "${SERVICE_FILE_COUNT}" -gt 0 ] && unset "MoonrakerTelegramBot_data[0]" local filecount=0
for data in "${data_arr[@]}"; do
#count+1 for each found data-item from array [ -e "${data}" ] && filecount=$(("${filecount}" + 1))
for mtbd in "${MoonrakerTelegramBot_data[@]}"
do
if [ -e "${mtbd}" ]; then
mtbcount=$((mtbcount + 1))
fi
done done
if [ "${mtbcount}" == "${#MoonrakerTelegramBot_data[*]}" ]; then
MOONRAKER_TELEGRAM_BOT_STATUS="${green}Installed!${white} " if [ "${filecount}" == "${#data_arr[*]}" ]; then
elif [ "${mtbcount}" == 0 ]; then status="$(printf "${green}Installed: %-5s${white}" "${sf_count}")"
MOONRAKER_TELEGRAM_BOT_STATUS="${red}Not installed!${white} " elif [ "${filecount}" == 0 ]; then
status="${red}Not installed!${white} "
else else
MOONRAKER_TELEGRAM_BOT_STATUS="${yellow}Incomplete!${white} " status="${yellow}Incomplete!${white} "
fi fi
echo "${status}"
} }
function get_local_telegram_bot_commit(){ function get_local_telegram_bot_commit(){
@@ -146,7 +146,7 @@ function get_remote_telegram_bot_commit(){
echo "${commit}" echo "${commit}"
} }
function compare_prettygcode_versions(){ function compare_telegram_bot_versions(){
unset MOONRAKER_TELEGRAM_BOT_UPDATE_AVAIL unset MOONRAKER_TELEGRAM_BOT_UPDATE_AVAIL
local versions local_ver remote_ver local versions local_ver remote_ver
local_ver="$(get_local_telegram_bot_commit)" local_ver="$(get_local_telegram_bot_commit)"
@@ -155,7 +155,7 @@ function compare_prettygcode_versions(){
versions="${yellow}$(printf " %-14s" "${local_ver}")${white}" versions="${yellow}$(printf " %-14s" "${local_ver}")${white}"
versions+="|${green}$(printf " %-13s" "${remote_ver}")${white}" versions+="|${green}$(printf " %-13s" "${remote_ver}")${white}"
# add moonraker-telegram-bot to the update all array for the update all function in the updater # add moonraker-telegram-bot to the update all array for the update all function in the updater
MOONRAKER_TELEGRAM_BOT_UPDATE_AVAIL="true" && update_arr+=(update_MoonrakerTelegramBot) MOONRAKER_TELEGRAM_BOT_UPDATE_AVAIL="true" && update_arr+=(update_telegram_bot)
else else
versions="${green}$(printf " %-14s" "${local_ver}")${white}" versions="${green}$(printf " %-14s" "${local_ver}")${white}"
versions+="|${green}$(printf " %-13s" "${remote_ver}")${white}" versions+="|${green}$(printf " %-13s" "${remote_ver}")${white}"

View File

@@ -23,7 +23,7 @@ main_ui(){
echo -e "| 4) [Advanced] | Mainsail: $MAINSAIL_STATUS|" echo -e "| 4) [Advanced] | Mainsail: $MAINSAIL_STATUS|"
echo -e "| 5) [Backup] | Fluidd: $FLUIDD_STATUS|" echo -e "| 5) [Backup] | Fluidd: $FLUIDD_STATUS|"
echo -e "| | KlipperScreen: $KLIPPERSCREEN_STATUS|" echo -e "| | KlipperScreen: $KLIPPERSCREEN_STATUS|"
echo -e "| 6) [Settings] | Telegram Bot: $MOONRAKER_TELEGRAM_BOT_STATUS|" echo -e "| 6) [Settings] | Telegram Bot: $(get_telegram_bot_status)|"
echo -e "| | |" echo -e "| | |"
echo -e "| $(get_kiauh_version)| Octoprint: $OCTOPRINT_STATUS|" echo -e "| $(get_kiauh_version)| Octoprint: $OCTOPRINT_STATUS|"
quit_footer quit_footer
@@ -69,7 +69,6 @@ main_menu(){
mainsail_status mainsail_status
octoprint_status octoprint_status
klipperscreen_status klipperscreen_status
MoonrakerTelegramBot_status
main_ui main_ui
while true; do while true; do
read -p "${cyan}Perform action:${white} " action; echo read -p "${cyan}Perform action:${white} " action; echo

View File

@@ -33,7 +33,7 @@ update_ui(){
echo -e "| | | | " echo -e "| | | | "
echo -e "| Other: |---------------|--------------| " echo -e "| Other: |---------------|--------------| "
echo -e "| 6) [PrettyGCode] |$(compare_prettygcode_versions)| " echo -e "| 6) [PrettyGCode] |$(compare_prettygcode_versions)| "
echo -e "| 7) [Telegram Bot] |$(compare_prettygcode_versions)| " echo -e "| 7) [Telegram Bot] |$(compare_telegram_bot_versions)| "
echo -e "| |------------------------------| " echo -e "| |------------------------------| "
echo -e "| 8) [System] | $DISPLAY_SYS_UPDATE | " echo -e "| 8) [System] | $DISPLAY_SYS_UPDATE | "
back_footer back_footer
@@ -60,7 +60,7 @@ update_menu(){
6) 6)
do_action "update_pgc_for_klipper" "update_ui";; do_action "update_pgc_for_klipper" "update_ui";;
7) 7)
do_action "update_MoonrakerTelegramBot" "update_ui";; do_action "update_telegram_bot" "update_ui";;
8) 8)
do_action "update_system" "update_ui";; do_action "update_system" "update_ui";;
a) a)