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

@ -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)
}
}
}