added 'MotionInterval' that is used instead of 'Interval' when motion is detected on a Stream

This commit is contained in:
BroodjeAap 2020-07-25 12:39:19 +00:00
parent 3e2e9a8182
commit f8b13dc3ef

View file

@ -22,8 +22,10 @@ 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"`
MotionInterval int `json:"motionInterval"`
WatchAreas []WatchArea `json:"watchAreas"` WatchAreas []WatchArea `json:"watchAreas"`
Timeouts float64 `json:"-"` Timeouts float64 `json:"-"`
MotionDetected bool `json:"-"`
FileLock sync.Mutex `json:"-"` FileLock sync.Mutex `json:"-"`
} }
@ -36,8 +38,10 @@ func NewStream(Name string, URL string) *Stream {
URL: URL, URL: URL,
Base64: base64, Base64: base64,
Interval: 5000, Interval: 5000,
MotionInterval: 500,
WatchAreas: make([]WatchArea, 0), WatchAreas: make([]WatchArea, 0),
Timeouts: 0, Timeouts: 0,
MotionDetected: false,
} }
stream.WriteStreamJSON() stream.WriteStreamJSON()
return &stream return &stream
@ -48,6 +52,7 @@ func StreamFromJSON(path string) *Stream {
streamJSONFile, _ := ioutil.ReadFile(path) streamJSONFile, _ := ioutil.ReadFile(path)
stream := Stream{ stream := Stream{
Timeouts: 0, Timeouts: 0,
MotionDetected: false,
} }
json.Unmarshal([]byte(streamJSONFile), &stream) json.Unmarshal([]byte(streamJSONFile), &stream)
return &stream return &stream
@ -95,13 +100,15 @@ func (s *Stream) Update() {
gocv.CvtColor(debug, &debug, gocv.ColorGrayToBGRA) gocv.CvtColor(debug, &debug, gocv.ColorGrayToBGRA)
contours := gocv.FindContours(diff, gocv.RetrievalExternal, gocv.ChainApproxSimple) contours := gocv.FindContours(diff, gocv.RetrievalExternal, gocv.ChainApproxSimple)
s.MotionDetected = false
for _, watchArea := range s.WatchAreas { for _, watchArea := range s.WatchAreas {
gocv.Rectangle(&debug, watchArea.Area, watchArea.Color, 10) gocv.Rectangle(&debug, watchArea.Area, watchArea.Color, 10)
if s.CheckWatchAreas(watchArea.Area, contours) { if s.CheckWatchAreas(watchArea.Area, contours) {
s.MotionDetected = true
go watchArea.CopyInstant(s.GetCurrentColorInstantPath(), *s) go watchArea.CopyInstant(s.GetCurrentColorInstantPath(), *s)
} }
} }
go gocv.IMWrite(s.GetDebugInstantPath(), debug) gocv.IMWrite(s.GetDebugInstantPath(), debug)
} }
// CheckWatchAreas unexported // CheckWatchAreas unexported
@ -119,11 +126,15 @@ func (s *Stream) CheckWatchAreas(area image.Rectangle, contours [][]image.Point)
// UpdateInterval calls Update() every interval // UpdateInterval calls Update() every interval
func (s *Stream) UpdateInterval() { func (s *Stream) UpdateInterval() {
for { for {
go s.Update() s.Update()
if s.MotionDetected {
time.Sleep(time.Duration(s.MotionInterval) * time.Millisecond)
} else {
expTimeout := math.Pow(2, s.Timeouts) expTimeout := math.Pow(2, s.Timeouts)
maxExpTimeout := (int)(math.Min(12, expTimeout)) maxExpTimeout := (int)(math.Min(12, expTimeout))
time.Sleep(time.Duration(s.Interval*maxExpTimeout) * time.Millisecond) time.Sleep(time.Duration(s.Interval*maxExpTimeout) * time.Millisecond)
} }
}
} }
// SaveStreamInstant writes the img to the CurrentStreamInstantPath, moves existing instant to PreviousStreamInstantPath // SaveStreamInstant writes the img to the CurrentStreamInstantPath, moves existing instant to PreviousStreamInstantPath