Skip to content

Commit

Permalink
Add support for MQTT authentication (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
0x2142 committed Apr 25, 2023
1 parent 469f6b4 commit 23479c3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
19 changes: 14 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ type WebAPI struct {
}

type MQTT struct {
Enabled bool `yaml:"enabled"`
Server string `yaml:"server"`
Port int `yaml:"port"`
Enabled bool `yaml:"enabled"`
Server string `yaml:"server"`
Port int `yaml:"port"`
Username string `yaml:"username"`
Password string `yaml:"password"`
}

type Alerts struct {
Expand Down Expand Up @@ -125,8 +127,15 @@ func validateConfig() {
}
}

if ConfigData.Frigate.MQTT.Port == 0 {
ConfigData.Frigate.MQTT.Port = 1883
if ConfigData.Frigate.MQTT.Enabled {
if ConfigData.Frigate.MQTT.Port == 0 {
ConfigData.Frigate.MQTT.Port = 1883
}
// Set MQTT config
frigate.MQTTServer = ConfigData.Frigate.MQTT.Server
frigate.MQTTPort = ConfigData.Frigate.MQTT.Port
frigate.MQTTUser = ConfigData.Frigate.MQTT.Username
frigate.MQTTPass = ConfigData.Frigate.MQTT.Password
}

// Check / Load alerting configuration
Expand Down
17 changes: 13 additions & 4 deletions events/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (
mqtt "github.com/eclipse/paho.mqtt.golang"
)

var MQTTServer string
var MQTTPort int
var MQTTUser string
var MQTTPass string

// MQTTEvent stores incoming MQTT payloads from Frigate
type MQTTEvent struct {
Before struct {
Expand All @@ -23,27 +28,31 @@ type MQTTEvent struct {
}

// SubscribeMQTT establishes subscription to MQTT server & listens for messages
func SubscribeMQTT(server string, port int) {
func SubscribeMQTT() {
// MQTT client configuration
opts := mqtt.NewClientOptions()
opts.AddBroker(fmt.Sprintf("tcp://%s:%d", server, port))
opts.AddBroker(fmt.Sprintf("tcp://%s:%d", MQTTServer, MQTTPort))
opts.SetClientID("frigate-notify")
opts.SetAutoReconnect(true)
opts.SetConnectionLostHandler(connectionLostHandler)
opts.SetOnConnectHandler(connectHandler)
if MQTTUser != "" && MQTTPass != "" {
opts.SetUsername(MQTTUser)
opts.SetPassword(MQTTPass)
}

var subscribed = false
var retry = 0
for !subscribed {
if retry >= 3 {
log.Fatalf("ERROR: Max retries exceeded. Failed to establish MQTT session to %s", server)
log.Fatalf("ERROR: Max retries exceeded. Failed to establish MQTT session to %s", MQTTServer)
}
// Connect to MQTT broker
client := mqtt.NewClient(opts)

if token := client.Connect(); token.Wait() && token.Error() != nil {
retry += 1
log.Printf("Could not connect to MQTT at %v: %v", server, token.Error())
log.Printf("Could not connect to MQTT at %v: %v", MQTTServer, token.Error())
log.Printf("Retrying in 10 seconds. Attempt %v of 3.", retry)
time.Sleep(10 * time.Second)
continue
Expand Down
6 changes: 5 additions & 1 deletion example-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ frigate:
mqtt:
# Set to true to enable event collection via MQTT
enabled:
# MQTT host ip
# MQTT host IP
server:
# MQTT host port. Default is 1883 if this is not set
port:
# MQTT Authentication. Leave both blank for anonymous
username:
password:


## Alerting methods
# Any combination of alert destinations can be configured
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func main() {
// Connect MQTT
if ConfigData.Frigate.MQTT.Enabled {
log.Println("Connecting to MQTT Server...")
frigate.SubscribeMQTT(ConfigData.Frigate.MQTT.Server, ConfigData.Frigate.MQTT.Port)
frigate.SubscribeMQTT()
log.Println("App running. Press Ctrl-C to quit.")
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
Expand Down

0 comments on commit 23479c3

Please sign in to comment.