package main

import (
	"fmt"
	"github.com/bluenviron/goroslib/v2/pkg/msgs/diagnostic_msgs"
	"strconv"
)

/*
全局路径转换失败
*/

var (
	errorType       = "5"
	errorCode       = "21051509"
	code12          = "21"
	hardwareIdIndex = map[string]string{
		"batter":        "00",
		"cloud":         "01",
		"gateway":       "02",
		"mcu":           "03",
		"imu":           "04",
		"odom":          "05",
		"motor":         "06",
		"preventFall":   "07",
		"sonar":         "08",
		"locate":        "09",
		"laserLadar":    "10",
		"electricDrive": "11",
		"infrared":      "12",
		"livoxLidar":    "13",
		"camera":        "14",
		"navigation":    "15",
		"system":        "16",
	}
)

func Topic() string {
	return "/diagnostics"
}

func Label() string {
	return "errorcode#" + errorType + "#" + errorCode
}

func Rule(data *diagnostic_msgs.DiagnosticArray) string {
	if len(data.Status) == 0 {
		return ""
	}
	for _, status := range data.Status {
		if status.Level != 5 || len(status.Values) == 0 {
			continue
		}

		code34 := fmt.Sprintf("%02d", status.Level)
		code56 := hardwareIdIndex[status.HardwareId]
		intVal, err := strconv.Atoi(status.Values[1].Value)
		if err != nil {
			fmt.Println("字段 【status.Values[1].Value】转整数报错：", err)
			return ""
		}
		code78 := fmt.Sprintf("%02d", intVal)
		if errorCode == code12+code34+code56+code78 {
			return Label()
		}
	}
	return ""
}
