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