From eb8b0d4d61b7f20657a165c0f7b33b0fdb1ed121 Mon Sep 17 00:00:00 2001 From: BroodjeAap Date: Thu, 9 Jul 2020 18:24:17 +0000 Subject: [PATCH] removed URL parameter from GetStreamInstant --- main.go | 2 +- stream.go | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index c24d339..eba6230 100755 --- a/main.go +++ b/main.go @@ -54,7 +54,7 @@ func (server Server) stream(w http.ResponseWriter, r *http.Request) { return } - img := stream.GetStreamInstant(URL) + img := stream.GetStreamInstant() mat, err := gocv.IMDecode(img, gocv.IMReadGrayScale) if err != nil { log.Fatal("Could not IMDecode img") diff --git a/stream.go b/stream.go index c3294b5..96b464c 100755 --- a/stream.go +++ b/stream.go @@ -28,22 +28,26 @@ func NewStream(URL string) Stream { Base64: base64, Interval: 5000, } - os.MkdirAll(stream.GetStreamStoreDirPath(), os.ModePerm) stream.WriteStreamJSON() return stream } // Update calls itself every Interval milliseconds -func (s Stream) Update() { // TODO make this not overflow, other thing calls this ? +func (s Stream) Update() { log.Print("Update:", s.URL) - // TODO make func that does getstreaminstant and savestreaminstant - time.Sleep(time.Duration(s.Interval) * time.Millisecond) - go s.Update() +} + +// UpdateInterval calls Update() every interval +func (s Stream) UpdateInterval() { + for { + s.Update() + time.Sleep(time.Duration(s.Interval) * time.Millisecond) + } } // GetStreamInstant http.Get(URL) and returns the response -func (s Stream) GetStreamInstant(URL string) []byte { - resp, err := http.Get(URL) +func (s Stream) GetStreamInstant() []byte { + resp, err := http.Get(s.URL) if err != nil { log.Fatal(err) } @@ -90,6 +94,9 @@ func (s Stream) GetStreamJSONPath() string { // WriteStreamJSON writes the Stream struct to GetStreamJSONPath() func (s Stream) WriteStreamJSON() { + if !FileExists(s.GetStreamStoreDirPath()) { + os.MkdirAll(s.GetStreamStoreDirPath(), os.ModePerm) + } file, _ := json.MarshalIndent(s, "", "") _ = ioutil.WriteFile(s.GetStreamJSONPath(), file, 0644) }