added some comments to main.go

This commit is contained in:
BroodjeAap 2020-08-18 18:23:08 +00:00
parent bbb4392783
commit 8c29d47eeb

11
main.go
View file

@ -20,10 +20,12 @@ var streamHTML = filepath.Join("templates", "stream.html")
var momentsHTML = filepath.Join("templates", "moments.html") var momentsHTML = filepath.Join("templates", "moments.html")
// Server is the main application struct // Server is the main application struct
// It holds a map[string]*Stream that contains all the Stream objects
type Server struct { type Server struct {
Streams map[string]*Stream Streams map[string]*Stream
} }
// index shows a table of available streams or when visited with a 'name' param, the stream with that name.
func (server Server) index(w http.ResponseWriter, r *http.Request) { func (server Server) index(w http.ResponseWriter, r *http.Request) {
if r.FormValue("name") == "" { if r.FormValue("name") == "" {
indexTemplate, err := template.ParseFiles(indexHTML, baseHTML) indexTemplate, err := template.ParseFiles(indexHTML, baseHTML)
@ -58,6 +60,7 @@ func (server Server) index(w http.ResponseWriter, r *http.Request) {
}) })
} }
// addStream adds the POSTed Stream to Server.Streams
func (server Server) addStream(w http.ResponseWriter, r *http.Request) { func (server Server) addStream(w http.ResponseWriter, r *http.Request) {
nameMessage := "" nameMessage := ""
URLMessage := "" URLMessage := ""
@ -126,6 +129,7 @@ func (server Server) addStream(w http.ResponseWriter, r *http.Request) {
}) })
} }
// addWatchArea adds the POSTed WatchArea to Stream.WatchAreas
func (server Server) addWatchArea(w http.ResponseWriter, r *http.Request) { func (server Server) addWatchArea(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" { if r.Method != "POST" {
return return
@ -188,6 +192,7 @@ 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)
} }
// streamRecordings shows a table of 'moments', a continuous series of motion detected instants
func (server Server) streamRecordings(w http.ResponseWriter, r *http.Request) { func (server Server) streamRecordings(w http.ResponseWriter, r *http.Request) {
streamName := r.FormValue("stream") streamName := r.FormValue("stream")
if streamName == "" { if streamName == "" {
@ -219,7 +224,7 @@ func (server Server) streamRecordings(w http.ResponseWriter, r *http.Request) {
foundMoment := false foundMoment := false
for i := range images { for i := range images {
if i == 0 { if i == 0 { // Skip first for images[i-1] after this if
continue continue
} }
previousImage := images[i-1] previousImage := images[i-1]
@ -238,6 +243,8 @@ func (server Server) streamRecordings(w http.ResponseWriter, r *http.Request) {
continue continue
} }
// if the time difference is lower then the Stream.Interval, it means the Stream.MotionInterval
// Was used, and motion was therefore detected.
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
@ -273,6 +280,7 @@ func (server Server) streamRecordings(w http.ResponseWriter, r *http.Request) {
}) })
} }
// main sets up the HTTP server and reconstructs the Streams, if available.
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))
@ -292,6 +300,7 @@ func main() {
continue continue
} }
// UnMarshal the stream.json into a Stream struct and start a goroutine for UpdateInterval
streamJSONPath := filepath.Join(GetStreamDirPath(), streamStoreDir.Name(), "stream.json") streamJSONPath := filepath.Join(GetStreamDirPath(), streamStoreDir.Name(), "stream.json")
stream := StreamFromJSON(streamJSONPath) stream := StreamFromJSON(streamJSONPath)
server.Streams[stream.Name] = stream server.Streams[stream.Name] = stream