Files
tello-commander/manage.sh
2023-05-15 22:41:39 +03:00

159 lines
4.2 KiB
Bash
Executable File

#!/bin/bash
DRONE_INTERFACE=$(yq '.commander.drone_interface' < config.yml)
DRONE_WPA_SUPP_CONF=$(yq '.commander.drone_wpa_supp' < config.yml)
NET_INTERFACE=$(yq '.commander.net_interface' < config.yml)
NET_WPA_SUPP_CONF=$(yq '.commander.net_wpa_supp' < config.yml)
list_related_network_interface_status() {
networkctl list
}
list_wifi_ssid(){
sudo iw dev $DRONE_INTERFACE scan | grep "SSID: "
}
connect_using_wpa_supp() {
sudo wpa_supplicant -D nl80211 -i $DRONE_INTERFACE -c network/$DRONE_WPA_SUPP_CONF
}
get_dhcp_ip () {
sudo dhclient $DRONE_INTERFACE
}
start_jupyter() {
venv/bin/python -m jupyter lab --ip='0.0.0.0' --NotebookApp.token='' --NotebookApp.password='' --no-browser --port=8888
}
start_codeserver(){
packages/code-server/code-server-4.12.0-linux-amd64/bin/code-server --config /home/uad/misc/tello-commander/packages/code-server/config.yaml --disable-getting-started-override --disable-workspace-trust --disable-telemetry ./
}
start_commander_service() {
venv/bin/python commander/commander.py $1
}
kill_everything() {
for p in $pids_dir/*.txt; do echo "killing $p"; pkill $(cat "$p"); done
}
pids_dir='./pids'
if [[ ! -d "$pids_dir" ]]; then
mkdir $pids_dir
fi
## NETWORK
if [ "$1" == "list-network" ]; then
list_related_network_interface_status
elif [ "$1" == "list-wifis" ]; then
list_wifi_ssid #connect $2 $3
elif [ "$1" == "connect-drone" ]; then
connect_using_wpa_supp > logs/wpa_supp.log 2>&1 &
wpa_supp_pid=$!
echo "started wpa supplicant to connect drone network with PID $wpa_supp_pid"
echo $wpa_supp_pid > $pids_dir/wpa_supp_pid.txt
elif [ "$1" == "disconnect-drone" ]; then
wpa_supp_pid_file="$pids_dir/wpa_supp_pid.txt"
if [ -f "$wpa_supp_pid_file" ]; then
pkill -P $(cat $wpa_supp_pid_file)
echo "stopped drone connection via wpa_supp"
fi
elif [ "$1" == "get-dhcp" ]; then
get_dhcp_ip > logs/dhcp_ip.log 2>&1 &
dhcp_ip_pid=$!
echo "requested ip address from drone router with PID $dhcp_ip_pid"
echo $dhcp_ip_pid > $pids_dir/dhcp_ip_pid.txt
elif [ "$1" == "kill-dhcp" ]; then
dhcp_ip_pid_file="$pids_dir/dhcp_ip_pid.txt"
if [ -f "$dhcp_ip_pid_file" ]; then
pkill -P $(cat $dhcp_ip_pid_file)
echo "killed dhcp client"
fi
## DEV
elif [ "$1" == "start-jupyter" ]; then
start_jupyter > logs/jupyter.log 2>&1 &
jupyter_pid=$!
echo "started jupyter with PID $jupyter_pid"
echo $jupyter_pid > $pids_dir/jupyter_pid.txt
#tail -f logs/jupyter.log
elif [ "$1" == "stop-jupyter" ]; then
jupyter_pid_file="$pids_dir/jupyter_pid.txt"
if [ -f "$jupyter_pid_file" ]; then
pkill -P $(cat $jupyter_pid_file)
echo "stopped jupyter"
fi
elif [ "$1" == "start-cs" ]; then
start_codeserver > logs/codeserver.log 2>&1 &
codeserver_pid=$!
echo "started code server with PID $codeserver_pid"
echo $codeserver_pid > $pids_dir/codeserver_pid.txt
#tail -f logs/codeserver.log
elif [ "$1" == "stop-cs" ]; then
codeserver_pid_file="$pids_dir/codeserver_pid.txt"
if [ -f "$codeserver_pid_file" ]; then
pkill -P $(cat $codeserver_pid_file)
echo "stopped code server"
fi
elif [ "$1" == "stop-all" ]; then
kill_everything
## DRONE
elif [ "$1" == "start-commander" ]; then
start_commander_service $2 > logs/commander.log 2>&1 &
commander_pid=$!
echo "started commander with PID $commander_pid"
echo $commander_pid > $pids_dir/commander_pid.txt
#tail -f logs/commander.log
elif [ "$1" == "stop-commander" ]; then
commander_pid_file="$pids_dir/commander_pid.txt"
if [ -f "$commander_pid_file" ]; then
pkill -P $(cat $commander_pid_file)
echo "stopped commander"
fi
elif [ "$1" == "prepare-flight" ]; then
./manage.sh connect-drone
./manage.sh get-dhcp
./manage.sh start-commander
echo "prepared to flight"
elif [ "$1" == "finish-flight" ]; then
./manage.sh disconnect-drone
./manage.sh kill-dhcp
./manage.sh stop-commander
echo "flight finished"
exit 0
else
echo "Invalid command. Please use one of:
- list-network
- list-wifis
- connect-/ disconnect-drone
- get-/ kill-dhcp
- start-/ stop-jupyter
- start-/ stop-cs
- start-/ stop-commander [port]
- stop-all
- prepare-/ finish-flight"
fi