GearJump.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 "GearJump"
  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(2) * 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 []int16) int {
  39. count := 0
  40. for i := 0; i < len(slice)-1; i++ {
  41. if slice[i] != slice[i+1] {
  42. count++
  43. }
  44. }
  45. return count
  46. }
  47. func FinalCallback(shareVars *sync.Map) {
  48. OutsideWorkshopFlag, ok := shareVars.Load("OutsideWorkshopFlag")
  49. GearPosSlice, ok1 := shareVars.Load("GearPosSlice")
  50. if ok && ok1 && OutsideWorkshopFlag.(bool) == true {
  51. count := countChanges(GearPosSlice.([]int16))
  52. if count >= 3 {
  53. event_lable := "GearJump"
  54. fmt.Println(event_lable)
  55. pjisuv_ticker.TickerChan <- pjisuv_ticker.TickInfo{FaultLabel: Label(), FaultHappenTime: pjisuv_ticker.GetNowTimeCustom()}
  56. }
  57. GearPosSlice = []int16{}
  58. shareVars.Store("GearPosSlice", GearPosSlice)
  59. }
  60. }