From f8b13dc3ef4c96ac6640eb523ae033c277c088f9 Mon Sep 17 00:00:00 2001 From: BroodjeAap Date: Sat, 25 Jul 2020 12:39:19 +0000 Subject: [PATCH] added 'MotionInterval' that is used instead of 'Interval' when motion is detected on a Stream --- stream.go | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/stream.go b/stream.go index 16fd4d5..f62ec7d 100755 --- a/stream.go +++ b/stream.go @@ -18,13 +18,15 @@ import ( // Stream represents a stream to monitor type Stream struct { - Name string `json:"name"` - URL string `json:"url"` - Base64 string `json:"base64"` - Interval int `json:"interval"` - WatchAreas []WatchArea `json:"watchAreas"` - Timeouts float64 `json:"-"` - FileLock sync.Mutex `json:"-"` + Name string `json:"name"` + URL string `json:"url"` + Base64 string `json:"base64"` + Interval int `json:"interval"` + MotionInterval int `json:"motionInterval"` + WatchAreas []WatchArea `json:"watchAreas"` + Timeouts float64 `json:"-"` + MotionDetected bool `json:"-"` + FileLock sync.Mutex `json:"-"` } // NewStream creates a new Stream Object @@ -32,12 +34,14 @@ func NewStream(Name string, URL string) *Stream { base64 := URLToBase64(URL) stream := Stream{ - Name: Name, - URL: URL, - Base64: base64, - Interval: 5000, - WatchAreas: make([]WatchArea, 0), - Timeouts: 0, + Name: Name, + URL: URL, + Base64: base64, + Interval: 5000, + MotionInterval: 500, + WatchAreas: make([]WatchArea, 0), + Timeouts: 0, + MotionDetected: false, } stream.WriteStreamJSON() return &stream @@ -47,7 +51,8 @@ func NewStream(Name string, URL string) *Stream { func StreamFromJSON(path string) *Stream { streamJSONFile, _ := ioutil.ReadFile(path) stream := Stream{ - Timeouts: 0, + Timeouts: 0, + MotionDetected: false, } json.Unmarshal([]byte(streamJSONFile), &stream) return &stream @@ -95,13 +100,15 @@ func (s *Stream) Update() { gocv.CvtColor(debug, &debug, gocv.ColorGrayToBGRA) contours := gocv.FindContours(diff, gocv.RetrievalExternal, gocv.ChainApproxSimple) + s.MotionDetected = false for _, watchArea := range s.WatchAreas { gocv.Rectangle(&debug, watchArea.Area, watchArea.Color, 10) if s.CheckWatchAreas(watchArea.Area, contours) { + s.MotionDetected = true go watchArea.CopyInstant(s.GetCurrentColorInstantPath(), *s) } } - go gocv.IMWrite(s.GetDebugInstantPath(), debug) + gocv.IMWrite(s.GetDebugInstantPath(), debug) } // CheckWatchAreas unexported @@ -119,10 +126,14 @@ func (s *Stream) CheckWatchAreas(area image.Rectangle, contours [][]image.Point) // UpdateInterval calls Update() every interval func (s *Stream) UpdateInterval() { for { - go s.Update() - expTimeout := math.Pow(2, s.Timeouts) - maxExpTimeout := (int)(math.Min(12, expTimeout)) - time.Sleep(time.Duration(s.Interval*maxExpTimeout) * time.Millisecond) + s.Update() + if s.MotionDetected { + time.Sleep(time.Duration(s.MotionInterval) * time.Millisecond) + } else { + expTimeout := math.Pow(2, s.Timeouts) + maxExpTimeout := (int)(math.Min(12, expTimeout)) + time.Sleep(time.Duration(s.Interval*maxExpTimeout) * time.Millisecond) + } } }