update ticks are started at startup and when a new stream is added

This commit is contained in:
BroodjeAap 2020-07-07 20:14:25 +00:00
parent 863d9e3577
commit 40fafe91e5
2 changed files with 12 additions and 4 deletions

View file

@ -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)

View file

@ -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()
}