LongTimeParallel.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package main
  2. import (
  3. "cicv-data-closedloop/pjisuv_ticker"
  4. "fmt"
  5. "math"
  6. "sync"
  7. "time"
  8. )
  9. var (
  10. Maxlenobj int32 = 0
  11. )
  12. // 定时任务触发器固定的
  13. func Topic() string {
  14. return pjisuv_ticker.TickerTopic
  15. }
  16. // ******* 禁止存在下划线_
  17. // 触发器标记
  18. func Label() string {
  19. return "LongTimeParallel"
  20. }
  21. func Rule(shareVars *sync.Map) {
  22. defer func() {
  23. if r := recover(); r != nil {
  24. fmt.Println("Recovered from panic:", r)
  25. }
  26. }()
  27. // 1 使用goroutine
  28. go func(shareVars *sync.Map) {
  29. // 2 定义触发器的间隔时间
  30. ticker := time.NewTicker(time.Duration(2) * time.Second)
  31. defer ticker.Stop()
  32. // 3 运行一个无限循环
  33. for {
  34. select {
  35. // 定时器触发时执行的代码
  36. case <-ticker.C:
  37. FinalCallback(shareVars)
  38. }
  39. }
  40. }(shareVars)
  41. }
  42. func getMaxValue(slice []int) int {
  43. max := slice[0]
  44. for _, value := range slice {
  45. if value > max {
  46. max = value
  47. }
  48. }
  49. return max
  50. }
  51. func isParallel(ObjectList [][]float32) bool {
  52. numParallelslice := []int{0}
  53. numParallel := 0
  54. lable1:
  55. for i := 0; i < len(ObjectList[1]); i++ {
  56. objx := ObjectList[0][i]
  57. objy := ObjectList[1][i]
  58. if math.Abs(float64(objx)) <= 2 && math.Abs(float64(objy)) <= 4.5 {
  59. for j := 0; j < len(ObjectList[1])-i-1; j++ {
  60. objxj := ObjectList[0][1+i+j]
  61. objyj := ObjectList[1][1+i+j]
  62. if math.Abs(float64(objxj)) <= 2 && math.Abs(float64(objyj)) <= 4.5 {
  63. numParallel++
  64. } else {
  65. i = i + j + 1
  66. numParallelslice = append(numParallelslice, numParallel)
  67. numParallel = 0
  68. continue lable1
  69. }
  70. }
  71. numParallelslice = append(numParallelslice, numParallel)
  72. break lable1
  73. } else {
  74. i++
  75. }
  76. }
  77. maxValue := getMaxValue(numParallelslice)
  78. fmt.Println(maxValue)
  79. if maxValue >= 100 {
  80. return true
  81. } else {
  82. return false
  83. }
  84. }
  85. func FinalCallback(shareVars *sync.Map) {
  86. OutsideWorkshopFlag, ok := shareVars.Load("OutsideWorkshopFlag")
  87. ObjDicOfTpperception, ok1 := shareVars.Load("objDicOfTpperception")
  88. ObjDic := ObjDicOfTpperception.(map[uint32][][]float32)
  89. AbsSpeed, _ := shareVars.Load("AbsSpeed")
  90. if ok && ok1 && OutsideWorkshopFlag.(bool) == true {
  91. for _, objValue := range ObjDic {
  92. Maxlenobj = max(Maxlenobj, int32(len(objValue[0])))
  93. if len(ObjDic[0]) <= 10 || !isParallel(objValue) && AbsSpeed.(float64) >= 1 {
  94. continue
  95. }
  96. event_lable := "LongTimeParallel"
  97. fmt.Println(event_lable)
  98. pjisuv_ticker.TickerChan <- pjisuv_ticker.TickInfo{FaultLabel: Label(), FaultHappenTime: pjisuv_ticker.GetNowTimeCustom()}
  99. }
  100. if Maxlenobj >= 100 {
  101. ObjDicOfTpperception = make(map[uint32][][]float32)
  102. shareVars.Store("ObjDicOfTpperception", ObjDicOfTpperception)
  103. Maxlenobj = 0
  104. }
  105. }
  106. }