diff --git a/main.go b/main.go index 2de9414..0e98af3 100755 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "path/filepath" "strconv" "text/template" + "time" ) 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) } +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() { staticFileServer := http.FileServer(http.Dir("./static")) http.Handle("/static/", http.StripPrefix("/static/", staticFileServer)) @@ -209,5 +272,6 @@ func main() { http.HandleFunc("/", server.index) http.HandleFunc("/addStream", server.addStream) http.HandleFunc("/addWatchArea", server.addWatchArea) + http.HandleFunc("/streamRecordings", server.streamRecordings) log.Fatal(http.ListenAndServe(":8080", nil)) } diff --git a/stream.go b/stream.go index af27c88..8ec92c7 100755 --- a/stream.go +++ b/stream.go @@ -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 func (s *Stream) SaveStreamInstant(mat gocv.Mat) { s.FileLock.Lock() diff --git a/templates/stream.html b/templates/stream.html index 600743c..ba52a4b 100755 --- a/templates/stream.html +++ b/templates/stream.html @@ -33,6 +33,7 @@