/
berrybooth.go
122 lines (103 loc) · 2.65 KB
/
berrybooth.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
package main
// #cgo CFLAGS: -I/Users/micah/Developer/include/gphoto2
// #cgo LDFLAGS: -L/Users/micah/Developer/lib -lgphoto2
// #include <gphoto2.h>
// #include <gphoto2/gphoto2-version.h>
import "C"
import (
"fmt"
"github.com/asaskevich/EventBus"
"github.com/micahwedemeyer/gphoto2go"
"github.com/spf13/viper"
"io"
"log"
"os"
"path"
"time"
)
var bus *EventBus.EventBus
func initLogger() {
f, _ := os.OpenFile("log/berrybooth.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
log.SetOutput(f)
}
func initViper() {
// Read in the actual config.toml if available
viper.SetConfigName("config")
err := viper.ReadInConfig() // Find and read the config file
if err != nil {
log.Fatalf("Error reading the config file: %s\n", err)
} else {
log.Printf("Config file read.")
}
}
func handleNewImage(filename string) {
fmt.Printf("New image: %s\n", filename)
}
func initEventBus() {
bus = EventBus.New()
log.Printf("Eventbus Initialized\n")
}
func initCamera() *gphoto2go.Camera {
camera := new(gphoto2go.Camera)
err := camera.Init()
if err < 0 {
log.Fatalf("No camera found. Exiting.\n")
} else {
bus.Publish("camera:init", camera)
}
return camera
}
func initCameraEventSource(camera *gphoto2go.Camera) {
go func() {
for {
eventChan := camera.AsyncWaitForEvent(1000)
evt := <-eventChan
if evt.Type == gphoto2go.EVENT_FILE_ADDED {
bus.Publish("photo:capture", camera, evt.Folder, evt.File)
}
}
}()
}
func handleCaptureEvent(camera *gphoto2go.Camera, folder string, fileName string) {
path := path.Join(viper.GetString("photos.path"), fileName)
reader := camera.FileReader(folder, fileName)
fWriter, _ := os.Create(path)
io.Copy(fWriter, reader)
fWriter.Close()
reader.Close()
bus.Publish("photo:saved", path)
}
func main() {
initLogger()
initViper()
log.Printf("Berrybooth Startup\n")
initEventBus()
bus.Subscribe("camera:init", func(camera *gphoto2go.Camera) {
model, _ := camera.Model()
log.Printf("Detected camera: %s\n", model)
})
bus.Subscribe("camera:init", initCameraEventSource)
bus.Subscribe("photo:capture", handleCaptureEvent)
bus.Subscribe("photo:saved", func(path string) {
log.Printf("File saved: %s\n", path)
})
initCamera()
/*
folders := camera.RListFolders("/")
for _, folder := range folders {
fmt.Printf("Folder: %s\n", folder)
files, _ := camera.ListFiles(folder)
for _, fileName := range files {
fmt.Printf("File: %s\n", folder+"/"+fileName)
reader := camera.FileReader(folder, fileName)
fWriter, _ := os.Create("/Users/micah/tmp/" + fileName)
io.Copy(fWriter, reader)
fWriter.Close()
reader.Close()
}
}
*/
for {
time.Sleep(time.Duration(1) * time.Second)
}
}