-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
257 lines (228 loc) · 5.28 KB
/
api.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"strconv"
"github.com/julienschmidt/httprouter"
)
type ClientId string
type ConnectedClient struct {
ClientId ClientId
ClientTransactionID uint32
Connected bool
}
type ApiServer struct {
ApiPort uint32
ServerTransactionID uint32
}
func NewApiServer(apiPort uint32) *ApiServer {
return &ApiServer{
ApiPort: apiPort,
}
}
func (srv *ApiServer) Start() {
router := httprouter.New()
srv.configureManagementAPI(router)
srv.configureCommonAPI(router)
srv.configureSwitchAPI(router)
srv.configureFocuserAPI(router)
log.Fatal(http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", srv.ApiPort), router))
}
func (srv *ApiServer) handleNotSupported(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
resp := stringResponse{
Value: "Command not supported",
}
srv.prepareAlpacaResponse(r, &resp.alpacaResponse)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(resp)
}
func (srv *ApiServer) prepareAlpacaResponse(r *http.Request, resp *alpacaResponse) {
ctid := getClientTransactionId(r)
if ctid < 0 {
ctid = 0
}
srv.ServerTransactionID += 1
resp.ClientTransactionID = uint32(ctid)
resp.ServerTransactionID = srv.ServerTransactionID
}
func (srv *ApiServer) validAlpacaRequest(r *http.Request) bool {
//if _, err := strconv.Atoi(c.Param("device_id")); err != nil {
// return false
//}
cid := getClientId(r)
if cid < 0 {
return false
}
ctidv := getClientTransactionId(r)
return ctidv >= 0
}
func getClientId(r *http.Request) int {
cidv := ""
if r.Method == "GET" {
cidv = r.URL.Query().Get("ClientID")
if cidv == "" {
cidv = r.URL.Query().Get("clientid")
if cidv == "" {
return -1
}
}
} else {
cidv = r.PostFormValue("ClientID")
if cidv == "" {
cidv = r.PostFormValue("clientid")
if cidv == "" {
return -1
}
}
}
cid, err := strconv.Atoi(cidv)
if err != nil {
return -1
}
if cid < 0 {
return -1
}
return cid
}
func getClientTransactionId(r *http.Request) int {
ctidv := ""
if r.Method == "GET" {
ctidv = r.URL.Query().Get("ClientTransactionID")
if ctidv == "" {
ctidv = r.URL.Query().Get("clienttransactionid")
if ctidv == "" {
return -1
}
}
} else { // PUT
ctidv = r.PostFormValue("ClientTransactionID")
if ctidv == "" {
ctidv = r.PostFormValue("clienttransactionid")
if ctidv == "" {
return -1
}
}
}
ctid, err := strconv.Atoi(ctidv)
if err != nil {
return -1
}
if ctid < 0 {
return -1
}
return ctid
}
func getIdFromRequest(r *http.Request) (result int32, err error) {
sid := ""
if r.Method == "GET" {
sid = r.URL.Query().Get("Id")
if sid == "" {
return -1, errors.New("id parameter missing")
}
} else { // i.e. PUT command
sid = r.PostFormValue("Id")
if sid == "" {
return -1, errors.New("id parameter missing")
}
}
iid, err := strconv.ParseInt(sid, 10, 32)
if err != nil {
return -1, errors.New("id parameter not numeric")
}
result = int32(iid)
if result < 0 {
return -1, errors.New("id parameter out of range")
}
result += 1 // offset for shared device
return result, nil
}
func getValueFromRequest(r *http.Request) (result int64, err error) {
svalue := ""
if r.Method == "PUT" {
svalue = r.PostFormValue("Value")
if svalue == "" {
return -1, errors.New("value parameter missing")
}
} else {
return -1, errors.New("expected PUT")
}
ivalue, err := strconv.ParseInt(svalue, 10, 64)
if err != nil {
return -1, errors.New("value parameter not numeric")
}
result = int64(ivalue)
if result < 0 {
return -1, errors.New("value parameter out of range")
}
//log.Println("Value:", result)
return result, nil
}
func getPositionFromRequest(r *http.Request) (result int32, err error) {
sposition := ""
if r.Method != "PUT" {
return -1, errors.New("expected PUT")
}
sposition = r.PostFormValue("Position")
if sposition == "" {
return -1, errors.New("position parameter missing")
}
ivalue, err := strconv.ParseInt(sposition, 10, 32)
if err != nil {
return -1, errors.New("position parameter not numeric")
}
result = int32(ivalue)
if result == 0 {
return -1, errors.New("position parameter out of range")
}
return result, nil
}
func getSwitchStateFromRequest(r *http.Request) (bstate bool, err error) {
sstate := ""
if r.Method == "GET" {
sstate = r.URL.Query().Get("State")
if sstate == "" {
err = errors.New("state parameter missing")
return
}
} else { // e.g PUT command
sstate = r.PostFormValue("State")
if sstate == "" {
err = errors.New("state parameter missing")
return
}
}
bstate, err = strconv.ParseBool(sstate)
if err != nil {
return
}
return
}
func getSwitchNameFromRequest(r *http.Request) (sname string, err error) {
if r.Method == "GET" {
sname = r.URL.Query().Get("Name")
if sname == "" {
err = errors.New("name parameter missing")
return
}
} else { // e.g PUT command
sname = r.PostFormValue("Name")
if sname == "" {
err = errors.New("name parameter missing")
return
}
}
return
}
func getConnectedFromRequest(r *http.Request) (connect bool, err error) {
// PUT command
connect, err = strconv.ParseBool(r.PostFormValue("Connected"))
if err != nil {
err = errors.New("connected parameter missing")
return
}
return
}