produce_window.go 11 KB

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