streamwatcher/watch.go
2020-10-25 11:42:56 +01:00

59 lines
1.5 KiB
Go

package main
import (
"image"
"image/color"
"io/ioutil"
"log"
"os"
"path/filepath"
"time"
)
const timeLayout = time.RFC3339Nano
// WatchArea defines one or more areas that should be monitored for motion
type WatchArea struct {
Name string `json:"name"`
Color color.RGBA `json:"color"`
Area image.Rectangle `json:"area"`
}
// WatchAreaStoreDirExists returns filepath.Join(s.GetStreamStoreDirPath(), w.Name)
func (w WatchArea) WatchAreaStoreDirExists(s Stream) bool {
return FileExists(w.GetWatchAreaStoreDir(s))
}
// GetWatchAreaStoreDir returns filepath.Join(s.GetStreamStoreDirPath(), w.Name)
func (w WatchArea) GetWatchAreaStoreDir(s Stream) string {
return filepath.Join(s.GetStreamStoreDirPath(), w.Name)
}
// GetWatchAreaStoreDirInstantPath returns filepath.Join(s.GetWatchAreaStoreDir(), "")
func (w WatchArea) GetWatchAreaStoreDirInstantPath(s Stream) string {
now := time.Now()
return filepath.Join(w.GetWatchAreaStoreDir(s), now.Format(timeLayout))
}
// CopyInstant makes a copy of src to GetWatchAreaStoreDirInstantPath(s)
func (w WatchArea) CopyInstant(src string, s Stream) {
if !FileExists(w.GetWatchAreaStoreDir(s)) {
os.MkdirAll(w.GetWatchAreaStoreDir(s), os.ModePerm)
}
dest := w.GetWatchAreaStoreDirInstantPath(s)
if !FileExists(src) {
log.Fatal("Nothing to copy ", src)
return
}
if FileExists(dest) {
os.Remove(dest)
}
srcData, err := ioutil.ReadFile(src)
if err != nil {
log.Println(err)
}
err = ioutil.WriteFile(dest, srcData, 0644)
if err != nil {
log.Println(err)
}
}