/
main.go
202 lines (165 loc) · 5.96 KB
/
main.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
// thermal-recorder - record thermal video footage of warm moving objects
// Copyright (C) 2018, The Cacophony Project
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"log"
"net"
"os"
arg "github.com/alexflint/go-arg"
"periph.io/x/periph/host"
"github.com/TheCacophonyProject/lepton3"
"github.com/TheCacophonyProject/thermal-recorder/location"
"github.com/TheCacophonyProject/thermal-recorder/motion"
"github.com/TheCacophonyProject/thermal-recorder/recorder"
"github.com/TheCacophonyProject/thermal-recorder/throttle"
)
const (
framesHz = lepton3.FramesHz // approx
cptvTempExt = "cptv.temp"
frameLogIntervalFirstMin = 15 * framesHz
frameLogInterval = 60 * 5 * framesHz
)
var (
version = "<not set>"
processor *motion.MotionProcessor
)
type Args struct {
ConfigFile string `arg:"-c,--config" help:"path to configuration file"`
UploaderConfigFile string `arg:"-u,--uploader-config" help:"path to uploader config file"`
Timestamps bool `arg:"-t,--timestamps" help:"include timestamps in log output"`
TestCptvFile string `arg:"-f, --testfile" help:"Run a CPTV file through to see what the results are"`
Verbose bool `arg:"-v, --verbose" help:"Make logging more verbose"`
LocationConfigFile string `arg:"-l, --location" help:"path to location config file"`
}
func (Args) Version() string {
return version
}
func procArgs() Args {
var args Args
args.ConfigFile = "/etc/thermal-recorder.yaml"
args.UploaderConfigFile = "/etc/thermal-uploader.yaml"
args.LocationConfigFile = location.DefaultLocationFile()
arg.MustParse(&args)
return args
}
func main() {
err := runMain()
if err != nil {
log.Fatal(err)
}
}
func runMain() error {
args := procArgs()
if !args.Timestamps {
log.SetFlags(0) // Removes default timestamp flag
}
log.Printf("running version: %s", version)
conf, err := ParseConfigFiles(args.ConfigFile, args.UploaderConfigFile, args.LocationConfigFile)
if err != nil {
return err
}
logConfig(conf)
if args.TestCptvFile != "" {
conf.Motion.Verbose = args.Verbose
results := NewCPTVPlaybackTester(conf).Detect(args.TestCptvFile)
log.Printf("Detected: %-16s Recorded: %-16s Motion frames: %d/%d", results.motionDetectedFrames, results.recordedFrames, results.motionDetectedCount, results.frameCount)
return nil
}
log.Println("starting d-bus service")
err = startService(conf.OutputDir)
if err != nil {
return err
}
deleteSnapshot(conf.OutputDir)
log.Println("host initialisation")
if _, err := host.Init(); err != nil {
return err
}
turret := NewTurretController(conf.Turret)
go turret.Start()
log.Println("deleting temp files")
if err := deleteTempFiles(conf.OutputDir); err != nil {
return err
}
for {
// Set up listener for frames sent by leptond.
os.Remove(conf.FrameInput)
listener, err := net.Listen("unixpacket", conf.FrameInput)
if err != nil {
return err
}
log.Print("waiting for camera connection")
conn, err := listener.Accept()
if err != nil {
log.Printf("socket accept failed: %v", err)
continue
}
// Prevent concurrent connections.
listener.Close()
err = handleConn(conn, conf, turret)
log.Printf("camera connection ended with: %v", err)
}
}
func handleConn(conn net.Conn, conf *Config, turret *TurretController) error {
totalFrames := 0
cptvRecorder := NewCPTVFileRecorder(conf)
defer cptvRecorder.Stop()
var recorder recorder.Recorder = cptvRecorder
if conf.Throttler.ApplyThrottling {
minRecordingLength := conf.Recorder.MinSecs + conf.Recorder.PreviewSecs
recorder = throttle.NewThrottledRecorder(cptvRecorder, &conf.Throttler, minRecordingLength, new(throttle.ThrottledEventRecorder))
}
processor = motion.NewMotionProcessor(&conf.Motion, &conf.Recorder, &conf.Location, nil, recorder)
rawFrame := new(lepton3.RawFrame)
log.Print("new camera connection, reading frames")
for {
_, err := conn.Read(rawFrame[:])
if err != nil {
return err
}
totalFrames++
if totalFrames%frameLogIntervalFirstMin == 0 &&
totalFrames <= 60*framesHz || totalFrames%frameLogInterval == 0 {
log.Printf("%d frames for this connection", totalFrames)
}
processor.Process(rawFrame)
}
}
func logConfig(conf *Config) {
log.Printf("device name: %s", conf.DeviceName)
log.Printf("frame input: %s", conf.FrameInput)
log.Printf("output dir: %s", conf.OutputDir)
log.Printf("recording limits: %ds to %ds", conf.Recorder.MinSecs, conf.Recorder.MaxSecs)
log.Printf("preview seconds: %d", conf.Recorder.PreviewSecs)
log.Printf("minimum disk space: %d", conf.MinDiskSpace)
log.Printf("motion: %+v", conf.Motion)
log.Printf("throttler: %+v", conf.Throttler)
log.Printf("location latitude: %v", conf.Location.Latitude)
log.Printf("location longitude: %v", conf.Location.Longitude)
if conf.Recorder.UseSunriseSunsetWindow {
log.Printf("recording window is based on sunrise and sunset (sunrise offset is %v minutes, sunset offset is %v minutes", conf.Recorder.SunriseOffset, conf.Recorder.SunsetOffset)
} else if !conf.Recorder.WindowStart.IsZero() {
log.Printf("recording window: %02d:%02d to %02d:%02d",
conf.Recorder.WindowStart.Hour(), conf.Recorder.WindowStart.Minute(),
conf.Recorder.WindowEnd.Hour(), conf.Recorder.WindowEnd.Minute())
}
if conf.Turret.Active {
log.Printf("Turret active")
log.Printf("\tPID: %v", conf.Turret.PID)
log.Printf("\tServoX: %+v", conf.Turret.ServoX)
log.Printf("\tServoY: %+v", conf.Turret.ServoY)
}
}