produce_window.go 11 KB

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