ran gofmt -s

This commit is contained in:
BroodjeAap 2020-10-25 11:42:56 +01:00
parent 1697cbf1c7
commit f8c6d7fc4d
19 changed files with 751 additions and 751 deletions

0
.air.conf Executable file → Normal file
View file

0
.drone.yml Executable file → Normal file
View file

0
Dockerfile Executable file → Normal file
View file

636
main.go Executable file → Normal file
View file

@ -1,318 +1,318 @@
package main package main
import ( import (
"encoding/json" "encoding/json"
"image" "image"
"image/color" "image/color"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"path/filepath" "path/filepath"
"strconv" "strconv"
"text/template" "text/template"
"time" "time"
) )
var baseHTML = filepath.Join("templates", "base.html") var baseHTML = filepath.Join("templates", "base.html")
var indexHTML = filepath.Join("templates", "index.html") var indexHTML = filepath.Join("templates", "index.html")
var addStreamHTML = filepath.Join("templates", "add_stream.html") var addStreamHTML = filepath.Join("templates", "add_stream.html")
var streamHTML = filepath.Join("templates", "stream.html") var streamHTML = filepath.Join("templates", "stream.html")
var momentsHTML = filepath.Join("templates", "moments.html") var momentsHTML = filepath.Join("templates", "moments.html")
// Server is the main application struct // Server is the main application struct
// It holds a map[string]*Stream that contains all the Stream objects // It holds a map[string]*Stream that contains all the Stream objects
type Server struct { type Server struct {
Streams map[string]*Stream Streams map[string]*Stream
} }
// index shows a table of available streams or when visited with a 'name' param, the stream with that name. // index shows a table of available streams or when visited with a 'name' param, the stream with that name.
func (server Server) index(w http.ResponseWriter, r *http.Request) { func (server Server) index(w http.ResponseWriter, r *http.Request) {
if r.FormValue("name") == "" { if r.FormValue("name") == "" {
indexTemplate, err := template.ParseFiles(indexHTML, baseHTML) indexTemplate, err := template.ParseFiles(indexHTML, baseHTML)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return return
} }
indexTemplate.Execute(w, server) indexTemplate.Execute(w, server)
return return
} }
name := r.FormValue("name") name := r.FormValue("name")
stream, exists := server.Streams[name] stream, exists := server.Streams[name]
if !exists { if !exists {
return return
} }
streamTemplate, err := template.ParseFiles(streamHTML, baseHTML) streamTemplate, err := template.ParseFiles(streamHTML, baseHTML)
watchAreasJSON, _ := json.Marshal(stream.WatchAreas) watchAreasJSON, _ := json.Marshal(stream.WatchAreas)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return return
} }
streamTemplate.Execute(w, struct { streamTemplate.Execute(w, struct {
Streams map[string]*Stream Streams map[string]*Stream
Stream *Stream Stream *Stream
WatchAreasJSON string WatchAreasJSON string
}{ }{
server.Streams, server.Streams,
stream, stream,
string(watchAreasJSON[:]), string(watchAreasJSON[:]),
}) })
} }
// addStream adds the POSTed Stream to Server.Streams // addStream adds the POSTed Stream to Server.Streams
func (server Server) addStream(w http.ResponseWriter, r *http.Request) { func (server Server) addStream(w http.ResponseWriter, r *http.Request) {
nameMessage := "" nameMessage := ""
URLMessage := "" URLMessage := ""
intervalMessage := "" intervalMessage := ""
motionIntervalMessage := "" motionIntervalMessage := ""
if r.Method == "POST" { if r.Method == "POST" {
name := r.FormValue("name") name := r.FormValue("name")
URL := r.FormValue("URL") URL := r.FormValue("URL")
interval := r.FormValue("interval") interval := r.FormValue("interval")
motionInterval := r.FormValue("motion_interval") motionInterval := r.FormValue("motion_interval")
if name == "" { if name == "" {
nameMessage = "Name is required" nameMessage = "Name is required"
} }
if URL == "" { if URL == "" {
URLMessage = "URL is required" URLMessage = "URL is required"
} }
if interval == "" { if interval == "" {
intervalMessage = "Interval is required" intervalMessage = "Interval is required"
} }
if motionInterval == "" { if motionInterval == "" {
motionIntervalMessage = "MotionInterval is required" motionIntervalMessage = "MotionInterval is required"
} }
_, exists := server.Streams[name] _, exists := server.Streams[name]
if exists { if exists {
nameMessage = "Name already used" nameMessage = "Name already used"
} }
if !exists && name != "" && URL != "" && interval != "" && motionInterval != "" { if !exists && name != "" && URL != "" && interval != "" && motionInterval != "" {
intInterval, err := strconv.ParseInt(interval, 10, 64) intInterval, err := strconv.ParseInt(interval, 10, 64)
if err != nil { if err != nil {
log.Println("Illigal value for Interval: ", interval) log.Println("Illigal value for Interval: ", interval)
intInterval = 5000 intInterval = 5000
} }
intMotionInterval, err := strconv.ParseInt(motionInterval, 10, 64) intMotionInterval, err := strconv.ParseInt(motionInterval, 10, 64)
if err != nil { if err != nil {
log.Println("Illigal value for MotionInterval: ", motionInterval) log.Println("Illigal value for MotionInterval: ", motionInterval)
intMotionInterval = 1000 intMotionInterval = 1000
} }
server.Streams[name] = NewStream( server.Streams[name] = NewStream(
name, name,
URL, URL,
(int)(intInterval), (int)(intInterval),
(int)(intMotionInterval), (int)(intMotionInterval),
) )
go server.Streams[name].UpdateInterval() go server.Streams[name].UpdateInterval()
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.Println(err) log.Println(err)
return return
} }
addStreamTemplate.Execute(w, struct { addStreamTemplate.Execute(w, struct {
Streams map[string]*Stream Streams map[string]*Stream
NameMessage string NameMessage string
URLMessage string URLMessage string
IntervalMessage string IntervalMessage string
MotionIntervalMessage string MotionIntervalMessage string
}{ }{
server.Streams, server.Streams,
nameMessage, nameMessage,
URLMessage, URLMessage,
intervalMessage, intervalMessage,
motionIntervalMessage, motionIntervalMessage,
}) })
} }
// addWatchArea adds the POSTed WatchArea to Stream.WatchAreas // addWatchArea adds the POSTed WatchArea to Stream.WatchAreas
func (server Server) addWatchArea(w http.ResponseWriter, r *http.Request) { func (server Server) addWatchArea(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" { if r.Method != "POST" {
return return
} }
streamName := r.FormValue("streamName") streamName := r.FormValue("streamName")
if streamName == "" { if streamName == "" {
return return
} }
stream, exists := server.Streams[streamName] stream, exists := server.Streams[streamName]
if !exists { if !exists {
return return
} }
name := r.FormValue("name") name := r.FormValue("name")
if name == "" { if name == "" {
return return
} }
x0 := r.FormValue("x0") x0 := r.FormValue("x0")
if x0 == "" { if x0 == "" {
return return
} }
y0 := r.FormValue("y0") y0 := r.FormValue("y0")
if y0 == "" { if y0 == "" {
return return
} }
x1 := r.FormValue("x1") x1 := r.FormValue("x1")
if x1 == "" { if x1 == "" {
return return
} }
y1 := r.FormValue("y1") y1 := r.FormValue("y1")
if y1 == "" { if y1 == "" {
return return
} }
R := r.FormValue("R") R := r.FormValue("R")
if R == "" { if R == "" {
return return
} }
G := r.FormValue("G") G := r.FormValue("G")
if G == "" { if G == "" {
return return
} }
B := r.FormValue("B") B := r.FormValue("B")
if B == "" { if B == "" {
return return
} }
stream.AddWatchArea( stream.AddWatchArea(
name, name,
image.Rect( image.Rect(
StrToInt(x0), StrToInt(x0),
StrToInt(y0), StrToInt(y0),
StrToInt(x1), StrToInt(x1),
StrToInt(y1), StrToInt(y1),
), ),
color.RGBA{ color.RGBA{
uint8(StrToInt(R)), uint8(StrToInt(R)),
uint8(StrToInt(G)), uint8(StrToInt(G)),
uint8(StrToInt(B)), uint8(StrToInt(B)),
255, 255,
}, },
) )
http.Redirect(w, r, "/?name="+streamName, http.StatusTemporaryRedirect) http.Redirect(w, r, "/?name="+streamName, http.StatusTemporaryRedirect)
} }
// streamRecordings shows a table of 'moments', a continuous series of motion detected instants // streamRecordings shows a table of 'moments', a continuous series of motion detected instants
func (server Server) streamRecordings(w http.ResponseWriter, r *http.Request) { func (server Server) streamRecordings(w http.ResponseWriter, r *http.Request) {
streamName := r.FormValue("stream") streamName := r.FormValue("stream")
if streamName == "" { if streamName == "" {
return return
} }
watchAreaName := r.FormValue("watchArea") watchAreaName := r.FormValue("watchArea")
if watchAreaName == "" { if watchAreaName == "" {
return return
} }
stream, exists := server.Streams[streamName] stream, exists := server.Streams[streamName]
if !exists { if !exists {
return return
} }
watchArea, exists := stream.GetWatchAreaByName(watchAreaName) watchArea, exists := stream.GetWatchAreaByName(watchAreaName)
if !exists { if !exists {
return return
} }
images, err := ioutil.ReadDir(watchArea.GetWatchAreaStoreDir(*stream)) images, err := ioutil.ReadDir(watchArea.GetWatchAreaStoreDir(*stream))
if err != nil { if err != nil {
log.Println("Could not read watchArea directory", err) log.Println("Could not read watchArea directory", err)
return return
} }
moments := make([][]string, 0) moments := make([][]string, 0)
currentMoment := make([]string, 0) currentMoment := make([]string, 0)
momentIndex := 0 momentIndex := 0
foundMoment := false foundMoment := false
for i := range images { for i := range images {
if i == 0 { // Skip first for images[i-1] after this if if i == 0 { // Skip first for images[i-1] after this if
continue continue
} }
previousImage := images[i-1] previousImage := images[i-1]
previousTime, err := time.Parse(timeLayout, previousImage.Name()) previousTime, err := time.Parse(timeLayout, previousImage.Name())
if err != nil { if err != nil {
log.Println("Can't parse: ", previousImage.Name()) log.Println("Can't parse: ", previousImage.Name())
log.Println(err) log.Println(err)
continue continue
} }
currentImage := images[i] currentImage := images[i]
currentTime, err := time.Parse(timeLayout, currentImage.Name()) currentTime, err := time.Parse(timeLayout, currentImage.Name())
if err != nil { if err != nil {
log.Println("Can't parse: ", currentImage.Name()) log.Println("Can't parse: ", currentImage.Name())
log.Println(err) log.Println(err)
continue continue
} }
// if the time difference is lower then the Stream.Interval, it means the Stream.MotionInterval // if the time difference is lower then the Stream.Interval, it means the Stream.MotionInterval
// Was used, and motion was therefore detected. // Was used, and motion was therefore detected.
timeDifference := currentTime.Sub(previousTime) timeDifference := currentTime.Sub(previousTime)
lessThenInterval := timeDifference < (time.Duration(stream.Interval) * time.Millisecond) lessThenInterval := timeDifference < (time.Duration(stream.Interval) * time.Millisecond)
if lessThenInterval { if lessThenInterval {
foundMoment = true foundMoment = true
currentMoment = append(currentMoment, previousImage.Name()) currentMoment = append(currentMoment, previousImage.Name())
} }
if !lessThenInterval || i == len(images) { if !lessThenInterval || i == len(images) {
if foundMoment { if foundMoment {
currentMoment = append(currentMoment, currentImage.Name()) currentMoment = append(currentMoment, currentImage.Name())
moments = append(moments, currentMoment) moments = append(moments, currentMoment)
currentMoment = make([]string, 0) currentMoment = make([]string, 0)
momentIndex++ momentIndex++
} }
foundMoment = false foundMoment = false
} }
} }
momentsTemplate, err := template.ParseFiles(momentsHTML, baseHTML) momentsTemplate, err := template.ParseFiles(momentsHTML, baseHTML)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return return
} }
momentsJSON, _ := json.Marshal(moments) momentsJSON, _ := json.Marshal(moments)
err = momentsTemplate.Execute(w, struct { err = momentsTemplate.Execute(w, struct {
Streams map[string]*Stream Streams map[string]*Stream
MomentsJSON string MomentsJSON string
Moments [][]string Moments [][]string
Stream *Stream Stream *Stream
WatchArea *WatchArea WatchArea *WatchArea
}{ }{
server.Streams, server.Streams,
string(momentsJSON[:]), string(momentsJSON[:]),
moments, moments,
stream, stream,
watchArea, watchArea,
}) })
} }
// main sets up the HTTP server and reconstructs the Streams, if available. // main sets up the HTTP server and reconstructs the Streams, if available.
func main() { func main() {
staticFileServer := http.FileServer(http.Dir("./static")) staticFileServer := http.FileServer(http.Dir("./static"))
http.Handle("/static/", http.StripPrefix("/static/", staticFileServer)) http.Handle("/static/", http.StripPrefix("/static/", staticFileServer))
streamFileServer := http.FileServer(http.Dir("./streams")) streamFileServer := http.FileServer(http.Dir("./streams"))
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 {
log.Fatal("Could not read Streamdir") log.Fatal("Could not read Streamdir")
} }
for _, streamStoreDir := range streams { for _, streamStoreDir := range streams {
if !streamStoreDir.IsDir() { if !streamStoreDir.IsDir() {
continue continue
} }
// UnMarshal the stream.json into a Stream struct and start a goroutine for UpdateInterval // UnMarshal the stream.json into a Stream struct and start a goroutine for UpdateInterval
streamJSONPath := filepath.Join(GetStreamDirPath(), streamStoreDir.Name(), "stream.json") streamJSONPath := filepath.Join(GetStreamDirPath(), streamStoreDir.Name(), "stream.json")
stream := StreamFromJSON(streamJSONPath) stream := StreamFromJSON(streamJSONPath)
server.Streams[stream.Name] = stream server.Streams[stream.Name] = stream
go stream.UpdateInterval() go stream.UpdateInterval()
} }
http.HandleFunc("/", server.index) http.HandleFunc("/", server.index)
http.HandleFunc("/addStream", server.addStream) http.HandleFunc("/addStream", server.addStream)
http.HandleFunc("/addWatchArea", server.addWatchArea) http.HandleFunc("/addWatchArea", server.addWatchArea)
http.HandleFunc("/streamRecordings", server.streamRecordings) http.HandleFunc("/streamRecordings", server.streamRecordings)
log.Println("Starting StreamWatcher Server on :8080") log.Println("Starting StreamWatcher Server on :8080")
log.Fatal(http.ListenAndServe(":8080", nil)) log.Fatal(http.ListenAndServe(":8080", nil))
} }

70
main_test.go Executable file → Normal file
View file

@ -1,35 +1,35 @@
package main package main
import ( import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"path/filepath" "path/filepath"
"testing" "testing"
) )
func TestIndexHandler(t *testing.T) { func TestIndexHandler(t *testing.T) {
// TestIndexHandler tests the index // TestIndexHandler tests the index
req, err := http.NewRequest("GET", "/", nil) req, err := http.NewRequest("GET", "/", nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
handler := http.HandlerFunc(index) handler := http.HandlerFunc(index)
handler.ServeHTTP(rr, req) handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK { if status := rr.Code; status != http.StatusOK {
t.Errorf("Index returned wrong status code: got %v want %v", t.Errorf("Index returned wrong status code: got %v want %v",
status, http.StatusOK) status, http.StatusOK)
} }
indexTemplate, err := ioutil.ReadFile(filepath.Join("templates", "index.html")) indexTemplate, err := ioutil.ReadFile(filepath.Join("templates", "index.html"))
if err != nil { if err != nil {
t.Fatal("Can't read index.html") t.Fatal("Can't read index.html")
} }
if rr.Body.String() != string(indexTemplate) { if rr.Body.String() != string(indexTemplate) {
t.Fatal("Index response is not the same as index.html") t.Fatal("Index response is not the same as index.html")
} }
} }

0
requirements.yaml Executable file → Normal file
View file

0
static/css/slider.css Executable file → Normal file
View file

0
static/css/style.css Executable file → Normal file
View file

0
static/js/add_stream.js Executable file → Normal file
View file

0
static/js/moments.js Executable file → Normal file
View file

0
static/js/stream.js Executable file → Normal file
View file

612
stream.go Executable file → Normal file
View file

@ -1,306 +1,306 @@
package main package main
import ( import (
"encoding/json" "encoding/json"
"image" "image"
"image/color" "image/color"
"io/ioutil" "io/ioutil"
"log" "log"
"math" "math"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
"gocv.io/x/gocv" "gocv.io/x/gocv"
) )
// Stream represents a stream to monitor // Stream represents a stream to monitor
type Stream struct { type Stream struct {
Name string `json:"name"` Name string `json:"name"`
URL string `json:"url"` URL string `json:"url"`
Interval int `json:"interval"` Interval int `json:"interval"`
MotionInterval int `json:"motionInterval"` MotionInterval int `json:"motionInterval"`
WatchAreas []WatchArea `json:"watchAreas"` WatchAreas []WatchArea `json:"watchAreas"`
Timeouts float64 `json:"-"` Timeouts float64 `json:"-"`
MotionDetected bool `json:"-"` MotionDetected bool `json:"-"`
} }
// NewStream creates a new Stream Object // NewStream creates a new Stream Object
func NewStream(Name string, URL string, Interval int, MotionInterval int) *Stream { func NewStream(Name string, URL string, Interval int, MotionInterval int) *Stream {
stream := Stream{ stream := Stream{
Name: Name, Name: Name,
URL: URL, URL: URL,
Interval: Interval, Interval: Interval,
MotionInterval: MotionInterval, MotionInterval: MotionInterval,
WatchAreas: make([]WatchArea, 0), WatchAreas: make([]WatchArea, 0),
Timeouts: 0, Timeouts: 0,
MotionDetected: false, MotionDetected: false,
} }
stream.WriteStreamJSON() stream.WriteStreamJSON()
return &stream return &stream
} }
// StreamFromJSON takes a filepath to a JSON file, and returns the unmarshalled Stream object // StreamFromJSON takes a filepath to a JSON file, and returns the unmarshalled Stream object
func StreamFromJSON(path string) *Stream { func StreamFromJSON(path string) *Stream {
streamJSONFile, _ := ioutil.ReadFile(path) streamJSONFile, _ := ioutil.ReadFile(path)
stream := Stream{ stream := Stream{
Timeouts: 0, Timeouts: 0,
MotionDetected: false, MotionDetected: false,
} }
json.Unmarshal([]byte(streamJSONFile), &stream) json.Unmarshal([]byte(streamJSONFile), &stream)
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, " - ", len(s.WatchAreas), " - ", 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.Println(err) log.Println(err)
s.Timeouts++ // For increasing the Interval time every timeout s.Timeouts++ // For increasing the Interval time every timeout
return return
} }
s.Timeouts = 0 s.Timeouts = 0
img, err := ioutil.ReadAll(resp.Body) img, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close() defer resp.Body.Close()
// Get the image from the response and write it to disk in a goroutine // Get the image from the response and write it to disk in a goroutine
mat, err := gocv.IMDecode(img, gocv.IMReadColor) mat, err := gocv.IMDecode(img, gocv.IMReadColor)
if err != nil { if err != nil {
log.Println("Could not IMDecode img", err) log.Println("Could not IMDecode img", err)
return return
} }
go gocv.IMWrite(s.GetCurrentColorInstantPath(), mat) go gocv.IMWrite(s.GetCurrentColorInstantPath(), mat)
// Turn the current image into greyscale and also write that to disk in a goroutine // Turn the current image into greyscale and also write that to disk in a goroutine
currentGrey := gocv.NewMat() currentGrey := gocv.NewMat()
gocv.CvtColor(mat, &currentGrey, gocv.ColorBGRAToGray) gocv.CvtColor(mat, &currentGrey, gocv.ColorBGRAToGray)
gocv.GaussianBlur(currentGrey, &currentGrey, image.Point{X: 21, Y: 21}, 0, 0, gocv.BorderReflect) gocv.GaussianBlur(currentGrey, &currentGrey, image.Point{X: 21, Y: 21}, 0, 0, gocv.BorderReflect)
go s.SaveStreamInstant(currentGrey) go s.SaveStreamInstant(currentGrey)
if !s.PreviousInstantPathExists() { if !s.PreviousInstantPathExists() {
return return
} }
// If there is a previous image saved to disk, calculate the difference between the two and amplify those differences // If there is a previous image saved to disk, calculate the difference between the two and amplify those differences
diff := gocv.NewMat() diff := gocv.NewMat()
previousGrey := s.IMReadPrevious() previousGrey := s.IMReadPrevious()
gocv.AbsDiff(previousGrey, currentGrey, &diff) gocv.AbsDiff(previousGrey, currentGrey, &diff)
gocv.Threshold(diff, &diff, 100, 255, gocv.ThresholdBinary) gocv.Threshold(diff, &diff, 100, 255, gocv.ThresholdBinary)
dilateKernel := gocv.GetStructuringElement(gocv.MorphRect, image.Pt(20, 20)) dilateKernel := gocv.GetStructuringElement(gocv.MorphRect, image.Pt(20, 20))
gocv.Dilate(diff, &diff, dilateKernel) gocv.Dilate(diff, &diff, dilateKernel)
debug := gocv.NewMat() debug := gocv.NewMat()
diff.CopyTo(&debug) diff.CopyTo(&debug)
gocv.CvtColor(debug, &debug, gocv.ColorGrayToBGRA) gocv.CvtColor(debug, &debug, gocv.ColorGrayToBGRA)
// Get the contours and check if they are in any of the WatchAreas // Get the contours and check if they are in any of the WatchAreas
contours := gocv.FindContours(diff, gocv.RetrievalExternal, gocv.ChainApproxSimple) contours := gocv.FindContours(diff, gocv.RetrievalExternal, gocv.ChainApproxSimple)
s.MotionDetected = false s.MotionDetected = false
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) {
s.MotionDetected = true s.MotionDetected = true
go watchArea.CopyInstant(s.GetCurrentColorInstantPath(), *s) go watchArea.CopyInstant(s.GetCurrentColorInstantPath(), *s)
} }
} }
gocv.IMWrite(s.GetDebugInstantPath(), debug) 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) {
return true return true
} }
} }
} }
return false return false
} }
// UpdateInterval calls Update() every interval // UpdateInterval calls Update() every interval
func (s *Stream) UpdateInterval() { func (s *Stream) UpdateInterval() {
for { for {
s.Update() s.Update()
if s.MotionDetected { if s.MotionDetected {
time.Sleep(time.Duration(s.MotionInterval) * time.Millisecond) time.Sleep(time.Duration(s.MotionInterval) * time.Millisecond)
} else { } else {
expTimeout := math.Pow(2, s.Timeouts) expTimeout := math.Pow(2, s.Timeouts)
maxExpTimeout := (int)(math.Min(12, expTimeout)) maxExpTimeout := (int)(math.Min(12, expTimeout))
time.Sleep(time.Duration(s.Interval*maxExpTimeout) * time.Millisecond) time.Sleep(time.Duration(s.Interval*maxExpTimeout) * time.Millisecond)
} }
} }
} }
// GetWatchAreaByName finds the WatchArea with the name, if it doesn't exist returns nil, false // GetWatchAreaByName finds the WatchArea with the name, if it doesn't exist returns nil, false
func (s *Stream) GetWatchAreaByName(name string) (*WatchArea, bool) { func (s *Stream) GetWatchAreaByName(name string) (*WatchArea, bool) {
for _, watchArea := range s.WatchAreas { for _, watchArea := range s.WatchAreas {
if name == watchArea.Name { if name == watchArea.Name {
return &watchArea, true return &watchArea, true
} }
} }
return nil, false return nil, false
} }
// 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) {
streamStoreDir := s.GetStreamStoreDirPath() streamStoreDir := s.GetStreamStoreDirPath()
os.MkdirAll(streamStoreDir, os.ModePerm) os.MkdirAll(streamStoreDir, os.ModePerm)
currentStreamInstantPath := s.GetCurrentInstantPath() currentStreamInstantPath := s.GetCurrentInstantPath()
swap := FileExists(s.GetCurrentInstantPath()) swap := FileExists(s.GetCurrentInstantPath())
if swap { if swap {
currentStreamInstantPath = s.GetNextInstantPath() currentStreamInstantPath = s.GetNextInstantPath()
} }
gocv.IMWrite(currentStreamInstantPath, mat) gocv.IMWrite(currentStreamInstantPath, mat)
if swap { if swap {
s.SwapInstants(s.GetPreviousInstantPath(), s.GetCurrentInstantPath(), s.GetNextInstantPath()) s.SwapInstants(s.GetPreviousInstantPath(), s.GetCurrentInstantPath(), s.GetNextInstantPath())
} }
} }
// 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.Println("Couldn't copy current image instant to previous image instant", err) log.Println("Couldn't copy current image instant to previous image instant", err)
} }
err = os.Rename(next, current) err = os.Rename(next, current)
if err != nil { if err != nil {
log.Println("Couldn't copy next image instant to current image instant", err) log.Println("Couldn't copy next image instant to current image instant", err)
} }
} }
// AddWatchArea adds a watch area // AddWatchArea adds a watch area
func (s *Stream) AddWatchArea(name string, area image.Rectangle, color color.RGBA) { 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, Name: name,
Color: color, Color: color,
Area: area, Area: area,
}) })
s.WriteStreamJSON() 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)
} }
file, _ := json.MarshalIndent(s, "", "\t") file, _ := json.MarshalIndent(s, "", "\t")
_ = ioutil.WriteFile(s.GetStreamJSONPath(), file, 0644) _ = ioutil.WriteFile(s.GetStreamJSONPath(), file, 0644)
} }
// GetStreamStoreDirPath returns filepath.Join(s.GetStreamDirPath(), s.Name) // GetStreamStoreDirPath returns filepath.Join(s.GetStreamDirPath(), s.Name)
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(GetStreamStoreDirPath(), "previous.jpg") // GetPreviousInstantPath returns filepath.Join(GetStreamStoreDirPath(), "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.GetStreamStoreDirPath(), "current.jpg") // GetCurrentInstantPath returns filepath.Join(s.GetStreamStoreDirPath(), "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.GetStreamStoreDirPath(), "current_color.jpg") // GetCurrentColorInstantPath returns filepath.Join(s.GetStreamStoreDirPath(), "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.GetStreamStoreDirPath(), "last.jpg") // GetNextInstantPath returns filepath.Join(s.GetStreamStoreDirPath(), "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.GetStreamStoreDirPath(), "debug.jpg") // GetDebugInstantPath returns filepath.Join(s.GetStreamStoreDirPath(), "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)
} }

0
templates/add_stream.html Executable file → Normal file
View file

0
templates/base.html Executable file → Normal file
View file

0
templates/index.html Executable file → Normal file
View file

0
templates/moments.html Executable file → Normal file
View file

0
templates/stream.html Executable file → Normal file
View file

66
util.go Executable file → Normal file
View file

@ -1,33 +1,33 @@
package main package main
import ( import (
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
) )
// FileExists returns true if path exists // FileExists returns true if path exists
func FileExists(path string) bool { func FileExists(path string) bool {
_, err := os.Stat(path) _, err := os.Stat(path)
return !os.IsNotExist(err) return !os.IsNotExist(err)
} }
// GetStreamDirPath returns filepath.Join(".", "streams") // GetStreamDirPath returns filepath.Join(".", "streams")
func GetStreamDirPath() string { func GetStreamDirPath() string {
return filepath.Join(".", "streams") return filepath.Join(".", "streams")
} }
// StreamStorePathExists returns FileExists(GetStreamDirPath()) // StreamStorePathExists returns FileExists(GetStreamDirPath())
func StreamStorePathExists() bool { func StreamStorePathExists() bool {
return FileExists(GetStreamDirPath()) return FileExists(GetStreamDirPath())
} }
// StrToInt uses strconv.ParseInt // StrToInt uses strconv.ParseInt
func StrToInt(str string) int { func StrToInt(str string) int {
i, err := strconv.ParseInt(str, 10, 64) i, err := strconv.ParseInt(str, 10, 64)
if err != nil { if err != nil {
log.Println("Could not convert string to int ", str) log.Println("Could not convert string to int ", str)
} }
return int(i) return int(i)
} }

118
watch.go Executable file → Normal file
View file

@ -1,59 +1,59 @@
package main package main
import ( import (
"image" "image"
"image/color" "image/color"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
) )
const timeLayout = time.RFC3339Nano const timeLayout = time.RFC3339Nano
// WatchArea defines one or more areas that should be monitored for motion // WatchArea defines one or more areas that should be monitored for motion
type WatchArea struct { type WatchArea struct {
Name string `json:"name"` Name string `json:"name"`
Color color.RGBA `json:"color"` Color color.RGBA `json:"color"`
Area image.Rectangle `json:"area"` Area image.Rectangle `json:"area"`
} }
// WatchAreaStoreDirExists returns filepath.Join(s.GetStreamStoreDirPath(), w.Name) // WatchAreaStoreDirExists returns filepath.Join(s.GetStreamStoreDirPath(), w.Name)
func (w WatchArea) WatchAreaStoreDirExists(s Stream) bool { func (w WatchArea) WatchAreaStoreDirExists(s Stream) bool {
return FileExists(w.GetWatchAreaStoreDir(s)) return FileExists(w.GetWatchAreaStoreDir(s))
} }
// GetWatchAreaStoreDir returns filepath.Join(s.GetStreamStoreDirPath(), w.Name) // GetWatchAreaStoreDir returns filepath.Join(s.GetStreamStoreDirPath(), w.Name)
func (w WatchArea) GetWatchAreaStoreDir(s Stream) string { func (w WatchArea) GetWatchAreaStoreDir(s Stream) string {
return filepath.Join(s.GetStreamStoreDirPath(), w.Name) return filepath.Join(s.GetStreamStoreDirPath(), w.Name)
} }
// GetWatchAreaStoreDirInstantPath returns filepath.Join(s.GetWatchAreaStoreDir(), "") // GetWatchAreaStoreDirInstantPath returns filepath.Join(s.GetWatchAreaStoreDir(), "")
func (w WatchArea) GetWatchAreaStoreDirInstantPath(s Stream) string { func (w WatchArea) GetWatchAreaStoreDirInstantPath(s Stream) string {
now := time.Now() now := time.Now()
return filepath.Join(w.GetWatchAreaStoreDir(s), now.Format(timeLayout)) return filepath.Join(w.GetWatchAreaStoreDir(s), now.Format(timeLayout))
} }
// CopyInstant makes a copy of src to GetWatchAreaStoreDirInstantPath(s) // CopyInstant makes a copy of src to GetWatchAreaStoreDirInstantPath(s)
func (w WatchArea) CopyInstant(src string, s Stream) { func (w WatchArea) CopyInstant(src string, s Stream) {
if !FileExists(w.GetWatchAreaStoreDir(s)) { if !FileExists(w.GetWatchAreaStoreDir(s)) {
os.MkdirAll(w.GetWatchAreaStoreDir(s), os.ModePerm) os.MkdirAll(w.GetWatchAreaStoreDir(s), os.ModePerm)
} }
dest := w.GetWatchAreaStoreDirInstantPath(s) dest := w.GetWatchAreaStoreDirInstantPath(s)
if !FileExists(src) { if !FileExists(src) {
log.Fatal("Nothing to copy ", src) log.Fatal("Nothing to copy ", src)
return return
} }
if FileExists(dest) { if FileExists(dest) {
os.Remove(dest) os.Remove(dest)
} }
srcData, err := ioutil.ReadFile(src) srcData, err := ioutil.ReadFile(src)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }
err = ioutil.WriteFile(dest, srcData, 0644) err = ioutil.WriteFile(dest, srcData, 0644)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }
} }