-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
98 lines (86 loc) · 2.84 KB
/
handlers.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/julienschmidt/httprouter"
)
// Index handles the default route (GET /).
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "relayctl reporting for duty!\n")
}
// RelayIndex handles the relays index action (GET /relays).
func RelayIndex(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var relays []*Relay
for _, relay := range module {
relays = append(relays, relay)
}
writeResponse(w, http.StatusOK, &JsonResponse{Data: relays})
}
// RelayShow handles the relays show action (GET /relays/:id).
func RelayShow(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
id := params.ByName("id")
relay, err := findRelay(id)
if err != nil {
writeError(w, err)
return
}
writeResponse(w, http.StatusOK, &JsonResponse{Data: relay})
}
// RelayOn handles the relays on action (GET /relays/:id/on).
func RelayOn(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
id := params.ByName("id")
switchRelay(w, id, PIN_ON)
}
// RelayOff handles the relays off action (GET /relays/:id/off).
func RelayOff(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
id := params.ByName("id")
switchRelay(w, id, PIN_OFF)
}
// findRelay locates Relay with the given ID on the module.
func findRelay(id string) (*Relay, error) {
relay, ok := module[id]
if !ok {
// No relay with the ID given in the URL has been found
return nil, ErrRelayNotFound
}
return relay, nil
}
// switchRelay provides unified wrapper for Relay On/Off actions.
func switchRelay(w http.ResponseWriter, id string, mode int) {
relay, err := findRelay(id)
if err != nil {
writeError(w, err)
return
}
if err := setState(id, mode); err != nil {
writeError(w, err)
return
}
writeResponse(w, http.StatusOK, &JsonResponse{Data: relay})
}
// setState switches given pin to requested state.
func setState(id string, mode int) error {
ps := modePinState[mode]
if err := pins[id].Write(ps.value); err != nil {
// Writing requested state to the given pin failed
return ErrStateChangeFailed
}
module[id].State = ps.state
return nil
}
// writeError wraps writing API error response.
func writeError(w http.ResponseWriter, err error) {
status := errHttpStatus[err]
apiError := &ApiError{Status: status, Title: err.Error()}
writeResponse(w, status, &JsonErrorResponse{Error: apiError})
}
// writeResponse writes standard JSON API response with status code.
func writeResponse(w http.ResponseWriter, status int, response interface{}) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(status)
if err := json.NewEncoder(w).Encode(response); err != nil {
apiError := &ApiError{Status: http.StatusInternalServerError, Title: "internal server error"}
writeResponse(w, http.StatusInternalServerError, apiError)
}
}