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
|
// vim: shiftwidth=4 tabstop=4 noexpandtab
package main
import (
"log"
"os"
"os/exec"
"path/filepath"
"net"
"net/textproto"
"bufio"
)
type IpcServer struct {
connections map[string]net.Conn
listener net.Listener
}
func NewIpcServer() IpcServer {
return IpcServer{
connections: make(map[string]net.Conn),
}
}
func (ipc *IpcServer) Run(ipcSocket string) {
log.Println("Running IPC server")
var err error
ipc.listener,err = net.Listen("unix", ipcSocket)
if err != nil {
log.Fatal(err)
}
for {
connection,err := ipc.listener.Accept()
if err != nil {
log.Print(err)
}
bufioreader := bufio.NewReader(connection)
tpreader := textproto.NewReader(bufioreader)
line,err := tpreader.ReadLine()
log.Printf("IPC: '%s'", line)
ipc.connections[line] = connection
go ipc.HandleConnection(line)
}
}
func (ipc *IpcServer) HandleConnection(connection string) {
for {
msg := ipc.ReadLineFrom(connection)
if msg != "" {
log.Printf("Got IPC message '%s' from '%s'", msg, connection)
if connection == "picamera" {
camera.statemachine.SendEvent(msg)
}
}
}
}
func (ipc *IpcServer) WriteLineTo(connection string, line string) {
conn := ipc.connections[connection]
_,err := conn.Write([]byte(line))
if err != nil {
log.Print(err)
}
}
func (ipc *IpcServer) ReadLineFrom(connection string) string {
conn := ipc.connections[connection]
bufioreader := bufio.NewReader(conn)
tpreader := textproto.NewReader(bufioreader)
line,_ := tpreader.ReadLine()
return line
}
func (ipc *IpcServer) Cleanup() {
log.Println("Releasing IPC socket ...")
for _,connection := range ipc.connections {
connection.Close()
}
ipc.listener.Close()
}
func PicameraSubprocess(ipcSocket string) {
var cmd *exec.Cmd
if config.Flag.Debug {
pwd,err := os.Getwd()
if err != nil {
log.Fatal(err)
}
repoDir := filepath.Dir(pwd)
pythonPackage := repoDir + "/python/birdscan"
cmd = exec.Command("python3", pythonPackage, "--debug", "-s", ipcSocket)
} else {
cmd = exec.Command("python3", "/usr/lib/python3.9/site-packages/birdscan/", "-s", ipcSocket)
}
log.Println("Starting picamera service")
err := cmd.Run()
log.Println("picamera service terminated!")
if err != nil {
log.Print(err)
}
}
|