Skip to content

Start_simulator.sh

#!/bin/bash
set -Eeuo pipefail

# ANSI color codes
GREEN="\033[1;32m"
RED="\033[1;31m"
YELLOW="\033[1;33m"
NC="\033[0m"  # No Color

# -------- Stop anything still running --------
echo -e "${YELLOW}[INFO] Stopping any existing simulator processes and ports...${NC}"
if bash ./kill_simulation_modbus_servers.sh; then
  echo -e "${GREEN}[OK] Kill script completed.${NC}"
else
  echo -e "${YELLOW}[WARN] Kill script returned non-zero (continuing).${NC}"
fi

mkdir -p logs

# -------- List your simulators here (Label:ScriptPath) --------
SCRIPTS=(
  "FFR Meter:ffrMeter_simulator_v5.py"
  "Fire Suppression:fss_simulator_v1.py"
  "PCS:pcs_simulator_v3.py"
  "SensorB:sensorB_simulator_v2.py"
  "Solar Meter:solarMeter_simulator_v6.py"
  "AC:ac_simulator_v1.py"
  "BCMS:bcms_simulator_v14.py"
  "Grid Meter:gridMeter_simulator_v7.py"
)

fail_count=0

start_one () {
  local label="$1"
  local script="$2"
  local log="logs/simulator-startingLog-${script%.py}.log"

  if [[ ! -f "$script" ]]; then
    echo -e "${RED}[ERROR] $label — file not found: $script${NC}"
    ((fail_count++))
    return
  fi

  : > "$log"
  echo -e "${YELLOW}[INFO] Starting $label -> $script${NC}"
  nohup python3 "$script" >>"$log" 2>&1 &
  local pid=$!
  sleep 2

  if ps -p "$pid" >/dev/null 2>&1; then
    echo -e "${GREEN}[OK] $label — started (pid $pid), log: $log${NC}"
  else
    echo -e "${RED}[ERROR] $label — failed to start (see $log)${NC}"
    if [[ -s "$log" ]]; then
      tail -n 15 "$log"
    fi
    ((fail_count++))
  fi
}

echo -e "${YELLOW}[INFO] Starting BESS simulators...${NC}"
for entry in "${SCRIPTS[@]}"; do
  IFS=":" read -r label script <<<"$entry"
  start_one "$label" "$script"
done

if (( fail_count > 0 )); then
  echo -e "${RED}[ERROR] $fail_count simulator(s) failed to start.${NC}"
  exit 1
fi

echo -e "${GREEN}[INFO] All simulators started successfully.${NC}"