package main import ( "cicv-data-closedloop/pjisuv_ticker" "fmt" "sync" "time" ) type Point struct { x, y float64 } var ( vertices = []Point{ {x: 456095.447, y: 4397845.34}, {x: 456445.083, y: 4398067.52}, {x: 456536.143, y: 4397909.14}, {x: 456168.902, y: 4397709.52}, } ) func isPointInPolygon(p Point, vertices []Point) bool { intersections := 0 for i := 0; i < len(vertices); i++ { j := (i + 1) % len(vertices) if rayIntersectsSegment(p, vertices[i], vertices[j]) { intersections++ } } return intersections%2 == 1 } func rayIntersectsSegment(p, p1, p2 Point) bool { if p1.y > p2.y { p1, p2 = p2, p1 } if p.y == p1.y || p.y == p2.y { p.y += 0.0001 } if p.y < p1.y || p.y > p2.y { return false } if p.x > max(p1.x, p2.x) { return false } if p.x < min(p1.x, p2.x) { return true } blueSlope := (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y) + p1.x return p.x < blueSlope } func min(a, b float64) float64 { if a < b { return a } return b } func max(a, b float64) float64 { if a > b { return a } return b } // 定时任务触发器固定的 func Topic() string { return pjisuv_ticker.TickerTopic } // ******* 禁止存在下划线_ // 触发器标记 func Label() string { return "HuaLong" } 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 FinalCallback(shareVars *sync.Map) { PositionX, ok1 := shareVars.Load("PositionXOfCicvLocation") PositionY, ok2 := shareVars.Load("PositionYOfCicvLocation") AbsSpeed, ok3 := shareVars.Load("AbsSpeed") if ok1 && ok2 && ok3 { p := Point{x: PositionX.(float64), y: PositionY.(float64)} if !isPointInPolygon(p, vertices) && AbsSpeed.(float64) >= 1 { eventLabel := "OutOperationZone" fmt.Println(eventLabel) pjisuv_ticker.TickerChan <- pjisuv_ticker.TickInfo{FaultLabel: Label(), FaultHappenTime: pjisuv_ticker.GetNowTimeCustom()} } } }