produce_window.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. "os"
  12. "sync"
  13. "time"
  14. )
  15. var (
  16. subscriber0Mutex sync.Mutex
  17. subscriber1Mutex sync.Mutex
  18. subscriber2Mutex sync.Mutex
  19. subscriber3Mutex sync.Mutex
  20. subscriber4Mutex sync.Mutex
  21. m sync.RWMutex
  22. velocityX float64
  23. velocityY float64
  24. yaw float64
  25. )
  26. // PrepareTimeWindowProducerQueue 负责监听所有主题并修改时间窗口
  27. func PrepareTimeWindowProducerQueue() {
  28. c_log.GlobalLogger.Info("订阅者 goroutine,启动。")
  29. //创建订阅者0订阅主题 nodefault_info
  30. c_log.GlobalLogger.Info("创建订阅者订阅话题 ", masterConfig.TopicOfNodeFaultInfo)
  31. subscriber0, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
  32. Node: commonConfig.RosNode,
  33. Topic: masterConfig.TopicOfNodeFaultInfo,
  34. Callback: func(data *kinglong_msgs.FaultInfo) {
  35. if len(masterConfig.RuleOfNodefaultInfo) == 0 {
  36. //c_log.GlobalLogger.Info("话题 nodefault_info没有触发器")
  37. return
  38. }
  39. entity.Subscriber0TimeMutex.Lock()
  40. if time.Since(entity.Subscriber0Time).Seconds() > 1 {
  41. entity.Subscriber0TimeMutex.Unlock()
  42. subscriber0Mutex.Lock()
  43. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  44. lastTimeWindow := entity.GetLastTimeWindow() // 获取最后一个时间窗口
  45. var faultLabel string
  46. for _, f := range masterConfig.RuleOfNodefaultInfo {
  47. faultLabel = f(data)
  48. if faultLabel != "" {
  49. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  50. entity.Subscriber0Time = time.Now()
  51. break
  52. }
  53. }
  54. subscriber0Mutex.Unlock()
  55. }
  56. entity.Subscriber0TimeMutex.Unlock()
  57. }})
  58. if err != nil {
  59. c_log.GlobalLogger.Info("创建订阅者发生故障:", err)
  60. os.Exit(-1)
  61. }
  62. // 创建订阅者1订阅主题 cicv_location
  63. c_log.GlobalLogger.Info("创建订阅者订阅话题 ", masterConfig.TopicOfCicvLocation)
  64. subscriber1, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
  65. Node: commonConfig.RosNode,
  66. Topic: masterConfig.TopicOfCicvLocation,
  67. Callback: func(data *kinglong_msgs.PerceptionLocalization) {
  68. m.RLock()
  69. velocityX = data.VelocityX
  70. velocityY = data.VelocityY
  71. yaw = data.Yaw
  72. m.RUnlock()
  73. if len(masterConfig.RuleOfCicvLocation) == 0 {
  74. c_log.GlobalLogger.Info("话题 cicv_location 没有触发器")
  75. return
  76. }
  77. entity.Subscriber1TimeMutex.Lock()
  78. if time.Since(entity.Subscriber1Time).Seconds() > 1 {
  79. subscriber1Mutex.Lock()
  80. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  81. lastTimeWindow := entity.GetLastTimeWindow() // 获取最后一个时间窗口
  82. // 更新共享变量
  83. var faultLabel string
  84. for _, f := range masterConfig.RuleOfCicvLocation {
  85. faultLabel = f(data)
  86. if faultLabel != "" {
  87. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  88. entity.Subscriber1Time = time.Now()
  89. break
  90. }
  91. }
  92. subscriber1Mutex.Unlock()
  93. }
  94. entity.Subscriber1TimeMutex.Unlock()
  95. },
  96. })
  97. if err != nil {
  98. c_log.GlobalLogger.Info("创建订阅者发生故障:", err)
  99. os.Exit(-1)
  100. }
  101. c_log.GlobalLogger.Info("创建订阅者订阅话题 ", masterConfig.TopicOfTpperception)
  102. subscriber2, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
  103. Node: commonConfig.RosNode,
  104. Topic: masterConfig.TopicOfTpperception,
  105. Callback: func(data *kinglong_msgs.PerceptionObjects) {
  106. if len(masterConfig.RuleOfTpperception) == 0 {
  107. c_log.GlobalLogger.Info("话题 tpperception 没有触发器")
  108. return
  109. }
  110. entity.Subscriber2TimeMutex.Lock()
  111. // 判断是否是连续故障码
  112. if time.Since(entity.Subscriber2Time).Seconds() > 1 {
  113. // 2 不是连续故障码
  114. subscriber2Mutex.Lock()
  115. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  116. lastTimeWindow := entity.GetLastTimeWindow() // 获取最后一个时间窗口
  117. var faultLabel string
  118. //c_log.GlobalLogger.Infof("TTC数据为:【velocityX】=%v,【velocityY】=%v", velocityX, velocityY)
  119. for _, f := range masterConfig.RuleOfTpperception {
  120. faultLabel = f(data, velocityX, velocityY, yaw)
  121. if faultLabel != "" {
  122. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  123. entity.Subscriber2Time = time.Now()
  124. break
  125. }
  126. }
  127. subscriber2Mutex.Unlock()
  128. }
  129. entity.Subscriber2TimeMutex.Unlock()
  130. }})
  131. if err != nil {
  132. c_log.GlobalLogger.Info("创建订阅者2发生故障:", err)
  133. os.Exit(-1)
  134. }
  135. // 创建订阅者3订阅主题 fault_info
  136. c_log.GlobalLogger.Info("创建订阅者3订阅话题 ", masterConfig.TopicOfFaultInfo)
  137. subscriber3, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
  138. Node: commonConfig.RosNode,
  139. Topic: masterConfig.TopicOfFaultInfo,
  140. Callback: func(data *kinglong_msgs.FaultVec) {
  141. if len(masterConfig.RuleOfFaultInfo) == 0 {
  142. c_log.GlobalLogger.Info("话题 fault_info 没有触发器")
  143. return
  144. }
  145. entity.Subscriber3TimeMutex.Lock()
  146. if time.Since(entity.Subscriber3Time).Seconds() > 1 {
  147. // 2 不是连续故障码
  148. subscriber3Mutex.Lock()
  149. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  150. lastTimeWindow := entity.GetLastTimeWindow() // 获取最后一个时间窗口
  151. var faultLabel string
  152. for _, f := range masterConfig.RuleOfFaultInfo {
  153. faultLabel = f(data)
  154. if faultLabel != "" {
  155. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  156. entity.Subscriber3Time = time.Now()
  157. break
  158. }
  159. }
  160. subscriber3Mutex.Unlock()
  161. }
  162. entity.Subscriber3TimeMutex.Unlock()
  163. }})
  164. if err != nil {
  165. c_log.GlobalLogger.Info("创建订阅者3发生故障:", err)
  166. os.Exit(-1)
  167. }
  168. // 创建订阅者4订阅主题 data_read
  169. // TODO 高频率触发
  170. c_log.GlobalLogger.Info("创建订阅者4订阅话题 ", masterConfig.TopicOfDataRead)
  171. subscriber4, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
  172. Node: commonConfig.RosNode,
  173. Topic: masterConfig.TopicOfDataRead,
  174. Callback: func(data *kinglong_msgs.Retrieval) {
  175. if len(masterConfig.RuleOfDataRead) == 0 {
  176. //c_log.GlobalLogger.Info("话题 data_read 没有触发器")
  177. return
  178. }
  179. entity.Subscriber4TimeMutex.Lock()
  180. if time.Since(entity.Subscriber4Time).Seconds() > 1 {
  181. subscriber4Mutex.Lock()
  182. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  183. lastTimeWindow := entity.GetLastTimeWindow() // 获取最后一个时间窗口
  184. var faultLabel string
  185. for _, f := range masterConfig.RuleOfDataRead {
  186. faultLabel = f(data)
  187. if faultLabel != "" {
  188. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  189. entity.Subscriber4Time = time.Now()
  190. break
  191. }
  192. }
  193. subscriber4Mutex.Unlock()
  194. }
  195. entity.Subscriber4TimeMutex.Unlock()
  196. },
  197. })
  198. if err != nil {
  199. c_log.GlobalLogger.Info("创建订阅者3发生故障:", err)
  200. os.Exit(-1)
  201. }
  202. select {
  203. case signal := <-service.ChannelKillWindowProducer:
  204. if signal == 1 {
  205. defer service.AddKillTimes("3")
  206. subscriber0.Close()
  207. subscriber1.Close()
  208. subscriber2.Close()
  209. subscriber3.Close()
  210. subscriber4.Close()
  211. commonConfig.RosNode.Close()
  212. return
  213. }
  214. }
  215. }
  216. func saveTimeWindow(faultLabel string, faultHappenTime string, lastTimeWindow *entity.TimeWindow) {
  217. masterTopics, slaveTopics := getTopicsOfNode(faultLabel)
  218. if lastTimeWindow == nil || util.TimeCustom1GreaterTimeCustom2(faultHappenTime, lastTimeWindow.TimeWindowEnd) {
  219. // 2-1 如果是不在旧故障窗口内,添加一个新窗口
  220. newTimeWindow := entity.TimeWindow{
  221. FaultTime: faultHappenTime,
  222. TimeWindowBegin: util.TimeCustomChange(faultHappenTime, -commonConfig.PlatformConfig.TaskBeforeTime),
  223. TimeWindowEnd: util.TimeCustomChange(faultHappenTime, commonConfig.PlatformConfig.TaskAfterTime),
  224. Length: commonConfig.PlatformConfig.TaskBeforeTime + commonConfig.PlatformConfig.TaskAfterTime + 1,
  225. Labels: []string{faultLabel},
  226. MasterTopics: masterTopics,
  227. SlaveTopics: slaveTopics,
  228. }
  229. c_log.GlobalLogger.Infof("不在旧故障窗口内,向生产者队列添加一个新窗口,【Lable】=%v,【FaultTime】=%v,【Length】=%v", newTimeWindow.Labels, newTimeWindow.FaultTime, newTimeWindow.Length)
  230. entity.AddTimeWindowToTimeWindowProducerQueue(newTimeWindow)
  231. } else {
  232. // 2-2 如果在旧故障窗口内
  233. entity.TimeWindowProducerQueueMutex.RLock()
  234. defer entity.TimeWindowProducerQueueMutex.RUnlock()
  235. // 2-2-1 更新故障窗口end时间
  236. maxEnd := util.TimeCustomChange(lastTimeWindow.TimeWindowBegin, commonConfig.PlatformConfig.TaskMaxTime)
  237. expectEnd := util.TimeCustomChange(faultHappenTime, commonConfig.PlatformConfig.TaskAfterTime)
  238. if util.TimeCustom1GreaterTimeCustom2(expectEnd, maxEnd) {
  239. lastTimeWindow.TimeWindowEnd = maxEnd
  240. lastTimeWindow.Length = commonConfig.PlatformConfig.TaskMaxTime
  241. } else {
  242. if util.TimeCustom1GreaterTimeCustom2(expectEnd, lastTimeWindow.TimeWindowEnd) {
  243. lastTimeWindow.TimeWindowEnd = expectEnd
  244. lastTimeWindow.Length = util.CalculateDifferenceOfTimeCustom(lastTimeWindow.TimeWindowBegin, expectEnd)
  245. }
  246. }
  247. // 2-2-2 更新label
  248. labels := lastTimeWindow.Labels
  249. lastTimeWindow.Labels = util.AppendIfNotExists(labels, faultLabel)
  250. // 2-2-3 更新 topic
  251. sourceMasterTopics := lastTimeWindow.MasterTopics
  252. lastTimeWindow.MasterTopics = util.MergeSlice(sourceMasterTopics, masterTopics)
  253. sourceSlaveTopics := lastTimeWindow.SlaveTopics
  254. lastTimeWindow.SlaveTopics = util.MergeSlice(sourceSlaveTopics, slaveTopics)
  255. c_log.GlobalLogger.Infof("在旧故障窗口内,更新生产者队列最新的窗口,【Lable】=%v,【FaultTime】=%v,【Length】=%v", lastTimeWindow.Labels, lastTimeWindow.FaultTime, lastTimeWindow.Length)
  256. }
  257. }
  258. func getTopicsOfNode(faultLabel string) (masterTopics []string, slaveTopics []string) {
  259. // 获取所有需要采集的topic
  260. var faultCodeTopics []string
  261. for _, code := range commonConfig.CloudConfig.Triggers {
  262. if code.Label == faultLabel {
  263. faultCodeTopics = code.Topics
  264. }
  265. }
  266. // 根据不同节点采集的topic进行分配采集
  267. for _, acceptTopic := range faultCodeTopics {
  268. for _, host := range commonConfig.CloudConfig.Hosts {
  269. for _, topic := range host.Topics {
  270. if host.Name == commonConfig.CloudConfig.Hosts[0].Name && acceptTopic == topic {
  271. masterTopics = append(masterTopics, acceptTopic)
  272. }
  273. if host.Name == commonConfig.CloudConfig.Hosts[1].Name && acceptTopic == topic {
  274. slaveTopics = append(slaveTopics, acceptTopic)
  275. }
  276. }
  277. }
  278. }
  279. return masterTopics, slaveTopics
  280. }