diff --git a/README.md b/README.md index 6441d3a..b4f734f 100644 --- a/README.md +++ b/README.md @@ -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. | "" | diff --git a/machinery/src/components/Config.go b/machinery/src/components/Config.go index 6e76779..1f2b4da 100644 --- a/machinery/src/components/Config.go +++ b/machinery/src/components/Config.go @@ -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") @@ -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 diff --git a/machinery/src/computervision/main.go b/machinery/src/computervision/main.go index 83cfaf2..28ca411 100644 --- a/machinery/src/computervision/main.go +++ b/machinery/src/computervision/main.go @@ -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.") + } } }