produce_window.go 8.2 KB

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