produce_window.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. package service
  2. import (
  3. commonConfig "cicv-data-closedloop/aarch64/kinglong/common/config"
  4. "cicv-data-closedloop/aarch64/kinglong/common/service"
  5. masterConfig "cicv-data-closedloop/aarch64/kinglong/master/package/config"
  6. "cicv-data-closedloop/common/config/c_log"
  7. "cicv-data-closedloop/common/entity"
  8. "cicv-data-closedloop/common/util"
  9. "cicv-data-closedloop/kinglong_msgs"
  10. "github.com/bluenviron/goroslib/v2"
  11. "sync"
  12. "time"
  13. )
  14. var (
  15. subscriber0Mutex sync.Mutex
  16. subscriber1Mutex sync.Mutex
  17. subscriber2Mutex sync.Mutex
  18. subscriber3Mutex sync.Mutex
  19. subscriber4Mutex sync.Mutex
  20. m sync.RWMutex
  21. velocityX float64
  22. velocityY float64
  23. yaw float64
  24. )
  25. // PrepareTimeWindowProducerQueue 负责监听所有主题并修改时间窗口
  26. func PrepareTimeWindowProducerQueue() {
  27. c_log.GlobalLogger.Info("订阅者 goroutine,启动。")
  28. var err error
  29. subscribers := make([]*goroslib.Subscriber, len(commonConfig.SubscribeTopics))
  30. subscribersTimes := make([]time.Time, len(commonConfig.SubscribeTopics))
  31. subscribersTimeMutexes := make([]sync.Mutex, len(commonConfig.SubscribeTopics))
  32. subscribersMutexes := make([]sync.Mutex, len(commonConfig.SubscribeTopics))
  33. for i, topic := range commonConfig.SubscribeTopics {
  34. c_log.GlobalLogger.Info("创建订阅者订阅话题:" + topic)
  35. if topic == masterConfig.TopicOfCicvLocation {
  36. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  37. Node: commonConfig.RosNode,
  38. Topic: topic,
  39. Callback: func(data *kinglong_msgs.PerceptionLocalization) {
  40. // 更新共享变量
  41. m.RLock()
  42. velocityX = data.VelocityX
  43. velocityY = data.VelocityY
  44. yaw = data.Yaw
  45. m.RUnlock()
  46. if len(masterConfig.TopicOfCicvLocation) == 0 {
  47. c_log.GlobalLogger.Infof("话题 %v 没有触发器", topic)
  48. return
  49. }
  50. subscribersTimeMutexes[i].Lock()
  51. if time.Since(subscribersTimes[i]).Seconds() > 1 {
  52. subscribersMutexes[i].Lock()
  53. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  54. lastTimeWindow := entity.GetLastTimeWindow() // 获取最后一个时间窗口
  55. var faultLabel string
  56. for _, f := range masterConfig.RuleOfCicvLocation {
  57. faultLabel = f(data)
  58. if faultLabel != "" {
  59. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  60. break
  61. }
  62. }
  63. subscribersMutexes[i].Unlock()
  64. }
  65. subscribersTimeMutexes[i].Unlock()
  66. },
  67. })
  68. }
  69. if topic == masterConfig.TopicOfTpperception {
  70. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  71. Node: commonConfig.RosNode,
  72. Topic: topic,
  73. Callback: func(data *kinglong_msgs.PerceptionObjects) {
  74. if len(masterConfig.TopicOfTpperception) == 0 {
  75. c_log.GlobalLogger.Infof("话题 %v 没有触发器", topic)
  76. return
  77. }
  78. subscribersTimeMutexes[i].Lock()
  79. if time.Since(subscribersTimes[i]).Seconds() > 1 {
  80. subscribersMutexes[i].Lock()
  81. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  82. lastTimeWindow := entity.GetLastTimeWindow() // 获取最后一个时间窗口
  83. var faultLabel string
  84. for _, f := range masterConfig.RuleOfTpperception {
  85. faultLabel = f(data, velocityX, velocityY, yaw)
  86. if faultLabel != "" {
  87. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  88. break
  89. }
  90. }
  91. subscribersMutexes[i].Unlock()
  92. }
  93. subscribersTimeMutexes[i].Unlock()
  94. },
  95. })
  96. }
  97. if topic == masterConfig.TopicOfDataRead {
  98. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  99. Node: commonConfig.RosNode,
  100. Topic: topic,
  101. Callback: func(data *kinglong_msgs.Retrieval) {
  102. if len(masterConfig.TopicOfDataRead) == 0 {
  103. c_log.GlobalLogger.Infof("话题 %v 没有触发器", topic)
  104. return
  105. }
  106. subscribersTimeMutexes[i].Lock()
  107. if time.Since(subscribersTimes[i]).Seconds() > 1 {
  108. subscribersMutexes[i].Lock()
  109. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  110. lastTimeWindow := entity.GetLastTimeWindow() // 获取最后一个时间窗口
  111. var faultLabel string
  112. for _, f := range masterConfig.RuleOfDataRead {
  113. faultLabel = f(data)
  114. if faultLabel != "" {
  115. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  116. subscribersTimes[i] = time.Now()
  117. break
  118. }
  119. }
  120. subscribersMutexes[i].Unlock()
  121. }
  122. subscribersTimeMutexes[i].Unlock()
  123. },
  124. })
  125. }
  126. if err != nil {
  127. c_log.GlobalLogger.Info("创建订阅者报错:", err)
  128. //TODO 如何回传日志
  129. continue
  130. }
  131. }
  132. ////创建订阅者0订阅主题 nodefault_info
  133. //c_log.GlobalLogger.Info("创建订阅者订阅话题 ", masterConfig.TopicOfNodeFaultInfo)
  134. //subscriber0, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
  135. // Node: commonConfig.RosNode,
  136. // Topic: masterConfig.TopicOfNodeFaultInfo,
  137. // Callback: func(data *kinglong_msgs.FaultInfo) {
  138. // if len(masterConfig.RuleOfNodefaultInfo) == 0 {
  139. // //c_log.GlobalLogger.Info("话题 nodefault_info没有触发器")
  140. // return
  141. // }
  142. // entity.Subscriber0TimeMutex.Lock()
  143. // if time.Since(entity.Subscriber0Time).Seconds() > 1 {
  144. // entity.Subscriber0TimeMutex.Unlock()
  145. // subscriber0Mutex.Lock()
  146. // faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  147. // lastTimeWindow := entity.GetLastTimeWindow() // 获取最后一个时间窗口
  148. // var faultLabel string
  149. // for _, f := range masterConfig.RuleOfNodefaultInfo {
  150. // faultLabel = f(data)
  151. // if faultLabel != "" {
  152. // saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  153. // entity.Subscriber0Time = time.Now()
  154. // break
  155. // }
  156. // }
  157. // subscriber0Mutex.Unlock()
  158. // }
  159. // entity.Subscriber0TimeMutex.Unlock()
  160. // }})
  161. //if err != nil {
  162. // c_log.GlobalLogger.Info("创建订阅者发生故障:", err)
  163. // os.Exit(-1)
  164. //}
  165. //// 创建订阅者1订阅主题 cicv_location
  166. //c_log.GlobalLogger.Info("创建订阅者订阅话题 ", masterConfig.TopicOfCicvLocation)
  167. //subscriber1, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
  168. // Node: commonConfig.RosNode,
  169. // Topic: masterConfig.TopicOfCicvLocation,
  170. // Callback: func(data *kinglong_msgs.PerceptionLocalization) {
  171. // m.RLock()
  172. // velocityX = data.VelocityX
  173. // velocityY = data.VelocityY
  174. // yaw = data.Yaw
  175. // m.RUnlock()
  176. //
  177. // if len(masterConfig.RuleOfCicvLocation) == 0 {
  178. // c_log.GlobalLogger.Info("话题 cicv_location 没有触发器")
  179. // return
  180. // }
  181. // entity.Subscriber1TimeMutex.Lock()
  182. // if time.Since(entity.Subscriber1Time).Seconds() > 1 {
  183. // subscriber1Mutex.Lock()
  184. // faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  185. // lastTimeWindow := entity.GetLastTimeWindow() // 获取最后一个时间窗口
  186. // // 更新共享变量
  187. // var faultLabel string
  188. // for _, f := range masterConfig.RuleOfCicvLocation {
  189. // faultLabel = f(data)
  190. // if faultLabel != "" {
  191. // saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  192. // entity.Subscriber1Time = time.Now()
  193. // break
  194. // }
  195. // }
  196. // subscriber1Mutex.Unlock()
  197. // }
  198. // entity.Subscriber1TimeMutex.Unlock()
  199. // },
  200. //})
  201. //if err != nil {
  202. // c_log.GlobalLogger.Info("创建订阅者发生故障:", err)
  203. // os.Exit(-1)
  204. //}
  205. //c_log.GlobalLogger.Info("创建订阅者订阅话题 ", masterConfig.TopicOfTpperception)
  206. //subscriber2, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
  207. // Node: commonConfig.RosNode,
  208. // Topic: masterConfig.TopicOfTpperception,
  209. // Callback: func(data *kinglong_msgs.PerceptionObjects) {
  210. // if len(masterConfig.RuleOfTpperception) == 0 {
  211. // c_log.GlobalLogger.Info("话题 tpperception 没有触发器")
  212. // return
  213. // }
  214. // entity.Subscriber2TimeMutex.Lock()
  215. // // 判断是否是连续故障码
  216. // if time.Since(entity.Subscriber2Time).Seconds() > 1 {
  217. // // 2 不是连续故障码
  218. // subscriber2Mutex.Lock()
  219. // faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  220. // lastTimeWindow := entity.GetLastTimeWindow() // 获取最后一个时间窗口
  221. // var faultLabel string
  222. // //c_log.GlobalLogger.Infof("TTC数据为:【velocityX】=%v,【velocityY】=%v", velocityX, velocityY)
  223. // for _, f := range masterConfig.RuleOfTpperception {
  224. // faultLabel = f(data, velocityX, velocityY, yaw)
  225. // if faultLabel != "" {
  226. // saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  227. // entity.Subscriber2Time = time.Now()
  228. // break
  229. // }
  230. // }
  231. // subscriber2Mutex.Unlock()
  232. // }
  233. // entity.Subscriber2TimeMutex.Unlock()
  234. // }})
  235. //if err != nil {
  236. // c_log.GlobalLogger.Info("创建订阅者2发生故障:", err)
  237. // os.Exit(-1)
  238. //}
  239. //// 创建订阅者3订阅主题 fault_info
  240. //c_log.GlobalLogger.Info("创建订阅者3订阅话题 ", masterConfig.TopicOfFaultInfo)
  241. //subscriber3, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
  242. // Node: commonConfig.RosNode,
  243. // Topic: masterConfig.TopicOfFaultInfo,
  244. // Callback: func(data *kinglong_msgs.FaultVec) {
  245. // if len(masterConfig.RuleOfFaultInfo) == 0 {
  246. // c_log.GlobalLogger.Info("话题 fault_info 没有触发器")
  247. // return
  248. // }
  249. // entity.Subscriber3TimeMutex.Lock()
  250. // if time.Since(entity.Subscriber3Time).Seconds() > 1 {
  251. // // 2 不是连续故障码
  252. // subscriber3Mutex.Lock()
  253. // faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  254. // lastTimeWindow := entity.GetLastTimeWindow() // 获取最后一个时间窗口
  255. // var faultLabel string
  256. // for _, f := range masterConfig.RuleOfFaultInfo {
  257. // faultLabel = f(data)
  258. // if faultLabel != "" {
  259. // saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  260. // entity.Subscriber3Time = time.Now()
  261. // break
  262. // }
  263. // }
  264. // subscriber3Mutex.Unlock()
  265. // }
  266. // entity.Subscriber3TimeMutex.Unlock()
  267. // }})
  268. //if err != nil {
  269. // c_log.GlobalLogger.Info("创建订阅者3发生故障:", err)
  270. // os.Exit(-1)
  271. //}
  272. //// 创建订阅者4订阅主题 data_read
  273. //// TODO 高频率触发
  274. //c_log.GlobalLogger.Info("创建订阅者4订阅话题 ", masterConfig.TopicOfDataRead)
  275. //subscriber4, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
  276. // Node: commonConfig.RosNode,
  277. // Topic: masterConfig.TopicOfDataRead,
  278. // Callback: func(data *kinglong_msgs.Retrieval) {
  279. // if len(masterConfig.RuleOfDataRead) == 0 {
  280. // //c_log.GlobalLogger.Info("话题 data_read 没有触发器")
  281. // return
  282. // }
  283. // entity.Subscriber4TimeMutex.Lock()
  284. // if time.Since(entity.Subscriber4Time).Seconds() > 1 {
  285. // subscriber4Mutex.Lock()
  286. // faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  287. // lastTimeWindow := entity.GetLastTimeWindow() // 获取最后一个时间窗口
  288. // var faultLabel string
  289. // for _, f := range masterConfig.RuleOfDataRead {
  290. // faultLabel = f(data)
  291. // if faultLabel != "" {
  292. // saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  293. // entity.Subscriber4Time = time.Now()
  294. // break
  295. // }
  296. // }
  297. // subscriber4Mutex.Unlock()
  298. // }
  299. // entity.Subscriber4TimeMutex.Unlock()
  300. // },
  301. //})
  302. //if err != nil {
  303. // c_log.GlobalLogger.Info("创建订阅者3发生故障:", err)
  304. // os.Exit(-1)
  305. //}
  306. select {
  307. case signal := <-service.ChannelKillWindowProducer:
  308. if signal == 1 {
  309. commonConfig.RosNode.Close()
  310. service.AddKillTimes("3")
  311. return
  312. }
  313. }
  314. }
  315. func saveTimeWindow(faultLabel string, faultHappenTime string, lastTimeWindow *entity.TimeWindow) {
  316. masterTopics, slaveTopics := getTopicsOfNode(faultLabel)
  317. if lastTimeWindow == nil || util.TimeCustom1GreaterTimeCustom2(faultHappenTime, lastTimeWindow.TimeWindowEnd) {
  318. // 2-1 如果是不在旧故障窗口内,添加一个新窗口
  319. newTimeWindow := entity.TimeWindow{
  320. FaultTime: faultHappenTime,
  321. TimeWindowBegin: util.TimeCustomChange(faultHappenTime, -commonConfig.PlatformConfig.TaskBeforeTime),
  322. TimeWindowEnd: util.TimeCustomChange(faultHappenTime, commonConfig.PlatformConfig.TaskAfterTime),
  323. Length: commonConfig.PlatformConfig.TaskBeforeTime + commonConfig.PlatformConfig.TaskAfterTime + 1,
  324. Labels: []string{faultLabel},
  325. MasterTopics: masterTopics,
  326. SlaveTopics: slaveTopics,
  327. }
  328. c_log.GlobalLogger.Infof("不在旧故障窗口内,向生产者队列添加一个新窗口,【Lable】=%v,【FaultTime】=%v,【Length】=%v", newTimeWindow.Labels, newTimeWindow.FaultTime, newTimeWindow.Length)
  329. entity.AddTimeWindowToTimeWindowProducerQueue(newTimeWindow)
  330. } else {
  331. // 2-2 如果在旧故障窗口内
  332. entity.TimeWindowProducerQueueMutex.RLock()
  333. defer entity.TimeWindowProducerQueueMutex.RUnlock()
  334. // 2-2-1 更新故障窗口end时间
  335. maxEnd := util.TimeCustomChange(lastTimeWindow.TimeWindowBegin, commonConfig.PlatformConfig.TaskMaxTime)
  336. expectEnd := util.TimeCustomChange(faultHappenTime, commonConfig.PlatformConfig.TaskAfterTime)
  337. if util.TimeCustom1GreaterTimeCustom2(expectEnd, maxEnd) {
  338. lastTimeWindow.TimeWindowEnd = maxEnd
  339. lastTimeWindow.Length = commonConfig.PlatformConfig.TaskMaxTime
  340. } else {
  341. if util.TimeCustom1GreaterTimeCustom2(expectEnd, lastTimeWindow.TimeWindowEnd) {
  342. lastTimeWindow.TimeWindowEnd = expectEnd
  343. lastTimeWindow.Length = util.CalculateDifferenceOfTimeCustom(lastTimeWindow.TimeWindowBegin, expectEnd)
  344. }
  345. }
  346. // 2-2-2 更新label
  347. labels := lastTimeWindow.Labels
  348. lastTimeWindow.Labels = util.AppendIfNotExists(labels, faultLabel)
  349. // 2-2-3 更新 topic
  350. sourceMasterTopics := lastTimeWindow.MasterTopics
  351. lastTimeWindow.MasterTopics = util.MergeSlice(sourceMasterTopics, masterTopics)
  352. sourceSlaveTopics := lastTimeWindow.SlaveTopics
  353. lastTimeWindow.SlaveTopics = util.MergeSlice(sourceSlaveTopics, slaveTopics)
  354. c_log.GlobalLogger.Infof("在旧故障窗口内,更新生产者队列最新的窗口,【Lable】=%v,【FaultTime】=%v,【Length】=%v", lastTimeWindow.Labels, lastTimeWindow.FaultTime, lastTimeWindow.Length)
  355. }
  356. }
  357. func getTopicsOfNode(faultLabel string) (masterTopics []string, slaveTopics []string) {
  358. // 获取所有需要采集的topic
  359. var faultCodeTopics []string
  360. for _, code := range commonConfig.CloudConfig.Triggers {
  361. if code.Label == faultLabel {
  362. faultCodeTopics = code.Topics
  363. }
  364. }
  365. // 根据不同节点采集的topic进行分配采集
  366. for _, acceptTopic := range faultCodeTopics {
  367. for _, host := range commonConfig.CloudConfig.Hosts {
  368. for _, topic := range host.Topics {
  369. if host.Name == commonConfig.CloudConfig.Hosts[0].Name && acceptTopic == topic {
  370. masterTopics = append(masterTopics, acceptTopic)
  371. }
  372. if host.Name == commonConfig.CloudConfig.Hosts[1].Name && acceptTopic == topic {
  373. slaveTopics = append(slaveTopics, acceptTopic)
  374. }
  375. }
  376. }
  377. }
  378. return masterTopics, slaveTopics
  379. }