ran gofmt -s
This commit is contained in:
parent
1697cbf1c7
commit
f8c6d7fc4d
19 changed files with 751 additions and 751 deletions
0
.air.conf
Executable file → Normal file
0
.air.conf
Executable file → Normal file
0
.drone.yml
Executable file → Normal file
0
.drone.yml
Executable file → Normal file
0
Dockerfile
Executable file → Normal file
0
Dockerfile
Executable file → Normal file
636
main.go
Executable file → Normal file
636
main.go
Executable file → Normal file
|
@ -1,318 +1,318 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"image"
|
||||
"image/color"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"text/template"
|
||||
"time"
|
||||
)
|
||||
|
||||
var baseHTML = filepath.Join("templates", "base.html")
|
||||
var indexHTML = filepath.Join("templates", "index.html")
|
||||
var addStreamHTML = filepath.Join("templates", "add_stream.html")
|
||||
var streamHTML = filepath.Join("templates", "stream.html")
|
||||
var momentsHTML = filepath.Join("templates", "moments.html")
|
||||
|
||||
// Server is the main application struct
|
||||
// It holds a map[string]*Stream that contains all the Stream objects
|
||||
type Server struct {
|
||||
Streams map[string]*Stream
|
||||
}
|
||||
|
||||
// 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) {
|
||||
if r.FormValue("name") == "" {
|
||||
indexTemplate, err := template.ParseFiles(indexHTML, baseHTML)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
indexTemplate.Execute(w, server)
|
||||
return
|
||||
}
|
||||
name := r.FormValue("name")
|
||||
stream, exists := server.Streams[name]
|
||||
if !exists {
|
||||
return
|
||||
}
|
||||
streamTemplate, err := template.ParseFiles(streamHTML, baseHTML)
|
||||
|
||||
watchAreasJSON, _ := json.Marshal(stream.WatchAreas)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
streamTemplate.Execute(w, struct {
|
||||
Streams map[string]*Stream
|
||||
Stream *Stream
|
||||
WatchAreasJSON string
|
||||
}{
|
||||
server.Streams,
|
||||
stream,
|
||||
string(watchAreasJSON[:]),
|
||||
})
|
||||
}
|
||||
|
||||
// addStream adds the POSTed Stream to Server.Streams
|
||||
func (server Server) addStream(w http.ResponseWriter, r *http.Request) {
|
||||
nameMessage := ""
|
||||
URLMessage := ""
|
||||
intervalMessage := ""
|
||||
motionIntervalMessage := ""
|
||||
if r.Method == "POST" {
|
||||
name := r.FormValue("name")
|
||||
URL := r.FormValue("URL")
|
||||
interval := r.FormValue("interval")
|
||||
motionInterval := r.FormValue("motion_interval")
|
||||
if name == "" {
|
||||
nameMessage = "Name is required"
|
||||
}
|
||||
if URL == "" {
|
||||
URLMessage = "URL is required"
|
||||
}
|
||||
if interval == "" {
|
||||
intervalMessage = "Interval is required"
|
||||
}
|
||||
if motionInterval == "" {
|
||||
motionIntervalMessage = "MotionInterval is required"
|
||||
}
|
||||
_, exists := server.Streams[name]
|
||||
if exists {
|
||||
nameMessage = "Name already used"
|
||||
}
|
||||
if !exists && name != "" && URL != "" && interval != "" && motionInterval != "" {
|
||||
intInterval, err := strconv.ParseInt(interval, 10, 64)
|
||||
if err != nil {
|
||||
log.Println("Illigal value for Interval: ", interval)
|
||||
intInterval = 5000
|
||||
}
|
||||
intMotionInterval, err := strconv.ParseInt(motionInterval, 10, 64)
|
||||
if err != nil {
|
||||
log.Println("Illigal value for MotionInterval: ", motionInterval)
|
||||
intMotionInterval = 1000
|
||||
}
|
||||
server.Streams[name] = NewStream(
|
||||
name,
|
||||
URL,
|
||||
(int)(intInterval),
|
||||
(int)(intMotionInterval),
|
||||
)
|
||||
go server.Streams[name].UpdateInterval()
|
||||
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
||||
}
|
||||
}
|
||||
|
||||
addStreamTemplate, err := template.ParseFiles(addStreamHTML, baseHTML)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
addStreamTemplate.Execute(w, struct {
|
||||
Streams map[string]*Stream
|
||||
NameMessage string
|
||||
URLMessage string
|
||||
IntervalMessage string
|
||||
MotionIntervalMessage string
|
||||
}{
|
||||
server.Streams,
|
||||
nameMessage,
|
||||
URLMessage,
|
||||
intervalMessage,
|
||||
motionIntervalMessage,
|
||||
})
|
||||
}
|
||||
|
||||
// addWatchArea adds the POSTed WatchArea to Stream.WatchAreas
|
||||
func (server Server) addWatchArea(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "POST" {
|
||||
return
|
||||
}
|
||||
streamName := r.FormValue("streamName")
|
||||
if streamName == "" {
|
||||
return
|
||||
}
|
||||
stream, exists := server.Streams[streamName]
|
||||
if !exists {
|
||||
return
|
||||
}
|
||||
name := r.FormValue("name")
|
||||
if name == "" {
|
||||
return
|
||||
}
|
||||
x0 := r.FormValue("x0")
|
||||
if x0 == "" {
|
||||
return
|
||||
}
|
||||
y0 := r.FormValue("y0")
|
||||
if y0 == "" {
|
||||
return
|
||||
}
|
||||
x1 := r.FormValue("x1")
|
||||
if x1 == "" {
|
||||
return
|
||||
}
|
||||
y1 := r.FormValue("y1")
|
||||
if y1 == "" {
|
||||
return
|
||||
}
|
||||
R := r.FormValue("R")
|
||||
if R == "" {
|
||||
return
|
||||
}
|
||||
G := r.FormValue("G")
|
||||
if G == "" {
|
||||
return
|
||||
}
|
||||
B := r.FormValue("B")
|
||||
if B == "" {
|
||||
return
|
||||
}
|
||||
stream.AddWatchArea(
|
||||
name,
|
||||
image.Rect(
|
||||
StrToInt(x0),
|
||||
StrToInt(y0),
|
||||
StrToInt(x1),
|
||||
StrToInt(y1),
|
||||
),
|
||||
color.RGBA{
|
||||
uint8(StrToInt(R)),
|
||||
uint8(StrToInt(G)),
|
||||
uint8(StrToInt(B)),
|
||||
255,
|
||||
},
|
||||
)
|
||||
http.Redirect(w, r, "/?name="+streamName, http.StatusTemporaryRedirect)
|
||||
}
|
||||
|
||||
// streamRecordings shows a table of 'moments', a continuous series of motion detected instants
|
||||
func (server Server) streamRecordings(w http.ResponseWriter, r *http.Request) {
|
||||
streamName := r.FormValue("stream")
|
||||
if streamName == "" {
|
||||
return
|
||||
}
|
||||
watchAreaName := r.FormValue("watchArea")
|
||||
if watchAreaName == "" {
|
||||
return
|
||||
}
|
||||
|
||||
stream, exists := server.Streams[streamName]
|
||||
if !exists {
|
||||
return
|
||||
}
|
||||
|
||||
watchArea, exists := stream.GetWatchAreaByName(watchAreaName)
|
||||
if !exists {
|
||||
return
|
||||
}
|
||||
|
||||
images, err := ioutil.ReadDir(watchArea.GetWatchAreaStoreDir(*stream))
|
||||
if err != nil {
|
||||
log.Println("Could not read watchArea directory", err)
|
||||
return
|
||||
}
|
||||
moments := make([][]string, 0)
|
||||
currentMoment := make([]string, 0)
|
||||
momentIndex := 0
|
||||
foundMoment := false
|
||||
|
||||
for i := range images {
|
||||
if i == 0 { // Skip first for images[i-1] after this if
|
||||
continue
|
||||
}
|
||||
previousImage := images[i-1]
|
||||
previousTime, err := time.Parse(timeLayout, previousImage.Name())
|
||||
if err != nil {
|
||||
log.Println("Can't parse: ", previousImage.Name())
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
currentImage := images[i]
|
||||
currentTime, err := time.Parse(timeLayout, currentImage.Name())
|
||||
if err != nil {
|
||||
log.Println("Can't parse: ", currentImage.Name())
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
// if the time difference is lower then the Stream.Interval, it means the Stream.MotionInterval
|
||||
// Was used, and motion was therefore detected.
|
||||
timeDifference := currentTime.Sub(previousTime)
|
||||
lessThenInterval := timeDifference < (time.Duration(stream.Interval) * time.Millisecond)
|
||||
if lessThenInterval {
|
||||
foundMoment = true
|
||||
currentMoment = append(currentMoment, previousImage.Name())
|
||||
}
|
||||
if !lessThenInterval || i == len(images) {
|
||||
if foundMoment {
|
||||
currentMoment = append(currentMoment, currentImage.Name())
|
||||
moments = append(moments, currentMoment)
|
||||
currentMoment = make([]string, 0)
|
||||
momentIndex++
|
||||
}
|
||||
foundMoment = false
|
||||
}
|
||||
}
|
||||
momentsTemplate, err := template.ParseFiles(momentsHTML, baseHTML)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
momentsJSON, _ := json.Marshal(moments)
|
||||
err = momentsTemplate.Execute(w, struct {
|
||||
Streams map[string]*Stream
|
||||
MomentsJSON string
|
||||
Moments [][]string
|
||||
Stream *Stream
|
||||
WatchArea *WatchArea
|
||||
}{
|
||||
server.Streams,
|
||||
string(momentsJSON[:]),
|
||||
moments,
|
||||
stream,
|
||||
watchArea,
|
||||
})
|
||||
}
|
||||
|
||||
// main sets up the HTTP server and reconstructs the Streams, if available.
|
||||
func main() {
|
||||
staticFileServer := http.FileServer(http.Dir("./static"))
|
||||
http.Handle("/static/", http.StripPrefix("/static/", staticFileServer))
|
||||
|
||||
streamFileServer := http.FileServer(http.Dir("./streams"))
|
||||
http.Handle("/streams/", http.StripPrefix("/streams/", streamFileServer))
|
||||
|
||||
server := Server{
|
||||
Streams: make(map[string]*Stream),
|
||||
}
|
||||
streams, err := ioutil.ReadDir(GetStreamDirPath())
|
||||
if err != nil {
|
||||
log.Fatal("Could not read Streamdir")
|
||||
}
|
||||
for _, streamStoreDir := range streams {
|
||||
if !streamStoreDir.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
// UnMarshal the stream.json into a Stream struct and start a goroutine for UpdateInterval
|
||||
streamJSONPath := filepath.Join(GetStreamDirPath(), streamStoreDir.Name(), "stream.json")
|
||||
stream := StreamFromJSON(streamJSONPath)
|
||||
server.Streams[stream.Name] = stream
|
||||
go stream.UpdateInterval()
|
||||
}
|
||||
|
||||
http.HandleFunc("/", server.index)
|
||||
http.HandleFunc("/addStream", server.addStream)
|
||||
http.HandleFunc("/addWatchArea", server.addWatchArea)
|
||||
http.HandleFunc("/streamRecordings", server.streamRecordings)
|
||||
log.Println("Starting StreamWatcher Server on :8080")
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
}
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"image"
|
||||
"image/color"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"text/template"
|
||||
"time"
|
||||
)
|
||||
|
||||
var baseHTML = filepath.Join("templates", "base.html")
|
||||
var indexHTML = filepath.Join("templates", "index.html")
|
||||
var addStreamHTML = filepath.Join("templates", "add_stream.html")
|
||||
var streamHTML = filepath.Join("templates", "stream.html")
|
||||
var momentsHTML = filepath.Join("templates", "moments.html")
|
||||
|
||||
// Server is the main application struct
|
||||
// It holds a map[string]*Stream that contains all the Stream objects
|
||||
type Server struct {
|
||||
Streams map[string]*Stream
|
||||
}
|
||||
|
||||
// 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) {
|
||||
if r.FormValue("name") == "" {
|
||||
indexTemplate, err := template.ParseFiles(indexHTML, baseHTML)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
indexTemplate.Execute(w, server)
|
||||
return
|
||||
}
|
||||
name := r.FormValue("name")
|
||||
stream, exists := server.Streams[name]
|
||||
if !exists {
|
||||
return
|
||||
}
|
||||
streamTemplate, err := template.ParseFiles(streamHTML, baseHTML)
|
||||
|
||||
watchAreasJSON, _ := json.Marshal(stream.WatchAreas)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
streamTemplate.Execute(w, struct {
|
||||
Streams map[string]*Stream
|
||||
Stream *Stream
|
||||
WatchAreasJSON string
|
||||
}{
|
||||
server.Streams,
|
||||
stream,
|
||||
string(watchAreasJSON[:]),
|
||||
})
|
||||
}
|
||||
|
||||
// addStream adds the POSTed Stream to Server.Streams
|
||||
func (server Server) addStream(w http.ResponseWriter, r *http.Request) {
|
||||
nameMessage := ""
|
||||
URLMessage := ""
|
||||
intervalMessage := ""
|
||||
motionIntervalMessage := ""
|
||||
if r.Method == "POST" {
|
||||
name := r.FormValue("name")
|
||||
URL := r.FormValue("URL")
|
||||
interval := r.FormValue("interval")
|
||||
motionInterval := r.FormValue("motion_interval")
|
||||
if name == "" {
|
||||
nameMessage = "Name is required"
|
||||
}
|
||||
if URL == "" {
|
||||
URLMessage = "URL is required"
|
||||
}
|
||||
if interval == "" {
|
||||
intervalMessage = "Interval is required"
|
||||
}
|
||||
if motionInterval == "" {
|
||||
motionIntervalMessage = "MotionInterval is required"
|
||||
}
|
||||
_, exists := server.Streams[name]
|
||||
if exists {
|
||||
nameMessage = "Name already used"
|
||||
}
|
||||
if !exists && name != "" && URL != "" && interval != "" && motionInterval != "" {
|
||||
intInterval, err := strconv.ParseInt(interval, 10, 64)
|
||||
if err != nil {
|
||||
log.Println("Illigal value for Interval: ", interval)
|
||||
intInterval = 5000
|
||||
}
|
||||
intMotionInterval, err := strconv.ParseInt(motionInterval, 10, 64)
|
||||
if err != nil {
|
||||
log.Println("Illigal value for MotionInterval: ", motionInterval)
|
||||
intMotionInterval = 1000
|
||||
}
|
||||
server.Streams[name] = NewStream(
|
||||
name,
|
||||
URL,
|
||||
(int)(intInterval),
|
||||
(int)(intMotionInterval),
|
||||
)
|
||||
go server.Streams[name].UpdateInterval()
|
||||
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
||||
}
|
||||
}
|
||||
|
||||
addStreamTemplate, err := template.ParseFiles(addStreamHTML, baseHTML)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
addStreamTemplate.Execute(w, struct {
|
||||
Streams map[string]*Stream
|
||||
NameMessage string
|
||||
URLMessage string
|
||||
IntervalMessage string
|
||||
MotionIntervalMessage string
|
||||
}{
|
||||
server.Streams,
|
||||
nameMessage,
|
||||
URLMessage,
|
||||
intervalMessage,
|
||||
motionIntervalMessage,
|
||||
})
|
||||
}
|
||||
|
||||
// addWatchArea adds the POSTed WatchArea to Stream.WatchAreas
|
||||
func (server Server) addWatchArea(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "POST" {
|
||||
return
|
||||
}
|
||||
streamName := r.FormValue("streamName")
|
||||
if streamName == "" {
|
||||
return
|
||||
}
|
||||
stream, exists := server.Streams[streamName]
|
||||
if !exists {
|
||||
return
|
||||
}
|
||||
name := r.FormValue("name")
|
||||
if name == "" {
|
||||
return
|
||||
}
|
||||
x0 := r.FormValue("x0")
|
||||
if x0 == "" {
|
||||
return
|
||||
}
|
||||
y0 := r.FormValue("y0")
|
||||
if y0 == "" {
|
||||
return
|
||||
}
|
||||
x1 := r.FormValue("x1")
|
||||
if x1 == "" {
|
||||
return
|
||||
}
|
||||
y1 := r.FormValue("y1")
|
||||
if y1 == "" {
|
||||
return
|
||||
}
|
||||
R := r.FormValue("R")
|
||||
if R == "" {
|
||||
return
|
||||
}
|
||||
G := r.FormValue("G")
|
||||
if G == "" {
|
||||
return
|
||||
}
|
||||
B := r.FormValue("B")
|
||||
if B == "" {
|
||||
return
|
||||
}
|
||||
stream.AddWatchArea(
|
||||
name,
|
||||
image.Rect(
|
||||
StrToInt(x0),
|
||||
StrToInt(y0),
|
||||
StrToInt(x1),
|
||||
StrToInt(y1),
|
||||
),
|
||||
color.RGBA{
|
||||
uint8(StrToInt(R)),
|
||||
uint8(StrToInt(G)),
|
||||
uint8(StrToInt(B)),
|
||||
255,
|
||||
},
|
||||
)
|
||||
http.Redirect(w, r, "/?name="+streamName, http.StatusTemporaryRedirect)
|
||||
}
|
||||
|
||||
// streamRecordings shows a table of 'moments', a continuous series of motion detected instants
|
||||
func (server Server) streamRecordings(w http.ResponseWriter, r *http.Request) {
|
||||
streamName := r.FormValue("stream")
|
||||
if streamName == "" {
|
||||
return
|
||||
}
|
||||
watchAreaName := r.FormValue("watchArea")
|
||||
if watchAreaName == "" {
|
||||
return
|
||||
}
|
||||
|
||||
stream, exists := server.Streams[streamName]
|
||||
if !exists {
|
||||
return
|
||||
}
|
||||
|
||||
watchArea, exists := stream.GetWatchAreaByName(watchAreaName)
|
||||
if !exists {
|
||||
return
|
||||
}
|
||||
|
||||
images, err := ioutil.ReadDir(watchArea.GetWatchAreaStoreDir(*stream))
|
||||
if err != nil {
|
||||
log.Println("Could not read watchArea directory", err)
|
||||
return
|
||||
}
|
||||
moments := make([][]string, 0)
|
||||
currentMoment := make([]string, 0)
|
||||
momentIndex := 0
|
||||
foundMoment := false
|
||||
|
||||
for i := range images {
|
||||
if i == 0 { // Skip first for images[i-1] after this if
|
||||
continue
|
||||
}
|
||||
previousImage := images[i-1]
|
||||
previousTime, err := time.Parse(timeLayout, previousImage.Name())
|
||||
if err != nil {
|
||||
log.Println("Can't parse: ", previousImage.Name())
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
currentImage := images[i]
|
||||
currentTime, err := time.Parse(timeLayout, currentImage.Name())
|
||||
if err != nil {
|
||||
log.Println("Can't parse: ", currentImage.Name())
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
// if the time difference is lower then the Stream.Interval, it means the Stream.MotionInterval
|
||||
// Was used, and motion was therefore detected.
|
||||
timeDifference := currentTime.Sub(previousTime)
|
||||
lessThenInterval := timeDifference < (time.Duration(stream.Interval) * time.Millisecond)
|
||||
if lessThenInterval {
|
||||
foundMoment = true
|
||||
currentMoment = append(currentMoment, previousImage.Name())
|
||||
}
|
||||
if !lessThenInterval || i == len(images) {
|
||||
if foundMoment {
|
||||
currentMoment = append(currentMoment, currentImage.Name())
|
||||
moments = append(moments, currentMoment)
|
||||
currentMoment = make([]string, 0)
|
||||
momentIndex++
|
||||
}
|
||||
foundMoment = false
|
||||
}
|
||||
}
|
||||
momentsTemplate, err := template.ParseFiles(momentsHTML, baseHTML)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
momentsJSON, _ := json.Marshal(moments)
|
||||
err = momentsTemplate.Execute(w, struct {
|
||||
Streams map[string]*Stream
|
||||
MomentsJSON string
|
||||
Moments [][]string
|
||||
Stream *Stream
|
||||
WatchArea *WatchArea
|
||||
}{
|
||||
server.Streams,
|
||||
string(momentsJSON[:]),
|
||||
moments,
|
||||
stream,
|
||||
watchArea,
|
||||
})
|
||||
}
|
||||
|
||||
// main sets up the HTTP server and reconstructs the Streams, if available.
|
||||
func main() {
|
||||
staticFileServer := http.FileServer(http.Dir("./static"))
|
||||
http.Handle("/static/", http.StripPrefix("/static/", staticFileServer))
|
||||
|
||||
streamFileServer := http.FileServer(http.Dir("./streams"))
|
||||
http.Handle("/streams/", http.StripPrefix("/streams/", streamFileServer))
|
||||
|
||||
server := Server{
|
||||
Streams: make(map[string]*Stream),
|
||||
}
|
||||
streams, err := ioutil.ReadDir(GetStreamDirPath())
|
||||
if err != nil {
|
||||
log.Fatal("Could not read Streamdir")
|
||||
}
|
||||
for _, streamStoreDir := range streams {
|
||||
if !streamStoreDir.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
// UnMarshal the stream.json into a Stream struct and start a goroutine for UpdateInterval
|
||||
streamJSONPath := filepath.Join(GetStreamDirPath(), streamStoreDir.Name(), "stream.json")
|
||||
stream := StreamFromJSON(streamJSONPath)
|
||||
server.Streams[stream.Name] = stream
|
||||
go stream.UpdateInterval()
|
||||
}
|
||||
|
||||
http.HandleFunc("/", server.index)
|
||||
http.HandleFunc("/addStream", server.addStream)
|
||||
http.HandleFunc("/addWatchArea", server.addWatchArea)
|
||||
http.HandleFunc("/streamRecordings", server.streamRecordings)
|
||||
log.Println("Starting StreamWatcher Server on :8080")
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
}
|
||||
|
|
70
main_test.go
Executable file → Normal file
70
main_test.go
Executable file → Normal file
|
@ -1,35 +1,35 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIndexHandler(t *testing.T) {
|
||||
// TestIndexHandler tests the index
|
||||
req, err := http.NewRequest("GET", "/", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rr := httptest.NewRecorder()
|
||||
handler := http.HandlerFunc(index)
|
||||
|
||||
handler.ServeHTTP(rr, req)
|
||||
|
||||
if status := rr.Code; status != http.StatusOK {
|
||||
t.Errorf("Index returned wrong status code: got %v want %v",
|
||||
status, http.StatusOK)
|
||||
}
|
||||
|
||||
indexTemplate, err := ioutil.ReadFile(filepath.Join("templates", "index.html"))
|
||||
if err != nil {
|
||||
t.Fatal("Can't read index.html")
|
||||
}
|
||||
|
||||
if rr.Body.String() != string(indexTemplate) {
|
||||
t.Fatal("Index response is not the same as index.html")
|
||||
}
|
||||
}
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIndexHandler(t *testing.T) {
|
||||
// TestIndexHandler tests the index
|
||||
req, err := http.NewRequest("GET", "/", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rr := httptest.NewRecorder()
|
||||
handler := http.HandlerFunc(index)
|
||||
|
||||
handler.ServeHTTP(rr, req)
|
||||
|
||||
if status := rr.Code; status != http.StatusOK {
|
||||
t.Errorf("Index returned wrong status code: got %v want %v",
|
||||
status, http.StatusOK)
|
||||
}
|
||||
|
||||
indexTemplate, err := ioutil.ReadFile(filepath.Join("templates", "index.html"))
|
||||
if err != nil {
|
||||
t.Fatal("Can't read index.html")
|
||||
}
|
||||
|
||||
if rr.Body.String() != string(indexTemplate) {
|
||||
t.Fatal("Index response is not the same as index.html")
|
||||
}
|
||||
}
|
||||
|
|
0
requirements.yaml
Executable file → Normal file
0
requirements.yaml
Executable file → Normal file
0
static/css/slider.css
Executable file → Normal file
0
static/css/slider.css
Executable file → Normal file
0
static/css/style.css
Executable file → Normal file
0
static/css/style.css
Executable file → Normal file
0
static/js/add_stream.js
Executable file → Normal file
0
static/js/add_stream.js
Executable file → Normal file
0
static/js/moments.js
Executable file → Normal file
0
static/js/moments.js
Executable file → Normal file
0
static/js/stream.js
Executable file → Normal file
0
static/js/stream.js
Executable file → Normal file
612
stream.go
Executable file → Normal file
612
stream.go
Executable file → Normal file
|
@ -1,306 +1,306 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"image"
|
||||
"image/color"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"math"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"gocv.io/x/gocv"
|
||||
)
|
||||
|
||||
// Stream represents a stream to monitor
|
||||
type Stream struct {
|
||||
Name string `json:"name"`
|
||||
URL string `json:"url"`
|
||||
Interval int `json:"interval"`
|
||||
MotionInterval int `json:"motionInterval"`
|
||||
WatchAreas []WatchArea `json:"watchAreas"`
|
||||
Timeouts float64 `json:"-"`
|
||||
MotionDetected bool `json:"-"`
|
||||
}
|
||||
|
||||
// NewStream creates a new Stream Object
|
||||
func NewStream(Name string, URL string, Interval int, MotionInterval int) *Stream {
|
||||
|
||||
stream := Stream{
|
||||
Name: Name,
|
||||
URL: URL,
|
||||
Interval: Interval,
|
||||
MotionInterval: MotionInterval,
|
||||
WatchAreas: make([]WatchArea, 0),
|
||||
Timeouts: 0,
|
||||
MotionDetected: false,
|
||||
}
|
||||
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{
|
||||
Timeouts: 0,
|
||||
MotionDetected: false,
|
||||
}
|
||||
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)
|
||||
resp, err := http.Get(s.URL)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
s.Timeouts++ // For increasing the Interval time every timeout
|
||||
return
|
||||
}
|
||||
s.Timeouts = 0
|
||||
img, err := ioutil.ReadAll(resp.Body)
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Get the image from the response and write it to disk in a goroutine
|
||||
mat, err := gocv.IMDecode(img, gocv.IMReadColor)
|
||||
if err != nil {
|
||||
log.Println("Could not IMDecode img", err)
|
||||
return
|
||||
}
|
||||
go gocv.IMWrite(s.GetCurrentColorInstantPath(), mat)
|
||||
|
||||
// Turn the current image into greyscale and also write that to disk in a goroutine
|
||||
currentGrey := gocv.NewMat()
|
||||
gocv.CvtColor(mat, ¤tGrey, gocv.ColorBGRAToGray)
|
||||
gocv.GaussianBlur(currentGrey, ¤tGrey, image.Point{X: 21, Y: 21}, 0, 0, gocv.BorderReflect)
|
||||
go s.SaveStreamInstant(currentGrey)
|
||||
|
||||
if !s.PreviousInstantPathExists() {
|
||||
return
|
||||
}
|
||||
// If there is a previous image saved to disk, calculate the difference between the two and amplify those differences
|
||||
diff := gocv.NewMat()
|
||||
previousGrey := s.IMReadPrevious()
|
||||
gocv.AbsDiff(previousGrey, currentGrey, &diff)
|
||||
gocv.Threshold(diff, &diff, 100, 255, gocv.ThresholdBinary)
|
||||
|
||||
dilateKernel := gocv.GetStructuringElement(gocv.MorphRect, image.Pt(20, 20))
|
||||
gocv.Dilate(diff, &diff, dilateKernel)
|
||||
|
||||
debug := gocv.NewMat()
|
||||
diff.CopyTo(&debug)
|
||||
gocv.CvtColor(debug, &debug, gocv.ColorGrayToBGRA)
|
||||
|
||||
// Get the contours and check if they are in any of the WatchAreas
|
||||
contours := gocv.FindContours(diff, gocv.RetrievalExternal, gocv.ChainApproxSimple)
|
||||
s.MotionDetected = false
|
||||
for _, watchArea := range s.WatchAreas {
|
||||
gocv.Rectangle(&debug, watchArea.Area, watchArea.Color, 10)
|
||||
if s.CheckWatchAreas(watchArea.Area, contours) {
|
||||
s.MotionDetected = true
|
||||
go watchArea.CopyInstant(s.GetCurrentColorInstantPath(), *s)
|
||||
}
|
||||
}
|
||||
gocv.IMWrite(s.GetDebugInstantPath(), debug)
|
||||
}
|
||||
|
||||
// CheckWatchAreas unexported
|
||||
func (s *Stream) CheckWatchAreas(area image.Rectangle, contours [][]image.Point) bool {
|
||||
for _, contour := range contours {
|
||||
for _, point := range contour {
|
||||
if point.In(area) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// UpdateInterval calls Update() every interval
|
||||
func (s *Stream) UpdateInterval() {
|
||||
for {
|
||||
s.Update()
|
||||
if s.MotionDetected {
|
||||
time.Sleep(time.Duration(s.MotionInterval) * time.Millisecond)
|
||||
} else {
|
||||
expTimeout := math.Pow(2, s.Timeouts)
|
||||
maxExpTimeout := (int)(math.Min(12, expTimeout))
|
||||
time.Sleep(time.Duration(s.Interval*maxExpTimeout) * time.Millisecond)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GetWatchAreaByName finds the WatchArea with the name, if it doesn't exist returns nil, false
|
||||
func (s *Stream) GetWatchAreaByName(name string) (*WatchArea, bool) {
|
||||
for _, watchArea := range s.WatchAreas {
|
||||
if name == watchArea.Name {
|
||||
return &watchArea, true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// SaveStreamInstant writes the img to the CurrentStreamInstantPath, moves existing instant to PreviousStreamInstantPath
|
||||
func (s *Stream) SaveStreamInstant(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.Println("Couldn't copy current image instant to previous image instant", err)
|
||||
}
|
||||
err = os.Rename(next, current)
|
||||
if err != nil {
|
||||
log.Println("Couldn't copy next image instant to current image instant", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return filepath.Join(s.GetStreamStoreDirPath(), "stream.json")
|
||||
}
|
||||
|
||||
// WriteStreamJSON writes the Stream struct to GetStreamJSONPath()
|
||||
func (s *Stream) WriteStreamJSON() {
|
||||
if !FileExists(s.GetStreamStoreDirPath()) {
|
||||
os.MkdirAll(s.GetStreamStoreDirPath(), os.ModePerm)
|
||||
}
|
||||
file, _ := json.MarshalIndent(s, "", "\t")
|
||||
_ = ioutil.WriteFile(s.GetStreamJSONPath(), file, 0644)
|
||||
}
|
||||
|
||||
// GetStreamStoreDirPath returns filepath.Join(s.GetStreamDirPath(), s.Name)
|
||||
func (s *Stream) GetStreamStoreDirPath() string {
|
||||
return filepath.Join(GetStreamDirPath(), s.Name)
|
||||
}
|
||||
|
||||
// GetPreviousInstantPath returns filepath.Join(GetStreamStoreDirPath(), "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())
|
||||
}
|
||||
|
||||
// GetPreviousURL returns the URL towards the static file location of current.jpg
|
||||
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 {
|
||||
return gocv.IMRead(s.GetPreviousInstantPath(), gocv.IMReadGrayScale)
|
||||
}
|
||||
|
||||
// GetCurrentInstantPath returns filepath.Join(s.GetStreamStoreDirPath(), "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())
|
||||
}
|
||||
|
||||
// GetCurrentURL returns the URL towards the static file location of current.jpg
|
||||
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 {
|
||||
return gocv.IMRead(s.GetCurrentInstantPath(), gocv.IMReadGrayScale)
|
||||
}
|
||||
|
||||
// GetCurrentColorInstantPath returns filepath.Join(s.GetStreamStoreDirPath(), "current_color.jpg")
|
||||
func (s *Stream) GetCurrentColorInstantPath() string {
|
||||
return filepath.Join(s.GetStreamStoreDirPath(), "current_color.jpg")
|
||||
}
|
||||
|
||||
// CurrentColorInstantPathExists returns FileExists(GetCurrentColorInstantPath())
|
||||
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 {
|
||||
return filepath.Join("/streams", s.Name, "current_color.jpg")
|
||||
}
|
||||
|
||||
// IMReadCurrentColor returns gocv.IMRead(GetCurrentColorInstantPath(), gocv.IMReadColor)
|
||||
func (s *Stream) IMReadCurrentColor() gocv.Mat {
|
||||
return gocv.IMRead(s.GetCurrentColorInstantPath(), gocv.IMReadColor)
|
||||
}
|
||||
|
||||
// GetNextInstantPath returns filepath.Join(s.GetStreamStoreDirPath(), "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())
|
||||
}
|
||||
|
||||
// GetNextURL returns the URL towards the static file location of next.jpg
|
||||
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 {
|
||||
return gocv.IMRead(s.GetNextInstantPath(), gocv.IMReadGrayScale)
|
||||
}
|
||||
|
||||
// GetDebugInstantPath returns filepath.Join(s.GetStreamStoreDirPath(), "debug.jpg")
|
||||
func (s *Stream) GetDebugInstantPath() string {
|
||||
return filepath.Join(s.GetStreamStoreDirPath(), "debug.jpg")
|
||||
}
|
||||
|
||||
// DebugInstantPathExists returns FileExists(GetDebugInstantPath())
|
||||
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 {
|
||||
return filepath.Join("/streams", s.Name, "debug.jpg")
|
||||
}
|
||||
|
||||
// IMReadDebug returns gocv.IMRead(GetDebugInstantPath(), gocv.IMReadGrayScale)
|
||||
func (s *Stream) IMReadDebug() gocv.Mat {
|
||||
return gocv.IMRead(s.GetDebugInstantPath(), gocv.IMReadColor)
|
||||
}
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"image"
|
||||
"image/color"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"math"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"gocv.io/x/gocv"
|
||||
)
|
||||
|
||||
// Stream represents a stream to monitor
|
||||
type Stream struct {
|
||||
Name string `json:"name"`
|
||||
URL string `json:"url"`
|
||||
Interval int `json:"interval"`
|
||||
MotionInterval int `json:"motionInterval"`
|
||||
WatchAreas []WatchArea `json:"watchAreas"`
|
||||
Timeouts float64 `json:"-"`
|
||||
MotionDetected bool `json:"-"`
|
||||
}
|
||||
|
||||
// NewStream creates a new Stream Object
|
||||
func NewStream(Name string, URL string, Interval int, MotionInterval int) *Stream {
|
||||
|
||||
stream := Stream{
|
||||
Name: Name,
|
||||
URL: URL,
|
||||
Interval: Interval,
|
||||
MotionInterval: MotionInterval,
|
||||
WatchAreas: make([]WatchArea, 0),
|
||||
Timeouts: 0,
|
||||
MotionDetected: false,
|
||||
}
|
||||
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{
|
||||
Timeouts: 0,
|
||||
MotionDetected: false,
|
||||
}
|
||||
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)
|
||||
resp, err := http.Get(s.URL)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
s.Timeouts++ // For increasing the Interval time every timeout
|
||||
return
|
||||
}
|
||||
s.Timeouts = 0
|
||||
img, err := ioutil.ReadAll(resp.Body)
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Get the image from the response and write it to disk in a goroutine
|
||||
mat, err := gocv.IMDecode(img, gocv.IMReadColor)
|
||||
if err != nil {
|
||||
log.Println("Could not IMDecode img", err)
|
||||
return
|
||||
}
|
||||
go gocv.IMWrite(s.GetCurrentColorInstantPath(), mat)
|
||||
|
||||
// Turn the current image into greyscale and also write that to disk in a goroutine
|
||||
currentGrey := gocv.NewMat()
|
||||
gocv.CvtColor(mat, ¤tGrey, gocv.ColorBGRAToGray)
|
||||
gocv.GaussianBlur(currentGrey, ¤tGrey, image.Point{X: 21, Y: 21}, 0, 0, gocv.BorderReflect)
|
||||
go s.SaveStreamInstant(currentGrey)
|
||||
|
||||
if !s.PreviousInstantPathExists() {
|
||||
return
|
||||
}
|
||||
// If there is a previous image saved to disk, calculate the difference between the two and amplify those differences
|
||||
diff := gocv.NewMat()
|
||||
previousGrey := s.IMReadPrevious()
|
||||
gocv.AbsDiff(previousGrey, currentGrey, &diff)
|
||||
gocv.Threshold(diff, &diff, 100, 255, gocv.ThresholdBinary)
|
||||
|
||||
dilateKernel := gocv.GetStructuringElement(gocv.MorphRect, image.Pt(20, 20))
|
||||
gocv.Dilate(diff, &diff, dilateKernel)
|
||||
|
||||
debug := gocv.NewMat()
|
||||
diff.CopyTo(&debug)
|
||||
gocv.CvtColor(debug, &debug, gocv.ColorGrayToBGRA)
|
||||
|
||||
// Get the contours and check if they are in any of the WatchAreas
|
||||
contours := gocv.FindContours(diff, gocv.RetrievalExternal, gocv.ChainApproxSimple)
|
||||
s.MotionDetected = false
|
||||
for _, watchArea := range s.WatchAreas {
|
||||
gocv.Rectangle(&debug, watchArea.Area, watchArea.Color, 10)
|
||||
if s.CheckWatchAreas(watchArea.Area, contours) {
|
||||
s.MotionDetected = true
|
||||
go watchArea.CopyInstant(s.GetCurrentColorInstantPath(), *s)
|
||||
}
|
||||
}
|
||||
gocv.IMWrite(s.GetDebugInstantPath(), debug)
|
||||
}
|
||||
|
||||
// CheckWatchAreas unexported
|
||||
func (s *Stream) CheckWatchAreas(area image.Rectangle, contours [][]image.Point) bool {
|
||||
for _, contour := range contours {
|
||||
for _, point := range contour {
|
||||
if point.In(area) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// UpdateInterval calls Update() every interval
|
||||
func (s *Stream) UpdateInterval() {
|
||||
for {
|
||||
s.Update()
|
||||
if s.MotionDetected {
|
||||
time.Sleep(time.Duration(s.MotionInterval) * time.Millisecond)
|
||||
} else {
|
||||
expTimeout := math.Pow(2, s.Timeouts)
|
||||
maxExpTimeout := (int)(math.Min(12, expTimeout))
|
||||
time.Sleep(time.Duration(s.Interval*maxExpTimeout) * time.Millisecond)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GetWatchAreaByName finds the WatchArea with the name, if it doesn't exist returns nil, false
|
||||
func (s *Stream) GetWatchAreaByName(name string) (*WatchArea, bool) {
|
||||
for _, watchArea := range s.WatchAreas {
|
||||
if name == watchArea.Name {
|
||||
return &watchArea, true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// SaveStreamInstant writes the img to the CurrentStreamInstantPath, moves existing instant to PreviousStreamInstantPath
|
||||
func (s *Stream) SaveStreamInstant(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.Println("Couldn't copy current image instant to previous image instant", err)
|
||||
}
|
||||
err = os.Rename(next, current)
|
||||
if err != nil {
|
||||
log.Println("Couldn't copy next image instant to current image instant", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return filepath.Join(s.GetStreamStoreDirPath(), "stream.json")
|
||||
}
|
||||
|
||||
// WriteStreamJSON writes the Stream struct to GetStreamJSONPath()
|
||||
func (s *Stream) WriteStreamJSON() {
|
||||
if !FileExists(s.GetStreamStoreDirPath()) {
|
||||
os.MkdirAll(s.GetStreamStoreDirPath(), os.ModePerm)
|
||||
}
|
||||
file, _ := json.MarshalIndent(s, "", "\t")
|
||||
_ = ioutil.WriteFile(s.GetStreamJSONPath(), file, 0644)
|
||||
}
|
||||
|
||||
// GetStreamStoreDirPath returns filepath.Join(s.GetStreamDirPath(), s.Name)
|
||||
func (s *Stream) GetStreamStoreDirPath() string {
|
||||
return filepath.Join(GetStreamDirPath(), s.Name)
|
||||
}
|
||||
|
||||
// GetPreviousInstantPath returns filepath.Join(GetStreamStoreDirPath(), "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())
|
||||
}
|
||||
|
||||
// GetPreviousURL returns the URL towards the static file location of current.jpg
|
||||
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 {
|
||||
return gocv.IMRead(s.GetPreviousInstantPath(), gocv.IMReadGrayScale)
|
||||
}
|
||||
|
||||
// GetCurrentInstantPath returns filepath.Join(s.GetStreamStoreDirPath(), "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())
|
||||
}
|
||||
|
||||
// GetCurrentURL returns the URL towards the static file location of current.jpg
|
||||
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 {
|
||||
return gocv.IMRead(s.GetCurrentInstantPath(), gocv.IMReadGrayScale)
|
||||
}
|
||||
|
||||
// GetCurrentColorInstantPath returns filepath.Join(s.GetStreamStoreDirPath(), "current_color.jpg")
|
||||
func (s *Stream) GetCurrentColorInstantPath() string {
|
||||
return filepath.Join(s.GetStreamStoreDirPath(), "current_color.jpg")
|
||||
}
|
||||
|
||||
// CurrentColorInstantPathExists returns FileExists(GetCurrentColorInstantPath())
|
||||
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 {
|
||||
return filepath.Join("/streams", s.Name, "current_color.jpg")
|
||||
}
|
||||
|
||||
// IMReadCurrentColor returns gocv.IMRead(GetCurrentColorInstantPath(), gocv.IMReadColor)
|
||||
func (s *Stream) IMReadCurrentColor() gocv.Mat {
|
||||
return gocv.IMRead(s.GetCurrentColorInstantPath(), gocv.IMReadColor)
|
||||
}
|
||||
|
||||
// GetNextInstantPath returns filepath.Join(s.GetStreamStoreDirPath(), "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())
|
||||
}
|
||||
|
||||
// GetNextURL returns the URL towards the static file location of next.jpg
|
||||
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 {
|
||||
return gocv.IMRead(s.GetNextInstantPath(), gocv.IMReadGrayScale)
|
||||
}
|
||||
|
||||
// GetDebugInstantPath returns filepath.Join(s.GetStreamStoreDirPath(), "debug.jpg")
|
||||
func (s *Stream) GetDebugInstantPath() string {
|
||||
return filepath.Join(s.GetStreamStoreDirPath(), "debug.jpg")
|
||||
}
|
||||
|
||||
// DebugInstantPathExists returns FileExists(GetDebugInstantPath())
|
||||
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 {
|
||||
return filepath.Join("/streams", s.Name, "debug.jpg")
|
||||
}
|
||||
|
||||
// IMReadDebug returns gocv.IMRead(GetDebugInstantPath(), gocv.IMReadGrayScale)
|
||||
func (s *Stream) IMReadDebug() gocv.Mat {
|
||||
return gocv.IMRead(s.GetDebugInstantPath(), gocv.IMReadColor)
|
||||
}
|
||||
|
|
0
templates/add_stream.html
Executable file → Normal file
0
templates/add_stream.html
Executable file → Normal file
0
templates/base.html
Executable file → Normal file
0
templates/base.html
Executable file → Normal file
0
templates/index.html
Executable file → Normal file
0
templates/index.html
Executable file → Normal file
0
templates/moments.html
Executable file → Normal file
0
templates/moments.html
Executable file → Normal file
0
templates/stream.html
Executable file → Normal file
0
templates/stream.html
Executable file → Normal file
66
util.go
Executable file → Normal file
66
util.go
Executable file → Normal file
|
@ -1,33 +1,33 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// FileExists returns true if path exists
|
||||
func FileExists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return !os.IsNotExist(err)
|
||||
}
|
||||
|
||||
// GetStreamDirPath returns filepath.Join(".", "streams")
|
||||
func GetStreamDirPath() string {
|
||||
return filepath.Join(".", "streams")
|
||||
}
|
||||
|
||||
// StreamStorePathExists returns FileExists(GetStreamDirPath())
|
||||
func StreamStorePathExists() bool {
|
||||
return FileExists(GetStreamDirPath())
|
||||
}
|
||||
|
||||
// StrToInt uses strconv.ParseInt
|
||||
func StrToInt(str string) int {
|
||||
i, err := strconv.ParseInt(str, 10, 64)
|
||||
if err != nil {
|
||||
log.Println("Could not convert string to int ", str)
|
||||
}
|
||||
return int(i)
|
||||
}
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// FileExists returns true if path exists
|
||||
func FileExists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return !os.IsNotExist(err)
|
||||
}
|
||||
|
||||
// GetStreamDirPath returns filepath.Join(".", "streams")
|
||||
func GetStreamDirPath() string {
|
||||
return filepath.Join(".", "streams")
|
||||
}
|
||||
|
||||
// StreamStorePathExists returns FileExists(GetStreamDirPath())
|
||||
func StreamStorePathExists() bool {
|
||||
return FileExists(GetStreamDirPath())
|
||||
}
|
||||
|
||||
// StrToInt uses strconv.ParseInt
|
||||
func StrToInt(str string) int {
|
||||
i, err := strconv.ParseInt(str, 10, 64)
|
||||
if err != nil {
|
||||
log.Println("Could not convert string to int ", str)
|
||||
}
|
||||
return int(i)
|
||||
}
|
||||
|
|
118
watch.go
Executable file → Normal file
118
watch.go
Executable file → Normal file
|
@ -1,59 +1,59 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
const timeLayout = time.RFC3339Nano
|
||||
|
||||
// WatchArea defines one or more areas that should be monitored for motion
|
||||
type WatchArea struct {
|
||||
Name string `json:"name"`
|
||||
Color color.RGBA `json:"color"`
|
||||
Area image.Rectangle `json:"area"`
|
||||
}
|
||||
|
||||
// WatchAreaStoreDirExists returns filepath.Join(s.GetStreamStoreDirPath(), w.Name)
|
||||
func (w WatchArea) WatchAreaStoreDirExists(s Stream) bool {
|
||||
return FileExists(w.GetWatchAreaStoreDir(s))
|
||||
}
|
||||
|
||||
// GetWatchAreaStoreDir returns filepath.Join(s.GetStreamStoreDirPath(), w.Name)
|
||||
func (w WatchArea) GetWatchAreaStoreDir(s Stream) string {
|
||||
return filepath.Join(s.GetStreamStoreDirPath(), w.Name)
|
||||
}
|
||||
|
||||
// GetWatchAreaStoreDirInstantPath returns filepath.Join(s.GetWatchAreaStoreDir(), "")
|
||||
func (w WatchArea) GetWatchAreaStoreDirInstantPath(s Stream) string {
|
||||
now := time.Now()
|
||||
return filepath.Join(w.GetWatchAreaStoreDir(s), now.Format(timeLayout))
|
||||
}
|
||||
|
||||
// CopyInstant makes a copy of src to GetWatchAreaStoreDirInstantPath(s)
|
||||
func (w WatchArea) CopyInstant(src string, s Stream) {
|
||||
if !FileExists(w.GetWatchAreaStoreDir(s)) {
|
||||
os.MkdirAll(w.GetWatchAreaStoreDir(s), os.ModePerm)
|
||||
}
|
||||
dest := w.GetWatchAreaStoreDirInstantPath(s)
|
||||
if !FileExists(src) {
|
||||
log.Fatal("Nothing to copy ", src)
|
||||
return
|
||||
}
|
||||
if FileExists(dest) {
|
||||
os.Remove(dest)
|
||||
}
|
||||
srcData, err := ioutil.ReadFile(src)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
err = ioutil.WriteFile(dest, srcData, 0644)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
package main
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
const timeLayout = time.RFC3339Nano
|
||||
|
||||
// WatchArea defines one or more areas that should be monitored for motion
|
||||
type WatchArea struct {
|
||||
Name string `json:"name"`
|
||||
Color color.RGBA `json:"color"`
|
||||
Area image.Rectangle `json:"area"`
|
||||
}
|
||||
|
||||
// WatchAreaStoreDirExists returns filepath.Join(s.GetStreamStoreDirPath(), w.Name)
|
||||
func (w WatchArea) WatchAreaStoreDirExists(s Stream) bool {
|
||||
return FileExists(w.GetWatchAreaStoreDir(s))
|
||||
}
|
||||
|
||||
// GetWatchAreaStoreDir returns filepath.Join(s.GetStreamStoreDirPath(), w.Name)
|
||||
func (w WatchArea) GetWatchAreaStoreDir(s Stream) string {
|
||||
return filepath.Join(s.GetStreamStoreDirPath(), w.Name)
|
||||
}
|
||||
|
||||
// GetWatchAreaStoreDirInstantPath returns filepath.Join(s.GetWatchAreaStoreDir(), "")
|
||||
func (w WatchArea) GetWatchAreaStoreDirInstantPath(s Stream) string {
|
||||
now := time.Now()
|
||||
return filepath.Join(w.GetWatchAreaStoreDir(s), now.Format(timeLayout))
|
||||
}
|
||||
|
||||
// CopyInstant makes a copy of src to GetWatchAreaStoreDirInstantPath(s)
|
||||
func (w WatchArea) CopyInstant(src string, s Stream) {
|
||||
if !FileExists(w.GetWatchAreaStoreDir(s)) {
|
||||
os.MkdirAll(w.GetWatchAreaStoreDir(s), os.ModePerm)
|
||||
}
|
||||
dest := w.GetWatchAreaStoreDirInstantPath(s)
|
||||
if !FileExists(src) {
|
||||
log.Fatal("Nothing to copy ", src)
|
||||
return
|
||||
}
|
||||
if FileExists(dest) {
|
||||
os.Remove(dest)
|
||||
}
|
||||
srcData, err := ioutil.ReadFile(src)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
err = ioutil.WriteFile(dest, srcData, 0644)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue