in between commit for the 'moments' page

This commit is contained in:
BroodjeAap 2020-08-02 15:53:24 +00:00
parent 65ce667185
commit fe23fd2355
3 changed files with 77 additions and 0 deletions

64
main.go
View file

@ -9,6 +9,7 @@ import (
"path/filepath" "path/filepath"
"strconv" "strconv"
"text/template" "text/template"
"time"
) )
var baseHTML = filepath.Join("templates", "base.html") var baseHTML = filepath.Join("templates", "base.html")
@ -181,6 +182,68 @@ func (server Server) addWatchArea(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/?name="+streamName, http.StatusTemporaryRedirect) http.Redirect(w, r, "/?name="+streamName, http.StatusTemporaryRedirect)
} }
func (server Server) streamRecordings(w http.ResponseWriter, r *http.Request) {
streamName := r.FormValue("stream")
if streamName == "" {
return
}
watchAreaName := r.FormValue("watchArea")
if watchAreaName == "" {
return
}
stream, exists := server.Streams[streamName]
if !exists {
return
}
watchArea, exists := stream.GetWatchAreaByName(watchAreaName)
if !exists {
return
}
images, err := ioutil.ReadDir(watchArea.GetWatchAreaStoreDir(*stream))
if err != nil {
log.Println("Could not read watchArea directory", err)
return
}
log.Println("got images: ", len(images))
moments := make([][]string, 0)
momentIndex := 0
foundMoment := false
for i := range images {
log.Println(i)
previousImage := images[i-1]
previousTime, err := time.Parse(timeLayout, previousImage.Name())
if err != nil {
log.Println("Can't parse: ", previousImage.Name())
log.Println(err)
continue
}
currentImage := images[i]
currentTime, err := time.Parse(timeLayout, currentImage.Name())
if err != nil {
log.Println("Can't parse: ", currentImage.Name())
log.Println(err)
continue
}
timeDifference := currentTime.Sub(previousTime)
if timeDifference < (time.Duration(stream.Interval) * time.Millisecond) {
foundMoment = true
moments[momentIndex] = append(moments[momentIndex], previousImage.Name())
} else {
if foundMoment {
momentIndex++
}
foundMoment = false
}
}
log.Println(moments)
}
func main() { func main() {
staticFileServer := http.FileServer(http.Dir("./static")) staticFileServer := http.FileServer(http.Dir("./static"))
http.Handle("/static/", http.StripPrefix("/static/", staticFileServer)) http.Handle("/static/", http.StripPrefix("/static/", staticFileServer))
@ -209,5 +272,6 @@ func main() {
http.HandleFunc("/", server.index) http.HandleFunc("/", server.index)
http.HandleFunc("/addStream", server.addStream) http.HandleFunc("/addStream", server.addStream)
http.HandleFunc("/addWatchArea", server.addWatchArea) http.HandleFunc("/addWatchArea", server.addWatchArea)
http.HandleFunc("/streamRecordings", server.streamRecordings)
log.Fatal(http.ListenAndServe(":8080", nil)) log.Fatal(http.ListenAndServe(":8080", nil))
} }

View file

@ -137,6 +137,16 @@ func (s *Stream) UpdateInterval() {
} }
} }
// GetWatchAreaByName finds the WatchArea with the name, if it doesn't exist returns nil, false
func (s *Stream) GetWatchAreaByName(name string) (*WatchArea, bool) {
for _, watchArea := range s.WatchAreas {
if name == watchArea.Name {
return &watchArea, true
}
}
return nil, false
}
// SaveStreamInstant writes the img to the CurrentStreamInstantPath, moves existing instant to PreviousStreamInstantPath // SaveStreamInstant writes the img to the CurrentStreamInstantPath, moves existing instant to PreviousStreamInstantPath
func (s *Stream) SaveStreamInstant(mat gocv.Mat) { func (s *Stream) SaveStreamInstant(mat gocv.Mat) {
s.FileLock.Lock() s.FileLock.Lock()

View file

@ -33,6 +33,7 @@
<th colspan="3">Color</th> <th colspan="3">Color</th>
<th colspan="4">Area</th> <th colspan="4">Area</th>
<th>Add/Remove</th> <th>Add/Remove</th>
<th>View</th>
</tr> </tr>
<tr> <tr>
<th></th> <th></th>
@ -44,6 +45,7 @@
<th>MaxX</th> <th>MaxX</th>
<th>MaxY</th> <th>MaxY</th>
<th></th> <th></th>
<th></th>
</tr> </tr>
</thead> </thead>
<tbody id="watchAreas"> <tbody id="watchAreas">
@ -58,6 +60,7 @@
<td>{{ $watchArea.Area.Max.X }}</td> <td>{{ $watchArea.Area.Max.X }}</td>
<td>{{ $watchArea.Area.Max.Y }}</td> <td>{{ $watchArea.Area.Max.Y }}</td>
<td>Remove</td> <td>Remove</td>
<td><a href="/streamRecordings?stream={{$.Stream.Name}}&watchArea={{$watchArea.Name}}">View</a></td>
</tr> </tr>
{{end}} {{end}}
</tbody> </tbody>