-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.go
184 lines (152 loc) · 4.21 KB
/
camera.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
package main
import (
"context"
"encoding/json"
"io/ioutil"
"math/rand"
"net/http"
"os"
"path/filepath"
"strconv"
"time"
"github.com/awfulbits/astro-raspicam/config"
"github.com/awfulbits/astro-raspicam/picamera"
"github.com/go-chi/chi"
"github.com/go-chi/render"
)
type key string
const imageSetKey key = "imageSetConfig"
// imageSetConfigR struct is used for both request and response
type imageSetConfigR struct {
ImageSetConfig picamera.ImageSetConfig `json:"imageSetConfig"`
}
func (sr *imageSetConfigR) Bind(r *http.Request) error {
return nil
}
func (sr imageSetConfigR) Render(w http.ResponseWriter, r *http.Request) error {
return nil
}
func (s *server) saveImageSetConfig(w http.ResponseWriter, r *http.Request) {
var err error
var imageSetConfigR imageSetConfigR
if err = render.Bind(r, &imageSetConfigR); err != nil {
render.Render(w, r, errInvalidRequest(err))
return
}
imageSetConfigR, err = saveImageSetConfigFile(imageSetConfigR.ImageSetConfig)
if err != nil {
render.Render(w, r, errInternalServer(err))
return
}
render.Render(w, r, imageSetConfigR)
}
func (s *server) capture(w http.ResponseWriter, r *http.Request) {
imageSetConfig := r.Context().Value(imageSetKey).(*picamera.ImageSetConfig)
imageSet := imageSetConfig.Generate()
if !s.captureInProgress {
go func() {
s.captureInProgress = !s.captureInProgress
if err := imageSet.Capture(); err != nil {
s.captureErr = err
}
s.captureInProgress = !s.captureInProgress
}()
} else {
render.Render(w, r, &errResponse{
HTTPStatusCode: 503,
StatusText: "Service Unavailable.",
ErrorText: "Camera sensor is busy",
})
return
}
w.Write([]byte("Capturing..."))
}
func (s *server) getImageSetConfig(w http.ResponseWriter, r *http.Request) {
imageSetConfig := r.Context().Value(imageSetKey).(picamera.ImageSetConfig)
render.Render(w, r, &imageSetConfigR{ImageSetConfig: imageSetConfig})
}
func (s *server) getImageSetConfigs(w http.ResponseWriter, r *http.Request) {
scs, err := getImageSetConfigs()
if err != nil {
render.Render(w, r, errInternalServer(err))
return
}
err = render.RenderList(w, r, scs)
if err != nil {
render.Render(w, r, errInternalServer(err))
return
}
}
/*********
Middleware
*********/
type imageSetConfigID struct {
ImageSetConfigID string `json:"imageSetConfigID,omitempty"`
}
func (sr *imageSetConfigID) Bind(r *http.Request) error {
return nil
}
func loadImageSetConfig(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var err error
var imageSetConfigID imageSetConfigID
if id := chi.URLParam(r, "imageSetConfigID"); id != "" {
imageSetConfigID.ImageSetConfigID = id
} else {
if err = render.Bind(r, &imageSetConfigID); err != nil {
render.Render(w, r, errInvalidRequest(err))
return
}
}
imageSetConfig, err := getImageSetConfigFile(imageSetConfigID.ImageSetConfigID)
ctx := context.WithValue(r.Context(), imageSetKey, imageSetConfig)
if err != nil {
render.Render(w, r, errInternalServer(err))
return
}
next.ServeHTTP(w, r.WithContext(ctx))
})
}
/***************
Helper functions
***************/
func saveImageSetConfigFile(sc picamera.ImageSetConfig) (scr imageSetConfigR, err error) {
if sc.ID == "" {
seededRand := rand.New(
rand.NewSource(time.Now().UnixNano()))
sc.ID = strconv.Itoa(seededRand.Int())
}
scJSON, err := json.Marshal(&sc)
fpath := filepath.Join(config.C.ImageSetConfigsPath, sc.ID)
err = ioutil.WriteFile(fpath, scJSON, 0644)
scr.ImageSetConfig = sc
return
}
func getImageSetConfigFile(id string) (sc picamera.ImageSetConfig, err error) {
fpath := filepath.Join(config.C.ImageSetConfigsPath, id)
file, err := ioutil.ReadFile(fpath)
if err != nil {
return
}
err = json.Unmarshal([]byte(file), &sc)
return
}
func getImageSetConfigs() (scs []render.Renderer, e error) {
root := config.C.ImageSetConfigsPath
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
sc, err := getImageSetConfigFile(info.Name())
if err != nil {
e = err
}
if sc.ID == info.Name() {
scs = append(scs, imageSetConfigR{ImageSetConfig: sc})
}
}
return nil
})
if err != nil {
e = err
}
return
}