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_ENV_DIR=${HOME}/moonraker-telegram-bot-env
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 =================#
PGC_DIR="${HOME}/pgcode"

View File

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

View File

@@ -33,7 +33,7 @@ update_ui(){
echo -e "| | | | "
echo -e "| Other: |---------------|--------------| "
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 "| 8) [System] | $DISPLAY_SYS_UPDATE | "
back_footer
@@ -60,7 +60,7 @@ update_menu(){
6)
do_action "update_pgc_for_klipper" "update_ui";;
7)
do_action "update_MoonrakerTelegramBot" "update_ui";;
do_action "update_telegram_bot" "update_ui";;
8)
do_action "update_system" "update_ui";;
a)