got basic 'moments' page working, showing the moments when motion was detected
This commit is contained in:
parent
ee0f8815fc
commit
b6dcd6a93b
2 changed files with 36 additions and 2 deletions
27
main.go
27
main.go
|
|
@ -16,6 +16,7 @@ var baseHTML = filepath.Join("templates", "base.html")
|
|||
var indexHTML = filepath.Join("templates", "index.html")
|
||||
var addStreamHTML = filepath.Join("templates", "add_stream.html")
|
||||
var streamHTML = filepath.Join("templates", "stream.html")
|
||||
var momentsHTML = filepath.Join("templates", "moments.html")
|
||||
|
||||
// Server is the main application struct
|
||||
type Server struct {
|
||||
|
|
@ -209,10 +210,14 @@ func (server Server) streamRecordings(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
log.Println("got images: ", len(images))
|
||||
moments := make([][]string, 0)
|
||||
currentMoment := make([]string, 0)
|
||||
momentIndex := 0
|
||||
foundMoment := false
|
||||
|
||||
for i := range images {
|
||||
if i == 0 {
|
||||
continue
|
||||
}
|
||||
log.Println(i)
|
||||
previousImage := images[i-1]
|
||||
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)
|
||||
if timeDifference < (time.Duration(stream.Interval) * time.Millisecond) {
|
||||
foundMoment = true
|
||||
moments[momentIndex] = append(moments[momentIndex], previousImage.Name())
|
||||
currentMoment = append(currentMoment, previousImage.Name())
|
||||
} else {
|
||||
if foundMoment {
|
||||
currentMoment = append(currentMoment, currentImage.Name())
|
||||
moments = append(moments, currentMoment)
|
||||
currentMoment = make([]string, 0)
|
||||
momentIndex++
|
||||
}
|
||||
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() {
|
||||
|
|
|
|||
11
templates/moments.html
Executable file
11
templates/moments.html
Executable 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}}
|
||||
Loading…
Add table
Reference in a new issue