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
|
// vim: shiftwidth=4 tabstop=4 noexpandtab
package main
import (
"log"
"os/exec"
"os"
"path/filepath"
"bufio"
)
type Camera struct {
statemachine Machine
}
func NewCamera() Camera {
return Camera{
statemachine: Machine{
name: "camera",
initial: "idle",
states: StateMap{
"idle": MachineState{
on: TransitionMap{
"take_single_picture": MachineTransition{
to: "single_picture",
},
},
},
"single_picture": MachineState{
on: TransitionMap{
"single_picture_taken": MachineTransition{
to: "idle",
},
},
},
},
api: make(chan string),
state_listeners: make([]*(chan string), 0),
hook: runCameraHooks,
},
}
}
func runCameraHooks(last string, next string, m *Machine) {
if last == "idle" && next == "single_picture" {
go singlePicture(m)
}
}
func singlePicture(m *Machine) {
// create command
var cmd *exec.Cmd
if !config.Flag.Debug {
cmd = exec.Command("/usr/bin/python3", "/usr/lib/python3.9/site-packages/birdscan/")
} else { // debug mode
pwd,err := os.Getwd()
if err != nil {
log.Fatal(err)
}
repoDir := filepath.Dir(pwd)
log.Printf("Repository path is assumed to be = '%s'", repoDir)
pythonPackage := repoDir + "/python/birdscan"
cmd = exec.Command("/usr/bin/python3", pythonPackage, "--debug")
}
// connect stdout of python process
stdout,err := cmd.StdoutPipe()
if err != nil {
log.Print(err)
}
defer stdout.Close()
// run command
err = cmd.Start()
if err != nil {
log.Print(err)
}
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
text := scanner.Text()
log.Printf("Python returned '%s'", text)
if text == "ok" {
break
}
}
err = cmd.Wait() // wait until command execution and io is complete
if err != nil {
log.Print(err)
}
// process result
m.SendEvent("single_picture_taken")
}
func (cam *Camera) run() {
cam.statemachine.Run()
}
// read until '\n'
func readLine(buff *[]byte, ) {
}
|