-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.go
128 lines (107 loc) · 2.91 KB
/
device.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
128
package api
import (
"context"
"io/ioutil"
"net/http"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/render"
"github.com/pkg/errors"
"github.com/smarthut/smarthut/model"
)
// DeviceResponse holds device data
type DeviceResponse struct {
*model.Device
}
func (api *API) deviceCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
device := new(model.Device)
if deviceName := chi.URLParam(r, "device_name"); deviceName != "" {
if err := api.db.One("Name", deviceName, device); err != nil {
handleError(errors.Wrapf(err, "unable to find device with name %s", deviceName), w, r)
return
}
}
ctx := context.WithValue(r.Context(), deviceKey, device)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func (api *API) listDevices(w http.ResponseWriter, r *http.Request) {
var devices []model.Device
if err := api.db.All(&devices); err != nil {
handleError(err, w, r)
return
}
render.JSON(w, r, devices)
}
func (api *API) createDevice(w http.ResponseWriter, r *http.Request) {
var d model.Device
if err := render.DecodeJSON(r.Body, &d); err != nil {
handleError(err, w, r)
return
}
d.CreatedAt = time.Now()
d.UpdatedAt = time.Now()
if err := api.db.Save(&d); err != nil {
handleError(err, w, r)
return
}
render.JSON(w, r, struct{}{})
}
func (api *API) getDevice(w http.ResponseWriter, r *http.Request) {
device := r.Context().Value(deviceKey).(*model.Device)
render.JSON(w, r, DeviceResponse{Device: device})
}
func (api *API) updateDevice(w http.ResponseWriter, r *http.Request) {
device := r.Context().Value(deviceKey).(*model.Device)
var params model.Device
if err := render.DecodeJSON(r.Body, ¶ms); err != nil {
handleError(err, w, r)
return
}
params.ID = device.ID
params.UpdatedAt = time.Now()
if err := api.db.Update(¶ms); err != nil {
handleError(err, w, r)
return
}
render.JSON(w, r, struct{}{})
}
func (api *API) deleteDevice(w http.ResponseWriter, r *http.Request) {
device := r.Context().Value(deviceKey).(*model.Device)
if err := api.db.DeleteStruct(&device); err != nil {
handleError(err, w, r)
return
}
render.JSON(w, r, struct{}{})
}
func (api *API) getSocket(w http.ResponseWriter, r *http.Request) {
device := r.Context().Value(deviceKey).(*model.Device)
resp, err := http.Get(device.Host + "/api/v1")
if err != nil {
handleError(err, w, r)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
handleError(err, w, r)
return
}
w.Write(body)
}
func (api *API) setSocket(w http.ResponseWriter, r *http.Request) {
device := r.Context().Value(deviceKey).(*model.Device)
resp, err := http.Post(device.Host+"/api/v1/socket", "application/json; charset=utf-8", r.Body)
if err != nil {
handleError(err, w, r)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
handleError(err, w, r)
return
}
w.Write(body)
}