FrequentStartsAndStops.go 1.6 KB

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