OutOperationZone.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package main
  2. import (
  3. "cicv-data-closedloop/pjisuv_ticker"
  4. "fmt"
  5. "sync"
  6. "time"
  7. )
  8. type Point struct {
  9. x, y float64
  10. }
  11. var (
  12. vertices = []Point{
  13. {x: 456095.447, y: 4397845.34},
  14. {x: 456445.083, y: 4398067.52},
  15. {x: 456536.143, y: 4397909.14},
  16. {x: 456168.902, y: 4397709.52},
  17. }
  18. )
  19. func isPointInPolygon(p Point, vertices []Point) bool {
  20. intersections := 0
  21. for i := 0; i < len(vertices); i++ {
  22. j := (i + 1) % len(vertices)
  23. if rayIntersectsSegment(p, vertices[i], vertices[j]) {
  24. intersections++
  25. }
  26. }
  27. return intersections%2 == 1
  28. }
  29. func rayIntersectsSegment(p, p1, p2 Point) bool {
  30. if p1.y > p2.y {
  31. p1, p2 = p2, p1
  32. }
  33. if p.y == p1.y || p.y == p2.y {
  34. p.y += 0.0001
  35. }
  36. if p.y < p1.y || p.y > p2.y {
  37. return false
  38. }
  39. if p.x > max(p1.x, p2.x) {
  40. return false
  41. }
  42. if p.x < min(p1.x, p2.x) {
  43. return true
  44. }
  45. blueSlope := (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y) + p1.x
  46. return p.x < blueSlope
  47. }
  48. func min(a, b float64) float64 {
  49. if a < b {
  50. return a
  51. }
  52. return b
  53. }
  54. func max(a, b float64) float64 {
  55. if a > b {
  56. return a
  57. }
  58. return b
  59. }
  60. // 定时任务触发器固定的
  61. func Topic() string {
  62. return pjisuv_ticker.TickerTopic
  63. }
  64. // ******* 禁止存在下划线_
  65. // 触发器标记
  66. func Label() string {
  67. return "HuaLong"
  68. }
  69. func Rule(shareVars *sync.Map) {
  70. defer func() {
  71. if r := recover(); r != nil {
  72. fmt.Println("Recovered from panic:", r)
  73. }
  74. }()
  75. // 1 使用goroutine
  76. go func(shareVars *sync.Map) {
  77. // 2 定义触发器的间隔时间
  78. ticker := time.NewTicker(time.Duration(2) * time.Second)
  79. defer ticker.Stop()
  80. // 3 运行一个无限循环
  81. for {
  82. select {
  83. // 定时器触发时执行的代码
  84. case <-ticker.C:
  85. FinalCallback(shareVars)
  86. }
  87. }
  88. }(shareVars)
  89. }
  90. func FinalCallback(shareVars *sync.Map) {
  91. PositionX, ok1 := shareVars.Load("PositionXOfCicvLocation")
  92. PositionY, ok2 := shareVars.Load("PositionYOfCicvLocation")
  93. AbsSpeed, ok3 := shareVars.Load("AbsSpeed")
  94. if ok1 && ok2 && ok3 {
  95. p := Point{x: PositionX.(float64), y: PositionY.(float64)}
  96. if !isPointInPolygon(p, vertices) && AbsSpeed.(float64) >= 1 {
  97. eventLabel := "OutOperationZone"
  98. fmt.Println(eventLabel)
  99. pjisuv_ticker.TickerChan <- pjisuv_ticker.TickInfo{FaultLabel: Label(), FaultHappenTime: pjisuv_ticker.GetNowTimeCustom()}
  100. }
  101. }
  102. }