moved areas to a seperate Watch struct

This commit is contained in:
BroodjeAap 2020-07-11 15:30:38 +00:00
parent 1582a4714d
commit f4099dc4f5
2 changed files with 37 additions and 17 deletions

View file

@ -15,13 +15,13 @@ import (
"gocv.io/x/gocv" "gocv.io/x/gocv"
) )
// Stream Unexported // Stream represents a stream to monitor
type Stream struct { type Stream struct {
URL string `json:"url"` URL string `json:"url"`
Base64 string `json:"base64"` Base64 string `json:"base64"`
Interval int `json:"interval"` Interval int `json:"interval"`
Areas map[string][]image.Rectangle `json:"areas"` Watches []Watch `json:"watches"`
FileLock sync.Mutex `json:"lock"` FileLock sync.Mutex `json:"lock"`
} }
// NewStream creates a new Stream Object // NewStream creates a new Stream Object
@ -32,13 +32,20 @@ func NewStream(URL string) Stream {
URL: URL, URL: URL,
Base64: base64, Base64: base64,
Interval: 5000, Interval: 5000,
Areas: make(map[string][]image.Rectangle), Watches: make([]Watch, 0),
} }
rectList := make([]image.Rectangle, 0) watch := Watch{
rectList = append(rectList, image.Rect(30, 30, 400, 200)) Name: "test",
rectList = append(rectList, image.Rect(400, 30, 500, 200)) Color: color.RGBA{255, 0, 255, 255},
rectList = append(rectList, image.Rect(400, 200, 500, 500)) Areas: []image.Rectangle{
stream.Areas["test"] = rectList image.Rect(30, 30, 400, 200),
image.Rect(400, 30, 500, 200),
image.Rect(400, 200, 500, 500),
},
}
log.Println(watch)
stream.Watches = append(stream.Watches, watch)
log.Println(stream)
stream.WriteStreamJSON() stream.WriteStreamJSON()
return stream return stream
} }
@ -82,9 +89,9 @@ func (s Stream) Update() {
contours := gocv.FindContours(diff, gocv.RetrievalExternal, gocv.ChainApproxSimple) contours := gocv.FindContours(diff, gocv.RetrievalExternal, gocv.ChainApproxSimple)
for _, areas := range s.Areas { for _, watch := range s.Watches {
for _, area := range areas { for _, area := range watch.Areas {
gocv.Rectangle(&debug, area, color.RGBA{255, 0, 255, 255}, 10) gocv.Rectangle(&debug, area, watch.Color, 10)
for contourIndex, contour := range contours { for contourIndex, contour := range contours {
for _, point := range contour { for _, point := range contour {
@ -104,7 +111,7 @@ func (s Stream) Update() {
// UpdateInterval calls Update() every interval // UpdateInterval calls Update() every interval
func (s Stream) UpdateInterval() { func (s Stream) UpdateInterval() {
for { for {
s.Update() go s.Update()
time.Sleep(time.Duration(s.Interval) * time.Millisecond) time.Sleep(time.Duration(s.Interval) * time.Millisecond)
} }
} }
@ -169,7 +176,7 @@ func (s Stream) WriteStreamJSON() {
if !FileExists(s.GetStreamStoreDirPath()) { if !FileExists(s.GetStreamStoreDirPath()) {
os.MkdirAll(s.GetStreamStoreDirPath(), os.ModePerm) os.MkdirAll(s.GetStreamStoreDirPath(), os.ModePerm)
} }
file, _ := json.MarshalIndent(s, "", "") file, _ := json.MarshalIndent(s, "", "\t")
_ = ioutil.WriteFile(s.GetStreamJSONPath(), file, 0644) _ = ioutil.WriteFile(s.GetStreamJSONPath(), file, 0644)
} }

13
watch.go Executable file
View file

@ -0,0 +1,13 @@
package main
import (
"image"
"image/color"
)
// 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"`
}