forked from phsm/cameraleech
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
119 lines (99 loc) · 2.98 KB
/
http.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/gorilla/mux"
)
type jsonCameraListReply struct {
Data []jsonNameEntry `json:"data"`
}
type jsonNameEntry struct {
Name string `json:"{#CAMERA}"`
}
func newRouter() *mux.Router {
httpRouter := mux.NewRouter()
httpRouter.HandleFunc("/cameras.json", zabbixAutodiscoveryCamerasListJSON)
httpRouter.HandleFunc("/camera/{name}/frame", cameraFrame)
httpRouter.HandleFunc("/camera/{name}/fps", cameraFps)
httpRouter.HandleFunc("/camera/{name}/bitrate", cameraBitrate)
httpRouter.HandleFunc("/camera/{name}/outtime", cameraOutTime)
httpRouter.HandleFunc("/camera/{name}/dupframes", cameraDupFrames)
httpRouter.HandleFunc("/camera/{name}/dropframes", cameraDropFrames)
return httpRouter
}
func zabbixAutodiscoveryCamerasListJSON(w http.ResponseWriter, r *http.Request) {
reply := jsonCameraListReply{}
reply.Data = make([]jsonNameEntry, 0, 1024)
for _, leech := range leeches {
cam := jsonNameEntry{Name: leech.Config.Name}
reply.Data = append(reply.Data, cam)
}
json, err := json.MarshalIndent(reply, "", "\t")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, string(json))
}
func cameraFrame(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
camName := vars["name"]
leech, ok := leeches[camName]
if !ok {
http.Error(w, fmt.Sprintf("Didn't find camera \"%s\"", camName), http.StatusNotFound)
return
}
fmt.Fprintf(w, "%d", leech.Stats.Frame)
}
func cameraFps(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
camName := vars["name"]
leech, ok := leeches[camName]
if !ok {
http.Error(w, fmt.Sprintf("Didn't find camera \"%s\"", camName), http.StatusNotFound)
return
}
fmt.Fprintf(w, "%f", leech.Stats.Fps)
}
func cameraBitrate(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
camName := vars["name"]
leech, ok := leeches[camName]
if !ok {
http.Error(w, fmt.Sprintf("Didn't find camera \"%s\"", camName), http.StatusNotFound)
return
}
fmt.Fprintf(w, "%d", leech.Stats.Bitrate)
}
func cameraOutTime(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
camName := vars["name"]
leech, ok := leeches[camName]
if !ok {
http.Error(w, fmt.Sprintf("Didn't find camera \"%s\"", camName), http.StatusNotFound)
return
}
fmt.Fprintf(w, "%d", leech.Stats.OutTime/1000000)
}
func cameraDupFrames(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
camName := vars["name"]
leech, ok := leeches[camName]
if !ok {
http.Error(w, fmt.Sprintf("Didn't find camera \"%s\"", camName), http.StatusNotFound)
return
}
fmt.Fprintf(w, "%d", leech.Stats.DupFrames)
}
func cameraDropFrames(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
camName := vars["name"]
leech, ok := leeches[camName]
if !ok {
http.Error(w, fmt.Sprintf("Didn't find camera \"%s\"", camName), http.StatusNotFound)
return
}
fmt.Fprintf(w, "%d", leech.Stats.DropFrames)
}