From 27d0297c80b3339b4b4600317d146457748b3f1b Mon Sep 17 00:00:00 2001 From: BroodjeAap Date: Sat, 25 Jul 2020 11:16:40 +0000 Subject: [PATCH] moved loading Streams from json to a function --- main.go | 7 ++----- stream.go | 44 +++++++++++++++++++++++++++----------------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/main.go b/main.go index 59207c3..fd3833c 100755 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "encoding/json" "image" "image/color" "io/ioutil" @@ -161,10 +160,8 @@ func main() { } streamJSONPath := filepath.Join(GetStreamDirPath(), streamStoreDir.Name(), "stream.json") - streamJSONFile, _ := ioutil.ReadFile(streamJSONPath) - stream := Stream{} - json.Unmarshal([]byte(streamJSONFile), &stream) - server.Streams[stream.Name] = &stream + stream := StreamFromJSON(streamJSONPath) + server.Streams[stream.Name] = stream go stream.UpdateInterval() } diff --git a/stream.go b/stream.go index cc7eb9a..efc5f38 100755 --- a/stream.go +++ b/stream.go @@ -36,27 +36,37 @@ func NewStream(Name string, URL string) *Stream { Interval: 5000, WatchAreas: make([]WatchArea, 0), } - stream.WatchAreas = append(stream.WatchAreas, - WatchArea{ - Name: "test", - Color: color.RGBA{255, 0, 255, 255}, - Area: image.Rect(30, 30, 400, 200), - }, - WatchArea{ - Name: "test2", - Color: color.RGBA{0, 255, 255, 255}, - Area: image.Rect(800, 30, 1000, 500), - }, - WatchArea{ - Name: "test3", - Color: color.RGBA{255, 255, 0, 255}, - Area: image.Rect(50, 400, 450, 700), - }, - ) + /* + stream.WatchAreas = append(stream.WatchAreas, + WatchArea{ + Name: "test", + Color: color.RGBA{255, 0, 255, 255}, + Area: image.Rect(30, 30, 400, 200), + }, + WatchArea{ + Name: "test2", + Color: color.RGBA{0, 255, 255, 255}, + Area: image.Rect(800, 30, 1000, 500), + }, + WatchArea{ + Name: "test3", + Color: color.RGBA{255, 255, 0, 255}, + Area: image.Rect(50, 400, 450, 700), + }, + ) + */ stream.WriteStreamJSON() return &stream } +// StreamFromJSON takes a filepath to a JSON file, and returns the unmarshalled Stream object +func StreamFromJSON(path string) *Stream { + streamJSONFile, _ := ioutil.ReadFile(path) + stream := Stream{} + json.Unmarshal([]byte(streamJSONFile), &stream) + return &stream +} + // Update gets called by UpdateInterval Interval milliseconds to fetch the latest instant func (s *Stream) Update() { log.Print("Update: ", s.Name, " - ", len(s.WatchAreas), " - ", s.URL)