forked from brutella/hkcam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera_control.go
252 lines (216 loc) · 5.17 KB
/
camera_control.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
package hkcam
import (
"github.com/brutella/hc/log"
"github.com/nfnt/resize"
"github.com/radovskyb/watcher"
"bytes"
"encoding/json"
"fmt"
"image"
"image/jpeg"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"time"
)
const TypeCameraControl = "19BDAD9E-6102-48D5-B413-3F11253706AE"
// RefDate represents the reference date used to generate asset ids.
// Short ids are prefered and therefore we use 1st April 2019 as the reference date.
var RefDate = time.Date(2019, 4, 1, 0, 0, 0, 0, time.UTC)
type CameraControl struct {
TakeSnapshot *TakeSnapshot
Assets *Assets
GetAsset *GetAsset
DeleteAssets *DeleteAssets
CameraSnapshotReq func(width, height uint) (*image.Image, error)
snapshots []*snapshot
w *watcher.Watcher
}
func NewCameraControl() *CameraControl {
cc := CameraControl{}
cc.TakeSnapshot = NewTakeSnapshot()
cc.Assets = NewAssets()
cc.GetAsset = NewGetAsset()
cc.DeleteAssets = NewDeleteAssets()
return &cc
}
func (cc *CameraControl) SetupWithDir(dir string) {
r := regexp.MustCompile(".*\\.jpg")
fs, err := ioutil.ReadDir(dir)
if err != nil {
log.Info.Println(err)
}
for _, f := range fs {
if r.MatchString(f.Name()) == false {
continue
}
path := filepath.Join(dir, f.Name())
b, err := ioutil.ReadFile(path)
if err != nil {
log.Info.Println(f, err)
} else {
s := snapshot{
ID: f.Name(),
Date: f.ModTime().Format(time.RFC3339),
Bytes: b,
Path: path,
}
cc.add(&s)
}
}
cc.updateAssetsCharacteristic()
go cc.watch(dir, r)
cc.GetAsset.OnValueRemoteUpdate(func(buf []byte) {
var req GetAssetRequest
err := json.Unmarshal(buf, &req)
if err != nil {
log.Debug.Fatalln("GetAssetRequest:", err)
}
for _, s := range cc.snapshots {
if s.ID == req.ID {
r := bytes.NewReader(s.Bytes)
img, err := jpeg.Decode(r)
if err != nil {
log.Info.Printf("jpeg.Decode() %v", err)
cc.GetAsset.SetValue([]byte{})
return
}
scaled := resize.Resize(req.Width, req.Height, img, resize.Lanczos3)
imgBuf := new(bytes.Buffer)
if err := jpeg.Encode(imgBuf, scaled, nil); err != nil {
log.Info.Printf("jpeg.Encode() %v", err)
cc.GetAsset.SetValue([]byte{})
return
}
cc.GetAsset.SetValue(imgBuf.Bytes())
return
}
}
})
cc.DeleteAssets.OnValueRemoteUpdate(func(buf []byte) {
var req DeleteAssetsRequest
err := json.Unmarshal(buf, &req)
if err != nil {
log.Debug.Fatalln("GetAssetRequest:", err)
return
}
for _, id := range req.IDs {
err = cc.deleteWithID(id)
if err != nil {
log.Debug.Println("delete:", err)
}
}
})
cc.TakeSnapshot.OnValueRemoteUpdate(func(v bool) {
if v == true {
img, err := cc.CameraSnapshotReq(1920, 1080)
if err != nil {
log.Info.Println(err)
} else {
name := fmt.Sprintf("%.0f.jpg", time.Now().Sub(RefDate).Seconds())
path := filepath.Join(dir, name)
buf := new(bytes.Buffer)
if err := jpeg.Encode(buf, *img, nil); err != nil {
log.Debug.Printf("jpeg.Encode() %v", err)
} else {
ioutil.WriteFile(path, buf.Bytes(), os.ModePerm)
}
}
// Disable shutter after some timeout
go func() {
<-time.After(1 * time.Second)
cc.TakeSnapshot.SetValue(false)
}()
}
})
}
func (cc *CameraControl) add(s *snapshot) {
log.Debug.Println("add:", s.ID)
cc.snapshots = append(cc.snapshots, s)
}
func (cc *CameraControl) deleteWithID(id string) error {
log.Debug.Println("del:", id)
for _, s := range cc.snapshots {
if s.ID == id {
return os.Remove(s.Path)
}
}
return fmt.Errorf("File with id %s not found", id)
}
func (cc *CameraControl) removeWithID(id string) {
log.Debug.Println("rmv:", id)
for i, s := range cc.snapshots {
if s.ID == id {
cc.snapshots = append(cc.snapshots[:i], cc.snapshots[i+1:]...)
return
}
}
}
func (cc *CameraControl) updateAssetsCharacteristic() {
assets := []CameraAssetMetadata{}
for _, s := range cc.snapshots {
asset := CameraAssetMetadata{
ID: s.ID,
Date: s.Date,
}
assets = append(assets, asset)
}
p := AssetsMetadataResponse{
Assets: assets,
}
if b, err := json.Marshal(p); err != nil {
log.Info.Println(err)
} else {
log.Debug.Println(string(b))
cc.Assets.SetValue(b)
}
}
func (cc *CameraControl) watch(dir string, r *regexp.Regexp) {
w := watcher.New()
w.FilterOps(watcher.Create, watcher.Remove)
w.AddFilterHook(watcher.RegexFilterHook(r, false))
go func() {
for {
select {
case event := <-w.Event:
switch event.Op {
case watcher.Create:
b, err := ioutil.ReadFile(event.Path)
if err != nil {
log.Info.Println(event.Path, err)
} else {
s := snapshot{
ID: event.Name(),
Date: event.ModTime().Format(time.RFC3339),
Bytes: b,
Path: event.Path,
}
cc.add(&s)
}
case watcher.Remove:
cc.removeWithID(event.Name())
default:
break
}
cc.updateAssetsCharacteristic()
case err := <-w.Error:
log.Info.Fatalln(err)
case <-w.Closed:
return
}
}
}()
if err := w.Add(dir); err != nil {
log.Info.Fatalln(err)
}
if err := w.Start(time.Second * 1); err != nil {
log.Info.Fatalln(err)
}
}
type snapshot struct {
ID string
Date string
Bytes []byte
Path string
}