/
manage.go
165 lines (138 loc) · 3.66 KB
/
manage.go
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright (c) 2017-2019 Carnegie Mellon University. All rights reserved.
// Use of this source code is governed by BSD 3-clause license.
package main
import (
"net"
"net/http"
"time"
mux "github.com/gorilla/mux"
)
type cameraMonitor struct {
CameraInfos []cameraInfo
RefreshInterval time.Duration
ReqsChan chan cameraStatusRequest
ExitChan chan bool
DoneChan chan bool
}
var monitor *cameraMonitor
type cameraStatusRequest struct {
Name string
Resps chan cameraStatusResponse
}
type cameraInfo struct {
Name string
Host string
Port string
}
type cameraStatus struct {
Name string `json:"name"`
Status string `json:"status"`
ErrorMsg string `json:"error_message,omit_empty"`
Timestamp time.Time `json:"timestamp"`
}
type cameraStatusResponse struct {
StatusList []cameraStatus `json:"camera_list"`
}
func checkConnectivity(cameraURL string) (string, string) {
// resolve address
_, err := net.ResolveTCPAddr("tcp", cameraURL)
if err != nil {
return "R", err.Error()
}
// 1 second timeout
conn, err := net.DialTimeout("tcp", cameraURL, time.Duration(1)*time.Second)
if err != nil {
return "C", err.Error()
}
conn.Close()
return "S", "Online"
}
func asyncCheckConnectivity(info cameraInfo, statusChan chan<- cameraStatus) {
code, msg := checkConnectivity(net.JoinHostPort(info.Host, info.Port))
status := cameraStatus{
Name: info.Name,
Status: code,
ErrorMsg: msg,
Timestamp: time.Now(),
}
statusChan <- status
}
func (m *cameraMonitor) RunMonitor() {
statusChan := make(chan cameraStatus)
cameraStatusDict := make(map[string]cameraStatus)
for _, info := range m.CameraInfos {
cameraStatusDict[info.Name] = cameraStatus{
Name: info.Name,
Status: "U",
ErrorMsg: "Unknown: not checked yet",
Timestamp: time.Now(),
}
}
timer := time.NewTimer(m.RefreshInterval)
for _, info := range m.CameraInfos {
go asyncCheckConnectivity(info, statusChan)
}
servingLoop:
for {
select {
case <-m.ExitChan:
break servingLoop
case req := <-m.ReqsChan:
// get all status
statusList := []cameraStatus{}
if req.Name == "" {
for _, status := range cameraStatusDict {
statusList = append(statusList, status)
}
} else if status, ok := cameraStatusDict[req.Name]; ok {
statusList = append(statusList, status)
}
req.Resps <- cameraStatusResponse{
StatusList: statusList,
}
case status := <-statusChan:
cameraStatusDict[status.Name] = status
case <-timer.C:
for _, info := range m.CameraInfos {
go asyncCheckConnectivity(info, statusChan)
}
timer = time.NewTimer(m.RefreshInterval)
}
}
m.DoneChan <- true
}
// GetCameraStatusEndpoint responds to legacy CameraStatus queries
// (POST /camera/status, camera/status/{camera_name}).
func GetCameraStatusEndpoint(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
params := mux.Vars(r)
cameraName, ok := params["camera_name"]
if !ok {
cameraName = ""
}
respChan := make(chan cameraStatusResponse)
monitor.ReqsChan <- cameraStatusRequest{
Name: cameraName,
Resps: respChan,
}
statusResp := <-respChan
resp := GetCameraStatusResponse{
Success: true,
StatusList: statusResp.StatusList,
}
respondWithJSON(w, http.StatusOK, resp)
}
func initializeCameraMonitor(cameraInfos []cameraInfo, refreshInterval int) {
monitor = &cameraMonitor{
CameraInfos: cameraInfos,
RefreshInterval: time.Duration(refreshInterval) * time.Second,
ReqsChan: make(chan cameraStatusRequest),
ExitChan: make(chan bool),
DoneChan: make(chan bool),
}
go monitor.RunMonitor()
}
func exitCameraMonitor() {
monitor.ExitChan <- true
<-monitor.DoneChan
}