-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
145 lines (124 loc) · 4.39 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright (C) 2015 Clinton Freeman
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"database/sql"
"flag"
"github.com/MeasureTheFuture/scout/configuration"
"github.com/MeasureTheFuture/scout/controllers"
"github.com/MeasureTheFuture/scout/models"
"github.com/MeasureTheFuture/scout/processes"
"github.com/labstack/echo"
_ "github.com/lib/pq"
"log"
"os"
"strconv"
)
func main() {
var configFile string
var videoFile string
var logFile string
var debug bool
flag.StringVar(&configFile, "configFile", "scout.json", "The path to the configuration file")
flag.StringVar(&videoFile, "videoFile", "", "The path to a video file to detect motion from instead of a webcam")
flag.StringVar(&logFile, "logFile", "scout.log", "The output path for log files.")
flag.BoolVar(&debug, "debug", false, "Should we run scout in debug mode, and render frames of detected materials")
flag.Parse()
// Copy the old log file to a temporary location for transmission to the mothership
// and start a new log for this instance of scout.
tmpLog := "scout_tmp.log"
os.Link("scout.log", tmpLog)
os.Remove("scout.log")
f, err := os.OpenFile("scout.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Fatalf("Unable to open log file: %v", err)
}
defer f.Close()
log.SetOutput(f)
log.Printf("INFO: Starting scout.\n")
// Parse the configuration file.
config, err := configuration.Parse(configFile)
if err != nil {
log.Fatalf("ERROR: Can't parse configuration - %s", err)
}
// Open a connection to the database.
connection := "user=" + config.DBUserName + " dbname=" + config.DBName
if config.DBPassword != "" {
connection = connection + " password=" + config.DBPassword
}
db, err := sql.Open("postgres", connection)
if err != nil {
log.Fatalf("ERROR: Can't open database - %s", err)
}
defer db.Close()
// If no scout exists in the DB, bootstrap the DB by creating one.
c, err := models.NumScouts(db)
if err != nil {
log.Fatalf("ERROR: Unable to cound scouts in DB - %s", err)
}
if c == 0 {
ns := models.Scout{"", "0.0.0.0", 8080, false, "Location " + strconv.FormatInt(c+1, 10), "idle", &models.ScoutSummary{},
6160.0, 10, 128, 5, 500, 30.0, 0, 5.0, 2.0, 1.0, 200, 115000.0}
err = ns.Insert(db)
if err != nil {
log.Fatalf("ERROR: Unable to add initial scout to DB.")
}
}
// Start the background processes.
go processes.SaveLogToDB(tmpLog, db)
go processes.HealthHeartbeat(db)
go processes.Summarise(db, config)
deltaC := make(chan models.Command)
// Test to see if the scout is still in measurement mode on boot and resume if necessary.
go func() {
if _, err := os.Stat(".mtf-measure"); err == nil {
log.Printf("INFO: Resuming.")
deltaC <- models.START_MEASURE
}
}()
go processes.Monitor(db, deltaC, videoFile, debug)
// Start the user interface.
e := echo.New()
e.Static("/", config.StaticAssets)
e.Static("/css", config.StaticAssets+"/css")
e.Static("/fonts", config.StaticAssets+"/fonts")
e.Static("/img", config.StaticAssets+"/img")
// Front-end API for displaying results from the scouts.
e.GET("/scouts", func(c echo.Context) error {
return controllers.GetScouts(db, c)
})
e.GET("/scouts/:uuid/frame.jpg", func(c echo.Context) error {
return controllers.GetScoutFrame(db, c)
})
e.GET("/scouts/:uuid", func(c echo.Context) error {
return controllers.GetScout(db, c)
})
e.PUT("/scouts/:uuid", func(c echo.Context) error {
return controllers.UpdateScout(db, c, deltaC)
})
e.GET("/scouts/:uuid/clearMeasurements", func(c echo.Context) error {
log.Printf("clearing meaasurements")
return controllers.ClearMeasurements(db, c)
})
e.GET("/download.zip", func(c echo.Context) error {
return controllers.DownloadData(db, c)
})
// Start scout user-interface.
if err := e.Start(config.Address); err != nil {
e.Logger.Fatal(err)
}
}