Skip to content

Commit

Permalink
allow timetable and region to be set through environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricve committed Mar 19, 2023
1 parent af95c0f commit 08f5895
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 20 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ Next to attaching the configuration file, it is also possible to override the co
| `AGENT_HUB_PRIVATE_KEY` | The secret access key linked to your account in Kerberos Hub. | "" |
| `AGENT_HUB_USERNAME` | Your Kerberos Hub username, which owns the above access and secret keys. | "" |
| `AGENT_HUB_SITE` | The site ID of a site you've created in your Kerberos Hub account. | "" |
| `AGENT_HUB_TIME` | Enable the timetable for Kerberos Agent | "false" |
| `AGENT_HUB_TIMETABLE` | A (weekly) time table to specify when to make recordings "start1,end1,start2,end2;start1.. | "" |
| `AGENT_HUB_REGION_POLYGON` | A single polygon set for motion detection: "x1,y1;x2,y2;x3,y3;... | "" |
| `AGENT_MQTT_URI` | A MQTT broker endpoint that is used for bi-directional communication (live view, onvif, etc) | "tcp://mqtt.kerberos.io:1883" |
| `AGENT_MQTT_USERNAME` | Username of the MQTT broker. | "" |
| `AGENT_MQTT_PASSWORD` | Password of the MQTT broker. | "" |
Expand Down
81 changes: 79 additions & 2 deletions machinery/src/components/Config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ func ReadUserConfig() (userConfig models.User) {
for {
jsonFile, err := os.Open("./data/config/user.json")
if err != nil {
fmt.Println(err)
fmt.Println("Config file is not found " + "./data/config/user.json" + ", trying again in 5s.")
fmt.Println("Config file is not found " + "./data/config/user.json, trying again in 5s: " + err.Error())
time.Sleep(5 * time.Second)
} else {
fmt.Println("Successfully Opened user.json")
Expand Down Expand Up @@ -292,6 +291,84 @@ func OverrideWithEnvironmentVariables(configuration *models.Configuration) {
configuration.Config.HubSite = value
break

/* Conditions */

case "AGENT_HUB_TIME":
configuration.Config.Time = value
break
case "AGENT_HUB_TIMETABLE":
var timetable []*models.Timetable

// Convert value to timetable array with (start1, end1, start2, end2)
// Where days are limited by ; and time by ,
// su;mo;tu;we;th;fr;sa
// 0,43199,43200,86400;0,43199,43200,86400

// Split days
daysString := strings.Split(value, ";")
for _, dayString := range daysString {
// Split time
timeString := strings.Split(dayString, ",")
if len(timeString) == 4 {
start1, err := strconv.ParseInt(timeString[0], 10, 64)
if err != nil {
continue
}
end1, err := strconv.ParseInt(timeString[1], 10, 64)
if err != nil {
continue
}
start2, err := strconv.ParseInt(timeString[2], 10, 64)
if err != nil {
continue
}
end2, err := strconv.ParseInt(timeString[3], 10, 64)
if err != nil {
continue
}
timetable = append(timetable, &models.Timetable{
Start1: int(start1),
End1: int(end1),
Start2: int(start2),
End2: int(end2),
})
}
}
configuration.Config.Timetable = timetable
break

case "AGENT_HUB_REGION_POLYGON":
var coordinates []models.Coordinate

// Convert value to coordinates array
// 0,0;1,1;2,2;3,3
coordinatesString := strings.Split(value, ";")
for _, coordinateString := range coordinatesString {
coordinate := strings.Split(coordinateString, ",")
if len(coordinate) == 2 {
x, err := strconv.ParseFloat(coordinate[0], 64)
if err != nil {
continue
}
y, err := strconv.ParseFloat(coordinate[1], 64)
if err != nil {
continue
}
coordinates = append(coordinates, models.Coordinate{
X: x,
Y: y,
})
}
}

configuration.Config.Region.Polygon = []models.Polygon{
{
Coordinates: coordinates,
ID: "0",
},
}
break

/* MQTT settings for bi-directional communication */
case "AGENT_MQTT_URI":
configuration.Config.MQTTURI = value
Expand Down
39 changes: 21 additions & 18 deletions machinery/src/computervision/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,24 +132,27 @@ func ProcessMotion(motionCursor *pubsub.QueueCursor, configuration *models.Confi

// Check if within time interval
detectMotion := true
now := time.Now().In(loc)
weekday := now.Weekday()
hour := now.Hour()
minute := now.Minute()
second := now.Second()
timeInterval := config.Timetable[int(weekday)]
if timeInterval != nil {
start1 := timeInterval.Start1
end1 := timeInterval.End1
start2 := timeInterval.Start2
end2 := timeInterval.End2
currentTimeInSeconds := hour*60*60 + minute*60 + second
if (currentTimeInSeconds >= start1 && currentTimeInSeconds <= end1) ||
(currentTimeInSeconds >= start2 && currentTimeInSeconds <= end2) {

} else {
detectMotion = false
log.Log.Debug("ProcessMotion: Time interval not valid, disabling motion detection.")
timeEnabled := config.Time
if timeEnabled != "false" {
now := time.Now().In(loc)
weekday := now.Weekday()
hour := now.Hour()
minute := now.Minute()
second := now.Second()
timeInterval := config.Timetable[int(weekday)]
if timeInterval != nil {
start1 := timeInterval.Start1
end1 := timeInterval.End1
start2 := timeInterval.Start2
end2 := timeInterval.End2
currentTimeInSeconds := hour*60*60 + minute*60 + second
if (currentTimeInSeconds >= start1 && currentTimeInSeconds <= end1) ||
(currentTimeInSeconds >= start2 && currentTimeInSeconds <= end2) {

} else {
detectMotion = false
log.Log.Info("ProcessMotion: Time interval not valid, disabling motion detection.")
}
}
}

Expand Down

0 comments on commit 08f5895

Please sign in to comment.