From 40fafe91e50379047408ffc65b403a5c09125df8 Mon Sep 17 00:00:00 2001 From: BroodjeAap Date: Tue, 7 Jul 2020 20:14:25 +0000 Subject: [PATCH] update ticks are started at startup and when a new stream is added --- main.go | 7 +++++-- stream.go | 9 +++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 17791e2..c24d339 100755 --- a/main.go +++ b/main.go @@ -31,6 +31,7 @@ func (server Server) index(w http.ResponseWriter, r *http.Request) { if !exists { stream = NewStream(URL) server.Streams[URL] = stream + go stream.Update() } streamTemplate, err := template.ParseFiles(filepath.Join("templates", "stream.html")) @@ -48,8 +49,10 @@ func (server Server) stream(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "image/jpeg") URL := r.FormValue("URL") - stream := NewStream(URL) - stream.WriteStreamJSON() + stream, exists := server.Streams[URL] + if !exists { + return + } img := stream.GetStreamInstant(URL) mat, err := gocv.IMDecode(img, gocv.IMReadGrayScale) diff --git a/stream.go b/stream.go index 3739e56..c3294b5 100755 --- a/stream.go +++ b/stream.go @@ -22,16 +22,21 @@ type Stream struct { // NewStream creates a new Stream Object func NewStream(URL string) Stream { base64 := URLToBase64(URL) - return Stream{ + + stream := Stream{ URL: URL, Base64: base64, Interval: 5000, } + os.MkdirAll(stream.GetStreamStoreDirPath(), os.ModePerm) + stream.WriteStreamJSON() + return stream } // Update calls itself every Interval milliseconds -func (s Stream) Update() { +func (s Stream) Update() { // TODO make this not overflow, other thing calls this ? log.Print("Update:", s.URL) + // TODO make func that does getstreaminstant and savestreaminstant time.Sleep(time.Duration(s.Interval) * time.Millisecond) go s.Update() }