#! /usr/bin/env python3
import subprocess
import rospy
import os

file_path = "moveitServer2"
script_directory = os.path.dirname(os.path.abspath(__file__))

import signal

 
def close_rviz(terminal_name):
    # 使用pgrep查找终端进程
    pgrep_cmd = "pgrep -f '{}'".format(terminal_name)
    pids = subprocess.check_output(pgrep_cmd, shell=True).strip().split()
    
    # 如果找到了匹配的进程ID，发送SIGTERM信号
    for pid in pids:
        try:
            os.kill(int(pid), signal.SIGTERM)
        except ProcessLookupError:
            pass  # 进程可能已经不存在

def load_rviz():
    command = "roslaunch lstrobot_moveit_config_0605 demo.launch"
    subprocess.Popen(['gnome-terminal', '--', 'bash', '-c', command])
def launch_publish_pointcloud():
    file_name = "dycl_0506.py"
    absolute_path = os.path.join(script_directory, file_name)
    command = "python3 {}".format(absolute_path)
    subprocess.Popen(['gnome-terminal', '--', 'bash', '-c', command])
def launch_moveit_control_server():
    file_name = "moveitServer2.py"
    absolute_path = os.path.join(script_directory, file_name)
    command = "python3 {}".format(absolute_path)
    subprocess.Popen(['gnome-terminal', '--', 'bash', '-c', command])

def launch_publish_pointcloud_background():
    file_name = "dycl_0506.py"
    absolute_path = os.path.join(script_directory, file_name)
    command = ["python3", absolute_path]
    subprocess.call(command)
def launch_moveit_control_server_background():
    file_name = "moveitServer2.py"
    absolute_path = os.path.join(script_directory, file_name)
    command = ["python3", absolute_path]
    subprocess.call(command)