132 lines
4.1 KiB
Go
Executable file
132 lines
4.1 KiB
Go
Executable file
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"gocv.io/x/gocv"
|
|
)
|
|
|
|
// Stream Unexported
|
|
type Stream struct {
|
|
URL string `json:"url"`
|
|
Base64 string `json:"base64"`
|
|
Interval int `json:"interval"`
|
|
}
|
|
|
|
// NewStream creates a new Stream Object
|
|
func NewStream(URL string) Stream {
|
|
base64 := URLToBase64(URL)
|
|
return Stream{
|
|
URL: URL,
|
|
Base64: base64,
|
|
Interval: 5000,
|
|
}
|
|
}
|
|
|
|
// GetStreamInstant http.Get(URL) and returns the response
|
|
func (s Stream) GetStreamInstant(URL string) []byte {
|
|
resp, err := http.Get(URL)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
img, err := ioutil.ReadAll(resp.Body)
|
|
defer resp.Body.Close()
|
|
return img
|
|
}
|
|
|
|
// SaveStreamInstant writes the img to the CurrentStreamInstantPath, moves existing instant to PreviousStreamInstantPath
|
|
func (s Stream) SaveStreamInstant(URL string, mat gocv.Mat) {
|
|
streamStoreDir := s.GetStreamStoreDirPath()
|
|
os.MkdirAll(streamStoreDir, os.ModePerm)
|
|
|
|
currentStreamInstantPath := s.GetCurrentInstantPath()
|
|
swap := FileExists(s.GetCurrentInstantPath())
|
|
|
|
if swap {
|
|
currentStreamInstantPath = s.GetNextInstantPath()
|
|
}
|
|
|
|
gocv.IMWrite(currentStreamInstantPath, mat)
|
|
|
|
if swap {
|
|
s.SwapInstants(s.GetPreviousInstantPath(), s.GetCurrentInstantPath(), s.GetNextInstantPath())
|
|
}
|
|
}
|
|
|
|
// SwapInstants swaps the file location, first current -> previous and then next -> current
|
|
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")
|
|
}
|
|
err = os.Rename(next, current)
|
|
if err != nil {
|
|
log.Fatal("Couldn't copy next image instant to current image instant")
|
|
}
|
|
}
|
|
|
|
// GetStreamJSONPath returns filepath.Join(s.GetStreamStoreDirPath(), "stream.json")
|
|
func (s Stream) GetStreamJSONPath() string {
|
|
return filepath.Join(s.GetStreamStoreDirPath(), "stream.json")
|
|
}
|
|
|
|
// WriteStreamJSON writes the Stream struct to GetStreamJSONPath()
|
|
func (s Stream) WriteStreamJSON() {
|
|
file, _ := json.MarshalIndent(s, "", "")
|
|
_ = ioutil.WriteFile(s.GetStreamJSONPath(), file, 0644)
|
|
}
|
|
|
|
// GetStreamStoreDirPath returns filepath.Join(s.GetStreamDirPath(), s.Base64)
|
|
func (s Stream) GetStreamStoreDirPath() string {
|
|
return filepath.Join(GetStreamDirPath(), s.Base64)
|
|
}
|
|
|
|
// GetPreviousInstantPath returns filepath.Join(GetStreamDirPath(), s.Base64, "previous.jpg")
|
|
func (s Stream) GetPreviousInstantPath() string {
|
|
return filepath.Join(s.GetStreamStoreDirPath(), "previous.jpg")
|
|
}
|
|
|
|
// PreviousInstantPathExists returns FileExists(GetPreviousInstantPath())
|
|
func (s Stream) PreviousInstantPathExists() bool {
|
|
return FileExists(s.GetPreviousInstantPath())
|
|
}
|
|
|
|
// IMReadPrevious returns gocv.IMRead(GetPreviousInstantPath(), gocv.IMReadGrayScale)
|
|
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 {
|
|
return filepath.Join(s.GetStreamStoreDirPath(), "current.jpg")
|
|
}
|
|
|
|
// CurrentInstantPathExists returns FileExists(GetCurrentInstantPath())
|
|
func (s Stream) CurrentInstantPathExists() bool {
|
|
return FileExists(s.GetCurrentInstantPath())
|
|
}
|
|
|
|
// IMReadCurrent returns gocv.IMRead(GetCurrentInstantPath(), gocv.IMReadGrayScale)
|
|
func (s Stream) IMReadCurrent() gocv.Mat {
|
|
return gocv.IMRead(s.GetCurrentInstantPath(), gocv.IMReadGrayScale)
|
|
}
|
|
|
|
// GetNextInstantPath returns filepath.Join(s.GetStreamDirPath(), s.Base64, "last.jpg")
|
|
func (s Stream) GetNextInstantPath() string {
|
|
return filepath.Join(s.GetStreamStoreDirPath(), "last.jpg")
|
|
}
|
|
|
|
// NextInstantPathExists returns FileExists(GetNextInstantPath())
|
|
func (s Stream) NextInstantPathExists() bool {
|
|
return FileExists(s.GetNextInstantPath())
|
|
}
|
|
|
|
// IMReadNext returns gocv.IMRead(GetNextInstantPath(), gocv.IMReadGrayScale)
|
|
func (s Stream) IMReadNext() gocv.Mat {
|
|
return gocv.IMRead(s.GetNextInstantPath(), gocv.IMReadGrayScale)
|
|
}
|