Switching to map[string]*Stream and making all Stream methods (s * Stream) worked
This commit is contained in:
parent
f9a6c37350
commit
3579788727
2 changed files with 55 additions and 51 deletions
29
main.go
29
main.go
|
|
@ -18,7 +18,7 @@ var streamHTML = filepath.Join("templates", "stream.html")
|
|||
|
||||
// Server is the main application struct
|
||||
type Server struct {
|
||||
Streams map[string]Stream
|
||||
Streams map[string]*Stream
|
||||
}
|
||||
|
||||
func (server Server) index(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -41,7 +41,6 @@ func (server Server) index(w http.ResponseWriter, r *http.Request) {
|
|||
log.Fatal(err)
|
||||
}
|
||||
streamTemplate.Execute(w, stream)
|
||||
log.Println(server.Streams)
|
||||
}
|
||||
|
||||
func (server Server) addStream(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -66,6 +65,7 @@ func (server Server) addStream(w http.ResponseWriter, r *http.Request) {
|
|||
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
||||
}
|
||||
}
|
||||
|
||||
addStreamTemplate, err := template.ParseFiles(addStreamHTML, baseHTML)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
|
@ -123,22 +123,21 @@ func (server Server) addWatchArea(w http.ResponseWriter, r *http.Request) {
|
|||
if B == "" {
|
||||
return
|
||||
}
|
||||
stream.WatchAreas = append(stream.WatchAreas, WatchArea{
|
||||
Name: name,
|
||||
Color: color.RGBA{
|
||||
uint8(StrToInt(R)),
|
||||
uint8(StrToInt(G)),
|
||||
uint8(StrToInt(B)),
|
||||
255,
|
||||
},
|
||||
Area: image.Rect(
|
||||
stream.AddWatchArea(
|
||||
name,
|
||||
image.Rect(
|
||||
StrToInt(x0),
|
||||
StrToInt(y0),
|
||||
StrToInt(x1),
|
||||
StrToInt(y1),
|
||||
),
|
||||
})
|
||||
stream.WriteStreamJSON()
|
||||
color.RGBA{
|
||||
uint8(StrToInt(R)),
|
||||
uint8(StrToInt(G)),
|
||||
uint8(StrToInt(B)),
|
||||
255,
|
||||
},
|
||||
)
|
||||
http.Redirect(w, r, "/?name="+streamName, http.StatusTemporaryRedirect)
|
||||
}
|
||||
|
||||
|
|
@ -150,7 +149,7 @@ func main() {
|
|||
http.Handle("/streams/", http.StripPrefix("/streams/", streamFileServer))
|
||||
|
||||
server := Server{
|
||||
Streams: make(map[string]Stream),
|
||||
Streams: make(map[string]*Stream),
|
||||
}
|
||||
streams, err := ioutil.ReadDir(GetStreamDirPath())
|
||||
if err != nil {
|
||||
|
|
@ -165,7 +164,7 @@ func main() {
|
|||
streamJSONFile, _ := ioutil.ReadFile(streamJSONPath)
|
||||
stream := Stream{}
|
||||
json.Unmarshal([]byte(streamJSONFile), &stream)
|
||||
server.Streams[stream.Name] = stream
|
||||
server.Streams[stream.Name] = &stream
|
||||
go stream.UpdateInterval()
|
||||
}
|
||||
|
||||
|
|
|
|||
77
stream.go
77
stream.go
|
|
@ -26,7 +26,7 @@ type Stream struct {
|
|||
}
|
||||
|
||||
// NewStream creates a new Stream Object
|
||||
func NewStream(Name string, URL string) Stream {
|
||||
func NewStream(Name string, URL string) *Stream {
|
||||
base64 := URLToBase64(URL)
|
||||
|
||||
stream := Stream{
|
||||
|
|
@ -54,12 +54,12 @@ func NewStream(Name string, URL string) Stream {
|
|||
},
|
||||
)
|
||||
stream.WriteStreamJSON()
|
||||
return stream
|
||||
return &stream
|
||||
}
|
||||
|
||||
// Update gets called by UpdateInterval Interval milliseconds to fetch the latest instant
|
||||
func (s Stream) Update() {
|
||||
log.Print("Update: ", s.Name, " - ", s.URL)
|
||||
func (s *Stream) Update() {
|
||||
log.Print("Update: ", s.Name, " - ", len(s.WatchAreas), " - ", s.URL)
|
||||
resp, err := http.Get(s.URL)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
|
@ -98,14 +98,14 @@ func (s Stream) Update() {
|
|||
for _, watchArea := range s.WatchAreas {
|
||||
gocv.Rectangle(&debug, watchArea.Area, watchArea.Color, 10)
|
||||
if s.CheckWatchAreas(watchArea.Area, contours) {
|
||||
go watchArea.CopyInstant(s.GetCurrentColorInstantPath(), s)
|
||||
go watchArea.CopyInstant(s.GetCurrentColorInstantPath(), *s)
|
||||
}
|
||||
}
|
||||
go gocv.IMWrite(s.GetDebugInstantPath(), debug)
|
||||
}
|
||||
|
||||
// CheckWatchAreas unexported
|
||||
func (s Stream) CheckWatchAreas(area image.Rectangle, contours [][]image.Point) bool {
|
||||
func (s *Stream) CheckWatchAreas(area image.Rectangle, contours [][]image.Point) bool {
|
||||
for _, contour := range contours {
|
||||
for _, point := range contour {
|
||||
if point.In(area) {
|
||||
|
|
@ -117,7 +117,7 @@ func (s Stream) CheckWatchAreas(area image.Rectangle, contours [][]image.Point)
|
|||
}
|
||||
|
||||
// UpdateInterval calls Update() every interval
|
||||
func (s Stream) UpdateInterval() {
|
||||
func (s *Stream) UpdateInterval() {
|
||||
for {
|
||||
go s.Update()
|
||||
time.Sleep(time.Duration(s.Interval) * time.Millisecond)
|
||||
|
|
@ -125,7 +125,7 @@ func (s Stream) UpdateInterval() {
|
|||
}
|
||||
|
||||
// GetStreamInstant http.Get(URL) and returns the response
|
||||
func (s Stream) GetStreamInstant() gocv.Mat {
|
||||
func (s *Stream) GetStreamInstant() gocv.Mat {
|
||||
resp, err := http.Get(s.URL)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
|
@ -142,7 +142,7 @@ func (s Stream) GetStreamInstant() gocv.Mat {
|
|||
}
|
||||
|
||||
// SaveStreamInstant writes the img to the CurrentStreamInstantPath, moves existing instant to PreviousStreamInstantPath
|
||||
func (s Stream) SaveStreamInstant(mat gocv.Mat) {
|
||||
func (s *Stream) SaveStreamInstant(mat gocv.Mat) {
|
||||
s.FileLock.Lock()
|
||||
streamStoreDir := s.GetStreamStoreDirPath()
|
||||
os.MkdirAll(streamStoreDir, os.ModePerm)
|
||||
|
|
@ -163,7 +163,7 @@ func (s Stream) SaveStreamInstant(mat gocv.Mat) {
|
|||
}
|
||||
|
||||
// SwapInstants swaps the file location, first current -> previous and then next -> current
|
||||
func (s Stream) SwapInstants(previous string, current string, next string) {
|
||||
func (s *Stream) SwapInstants(previous string, current string, next string) {
|
||||
err := os.Rename(current, previous)
|
||||
if err != nil {
|
||||
log.Fatal("Couldn't copy current image instant to previous image instant")
|
||||
|
|
@ -174,18 +174,23 @@ func (s Stream) SwapInstants(previous string, current string, next string) {
|
|||
}
|
||||
}
|
||||
|
||||
// AddWatchArea adds the given WatchArea to the list of WatchAreas
|
||||
func (s Stream) AddWatchArea(watchArea WatchArea) {
|
||||
s.WatchAreas = append(s.WatchAreas, watchArea)
|
||||
// AddWatchArea adds a watch area
|
||||
func (s *Stream) AddWatchArea(name string, area image.Rectangle, color color.RGBA) {
|
||||
s.WatchAreas = append(s.WatchAreas, WatchArea{
|
||||
Name: name,
|
||||
Color: color,
|
||||
Area: area,
|
||||
})
|
||||
s.WriteStreamJSON()
|
||||
}
|
||||
|
||||
// GetStreamJSONPath returns filepath.Join(s.GetStreamStoreDirPath(), "stream.json")
|
||||
func (s Stream) GetStreamJSONPath() string {
|
||||
func (s *Stream) GetStreamJSONPath() string {
|
||||
return filepath.Join(s.GetStreamStoreDirPath(), "stream.json")
|
||||
}
|
||||
|
||||
// WriteStreamJSON writes the Stream struct to GetStreamJSONPath()
|
||||
func (s Stream) WriteStreamJSON() {
|
||||
func (s *Stream) WriteStreamJSON() {
|
||||
if !FileExists(s.GetStreamStoreDirPath()) {
|
||||
os.MkdirAll(s.GetStreamStoreDirPath(), os.ModePerm)
|
||||
}
|
||||
|
|
@ -194,106 +199,106 @@ func (s Stream) WriteStreamJSON() {
|
|||
}
|
||||
|
||||
// GetStreamStoreDirPath returns filepath.Join(s.GetStreamDirPath(), s.Base64)
|
||||
func (s Stream) GetStreamStoreDirPath() string {
|
||||
func (s *Stream) GetStreamStoreDirPath() string {
|
||||
return filepath.Join(GetStreamDirPath(), s.Name)
|
||||
}
|
||||
|
||||
// GetPreviousInstantPath returns filepath.Join(GetStreamDirPath(), s.Base64, "previous.jpg")
|
||||
func (s Stream) GetPreviousInstantPath() string {
|
||||
func (s *Stream) GetPreviousInstantPath() string {
|
||||
return filepath.Join(s.GetStreamStoreDirPath(), "previous.jpg")
|
||||
}
|
||||
|
||||
// PreviousInstantPathExists returns FileExists(GetPreviousInstantPath())
|
||||
func (s Stream) PreviousInstantPathExists() bool {
|
||||
func (s *Stream) PreviousInstantPathExists() bool {
|
||||
return FileExists(s.GetPreviousInstantPath())
|
||||
}
|
||||
|
||||
// GetPreviousURL returns the URL towards the static file location of current.jpg
|
||||
func (s Stream) GetPreviousURL() string {
|
||||
func (s *Stream) GetPreviousURL() string {
|
||||
return filepath.Join("/streams", s.Name, "previous.jpg")
|
||||
}
|
||||
|
||||
// IMReadPrevious returns gocv.IMRead(GetPreviousInstantPath(), gocv.IMReadGrayScale)
|
||||
func (s Stream) IMReadPrevious() gocv.Mat {
|
||||
func (s *Stream) IMReadPrevious() gocv.Mat {
|
||||
return gocv.IMRead(s.GetPreviousInstantPath(), gocv.IMReadGrayScale)
|
||||
}
|
||||
|
||||
// GetCurrentInstantPath returns filepath.Join(s.GetStreamDirPath(), s.Base64, "current.jpg")
|
||||
func (s Stream) GetCurrentInstantPath() string {
|
||||
func (s *Stream) GetCurrentInstantPath() string {
|
||||
return filepath.Join(s.GetStreamStoreDirPath(), "current.jpg")
|
||||
}
|
||||
|
||||
// CurrentInstantPathExists returns FileExists(GetCurrentInstantPath())
|
||||
func (s Stream) CurrentInstantPathExists() bool {
|
||||
func (s *Stream) CurrentInstantPathExists() bool {
|
||||
return FileExists(s.GetCurrentInstantPath())
|
||||
}
|
||||
|
||||
// GetCurrentURL returns the URL towards the static file location of current.jpg
|
||||
func (s Stream) GetCurrentURL() string {
|
||||
func (s *Stream) GetCurrentURL() string {
|
||||
return filepath.Join("/streams", s.Name, "current.jpg")
|
||||
}
|
||||
|
||||
// IMReadCurrent returns gocv.IMRead(GetCurrentInstantPath(), gocv.IMReadGrayScale)
|
||||
func (s Stream) IMReadCurrent() gocv.Mat {
|
||||
func (s *Stream) IMReadCurrent() gocv.Mat {
|
||||
return gocv.IMRead(s.GetCurrentInstantPath(), gocv.IMReadGrayScale)
|
||||
}
|
||||
|
||||
// GetCurrentColorInstantPath returns filepath.Join(s.GetStreamDirPath(), s.Base64, "current_color.jpg")
|
||||
func (s Stream) GetCurrentColorInstantPath() string {
|
||||
func (s *Stream) GetCurrentColorInstantPath() string {
|
||||
return filepath.Join(s.GetStreamStoreDirPath(), "current_color.jpg")
|
||||
}
|
||||
|
||||
// CurrentColorInstantPathExists returns FileExists(GetCurrentColorInstantPath())
|
||||
func (s Stream) CurrentColorInstantPathExists() bool {
|
||||
func (s *Stream) CurrentColorInstantPathExists() bool {
|
||||
return FileExists(s.GetCurrentColorInstantPath())
|
||||
}
|
||||
|
||||
// GetCurrentColorURL returns the URL towards the static file location of debug.jpg
|
||||
func (s Stream) GetCurrentColorURL() string {
|
||||
func (s *Stream) GetCurrentColorURL() string {
|
||||
return filepath.Join("/streams", s.Name, "current_color.jpg")
|
||||
}
|
||||
|
||||
// IMReadCurrentColor returns gocv.IMRead(GetCurrentColorInstantPath(), gocv.IMReadColor)
|
||||
func (s Stream) IMReadCurrentColor() gocv.Mat {
|
||||
func (s *Stream) IMReadCurrentColor() gocv.Mat {
|
||||
return gocv.IMRead(s.GetCurrentColorInstantPath(), gocv.IMReadColor)
|
||||
}
|
||||
|
||||
// GetNextInstantPath returns filepath.Join(s.GetStreamDirPath(), s.Base64, "last.jpg")
|
||||
func (s Stream) GetNextInstantPath() string {
|
||||
func (s *Stream) GetNextInstantPath() string {
|
||||
return filepath.Join(s.GetStreamStoreDirPath(), "last.jpg")
|
||||
}
|
||||
|
||||
// NextInstantPathExists returns FileExists(GetNextInstantPath())
|
||||
func (s Stream) NextInstantPathExists() bool {
|
||||
func (s *Stream) NextInstantPathExists() bool {
|
||||
return FileExists(s.GetNextInstantPath())
|
||||
}
|
||||
|
||||
// GetNextURL returns the URL towards the static file location of next.jpg
|
||||
func (s Stream) GetNextURL() string {
|
||||
func (s *Stream) GetNextURL() string {
|
||||
return filepath.Join("/streams", s.Name, "next.jpg")
|
||||
}
|
||||
|
||||
// IMReadNext returns gocv.IMRead(GetNextInstantPath(), gocv.IMReadGrayScale)
|
||||
func (s Stream) IMReadNext() gocv.Mat {
|
||||
func (s *Stream) IMReadNext() gocv.Mat {
|
||||
return gocv.IMRead(s.GetNextInstantPath(), gocv.IMReadGrayScale)
|
||||
}
|
||||
|
||||
// GetDebugInstantPath returns filepath.Join(s.GetStreamDirPath(), s.Base64, "debug.jpg")
|
||||
func (s Stream) GetDebugInstantPath() string {
|
||||
func (s *Stream) GetDebugInstantPath() string {
|
||||
return filepath.Join(s.GetStreamStoreDirPath(), "debug.jpg")
|
||||
}
|
||||
|
||||
// DebugInstantPathExists returns FileExists(GetDebugInstantPath())
|
||||
func (s Stream) DebugInstantPathExists() bool {
|
||||
func (s *Stream) DebugInstantPathExists() bool {
|
||||
return FileExists(s.GetDebugInstantPath())
|
||||
}
|
||||
|
||||
// GetDebugURL returns the URL towards the static file location of debug.jpg
|
||||
func (s Stream) GetDebugURL() string {
|
||||
func (s *Stream) GetDebugURL() string {
|
||||
return filepath.Join("/streams", s.Name, "debug.jpg")
|
||||
}
|
||||
|
||||
// IMReadDebug returns gocv.IMRead(GetDebugInstantPath(), gocv.IMReadGrayScale)
|
||||
func (s Stream) IMReadDebug() gocv.Mat {
|
||||
func (s *Stream) IMReadDebug() gocv.Mat {
|
||||
return gocv.IMRead(s.GetDebugInstantPath(), gocv.IMReadColor)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue