Skip to content

Commit

Permalink
Merge pull request #4 from 0x2142/add-camera-exclusions
Browse files Browse the repository at this point in the history
Add camera exclusions
  • Loading branch information
0x2142 authored Sep 14, 2023
2 parents 23479c3 + bf402d3 commit ab7dca5
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Currently Frigate only supports notifications through Home Assistant, which I do
- SMTP

**Other**
- Aliveness monitor via HTTP GET (for use with tools like [Uptime Kuma](https://github.com/louislam/uptime-kuma))
- Aliveness monitor via HTTP GET (for use with tools like [HealthChecks](https://github.com/healthchecks/healthchecks) or [Uptime Kuma](https://github.com/louislam/uptime-kuma))

## Setup

Expand Down Expand Up @@ -50,10 +50,10 @@ $ docker run -v /path/to/config.yml:/app/config.yml ghcr.io/0x2142/frigate-notif
## Future

Just a quick list of things I may add later:
- Ability to specify which cameras to alert on
- Dampening time between notifications
- Additional alerting methods

> If you use this code & have any specific feature requests - please feel free to open an issue with the details of what you would like to see added
## Screenshots

Expand Down
22 changes: 18 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ type Config struct {
}

type Frigate struct {
Server string `yaml:"server"`
Insecure bool `yaml:"ignoressl"`
WebAPI WebAPI `yaml:"webapi"`
MQTT MQTT `yaml:"mqtt"`
Server string `yaml:"server"`
Insecure bool `yaml:"ignoressl"`
WebAPI WebAPI `yaml:"webapi"`
MQTT MQTT `yaml:"mqtt"`
Cameras Cameras `yaml:"cameras"`
}

type WebAPI struct {
Expand All @@ -35,6 +36,10 @@ type MQTT struct {
Password string `yaml:"password"`
}

type Cameras struct {
Exclude []string `yaml:"exclude"`
}

type Alerts struct {
Discord Discord `yaml:"discord"`
Gotify Gotify `yaml:"gotify"`
Expand Down Expand Up @@ -138,6 +143,15 @@ func validateConfig() {
frigate.MQTTPass = ConfigData.Frigate.MQTT.Password
}

// Check for camera exclusions
if len(ConfigData.Frigate.Cameras.Exclude) > 0 {
log.Println("Cameras to exclude from alerting:")
for _, c := range ConfigData.Frigate.Cameras.Exclude {
log.Println(" -", c)
}
frigate.ExcludeCameras = ConfigData.Frigate.Cameras.Exclude
}

// Check / Load alerting configuration
if ConfigData.Alerts.Discord.Enabled {
log.Print("Discord alerting enabled.")
Expand Down
13 changes: 10 additions & 3 deletions events/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/0x2142/frigate-notify/notifier"
"github.com/0x2142/frigate-notify/util"
"golang.org/x/exp/slices"
)

const eventsURI = "/api/events"
Expand Down Expand Up @@ -42,14 +43,20 @@ func CheckForEvents() {
// Convert to human-readable timestamp
eventTime := time.Unix(int64(event.StartTime), 0)

log.Printf("Event ID %v detected %v in zone(s): %v", event.ID, event.Label, event.Zones)
log.Println("Event Start time: ", eventTime)

// Update last event check time with most recent timestamp
if event.StartTime > LastEventTime {
LastEventTime = event.StartTime
}

// Skip excluded cameras
if slices.Contains(ExcludeCameras, event.Camera) {
log.Printf("Skipping event from excluded camera: %v", event.Camera)
continue
}

log.Printf("Event ID %v detected %v in zone(s): %v", event.ID, event.Label, event.Zones)
log.Println("Event Start time: ", eventTime)

// If snapshot was collected, pull down image to send with alert
var snapshot io.Reader
var snapshotURL string
Expand Down
1 change: 1 addition & 0 deletions events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Event struct {

var FrigateServerURL string
var FrigateInsecure = false
var ExcludeCameras []string

// buildMessage constructs message payload for all alerting methods
func buildMessage(time time.Time, event Event) string {
Expand Down
9 changes: 8 additions & 1 deletion events/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/0x2142/frigate-notify/notifier"
mqtt "github.com/eclipse/paho.mqtt.golang"
"golang.org/x/exp/slices"
)

var MQTTServer string
Expand Down Expand Up @@ -68,10 +69,16 @@ func processEvent(client mqtt.Client, msg mqtt.Message) {
json.Unmarshal(msg.Payload(), &event)

if event.Type == "new" {
// Skip excluded cameras
if slices.Contains(ExcludeCameras, event.After.Camera) {
log.Printf("Skipping event from excluded camera: %v", event.After.Camera)
return
}

// Convert to human-readable timestamp
eventTime := time.Unix(int64(event.After.StartTime), 0)

log.Printf("Event ID %v detected %v in zone(s): %v", event.After.ID, event.After.Label, event.After.Zones)
log.Printf("Event ID %v on camera %v detected %v in zone(s): %v", event.After.ID, event.After.Camera, event.After.Label, event.After.Zones)
log.Println("Event Start time: ", eventTime)

// If snapshot was collected, pull down image to send with alert
Expand Down
6 changes: 6 additions & 0 deletions example-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ frigate:
# MQTT Authentication. Leave both blank for anonymous
username:
password:

cameras:
# List of cameras to exclude from being monitored
# Camera names must match frigate configuration
exclude:
- test_cam_01


## Alerting methods
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func main() {
log.Println("App Started.")
for {
frigate.CheckForEvents()
time.Sleep(30 * time.Second)
time.Sleep(time.Duration(ConfigData.Frigate.WebAPI.Interval) * time.Second)
}
}
// Connect MQTT
Expand Down

0 comments on commit ab7dca5

Please sign in to comment.