#!/bin/bash # NowYouSeeMe Complete Holodeck Environment Startup Script # This script launches the complete holodeck environment with all components set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Configuration SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" BUILD_DIR="$PROJECT_ROOT/build" BIN_DIR="$BUILD_DIR/bin" CONFIG_DIR="$PROJECT_ROOT/config" LOG_DIR="$PROJECT_ROOT/logs" # Create necessary directories mkdir -p "$LOG_DIR" mkdir -p "$CONFIG_DIR" # Function to print colored output print_status() { echo -e "${BLUE}[INFO]${NC} $1" } print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } # Function to check if a command exists command_exists() { command -v "$1" >/dev/null 2>&1 } # Function to check if a port is available port_available() { ! nc -z localhost "$1" 2>/dev/null } # Function to wait for a service to be ready wait_for_service() { local service_name="$1" local port="$2" local max_attempts=30 local attempt=1 print_status "Waiting for $service_name to be ready on port $port..." while [ $attempt -le $max_attempts ]; do if port_available "$port"; then print_success "$service_name is ready" return 0 fi echo -n "." sleep 1 attempt=$((attempt + 1)) done print_error "$service_name failed to start within $max_attempts seconds" return 1 } # Function to start a background service start_background_service() { local service_name="$1" local command="$2" local log_file="$3" print_status "Starting $service_name..." if [ -n "$log_file" ]; then nohup $command > "$log_file" 2>&1 & else nohup $command > /dev/null 2>&1 & fi local pid=$! echo $pid > "$LOG_DIR/${service_name}.pid" print_success "$service_name started with PID $pid" } # Function to stop a background service stop_background_service() { local service_name="$1" local pid_file="$LOG_DIR/${service_name}.pid" if [ -f "$pid_file" ]; then local pid=$(cat "$pid_file") if kill -0 "$pid" 2>/dev/null; then print_status "Stopping $service_name (PID $pid)..." kill "$pid" rm -f "$pid_file" print_success "$service_name stopped" else print_warning "$service_name was not running" rm -f "$pid_file" fi else print_warning "$service_name PID file not found" fi } # Function to check system requirements check_system_requirements() { print_status "Checking system requirements..." # Check for required commands local required_commands=("cmake" "make" "python3" "pip3") for cmd in "${required_commands[@]}"; do if ! command_exists "$cmd"; then print_error "$cmd is not installed" exit 1 fi done # Check for required Python packages local required_packages=("PyQt6" "opencv-python" "numpy" "psutil") for package in "${required_packages[@]}"; do if ! python3 -c "import $package" 2>/dev/null; then print_warning "$package Python package is not installed" fi done # Check for required system libraries if ! ldconfig -p | grep -q libopencv; then print_warning "OpenCV system libraries not found" fi if ! ldconfig -p | grep -q libeigen; then print_warning "Eigen3 system libraries not found" fi print_success "System requirements check completed" } # Function to build the project build_project() { print_status "Building NowYouSeeMe project..." if [ ! -d "$BUILD_DIR" ]; then mkdir -p "$BUILD_DIR" fi cd "$BUILD_DIR" if [ ! -f "CMakeCache.txt" ]; then print_status "Running CMake configuration..." cmake .. -DCMAKE_BUILD_TYPE=Release fi print_status "Compiling project..." make -j$(nproc) if [ $? -eq 0 ]; then print_success "Project built successfully" else print_error "Build failed" exit 1 fi } # Function to create configuration files create_configurations() { print_status "Creating configuration files..." # Main configuration cat > "$CONFIG_DIR/holodeck_config.json" << EOF { "holodeck": { "enabled": true, "view_type": "3D", "update_rate": 30 }, "device_management": { "discovery_port": 8080, "max_connections": 100, "discovery_interval": 5, "connection_timeout": 30 }, "azure_integration": { "subscription_id": "your-subscription-id", "resource_group": "nowyouseeme-rg", "location": "eastus", "tenant_id": "your-tenant-id", "client_id": "your-client-id", "client_secret": "your-client-secret" }, "slam_processing": { "map_scale": 1.0, "update_rate": 30, "enable_rf_fusion": true, "enable_vision_slam": true, "enable_nerf_rendering": true }, "ui_settings": { "window_width": 1600, "window_height": 1200, "theme": "dark", "auto_save": true } } EOF # Camera configuration cat > "$CONFIG_DIR/camera_config.json" << EOF { "camera": { "device_id": 0, "width": 1920, "height": 1080, "fps": 30, "exposure": -1, "gain": -1 }, "calibration": { "pattern_width": 9, "pattern_height": 6, "square_size": 0.025 } } EOF # CSI configuration cat > "$CONFIG_DIR/csi_config.json" << EOF { "csi": { "interface": "wlan0", "frequency": 2.4, "bandwidth": 20, "num_antennas": 4, "num_subcarriers": 64 }, "processing": { "enable_aoa": true, "enable_cir": true, "enable_beamforming": true } } EOF print_success "Configuration files created" } # Function to start core services start_core_services() { print_status "Starting core services..." # Start device manager if [ -f "$BIN_DIR/device_manager" ]; then start_background_service "device_manager" "$BIN_DIR/device_manager 8080 100" "$LOG_DIR/device_manager.log" wait_for_service "Device Manager" 8080 else print_warning "Device manager binary not found" fi # Start Azure integration if [ -f "$BIN_DIR/azure_integration" ]; then start_background_service "azure_integration" "$BIN_DIR/azure_integration $CONFIG_DIR/holodeck_config.json" "$LOG_DIR/azure_integration.log" else print_warning "Azure integration binary not found" fi # Start SLAM processor if [ -f "$BIN_DIR/ekf_fusion" ]; then start_background_service "slam_processor" "$BIN_DIR/ekf_fusion" "$LOG_DIR/slam_processor.log" else print_warning "SLAM processor binary not found" fi print_success "Core services started" } # Function to start UI start_ui() { print_status "Starting holodeck UI..." if command_exists "python3"; then cd "$PROJECT_ROOT" python3 src/ui/holodeck_ui.py & local ui_pid=$! echo $ui_pid > "$LOG_DIR/holodeck_ui.pid" print_success "Holodeck UI started with PID $ui_pid" else print_error "Python3 not found, cannot start UI" fi } # Function to start monitoring start_monitoring() { print_status "Starting system monitoring..." # Start resource monitoring cat > "$LOG_DIR/monitor_resources.sh" << 'EOF' #!/bin/bash while true; do echo "$(date): CPU: $(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | cut -d'%' -f1)%, Memory: $(free | grep Mem | awk '{printf "%.1f", $3/$2 * 100.0}')%" >> /tmp/holodeck_resources.log sleep 5 done EOF chmod +x "$LOG_DIR/monitor_resources.sh" start_background_service "resource_monitor" "$LOG_DIR/monitor_resources.sh" "$LOG_DIR/resource_monitor.log" print_success "System monitoring started" } # Function to show status show_status() { print_status "Holodeck Environment Status:" echo # Check running services local services=("device_manager" "azure_integration" "slam_processor" "holodeck_ui" "resource_monitor") for service in "${services[@]}"; do local pid_file="$LOG_DIR/${service}.pid" if [ -f "$pid_file" ]; then local pid=$(cat "$pid_file") if kill -0 "$pid" 2>/dev/null; then print_success "$service: Running (PID $pid)" else print_error "$service: Not running (stale PID file)" rm -f "$pid_file" fi else print_warning "$service: Not running" fi done echo # Show recent logs if [ -f "$LOG_DIR/device_manager.log" ]; then print_status "Recent device manager logs:" tail -n 5 "$LOG_DIR/device_manager.log" echo fi if [ -f "$LOG_DIR/azure_integration.log" ]; then print_status "Recent Azure integration logs:" tail -n 5 "$LOG_DIR/azure_integration.log" echo fi } # Function to stop all services stop_all_services() { print_status "Stopping all holodeck services..." local services=("device_manager" "azure_integration" "slam_processor" "holodeck_ui" "resource_monitor") for service in "${services[@]}"; do stop_background_service "$service" done print_success "All services stopped" } # Function to show help show_help() { echo "NowYouSeeMe Complete Holodeck Environment" echo echo "Usage: $0 [COMMAND]" echo echo "Commands:" echo " start Start the complete holodeck environment" echo " stop Stop all holodeck services" echo " restart Restart all holodeck services" echo " status Show status of all services" echo " build Build the project" echo " config Create configuration files" echo " check Check system requirements" echo " help Show this help message" echo echo "Examples:" echo " $0 start # Start the complete environment" echo " $0 status # Check service status" echo " $0 stop # Stop all services" } # Main script logic case "${1:-start}" in start) print_status "Starting NowYouSeeMe Complete Holodeck Environment..." check_system_requirements build_project create_configurations start_core_services start_ui start_monitoring print_success "Holodeck environment started successfully!" echo print_status "Services are starting up. Use '$0 status' to check status." print_status "Use '$0 stop' to stop all services." echo ;; stop) stop_all_services ;; restart) stop_all_services sleep 2 $0 start ;; status) show_status ;; build) build_project ;; config) create_configurations ;; check) check_system_requirements ;; help) show_help ;; *) print_error "Unknown command: $1" echo show_help exit 1 ;; esac # Cleanup function cleanup() { print_status "Cleaning up..." stop_all_services } # Set up signal handlers trap cleanup EXIT trap 'print_status "Interrupted by user"; exit 1' INT TERM exit 0