streamwatcher/watch.go

59 lines
1.5 KiB
Go
Executable file

package main
import (
"image"
"image/color"
"io/ioutil"
"log"
"os"
"path/filepath"
"time"
)
const timeLayout = "2006-01-02_15-4-5.jpg"
// Watch defines one or more areas that should be monitored for motion
type Watch struct {
Name string `json:"name"`
Color color.RGBA `json:"color"`
Areas []image.Rectangle `json:"areas"`
}
// WatchStoreDirExists returns filepath.Join(s.GetStreamStoreDirPath(), w.Name)
func (w Watch) WatchStoreDirExists(s Stream) bool {
return FileExists(w.GetWatchStoreDir(s))
}
// GetWatchStoreDir returns filepath.Join(s.GetStreamStoreDirPath(), w.Name)
func (w Watch) GetWatchStoreDir(s Stream) string {
return filepath.Join(s.GetStreamStoreDirPath(), w.Name)
}
// GetWatchStoreDirInstantPath returns filepath.Join(s.GetWatchStoreDir(), "")
func (w Watch) GetWatchStoreDirInstantPath(s Stream) string {
now := time.Now()
return filepath.Join(w.GetWatchStoreDir(s), now.Format(timeLayout))
}
// CopyInstant makes a copy of src to GetWatchStoreDirInstantPath(s)
func (w Watch) CopyInstant(src string, s Stream) {
if !FileExists(w.GetWatchStoreDir(s)) {
os.MkdirAll(w.GetWatchStoreDir(s), os.ModePerm)
}
dest := w.GetWatchStoreDirInstantPath(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)
}
}