This repository has been archived by the owner on Dec 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
monitor.go
95 lines (79 loc) · 2.96 KB
/
monitor.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
package www
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/markdaws/gohome/pkg/gohome"
)
// RegisterMonitorHandlers registers all of the monitor specific REST API routes
func RegisterMonitorHandlers(r *mux.Router, s *Server) {
//TODO: Need a way to check the SID used for the user against the current valid
//SIDs and make sure it has not expired, otherwise someone can listen forever
wsHelper := NewWSHelper(s.system.Services.Monitor, s.system.Services.EvtBus)
// Clients call to subscribe to items, api returns a monitorID that can then be used
// to subscribe and unsubscribe to notifications
r.HandleFunc("/v1/monitor/groups", apiSubscribeHandler(s.system, wsHelper)).Methods("POST")
// extends the timeout period for a monitor groups
r.HandleFunc("/v1/monitor/groups/{monitorID}", apiRefreshSubscribeHandler(s.system, wsHelper)).Methods("PUT")
// deletes a monitor group
r.HandleFunc("/v1/monitor/groups/{monitorID}", apiUnsubscribeHandler(s.system, wsHelper)).Methods("DELETE")
// web socket for receiving new events
r.HandleFunc("/v1/monitor/groups/{monitorID}", wsHelper.HTTPHandler())
}
func apiUnsubscribeHandler(system *gohome.System, wsHelper *WSHelper) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
monitorID := mux.Vars(r)["monitorID"]
if _, ok := system.Services.Monitor.Group(monitorID); !ok {
respBadRequest("monitorID is invalid", w)
return
}
system.Services.Monitor.Unsubscribe(monitorID)
w.WriteHeader(http.StatusOK)
}
}
func apiRefreshSubscribeHandler(system *gohome.System, wsHelper *WSHelper) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
monitorID := mux.Vars(r)["monitorID"]
if _, ok := system.Services.Monitor.Group(monitorID); !ok {
respBadRequest("monitorID is invalid", w)
return
}
system.Services.Monitor.SubscribeRenew(monitorID)
w.WriteHeader(http.StatusOK)
}
}
func apiSubscribeHandler(system *gohome.System, wsHelper *WSHelper) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1024*1024))
if err != nil {
respBadRequest("Content length too long, max length 1024 bytes", w)
return
}
var groupJSON jsonMonitorGroup
if err = json.Unmarshal(body, &groupJSON); err != nil {
respBadRequest("Content is not valid JSON", w)
return
}
group := &gohome.MonitorGroup{
Timeout: time.Duration(groupJSON.TimeoutInSeconds) * time.Second,
Features: make(map[string]bool),
Handler: wsHelper,
}
for _, featureID := range groupJSON.FeatureIDs {
group.Features[featureID] = true
}
mID, err := system.Services.Monitor.Subscribe(group, false)
if err != nil {
respBadRequest("Invalid input, unable to subscribe", w)
return
}
resp(apiResponse{
Data: &struct {
MonitorID string `json:"monitorId"`
}{MonitorID: mID},
}, w)
}
}