LingxinMeng 5 månader sedan
förälder
incheckning
35c42013f5
2 ändrade filer med 91 tillägg och 96 borttagningar
  1. 91 0
      test/regenerate_test.go
  2. 0 96
      test/rosbag_write_test.go

+ 91 - 0
test/regenerate_test.go

@@ -0,0 +1,91 @@
+package test
+
+import (
+	"fmt"
+	"github.com/aliyun/aliyun-oss-go-sdk/oss"
+	"strings"
+	"testing"
+)
+
+// 删除出了 callback 文件之外的所有文件
+func TestDeleteExceptCallback(t *testing.T) {
+	// Define OSS configuration
+	endpoint := "http://pji-bucket1.oss.icvdc.com"
+	accessKeyID := "n8glvFGS25MrLY7j"
+	accessKeySecret := "xZ2Fozoarpfw0z28FUhtg8cu0yDc5d"
+	bucketName := "pji-bucket1"
+
+	// Create an OSS client
+	client, err := oss.New(endpoint, accessKeyID, accessKeySecret, oss.UseCname(true))
+	if err != nil {
+		fmt.Printf("Failed to create OSS client: %v\n", err)
+		return
+	}
+
+	// Get the bucket
+	bucket, err := client.Bucket(bucketName)
+	if err != nil {
+		fmt.Printf("Failed to get bucket: %v\n", err)
+		return
+	}
+
+	// Define an array of directories (prefixes) to scan
+	//directories := []string{
+	//	"pjibot_delivery/pjibot-P1YVPS1M22CM00020/data_parse/",
+	//	"pjibot_delivery/pjibot-P1YYPS1M227M00107/data_parse/",
+	//}
+	directories := []string{
+		"pjibot_patrol/pjibot-P1YVXJ1M231M00022/data_parse",
+		"pjibot_patrol/pjibot-P1YVXJ1M231M00023/data_parse",
+	}
+
+	for _, dir := range directories {
+		// List objects in the directory
+		marker := ""
+		for {
+			lsRes, err := bucket.ListObjects(oss.Prefix(dir), oss.Marker(marker))
+			if err != nil {
+				fmt.Printf("Failed to list objects in directory %s: %v\n", dir, err)
+				break
+			}
+
+			for _, object := range lsRes.Objects {
+				objectKey := object.Key
+
+				// Process files not containing "callback"
+				if !strings.Contains(objectKey, "callback") {
+					err := bucket.DeleteObject(objectKey)
+					if err != nil {
+						fmt.Printf("Failed to delete object %s: %v\n", objectKey, err)
+					} else {
+						fmt.Printf("Deleted object: %s\n", objectKey)
+					}
+					continue
+				}
+
+				// Rename files containing "callback_done" to "callback"
+				if strings.Contains(objectKey, "callback_done") {
+					newKey := strings.Replace(objectKey, "callback_done", "callback", 1)
+					err, _ = bucket.CopyObject(objectKey, newKey)
+					if err != nil {
+						fmt.Printf("Failed to copy object %s to %s: %v\n", objectKey, newKey, err)
+						err = bucket.DeleteObject(objectKey)
+					} else {
+						err = bucket.DeleteObject(objectKey)
+						if err != nil {
+							fmt.Printf("Failed to delete original object %s after renaming: %v\n", objectKey, err)
+						} else {
+							fmt.Printf("Renamed object: %s to %s\n", objectKey, newKey)
+						}
+					}
+				}
+			}
+
+			// Exit the loop if all objects are processed
+			if !lsRes.IsTruncated {
+				break
+			}
+			marker = lsRes.NextMarker
+		}
+	}
+}

+ 0 - 96
test/rosbag_write_test.go

@@ -1,96 +0,0 @@
-package test
-
-import (
-	"cicv-data-closedloop/pjisuv_msgs"
-	"fmt"
-	"log"
-	"os"
-	"testing"
-	"time"
-
-	"github.com/bluenviron/goroslib/v2"
-	"github.com/foxglove/go-rosbag"
-)
-
-func TestWriteRosBag(t *testing.T) {
-
-	// 指定文件路径
-	filePath := "C:\\Users\\mlxengingin\\Desktop\\1118\\my-bag.bag"
-
-	// 以读写模式打开(或创建并清空)文件
-	f, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
-	if err != nil {
-		// 打开(或创建并清空)文件时出错
-		panic(fmt.Errorf("无法打开或创建文件: %v", err))
-	}
-	defer f.Close() // 确保文件在函数结束时被关闭
-
-	// 后续的文件写入操作...
-	t.Log("文件已成功打开(或创建并清空)。")
-
-	writer, err := rosbag.NewWriter(f)
-	if err != nil {
-		panic(err)
-	}
-
-	err = writer.WriteConnection(&rosbag.Connection{
-		Conn:  1,
-		Topic: "/cicv_location",
-		Data: rosbag.ConnectionHeader{
-			Topic:             "/cicv_location",
-			Type:              "perception_msgs/PerceptionLocalization",
-			MD5Sum:            "b871ae769d4b537baab00d1c75ff3891",
-			MessageDefinition: []byte("定位信息"),
-
-			//// optional fields
-			//CallerID: &callerID,
-			//Latching: &latching,
-		},
-	})
-	if err != nil {
-		panic(err)
-	}
-	// -------------------------------------------------------------------------------------
-
-	// create a node and connect to the master
-	n, err := goroslib.NewNode(goroslib.NodeConf{
-		Name:          "goroslib_sub",
-		MasterAddress: "127.0.0.1:11311",
-	})
-	if err != nil {
-		panic(err)
-	}
-	defer n.Close()
-
-	// create a subscriber
-	sub, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
-		Node:  n,
-		Topic: "/cicv_location",
-		Callback: func(msg *pjisuv_msgs.PerceptionLocalization) {
-			log.Printf("Incoming: %+v\n", msg)
-			nowInt := time.Now().Unix()
-			data := pjisuv_msgs.PerceptionLocalization{}
-			err = writer.WriteMessage(&rosbag.Message{
-				Conn: 1,
-				Time: uint64(nowInt),
-				Data: []byte(msg),
-			})
-			if err != nil {
-				panic(err)
-			}
-			t.Log("当前时间", uint64(nowInt))
-			t.Log("保存数据", data)
-		},
-	})
-	if err != nil {
-		panic(err)
-	}
-	defer sub.Close()
-
-	// // wait for CTRL-C
-	// c := make(chan os.Signal, 1)
-	// signal.Notify(c, os.Interrupt)
-	// <-c
-	// ------------------------------------------------------------------------------------
-
-}