1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- package main
- import (
- "cicv-data-closedloop/pjisuv_ticker"
- "fmt"
- "sync"
- "time"
- )
- // 定时任务触发器固定的
- func Topic() string {
- return pjisuv_ticker.TickerTopic
- }
- // ******* 禁止存在下划线_
- // 触发器标记
- func Label() string {
- return "GearJump"
- }
- func Rule(shareVars *sync.Map) {
- defer func() {
- if r := recover(); r != nil {
- fmt.Println("Recovered from panic:", r)
- }
- }()
- // 1 使用goroutine
- go func(shareVars *sync.Map) {
- // 2 定义触发器的间隔时间
- ticker := time.NewTicker(time.Duration(2) * time.Second)
- defer ticker.Stop()
- // 3 运行一个无限循环
- for {
- select {
- // 定时器触发时执行的代码
- case <-ticker.C:
- FinalCallback(shareVars)
- }
- }
- }(shareVars)
- }
- func countChanges(slice []int16) int {
- count := 0
- for i := 0; i < len(slice)-1; i++ {
- if slice[i] != slice[i+1] {
- count++
- }
- }
- return count
- }
- func FinalCallback(shareVars *sync.Map) {
- OutsideWorkshopFlag, ok := shareVars.Load("OutsideWorkshopFlag")
- GearPosSlice, ok1 := shareVars.Load("GearPosSlice")
- if ok && ok1 && OutsideWorkshopFlag.(bool) == true {
- count := countChanges(GearPosSlice.([]int16))
- if count >= 3 {
- event_lable := "GearJump"
- fmt.Println(event_lable)
- pjisuv_ticker.TickerChan <- pjisuv_ticker.TickInfo{FaultLabel: Label(), FaultHappenTime: pjisuv_ticker.GetNowTimeCustom()}
- }
- GearPosSlice = []int16{}
- shareVars.Store("GearPosSlice", GearPosSlice)
- }
- }
|