package cfg import ( "cicv-data-closedloop/common/config/c_log" "cicv-data-closedloop/common/util" "cicv-data-closedloop/pji/common/cfg" "github.com/bluenviron/goroslib/v2/pkg/msgs/std_msgs" "plugin" "strconv" "strings" ) var ( TopicOfObstacleDetection = "/obstacle_detection" RuleOfObstacleDetection []func(data *std_msgs.UInt8) string LabelMapTriggerId = make(map[string]string) ) func InitTriggerConfig() { // 下载所有触发器的文件 for _, trigger := range cfg.PlatformConfig.TaskTriggers { // 获取文件名 slice := strings.Split(trigger.TriggerScriptPath, "/") fileName := slice[len(slice)-1] // 下载 triggerLocalPath := cfg.CloudConfig.TriggersDir + fileName _ = util.CreateParentDir(triggerLocalPath) c_log.GlobalLogger.Info("下载触发器插件从", trigger.TriggerScriptPath, "到", triggerLocalPath) err := cfg.OssBucket.GetObjectToFile(trigger.TriggerScriptPath, triggerLocalPath) if err != nil { c_log.GlobalLogger.Error("下载oss上的触发器插件失败:", err) continue } // 载入插件到数组 open, err := plugin.Open(triggerLocalPath) if err != nil { c_log.GlobalLogger.Error("加载本地插件", triggerLocalPath, "失败。", err) } topic0, err := open.Lookup("Topic") if err != nil { c_log.GlobalLogger.Error("加载本地插件", triggerLocalPath, "中的Topic方法失败。", err) continue } topic1, ok := topic0.(func() string) if ok != true { c_log.GlobalLogger.Error("插件", triggerLocalPath, "中的Topic方法必须是(func() string):", err) continue } topic2 := topic1() rule, err := open.Lookup("Rule") if err != nil { c_log.GlobalLogger.Error("加载本地插件", triggerLocalPath, "中的Rule方法失败。", err) continue } // 判断规则类型 if TopicOfObstacleDetection == topic2 { f, ok := rule.(func(data *std_msgs.UInt8) string) if ok != true { c_log.GlobalLogger.Error("插件", triggerLocalPath, "中的Topic方法必须是(func(data *std_msgs.UInt8) string):", err) continue } RuleOfObstacleDetection = append(RuleOfObstacleDetection, f) } else { c_log.GlobalLogger.Error("未知的topic:", topic2) continue } label, err := open.Lookup("Label") if err != nil { c_log.GlobalLogger.Error("加载本地插件", triggerLocalPath, "中的TriggerName方法失败。", err) continue } labelFunc, ok := label.(func() string) if ok != true { c_log.GlobalLogger.Error("插件", triggerLocalPath, "中的Label方法必须是(func() string):", err) continue } labelString := labelFunc() LabelMapTriggerId[labelString] = strconv.Itoa(trigger.TriggerId) c_log.GlobalLogger.Info("主节点加载触发器插件:【ros topic】=", topic2, ",【触发器label】=", labelString, "【触发器ID】=", trigger.TriggerId, "【label和id映射关系】=", LabelMapTriggerId) } c_log.GlobalLogger.Info("主节点加载触发器插件 - 成功。") } // //var ( // TopicOfOdom = "/odom" // RuleOfOdom []func(data *nav_msgs.Odometry) string // TopicOfObstacleDetection = "/obstacle_detection" // RuleOfObstacleDetection []func(data *std_msgs.UInt8) string // TopicOfSysInfo = "/sys_info" // RuleOfSysInfo []func(data *pji_msgs.SysInfo) string // TopicOfLocateInfo = "/locate_info" // RuleOfLocateInfo []func(data *pji_msgs.LocateInfo) string // TopicOfImu = "/imu" // RuleOfImu []func(data *sensor_msgs.Imu) string // TopicOfDiagnostics = "/diagnostics" // RuleOfDiagnostics []func(data *diagnostic_msgs.DiagnosticArray) string // LabelMapTriggerId = make(map[string]string) // //) // //func InitTriggerConfig() { // // 下载所有触发器的文件 // for _, trigger := range cfg.PlatformConfig.TaskTriggers { // // 获取文件名 // slice := strings.Split(trigger.TriggerScriptPath, "/") // fileName := slice[len(slice)-1] // // 下载 // triggerLocalPath := cfg.CloudConfig.TriggersDir + fileName // cutil.CreateParentDir(triggerLocalPath) // c_log.GlobalLogger.Info("下载触发器插件从", trigger.TriggerScriptPath, "到", triggerLocalPath) // err := cfg.OssBucket.GetObjectToFile(trigger.TriggerScriptPath, triggerLocalPath) // if err != nil { // c_log.GlobalLogger.Error("下载oss上的触发器插件失败:", err) // continue // } // // 载入插件到数组 // open, err := plugin.Open(triggerLocalPath) // if err != nil { // c_log.GlobalLogger.Error("加载本地插件", triggerLocalPath, "失败。", err) // } // topic0, err := open.Lookup("Topic") // if err != nil { // c_log.GlobalLogger.Error("加载本地插件", triggerLocalPath, "中的Topic方法失败。", err) // continue // } // topic1, ok := topic0.(func() string) // if ok != true { // c_log.GlobalLogger.Error("插件", triggerLocalPath, "中的Topic方法必须是(func() string):", err) // continue // } // topic2 := topic1() // rule, err := open.Lookup("Rule") // if err != nil { // c_log.GlobalLogger.Error("加载本地插件", triggerLocalPath, "中的Rule方法失败。", err) // continue // } // if TopicOfOdom == topic2 { // f, ok := rule.(func(data *nav_msgs.Odometry) string) // if ok != true { // c_log.GlobalLogger.Error("插件", triggerLocalPath, "中的Topic方法必须是(func(data *nav_msgs.Odometry) string):", err) // continue // } // RuleOfOdom = append(RuleOfOdom, f) // } else if TopicOfObstacleDetection == topic2 { // f, ok := rule.(func(data *std_msgs.UInt8) string) // if ok != true { // c_log.GlobalLogger.Error("插件", triggerLocalPath, "中的Topic方法必须是(func(data *std_msgs.UInt8) string):", err) // continue // } // RuleOfObstacleDetection = append(RuleOfObstacleDetection, f) // } else if TopicOfSysInfo == topic2 { // f, ok := rule.(func(data *pji_msgs.SysInfo) string) // if ok != true { // c_log.GlobalLogger.Error("插件", triggerLocalPath, "中的Topic方法必须是(func(data *pji_msgs.SysInfo) string):", err) // continue // } // RuleOfSysInfo = append(RuleOfSysInfo, f) // } else if TopicOfLocateInfo == topic2 { // f, ok := rule.(func(data *pji_msgs.LocateInfo) string) // if ok != true { // c_log.GlobalLogger.Error("插件", triggerLocalPath, "中的Topic方法必须是(func(data *pji_msgs.LocateInfo) string):", err) // continue // } // RuleOfLocateInfo = append(RuleOfLocateInfo, f) // } else if TopicOfImu == topic2 { // f, ok := rule.(func(data *sensor_msgs.Imu) string) // if ok != true { // c_log.GlobalLogger.Error("插件", triggerLocalPath, "中的Topic方法必须是(func(data *sensor_msgs.Imu) string):", err) // continue // } // RuleOfImu = append(RuleOfImu, f) // } else if TopicOfDiagnostics == topic2 { // f, ok := rule.(func(data *diagnostic_msgs.DiagnosticArray) string) // if ok != true { // c_log.GlobalLogger.Error("插件", triggerLocalPath, "中的Topic方法必须是(func(data *diagnostic_msgs.DiagnosticArray) string):", err) // continue // } // RuleOfDiagnostics = append(RuleOfDiagnostics, f) // } else { // c_log.GlobalLogger.Error("未知的topic:", topic2) // continue // } // // label, err := open.Lookup("Label") // if err != nil { // c_log.GlobalLogger.Error("加载本地插件", triggerLocalPath, "中的TriggerName方法失败。", err) // continue // } // labelFunc, ok := label.(func() string) // if ok != true { // c_log.GlobalLogger.Error("插件", triggerLocalPath, "中的Label方法必须是(func() string):", err) // continue // } // labelString := labelFunc() // LabelMapTriggerId[labelString] = strconv.Itoa(trigger.TriggerId) // c_log.GlobalLogger.Info("主节点加载触发器插件:【ros topic】=", topic2, ",【触发器label】=", labelString, "【触发器ID】=", trigger.TriggerId, "【label和id映射关系】=", LabelMapTriggerId) // } // c_log.GlobalLogger.Info("主节点加载触发器插件 - 成功。") //}