-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.go
127 lines (101 loc) · 2.6 KB
/
routes.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
package xapper
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"github.com/gorilla/mux"
)
func Router() *mux.Router {
r := mux.NewRouter()
r.HandleFunc("/", index)
r.HandleFunc("/{device}", State)
r.HandleFunc("/{device}/{group}/{channel}/gain", GainAdjust)
r.HandleFunc("/{device}/{group}/{channel}/mute", Mute)
return r
}
func index(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
}
func device(vars map[string]string) (*Device, error) {
id, err := strconv.Atoi(vars["device"])
if err != nil {
return nil, err
}
if id < 0 || id > 8 {
return nil, errors.New("device id out of range")
}
return Global.Devices[id], nil
}
func State(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
vars := mux.Vars(r)
d, err := device(vars)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
for _, channels := range d.Channels {
for _, ch := range channels {
ch.mu.RLock()
defer ch.mu.RUnlock()
}
}
json.NewEncoder(w).Encode(d)
}
func GainAdjust(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
vars := mux.Vars(r)
d, err := device(vars)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
value, err := strconv.ParseInt(r.FormValue("value"), 10, 32)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
channelId, err := strconv.Atoi(vars["channel"])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ch := d.Channels[Group(vars["group"])][channelId-1]
gain, err := ch.SetGain(float32(value))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprint(w, gain)
}
func Mute(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
vars := mux.Vars(r)
d, err := device(vars)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
mute, err := strconv.ParseBool(r.FormValue("value"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
channelId, err := strconv.Atoi(vars["channel"])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ch := d.Channels[Group(vars["group"])][channelId-1]
muted, err := ch.Mute(mute)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write([]byte(strconv.FormatBool(muted)))
}