liuxiaolong
2020-08-06 dbc843d0b37f786fb816131bcc7ebca86dbe72e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package controllers
 
import (
    "car-service/extend/code"
    "car-service/models"
    "car-service/service"
    "fmt"
    "github.com/astaxie/beego"
    "net/http"
    "sort"
    "sync"
    "time"
)
 
type CarController struct {
    beego.Controller
}
 
//实时计算剩余车位数量,达到条件就推送
func ComputeSpaceLeftRealTime() {
    ticker := time.NewTicker(3 * time.Second)
    prePushLeft := 0
    sv := service.NewCarService()
    initCacheM := false
    for {
        select {
            case <-ticker.C:
                hikSta, flag := sv.Statistic()
                if flag {
                    left := hikSta.Left
                    if !initCacheM {
                        models.SetSpaceNo(hikSta.TotalPermPlace)
                        initCacheM = true
                    }
                    if left <=5 && left != prePushLeft {
                        go func() {
                            message := fmt.Sprintf("%s 剩余车位:%d个", time.Now().Format("2006-01-02 15:04:05"), left)
                            b, e := service.Push("育英智慧停车", message)
 
                            prePushLeft = left
 
                            fmt.Println("b:", b,"e:",e, "message:", message)
                        }()
                    }
                    updateSpaceLeft(left)
                }
 
        default:
            time.Sleep(500 * time.Millisecond)
        }
    }
}
 
var cacheSpaceLeft int
var cLock sync.RWMutex
func updateSpaceLeft(num int) {
    cLock.Lock()
    defer cLock.Unlock()
    cacheSpaceLeft = num
}
func getSpaceLeft() int {
    cLock.Lock()
    defer cLock.Unlock()
    return cacheSpaceLeft
}
 
// @Title 统计剩余车位
// @Description 统计剩余车位
// @Success 200 {object} models.CarStatistic
// @Failure 403 {string} json ""
// @router /statistic [get]
func (c *CarController) Statistic() {
    //sv := service.NewCarService()
    //hikStc := sv.Statistic()
    //left := hikStc.Left
    left := getSpaceLeft()
    sta := models.CarStatistic{
        Left: left,
    }
    resp := code.Code{
        Success: true,
        Status: http.StatusOK,
        Data: sta,
    }
    c.Data["json"] = resp
    c.ServeJSON()
}
 
// @Title 根据车牌号查找车位信息
// @Description 根据车牌号查找车位信息
// @Success 200 {object} models.CarPos
// @Failure 403 {string} json ""
// @router /carPos [get]
func (c *CarController) findCar() {
 
}
 
// @Title 车位占用情况
// @Description 车位占用情况
// @Success 200 {object} models.PosResult
// @Failure 403 {string} json ""
// @router /spaceNo [get]
func (c *CarController) SpaceNo() {
    userId := c.GetString("userId")
    sv := service.NewCarService()
    spaceNos := sv.FindSpaceNo(userId)
    sort.Sort(spaceNos)
    resp := code.Code{
        Success: true,
        Status: http.StatusOK,
        Data: spaceNos,
    }
    c.Data["json"] = resp
    c.ServeJSON()
}
 
 
func (c *CarController) BindCarSpace() {
 
    c.ServeJSON()
}
 
// @router /crossRecord [get]
func (c *CarController) CrossRecord() {
    sv := service.NewCarService()
    records := sv.CrossRecords()
    resp := code.Code{
        Success: true,
        Status:  http.StatusOK,
        Data:    records,
    }
    c.Data["json"] = resp
    c.ServeJSON()
}