LingxinMeng 1 yıl önce
ebeveyn
işleme
eb08bedc1a

+ 10 - 1
aarch64/pjisuv/common/config/c_resource.go

@@ -6,11 +6,20 @@ import (
 	"time"
 )
 
+//type DeviceMonitor struct {
+//	Id               int    `db:"id" json:"id"`                               // 自增id
+//	TotalCpuUsage    string `db:"total_cpu_usage" json:"totalCpuUsage"`       // cpu总占用
+//	TotalMemoryUsage string `db:"total_memory_usage" json:"totalMemoryUsage"` // 内存总占用
+//	Top10Process     string `db:"top10_process" json:"top10Process"`          // cpu占用前十的进程信息
+//	DeviceNumber     string `db:"device_number" json:"deviceNumber"`          // cpu占用前十的进程信息
+//	SocIp            string `db:"soc_ip" json:"socIp"`                        // cpu占用前十的进程信息
+//}
+
 // SendResourceUsage 保存资源占用情况
 func SendResourceUsage() {
 	for {
 		time.Sleep(time.Duration(2) * time.Second)
-		responseString, err := util.HttpPostWithHeaders(
+		responseString, err := util.HttpPostJsonWithHeaders(
 			CloudConfig.Monitor.Url,
 			map[string]string{"Authorization": "U9yKpD6kZZDDe4LFKK6myAxBUT1XRrDM"},
 			map[string]string{

+ 1 - 1
amd64/web_server/handler/h_monitor.go

@@ -18,7 +18,7 @@ type DeviceMonitor struct {
 	SocIp            string `db:"soc_ip" json:"socIp"`                        // cpu占用前十的进程信息
 }
 
-// SaveDeviceMonitor 保存实车上传的监控信息
+// 保存实车上传的监控信息
 func SaveDeviceMonitor(c *gin.Context) {
 	param := new(DeviceMonitor)
 	// 映射到结构体

+ 14 - 8
common/util/u_http.go

@@ -3,6 +3,7 @@ package util
 import (
 	"bytes"
 	"encoding/json"
+	"fmt"
 	"io"
 	"net/http"
 	"net/url"
@@ -58,17 +59,18 @@ func HttpPostJsonResponseString(url string, params map[string]string) (string, e
 	return string(body), nil
 }
 
-func HttpPostWithHeaders(baseUrl string, headers map[string]string, params map[string]string) (string, error) {
-	// 构建请求体
-	data := url.Values{}
-	for key, value := range params {
-		data.Set(key, value)
+func HttpPostJsonWithHeaders(baseUrl string, headers map[string]string, params map[string]string) (string, error) {
+	// 将参数转换为 JSON 字符串
+	jsonData, err := json.Marshal(params)
+	if err != nil {
+		fmt.Println("Error encoding JSON:", err)
+		return "", err
 	}
-	requestBody := bytes.NewBufferString(data.Encode())
 
 	// 创建请求
-	req, err := http.NewRequest("POST", baseUrl, requestBody)
+	req, err := http.NewRequest("POST", baseUrl, bytes.NewBuffer(jsonData))
 	if err != nil {
+		fmt.Println("Error creating request:", err)
 		return "", err
 	}
 
@@ -81,10 +83,14 @@ func HttpPostWithHeaders(baseUrl string, headers map[string]string, params map[s
 	client := &http.Client{}
 	resp, err := client.Do(req)
 	if err != nil {
+		fmt.Println("Error sending request:", err)
 		return "", err
 	}
+	defer resp.Body.Close()
 	body, err := io.ReadAll(resp.Body)
-
+	if err != nil {
+		return "", err
+	}
 	return string(body), nil
 }
 

+ 22 - 54
test/main/main.go

@@ -1,66 +1,34 @@
 package main
 
 import (
+	"cicv-data-closedloop/common/util"
 	"fmt"
-	"os"
-	"path/filepath"
-	"strings"
 )
 
-//// 生成插件命令到 sh文件
-//func main() {
-//	//go build --buildmode=plugin -o ./so/pji/errorcode#1#21031701.so ./trigger/pji/diagnostics/errorcode#1#21031701/main/main.go
-//	// 指定目录的路径
-//	dirPath := "D:\\code\\cicv-data-closedloop\\trigger\\pji\\diagnostics"
-//
-//	// 读取目录内容
-//	files, err := os.ReadDir(dirPath)
-//	if err != nil {
-//		log.Fatal(err)
-//	}
-//
-//	// 遍历目录中的文件和子目录
-//	content := ""
-//	for _, file := range files {
-//		// 如果是目录
-//		if file.IsDir() {
-//			// 打印子目录名称
-//
-//			fileName := strings.Replace(strings.Replace(file.Name(), " ", "", -1), "\n", "", -1)
-//			fmt.Println()
-//			content = content + "go build --buildmode=plugin -o ./so/pji/" + fileName + ".so ./trigger/pji/diagnostics/" + fileName + "/main/main.go" + "\n"
-//		}
-//	}
-//	util.WriteFile(content, "D:\\test.sh")
+//type DeviceMonitor struct {
+//	Id               int    `db:"id" json:"id"`                               // 自增id
+//	TotalCpuUsage    string `db:"total_cpu_usage" json:"totalCpuUsage"`       // cpu总占用
+//	TotalMemoryUsage string `db:"total_memory_usage" json:"totalMemoryUsage"` // 内存总占用
+//	Top10Process     string `db:"top10_process" json:"top10Process"`          // cpu占用前十的进程信息
+//	DeviceNumber     string `db:"device_number" json:"deviceNumber"`          // cpu占用前十的进程信息
+//	SocIp            string `db:"soc_ip" json:"socIp"`                        // cpu占用前十的进程信息
 //}
 
-func replaceSymbolInDir(root string, oldSymbol, newSymbol string) error {
-	return filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
-		if err != nil {
-			return err
-		}
-		if info.IsDir() {
-			// Rename directory
-			newPath := strings.Replace(path, oldSymbol, newSymbol, -1)
-			if newPath != path {
-				fmt.Printf("Renaming %s to %s\n", path, newPath)
-				return os.Rename(path, newPath)
-			}
-		}
-		return nil
-	})
-}
-
+// 保存资源占用情况
 func main() {
-	rootDir := "D:\\code\\cicv-data-closedloop\\trigger\\pji\\diagnostics"
-	oldSymbol := "#"
-	newSymbol := "_"
-
-	err := replaceSymbolInDir(rootDir, oldSymbol, newSymbol)
+	responseString, err := util.HttpPostJsonWithHeaders(
+		"http://36.110.106.142:12341/web_server/monitor/insert",
+		map[string]string{"Authorization": "U9yKpD6kZZDDe4LFKK6myAxBUT1XRrDM"},
+		map[string]string{
+			"totalCpuUsage":    "util.ToString(util.GetCpuPercent())",
+			"totalMemoryUsage": " util.ToString(util.GetMemoryPercent())",
+			"top10Process":     " util.ToString(util.GetTop10())",
+			"deviceNumber":     "LocalConfig.EquipmentNo",
+			"socIp":            "LocalConfig.Node.Ip",
+		},
+	)
 	if err != nil {
-		fmt.Println("Error:", err)
-		return
+		fmt.Printf("发送数据监控信息报错%v,响应信息为:%v", err, responseString)
 	}
-
-	fmt.Println("Replacement completed successfully.")
+	fmt.Printf("发送数据监控信息成功,响应信息为:%v", responseString)
 }