added 'MotionInterval' that is used instead of 'Interval' when motion is detected on a Stream
This commit is contained in:
parent
3e2e9a8182
commit
f8b13dc3ef
1 changed files with 30 additions and 19 deletions
49
stream.go
49
stream.go
|
|
@ -18,13 +18,15 @@ import (
|
||||||
|
|
||||||
// Stream represents a stream to monitor
|
// Stream represents a stream to monitor
|
||||||
type Stream struct {
|
type Stream struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
Base64 string `json:"base64"`
|
Base64 string `json:"base64"`
|
||||||
Interval int `json:"interval"`
|
Interval int `json:"interval"`
|
||||||
WatchAreas []WatchArea `json:"watchAreas"`
|
MotionInterval int `json:"motionInterval"`
|
||||||
Timeouts float64 `json:"-"`
|
WatchAreas []WatchArea `json:"watchAreas"`
|
||||||
FileLock sync.Mutex `json:"-"`
|
Timeouts float64 `json:"-"`
|
||||||
|
MotionDetected bool `json:"-"`
|
||||||
|
FileLock sync.Mutex `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewStream creates a new Stream Object
|
// NewStream creates a new Stream Object
|
||||||
|
|
@ -32,12 +34,14 @@ func NewStream(Name string, URL string) *Stream {
|
||||||
base64 := URLToBase64(URL)
|
base64 := URLToBase64(URL)
|
||||||
|
|
||||||
stream := Stream{
|
stream := Stream{
|
||||||
Name: Name,
|
Name: Name,
|
||||||
URL: URL,
|
URL: URL,
|
||||||
Base64: base64,
|
Base64: base64,
|
||||||
Interval: 5000,
|
Interval: 5000,
|
||||||
WatchAreas: make([]WatchArea, 0),
|
MotionInterval: 500,
|
||||||
Timeouts: 0,
|
WatchAreas: make([]WatchArea, 0),
|
||||||
|
Timeouts: 0,
|
||||||
|
MotionDetected: false,
|
||||||
}
|
}
|
||||||
stream.WriteStreamJSON()
|
stream.WriteStreamJSON()
|
||||||
return &stream
|
return &stream
|
||||||
|
|
@ -47,7 +51,8 @@ func NewStream(Name string, URL string) *Stream {
|
||||||
func StreamFromJSON(path string) *Stream {
|
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,10 +126,14 @@ 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()
|
||||||
expTimeout := math.Pow(2, s.Timeouts)
|
if s.MotionDetected {
|
||||||
maxExpTimeout := (int)(math.Min(12, expTimeout))
|
time.Sleep(time.Duration(s.MotionInterval) * time.Millisecond)
|
||||||
time.Sleep(time.Duration(s.Interval*maxExpTimeout) * time.Millisecond)
|
} else {
|
||||||
|
expTimeout := math.Pow(2, s.Timeouts)
|
||||||
|
maxExpTimeout := (int)(math.Min(12, expTimeout))
|
||||||
|
time.Sleep(time.Duration(s.Interval*maxExpTimeout) * time.Millisecond)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue