produce_window.go 8.5 KB

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