prepare to daemonize

This commit is contained in:
Steven Polley 2020-10-29 04:52:10 +00:00
parent 240ab31166
commit e2d30e1d65

27
main.go
View File

@ -15,6 +15,8 @@ const (
minimumMotionArea = 3000 // Motion detection minimum area needed to move minimumMotionArea = 3000 // Motion detection minimum area needed to move
recordLengthAfterMotion = 30 // Number of seconds to keep recording going after motion was last detected recordLengthAfterMotion = 30 // Number of seconds to keep recording going after motion was last detected
motionDetectInterval = 30 //number of frames between motion detection attempts // TBD: Implement this, currently does nothing motionDetectInterval = 30 //number of frames between motion detection attempts // TBD: Implement this, currently does nothing
deviceID = 0 // raspberry pi camera index
syncFolder = "/sync"
) )
var ( var (
@ -26,10 +28,6 @@ var (
) )
func init() { func init() {
if len(os.Args) < 2 {
fmt.Println("How to run:\n\tstorage-security [camera ID]")
return
}
img = gocv.NewMat() img = gocv.NewMat()
imgDelta = gocv.NewMat() imgDelta = gocv.NewMat()
@ -44,12 +42,17 @@ func main() {
defer imgThresh.Close() defer imgThresh.Close()
defer mog2.Close() defer mog2.Close()
// parse args f, err := os.OpenFile(fmt.Sprintf("%s/storage-security.log", syncFolder), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
deviceID := os.Args[1] if err != nil {
log.Fatalf("error opening log file: %v", err)
}
defer f.Close()
log.SetOutput(f)
webcam, err := gocv.OpenVideoCapture(deviceID) webcam, err := gocv.OpenVideoCapture(deviceID)
if err != nil { if err != nil {
fmt.Printf("Error opening video capture device: %v\n", deviceID) log.Fatalf("error opening video capture device: %v\n", deviceID)
return return
} }
defer webcam.Close() defer webcam.Close()
@ -59,7 +62,7 @@ func main() {
// This is a warm up ladies and gentlemen. // This is a warm up ladies and gentlemen.
for i := 0; i < 20; i++ { for i := 0; i < 20; i++ {
if ok := webcam.Read(&img); !ok { if ok := webcam.Read(&img); !ok {
fmt.Printf("Device closed: %v\n", deviceID) log.Fatalf("video capture device closed: %v\n", deviceID)
return return
} }
detectMotion(img) detectMotion(img)
@ -70,7 +73,7 @@ func main() {
// main loop // main loop
for { for {
if ok := webcam.Read(&img); !ok { if ok := webcam.Read(&img); !ok {
fmt.Printf("Device closed: %v\n", deviceID) log.Fatalf("video capture device closed: %v\n", deviceID)
return return
} }
if img.Empty() { if img.Empty() {
@ -81,11 +84,11 @@ func main() {
if detectMotion(img) { if detectMotion(img) {
// Determine if a new recording needs to start // Determine if a new recording needs to start
if time.Now().After(lastMotionDetectedTime.Add(time.Second * recordLengthAfterMotion)) { if time.Now().After(lastMotionDetectedTime.Add(time.Second * recordLengthAfterMotion)) {
fileName := fmt.Sprintf("storage-%s.avi", time.Now().Format(time.RFC3339)) fileName := fmt.Sprintf("%s/storage-security-%s.avi", syncFolder, time.Now().Format(time.RFC3339))
log.Printf("motion detected, started recording to file named %s", fileName) log.Printf("motion detected, started recording to file named %s", fileName)
currentRecording, err = gocv.VideoWriterFile(fileName, "MJPG", 25, img.Cols(), img.Rows(), true) currentRecording, err = gocv.VideoWriterFile(fileName, "MJPG", 25, img.Cols(), img.Rows(), true)
if err != nil { if err != nil {
fmt.Printf("error opening video writer device: %v\n", err) log.Fatalf("error opening video writer device: %v\n", err)
return return
} }
} }
@ -107,7 +110,7 @@ func main() {
log.Printf("motion has not been detected for the last %d seconds stopping recording to file", recordLengthAfterMotion) log.Printf("motion has not been detected for the last %d seconds stopping recording to file", recordLengthAfterMotion)
err = currentRecording.Close() err = currentRecording.Close()
if err != nil { if err != nil {
log.Printf("failed to close openCV file recording handle: %v", err) log.Fatalf("failed to close openCV file recording handle: %v", err)
} }
currentRecording = nil currentRecording = nil
} }