-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandlers.go
More file actions
119 lines (96 loc) · 2.33 KB
/
Handlers.go
File metadata and controls
119 lines (96 loc) · 2.33 KB
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"
"io"
"io/ioutil"
"net/http"
"strconv"
"github.com/gorilla/mux"
)
func Index(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(config)
}
func GetControllers(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(config.ControllerList)
}
func GetControllersById(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
controllerId, _ := strconv.Atoi(vars["controllerid"])
var controller Controller
found := 0
for i := 0; i < len(config.ControllerList); i++ {
if config.ControllerList[i].ID == controllerId {
controller = config.ControllerList[i]
found = 1
break
}
}
if found == 1 {
json.NewEncoder(w).Encode(controller)
} else {
fmt.Fprintf(w, "Controller not found")
}
}
func GetLEDs(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(config.LEDs)
}
func GetLEDByIndex(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
ledIndex, _ := strconv.Atoi(vars["ledindex"])
if ledIndex >= 10460 {
fmt.Fprintf(w, "LED not found")
} else {
json.NewEncoder(w).Encode(config.LEDs[ledIndex])
}
}
func SetLEDs(w http.ResponseWriter, r *http.Request) {
var LEDs [][]float32
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
if err != nil {
panic(err)
}
if err := r.Body.Close(); err != nil {
panic(err)
}
if err := json.Unmarshal(body, &LEDs); err != nil {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
w.WriteHeader(422)
if err := json.NewEncoder(w).Encode(err); err != nil {
panic(err)
}
} else {
if len(LEDs) == 10460 {
config.LEDs = LEDs
SendLed(config.LEDs)
}
}
if err := json.NewEncoder(w).Encode(LEDs); err != nil {
panic(err)
}
}
func SetLEDByIndex(w http.ResponseWriter, r *http.Request) {
var LED LEDWithId
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
if err != nil {
panic(err)
}
if err := r.Body.Close(); err != nil {
panic(err)
}
if err := json.Unmarshal(body, &LED); err != nil {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
w.WriteHeader(422)
if err := json.NewEncoder(w).Encode(err); err != nil {
panic(err)
}
} else {
if len(LED.LED) == 3 {
config.LEDs[LED.ID] = LED.LED
SendLed(config.LEDs)
}
}
if err := json.NewEncoder(w).Encode(LED); err != nil {
panic(err)
}
}