-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdispenser.go
140 lines (121 loc) · 3.66 KB
/
dispenser.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
129
130
131
132
133
134
135
136
137
138
139
140
package api
import (
"encoding/json"
"fmt"
"github.com/the-lightning-land/sweetd/state"
"net/http"
)
type dispenserUpdateResponse struct {
Id string `json:"id"`
}
type dispenserResponse struct {
Name string `json:"name"`
Api string `json:"api"`
Pos string `json:"pos"`
Version string `json:"version"`
State string `json:"state"`
DispenseOnTouch bool `json:"dispenseOnTouch"`
Update *dispenserUpdateResponse `json:"update"`
}
type patchDispenserOp struct {
Op string `json:"op"`
Name string `json:"name"`
Value interface{} `json:"value"`
}
type patchDispenserRequest []patchDispenserOp
func (a *Handler) getDispenser() *dispenserResponse {
var currentUpdateRes *dispenserUpdateResponse
currentUpdate, err := a.dispenser.GetCurrentUpdate()
if err != nil {
a.log.Errorf("unable to get current update: %v", err)
}
if currentUpdate != nil {
currentUpdateRes = &dispenserUpdateResponse{
Id: currentUpdate.Id,
}
}
return &dispenserResponse{
Name: a.dispenser.GetName(),
Version: a.dispenser.GetVersion(),
Api: a.dispenser.GetApiOnionID(),
Pos: a.dispenser.GetPosOnionID(),
State: state.String(a.dispenser.GetState()),
DispenseOnTouch: a.dispenser.ShouldDispenseOnTouch(),
Update: currentUpdateRes,
}
}
func (a *Handler) handleGetDispenser() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
res := a.getDispenser()
a.jsonResponse(w, res, http.StatusOK)
}
}
func (a *Handler) handlePatchDispenser() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
req := patchDispenserRequest{}
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
a.jsonError(w, err.Error(), http.StatusInternalServerError)
return
}
res := a.getDispenser()
shutdownChan := make(chan struct{})
for _, op := range req {
if op.Op == "set" {
if op.Name == "dispenseOnTouch" {
if value, ok := op.Value.(bool); ok {
err := a.dispenser.SetDispenseOnTouch(value)
if err != nil {
a.jsonError(w, "Could not set dispense on touch", http.StatusInternalServerError)
return
}
res.DispenseOnTouch = value
} else {
a.jsonError(w, fmt.Sprintf("%s value not a boolean, but %T", op.Name, op.Value), http.StatusBadRequest)
return
}
} else if op.Name == "name" {
if value, ok := op.Value.(string); ok {
err := a.dispenser.SetName(value)
if err != nil {
a.jsonError(w, "Could not set name", http.StatusInternalServerError)
return
}
res.Name = value
} else {
a.jsonError(w, fmt.Sprintf("%s value not a string, but %T", op.Name, op.Value), http.StatusInternalServerError)
return
}
} else {
a.jsonError(w, fmt.Sprintf("unknown field %s", op.Name), http.StatusBadRequest)
return
}
} else if op.Op == "reboot" {
res.State = state.String(state.StateStopping)
go func() {
<-shutdownChan
err := a.dispenser.Reboot()
if err != nil {
a.log.Errorf("unable to reboot: %v", err)
return
}
}()
} else if op.Op == "shutdown" {
res.State = state.String(state.StateStopping)
go func() {
<-shutdownChan
err := a.dispenser.ShutDown()
if err != nil {
a.log.Errorf("unable to shutdown: %v", err)
return
}
}()
} else {
a.jsonError(w, fmt.Sprintf("unknown op %s", op.Op), http.StatusBadRequest)
return
}
}
a.jsonResponse(w, res, http.StatusOK)
close(shutdownChan)
}
}