孟令鑫 há 1 ano atrás
pai
commit
d891d767d3

+ 20 - 0
common/util/u_exec.go

@@ -34,7 +34,18 @@ func ExecuteWithPath(path string, name string, arg ...string) (*exec.Cmd, error)
 		// 执行命令并等待它完成d
 		return cmd, nil
 	}
+}
 
+func ExecuteWithEnvAndDirAsync(envs []string, dir string, name string, arg ...string) (*exec.Cmd, error) {
+	cmd := exec.Command(name, arg...)
+	cmd.Dir = dir
+	for _, env := range envs {
+		cmd.Env = append(cmd.Env, env)
+	}
+	if err := cmd.Start(); err != nil {
+		return nil, err
+	}
+	return cmd, nil
 }
 
 // KillProcessByPid 必须使用该golang原生方法,通过直接执行kill命令行不通。
@@ -59,6 +70,15 @@ func Execute(name string, arg ...string) (*exec.Cmd, string, error) {
 	return cmd, string(combinedOutput), nil
 }
 
+func ExecuteWithEnvSync(envs []string, name string, arg ...string) (*exec.Cmd, string, error) {
+	cmd := exec.Command(name, arg...)
+	combinedOutput, err := cmd.CombinedOutput()
+	if err != nil {
+		return nil, "", err
+	}
+	return cmd, string(combinedOutput), nil
+}
+
 func ExecuteWithEnvAndDir(envs []string, dir string, name string, arg ...string) (*exec.Cmd, string, error) {
 	cmd := exec.Command(name, arg...)
 	cmd.Dir = dir

+ 2 - 1
kinglong/common/svc/rosbag_record.go

@@ -6,6 +6,7 @@ import (
 	"cicv-data-closedloop/kinglong/common/cutil"
 	"cicv-data-closedloop/kinglong/common/log"
 	"github.com/bluenviron/goroslib/v2"
+	"os"
 	"time"
 )
 
@@ -37,7 +38,7 @@ func BagRecord(nodeName string) {
 		// 不在此处压缩,因为 rosbag filter 时会报错。在上传到oss之前压缩即可。
 		// 包名格式:2023-11-15-17-35-20_0.bag
 		cutil.CreateParentDir(cfg2.CloudConfig.BagDataDir)
-		cmd, err := util.ExecuteWithPath(cfg2.CloudConfig.BagDataDir, "rosbag", command...)
+		cmd, err := util.ExecuteWithEnvAndDirAsync(os.Environ(), cfg2.CloudConfig.BagDataDir, "rosbag", command...)
 		if err != nil {
 			log.GlobalLogger.Error("执行record命令", command, "出错:", err)
 			continue

+ 3 - 2
kinglong/common/svc/rosbag_upload.go

@@ -1,6 +1,7 @@
 package svc
 
 import (
+	commonUtil "cicv-data-closedloop/common/util"
 	commonConfig "cicv-data-closedloop/kinglong/common/cfg"
 	"cicv-data-closedloop/kinglong/common/global"
 	"cicv-data-closedloop/kinglong/common/log"
@@ -68,7 +69,7 @@ outLoop:
 				oldName := bag
 				newName := bag + "_filter"
 				filterCommand := []string{"filter", oldName, newName, "\"" + strings.Join(topicsFilterSlice, " or ") + "\""}
-				_, output, err := util.Execute("rosbag", filterCommand...)
+				_, output, err := commonUtil.ExecuteWithEnvSync(os.Environ(), "rosbag", filterCommand...)
 				log.GlobalLogger.Info("正在过滤中,【FaultTime】=", currentTimeWindow.FaultTime, "【Label】=", currentTimeWindow.Labels, ",进度", i+1, "/", bagNumber, "。")
 				if err != nil {
 					log.GlobalLogger.Errorf("filter 命令执行出错【命令】=%v,【输出】=%v,【err】=%v", filterCommand, output, err)
@@ -90,7 +91,7 @@ outLoop:
 			oldName := bag
 			compressCommand := []string{"compress", "--bz2", oldName}
 			log.GlobalLogger.Info("正在压缩中,【FaultTime】=", currentTimeWindow.FaultTime, "【Label】=", currentTimeWindow.Labels, ",进度", i+1, "/", bagNumber, "。")
-			if _, output, err := util.Execute("rosbag", compressCommand...); err != nil {
+			if _, output, err := commonUtil.ExecuteWithEnvSync(os.Environ(), "rosbag", compressCommand...); err != nil {
 				log.GlobalLogger.Errorf("compress 命令执行出错【命令】=%v,【输出】=%v,【err】=%v", compressCommand, output, err)
 				continue
 			}

+ 0 - 9
kinglong/common/util/util_exec.go

@@ -4,15 +4,6 @@ import (
 	"os/exec"
 )
 
-func GetSubProcessPid(pid string) (string, error) {
-	pgrepCmd := exec.Command("pgrep", "-P", pid)
-	output, err := pgrepCmd.CombinedOutput()
-	if err != nil {
-		return "", err
-	}
-	return string(output), nil
-}
-
 func Execute(name string, arg ...string) (*exec.Cmd, string, error) {
 	cmd := exec.Command(name, arg...)
 	combinedOutput, err := cmd.CombinedOutput()

+ 1 - 1
pji/common/cfg/ros_cfg.go

@@ -14,11 +14,11 @@ func InitRosConfig() {
 	c_log.GlobalLogger.Info("初始化RosNode - 开始")
 	for {
 		time.Sleep(time.Duration(2) * time.Second)
+		c_log.GlobalLogger.Error("初始化RosNode - 进行中:", err)
 		if RosNode, err = goroslib.NewNode(goroslib.NodeConf{
 			Name:          "node" + util.GetNowTimeCustom(),
 			MasterAddress: CloudConfig.Ros.MasterAddress,
 		}); err != nil {
-			c_log.GlobalLogger.Error("初始化RosNode - 进行中:", err)
 			continue
 		}
 		c_log.GlobalLogger.Info("初始化RosNode - 成功:", CloudConfig.Ros.MasterAddress)

+ 2 - 1
pji/common/svc/rosbag_record.go

@@ -6,6 +6,7 @@ import (
 	"cicv-data-closedloop/pji/common/cfg"
 	"cicv-data-closedloop/pji/common/cutil"
 	"github.com/bluenviron/goroslib/v2"
+	"os"
 	"time"
 )
 
@@ -37,7 +38,7 @@ func BagRecord(nodeName string) {
 		// 不在此处压缩,因为 rosbag filter 时会报错。在上传到oss之前压缩即可。
 		// 包名格式:2023-11-15-17-35-20_0.bag
 		cutil.CreateParentDir(cfg.CloudConfig.BagDataDir)
-		cmd, err := util.ExecuteWithPath(cfg.CloudConfig.BagDataDir, "rosbag", command...)
+		cmd, err := util.ExecuteWithEnvAndDirAsync(os.Environ(), cfg.CloudConfig.BagDataDir, "rosbag", command...)
 		if err != nil {
 			c_log.GlobalLogger.Error("执行record命令", command, "出错:", err)
 			continue

+ 3 - 2
pji/common/svc/rosbag_upload.go

@@ -2,6 +2,7 @@ package svc
 
 import (
 	"cicv-data-closedloop/common/config/c_log"
+	commonUtil "cicv-data-closedloop/common/util"
 	commonConfig "cicv-data-closedloop/pji/common/cfg"
 	"cicv-data-closedloop/pji/common/global"
 	"cicv-data-closedloop/pji/common/util"
@@ -69,7 +70,7 @@ outLoop:
 				oldName := bag
 				newName := bag + "_filter"
 				filterCommand := []string{"filter", oldName, newName, "\"" + strings.Join(topicsFilterSlice, " or ") + "\""}
-				_, output, err := util.Execute("rosbag", filterCommand...)
+				_, output, err := commonUtil.ExecuteWithEnvSync(os.Environ(), "rosbag", filterCommand...)
 				c_log.GlobalLogger.Info("正在过滤中,【FaultTime】=", currentTimeWindow.FaultTime, "【Label】=", currentTimeWindow.Labels, ",进度", i+1, "/", bagNumber, "。")
 				if err != nil {
 					c_log.GlobalLogger.Errorf("filter命令执行出错【命令】=%v,【输出】=%v,【err】=%v", filterCommand, output, err)
@@ -91,7 +92,7 @@ outLoop:
 			oldName := bag
 			compressCommand := []string{"compress", "--bz2", oldName}
 			c_log.GlobalLogger.Info("正在压缩中,【FaultTime】=", currentTimeWindow.FaultTime, "【Label】=", currentTimeWindow.Labels, ",进度", i+1, "/", bagNumber, "。")
-			if _, output, err := util.Execute("rosbag", compressCommand...); err != nil {
+			if _, output, err := commonUtil.ExecuteWithEnvSync(os.Environ(), "rosbag", compressCommand...); err != nil {
 				c_log.GlobalLogger.Errorf("compress命令执行出错【命令】=%v,【输出】=%v,【err】=%v", compressCommand, output, err)
 				continue
 			}