Cadence.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package main
  2. import (
  3. "cicv-data-closedloop/pjisuv_ticker"
  4. "fmt"
  5. "sync"
  6. "time"
  7. )
  8. var (
  9. threshold float64 = 0.4
  10. )
  11. // 定时任务触发器固定的
  12. func Topic() string {
  13. return pjisuv_ticker.TickerTopic
  14. }
  15. // ******* 禁止存在下划线_
  16. // 触发器标记
  17. func Label() string {
  18. return "Cadence"
  19. }
  20. func Rule(shareVars *sync.Map) {
  21. defer func() {
  22. if r := recover(); r != nil {
  23. fmt.Println("Recovered from panic:", r)
  24. }
  25. }()
  26. // 1 使用goroutine
  27. go func(shareVars *sync.Map) {
  28. // 2 定义触发器的间隔时间
  29. ticker := time.NewTicker(time.Duration(2) * time.Second)
  30. defer ticker.Stop()
  31. // 3 运行一个无限循环
  32. for {
  33. select {
  34. // 定时器触发时执行的代码
  35. case <-ticker.C:
  36. FinalCallback(shareVars)
  37. }
  38. }
  39. }(shareVars)
  40. }
  41. func countChanges(slice []float64) int {
  42. count := 0
  43. lable1:
  44. for i := 0; i < len(slice); {
  45. if slice[i] <= -threshold || slice[i] > threshold {
  46. for j := 0; j < len(slice)-i-1; j++ {
  47. if (slice[i] <= -threshold && slice[1+i+j] >= threshold) || (slice[i] >= threshold && slice[1+i+j] <= -threshold) {
  48. count++
  49. i = i + j + 1
  50. continue lable1
  51. }
  52. }
  53. break lable1
  54. } else {
  55. i++
  56. }
  57. }
  58. return count
  59. }
  60. func FinalCallback(shareVars *sync.Map) {
  61. OutsideWorkshopFlag, ok := shareVars.Load("OutsideWorkshopFlag")
  62. AccelXSlice, ok1 := shareVars.Load("AccelXSlice")
  63. if ok && ok1 && OutsideWorkshopFlag.(bool) == true {
  64. count := countChanges(AccelXSlice.([]float64))
  65. if count >= 3 {
  66. event_lable := "Cadence"
  67. fmt.Println(event_lable)
  68. pjisuv_ticker.TickerChan <- pjisuv_ticker.TickInfo{FaultLabel: Label(), FaultHappenTime: pjisuv_ticker.GetNowTimeCustom()}
  69. }
  70. if len(AccelXSlice.([]float64)) >= 500 {
  71. AccelXSlice = []float64{}
  72. shareVars.Store("AccelXSlice", AccelXSlice)
  73. }
  74. }
  75. }