package main

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

/*
imu通讯严重丢失
*/

var (
	errorType       = "2"
	errorCode       = "21030401"
	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 ""
}