package svc import ( commonConfig "cicv-data-closedloop/aarch64/pjisuv/common/config" "cicv-data-closedloop/aarch64/pjisuv/common/service" masterConfig "cicv-data-closedloop/aarch64/pjisuv/master/package/config" "cicv-data-closedloop/common/config/c_log" "cicv-data-closedloop/common/entity" "cicv-data-closedloop/common/util" "cicv-data-closedloop/pjisuv_msgs" "github.com/bluenviron/goroslib/v2" "sync" "time" ) var ( m sync.RWMutex velocityX float64 velocityY float64 yaw float64 ) // PrepareTimeWindowProducerQueue 负责监听所有主题并修改时间窗口 func PrepareTimeWindowProducerQueue() { c_log.GlobalLogger.Info("订阅者 goroutine,启动。") c_log.GlobalLogger.Info("创建订阅者订阅话题 ", masterConfig.TopicOfCicvLocation) var err error subscribers := make([]*goroslib.Subscriber, len(commonConfig.SubscribeTopics)) subscribersTimes := make([]time.Time, len(commonConfig.SubscribeTopics)) subscribersTimeMutexes := make([]sync.Mutex, len(commonConfig.SubscribeTopics)) subscribersMutexes := make([]sync.Mutex, len(commonConfig.SubscribeTopics)) for i, topic := range commonConfig.SubscribeTopics { c_log.GlobalLogger.Info("创建订阅者订阅话题:" + topic) if topic == masterConfig.TopicOfCicvLocation { subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{ Node: commonConfig.RosNode, Topic: topic, Callback: func(data *pjisuv_msgs.PerceptionLocalization) { // 更新共享变量 m.RLock() velocityX = data.VelocityX velocityY = data.VelocityY yaw = data.Yaw m.RUnlock() if len(masterConfig.TopicOfCicvLocation) == 0 { c_log.GlobalLogger.Infof("话题 %v 没有触发器", topic) return } subscribersTimeMutexes[i].Lock() if time.Since(subscribersTimes[i]).Seconds() > 1 { subscribersMutexes[i].Lock() faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间 lastTimeWindow := entity.GetLastTimeWindow() // 获取最后一个时间窗口 var faultLabel string for _, f := range masterConfig.RuleOfCicvLocation { faultLabel = f(data) if faultLabel != "" { saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow) break } } subscribersMutexes[i].Unlock() } subscribersTimeMutexes[i].Unlock() }, }) } if topic == masterConfig.TopicOfTpperception { subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{ Node: commonConfig.RosNode, Topic: topic, Callback: func(data *pjisuv_msgs.PerceptionObjects) { if len(masterConfig.TopicOfTpperception) == 0 { c_log.GlobalLogger.Infof("话题 %v 没有触发器", topic) return } subscribersTimeMutexes[i].Lock() if time.Since(subscribersTimes[i]).Seconds() > 1 { subscribersMutexes[i].Lock() faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间 lastTimeWindow := entity.GetLastTimeWindow() // 获取最后一个时间窗口 var faultLabel string for _, f := range masterConfig.RuleOfTpperception { faultLabel = f(data, velocityX, velocityY, yaw) if faultLabel != "" { saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow) break } } subscribersMutexes[i].Unlock() } subscribersTimeMutexes[i].Unlock() }, }) } if topic == masterConfig.TopicOfDataRead { subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{ Node: commonConfig.RosNode, Topic: topic, Callback: func(data *pjisuv_msgs.Retrieval) { if len(masterConfig.TopicOfDataRead) == 0 { c_log.GlobalLogger.Infof("话题 %v 没有触发器", topic) return } subscribersTimeMutexes[i].Lock() if time.Since(subscribersTimes[i]).Seconds() > 1 { subscribersMutexes[i].Lock() faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间 lastTimeWindow := entity.GetLastTimeWindow() // 获取最后一个时间窗口 var faultLabel string for _, f := range masterConfig.RuleOfDataRead { faultLabel = f(data) if faultLabel != "" { saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow) subscribersTimes[i] = time.Now() break } } subscribersMutexes[i].Unlock() } subscribersTimeMutexes[i].Unlock() }, }) } if err != nil { c_log.GlobalLogger.Info("创建订阅者报错:", err) //TODO 如何回传日志 continue } } select { case signal := <-service.ChannelKillWindowProducer: if signal == 1 { commonConfig.RosNode.Close() service.AddKillTimes("3") return } } } func saveTimeWindow(faultLabel string, faultHappenTime string, lastTimeWindow *entity.TimeWindow) { masterTopics, slaveTopics := getTopicsOfNode(faultLabel) if lastTimeWindow == nil || util.TimeCustom1GreaterTimeCustom2(faultHappenTime, lastTimeWindow.TimeWindowEnd) { // 2-1 如果是不在旧故障窗口内,添加一个新窗口 newTimeWindow := entity.TimeWindow{ FaultTime: faultHappenTime, TimeWindowBegin: util.TimeCustomChange(faultHappenTime, -commonConfig.PlatformConfig.TaskBeforeTime), TimeWindowEnd: util.TimeCustomChange(faultHappenTime, commonConfig.PlatformConfig.TaskAfterTime), Length: commonConfig.PlatformConfig.TaskBeforeTime + commonConfig.PlatformConfig.TaskAfterTime + 1, Labels: []string{faultLabel}, MasterTopics: masterTopics, SlaveTopics: slaveTopics, } c_log.GlobalLogger.Infof("不在旧故障窗口内,向生产者队列添加一个新窗口,【Lable】=%v,【FaultTime】=%v,【Length】=%v", newTimeWindow.Labels, newTimeWindow.FaultTime, newTimeWindow.Length) entity.AddTimeWindowToTimeWindowProducerQueue(newTimeWindow) } else { // 2-2 如果在旧故障窗口内 entity.TimeWindowProducerQueueMutex.RLock() defer entity.TimeWindowProducerQueueMutex.RUnlock() // 2-2-1 更新故障窗口end时间 maxEnd := util.TimeCustomChange(lastTimeWindow.TimeWindowBegin, commonConfig.PlatformConfig.TaskMaxTime) expectEnd := util.TimeCustomChange(faultHappenTime, commonConfig.PlatformConfig.TaskAfterTime) if util.TimeCustom1GreaterTimeCustom2(expectEnd, maxEnd) { lastTimeWindow.TimeWindowEnd = maxEnd lastTimeWindow.Length = commonConfig.PlatformConfig.TaskMaxTime } else { if util.TimeCustom1GreaterTimeCustom2(expectEnd, lastTimeWindow.TimeWindowEnd) { lastTimeWindow.TimeWindowEnd = expectEnd lastTimeWindow.Length = util.CalculateDifferenceOfTimeCustom(lastTimeWindow.TimeWindowBegin, expectEnd) } } // 2-2-2 更新label labels := lastTimeWindow.Labels lastTimeWindow.Labels = util.AppendIfNotExists(labels, faultLabel) // 2-2-3 更新 topic sourceMasterTopics := lastTimeWindow.MasterTopics lastTimeWindow.MasterTopics = util.MergeSlice(sourceMasterTopics, masterTopics) sourceSlaveTopics := lastTimeWindow.SlaveTopics lastTimeWindow.SlaveTopics = util.MergeSlice(sourceSlaveTopics, slaveTopics) c_log.GlobalLogger.Infof("在旧故障窗口内,更新生产者队列最新的窗口,【Lable】=%v,【FaultTime】=%v,【Length】=%v", lastTimeWindow.Labels, lastTimeWindow.FaultTime, lastTimeWindow.Length) } } func getTopicsOfNode(faultLabel string) (masterTopics []string, slaveTopics []string) { // 获取所有需要采集的topic var faultCodeTopics []string for _, code := range commonConfig.CloudConfig.Triggers { if code.Label == faultLabel { faultCodeTopics = code.Topics } } // 根据不同节点采集的topic进行分配采集 for _, acceptTopic := range faultCodeTopics { for _, host := range commonConfig.CloudConfig.Hosts { for _, topic := range host.Topics { if host.Name == commonConfig.CloudConfig.Hosts[0].Name && acceptTopic == topic { masterTopics = append(masterTopics, acceptTopic) } if host.Name == commonConfig.CloudConfig.Hosts[1].Name && acceptTopic == topic { slaveTopics = append(slaveTopics, acceptTopic) } } } } return masterTopics, slaveTopics }