-
Notifications
You must be signed in to change notification settings - Fork 0
/
focuser.go
172 lines (155 loc) · 5.83 KB
/
focuser.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/julienschmidt/httprouter"
)
func (srv *ApiServer) configureFocuserAPI(router *httprouter.Router) {
// ASCOM Methods specifc to the Focuser API
router.GET("/setup/v1/focuser/1/setup", srv.handleFocuserSetup)
router.GET("/api/v1/focuser/1/absolute", srv.handleAbsolute)
router.GET("/api/v1/focuser/1/ismoving", srv.handleIsMoving)
router.GET("/api/v1/focuser/1/maxincrement", srv.handleMaxIncrement)
router.GET("/api/v1/focuser/1/maxstep", srv.handleMaxStep)
router.GET("/api/v1/focuser/1/position", srv.handlePosition)
router.GET("/api/v1/focuser/1/stepsize", srv.handleStepSize)
router.GET("/api/v1/focuser/1/tempcomp", srv.handleTempComp)
router.PUT("/api/v1/focuser/1/tempcomp", srv.handleNotSupported)
router.GET("/api/v1/focuser/1/tempcompavailable", srv.handleTempCompAvailable)
router.GET("/api/v1/focuser/1/temperature", srv.handleNotSupported)
router.PUT("/api/v1/focuser/1/halt", srv.handleHalt)
router.PUT("/api/v1/focuser/1/move", srv.handleMove)
}
// Handlers below are specific for the Focuser API
// Setup page.
func (srv *ApiServer) handleFocuserSetup(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprintln(w, "Alpaca MHP focuser server")
}
// True if the focuser is capable of absolute position; that is, being commanded to a specific step location.
func (srv *ApiServer) handleAbsolute(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
resp := booleanResponse{
Value: true,
}
srv.prepareAlpacaResponse(r, &resp.alpacaResponse)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(resp)
}
// True if the focuser is currently moving to a new position. False if the focuser is stationary.
func (srv *ApiServer) handleIsMoving(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
sm.Lock()
resp := booleanResponse{
Value: s.Moving,
}
sm.Unlock()
srv.prepareAlpacaResponse(r, &resp.alpacaResponse)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(resp)
}
// Maximum increment size allowed by the focuser; i.e. the maximum number of steps allowed in one move operation.
func (srv *ApiServer) handleMaxIncrement(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
resp := int32Response{
Value: int32(MhpGetMaxIncrement()),
}
srv.prepareAlpacaResponse(r, &resp.alpacaResponse)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(resp)
}
// Maximum step position permitted.
func (srv *ApiServer) handleMaxStep(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
resp := int32Response{
Value: int32(MhpGetMaxStep()),
}
srv.prepareAlpacaResponse(r, &resp.alpacaResponse)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(resp)
}
// Current focuser position, in steps.
func (srv *ApiServer) handlePosition(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
resp := int32Response{
Value: MhpGetPosition(),
}
srv.prepareAlpacaResponse(r, &resp.alpacaResponse)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(resp)
}
// Step size (microns) for the focuser
func (srv *ApiServer) handleStepSize(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
resp := int32Response{
Value: 26, // from measurements on TOA130NS focuser.
}
srv.prepareAlpacaResponse(r, &resp.alpacaResponse)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(resp)
}
// Gets the state of temperature compensation mode (if available), else always False.
func (srv *ApiServer) handleTempComp(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
resp := booleanResponse{
Value: false,
}
srv.prepareAlpacaResponse(r, &resp.alpacaResponse)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(resp)
}
// True if focuser has temperature compensation available.
func (srv *ApiServer) handleTempCompAvailable(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
resp := booleanResponse{
Value: false,
}
srv.prepareAlpacaResponse(r, &resp.alpacaResponse)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(resp)
}
// Immediately stop any focuser motion due to a previous Move(Int32) method call.
func (srv *ApiServer) handleHalt(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
resp := stringResponse{
Value: "",
}
srv.prepareAlpacaResponse(r, &resp.alpacaResponse)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(resp)
}
// Moves the focuser by the specified amount or to the specified position depending on the value of the Absolute property.
func (srv *ApiServer) handleMove(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
value, err := getPositionFromRequest(r)
if err != nil {
resp := stringResponse{
Value: err.Error(),
}
srv.prepareAlpacaResponse(r, &resp.alpacaResponse)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(resp)
return
}
// Move the focuser:
err = MhpMove(value)
if err != nil {
resp := stringResponse{
Value: err.Error(),
}
srv.prepareAlpacaResponse(r, &resp.alpacaResponse)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(resp)
log.Println("Error moving focuser ", err)
return
}
resp := stringResponse{
Value: "",
}
srv.prepareAlpacaResponse(r, &resp.alpacaResponse)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(resp)
}