got basic 'moments' page working, showing the moments when motion was detected

This commit is contained in:
BroodjeAap 2020-08-04 19:26:29 +00:00
parent ee0f8815fc
commit b6dcd6a93b
2 changed files with 36 additions and 2 deletions

27
main.go
View file

@ -16,6 +16,7 @@ var baseHTML = filepath.Join("templates", "base.html")
var indexHTML = filepath.Join("templates", "index.html") var indexHTML = filepath.Join("templates", "index.html")
var addStreamHTML = filepath.Join("templates", "add_stream.html") var addStreamHTML = filepath.Join("templates", "add_stream.html")
var streamHTML = filepath.Join("templates", "stream.html") var streamHTML = filepath.Join("templates", "stream.html")
var momentsHTML = filepath.Join("templates", "moments.html")
// Server is the main application struct // Server is the main application struct
type Server struct { type Server struct {
@ -209,10 +210,14 @@ func (server Server) streamRecordings(w http.ResponseWriter, r *http.Request) {
} }
log.Println("got images: ", len(images)) log.Println("got images: ", len(images))
moments := make([][]string, 0) moments := make([][]string, 0)
currentMoment := make([]string, 0)
momentIndex := 0 momentIndex := 0
foundMoment := false foundMoment := false
for i := range images { for i := range images {
if i == 0 {
continue
}
log.Println(i) log.Println(i)
previousImage := images[i-1] previousImage := images[i-1]
previousTime, err := time.Parse(timeLayout, previousImage.Name()) previousTime, err := time.Parse(timeLayout, previousImage.Name())
@ -233,15 +238,33 @@ func (server Server) streamRecordings(w http.ResponseWriter, r *http.Request) {
timeDifference := currentTime.Sub(previousTime) timeDifference := currentTime.Sub(previousTime)
if timeDifference < (time.Duration(stream.Interval) * time.Millisecond) { if timeDifference < (time.Duration(stream.Interval) * time.Millisecond) {
foundMoment = true foundMoment = true
moments[momentIndex] = append(moments[momentIndex], previousImage.Name()) currentMoment = append(currentMoment, previousImage.Name())
} else { } else {
if foundMoment { if foundMoment {
currentMoment = append(currentMoment, currentImage.Name())
moments = append(moments, currentMoment)
currentMoment = make([]string, 0)
momentIndex++ momentIndex++
} }
foundMoment = false foundMoment = false
} }
} }
log.Println(moments) momentsTemplate, err := template.ParseFiles(momentsHTML, baseHTML)
if err != nil {
log.Println(err)
return
}
momentsTemplate.Execute(w, struct {
Streams map[string]*Stream
Moments [][]string
Stream *Stream
WatchArea *WatchArea
}{
server.Streams,
moments,
stream,
watchArea,
})
} }
func main() { func main() {

11
templates/moments.html Executable file
View file

@ -0,0 +1,11 @@
{{template "base" .}}
{{define "head"}}
<script src="/static/js/moments.js"></script>
{{end}}
{{define "content"}}
{{range $moment := .Moments}}
{{range $image := $moment}}
<img src="/streams/{{$.Stream.Name}}/{{$.WatchArea.Name}}/{{$image}}">
{{end}}
{{end}}
{{end}}