-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrigatier.go
118 lines (106 loc) · 2.74 KB
/
frigatier.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
package Frigatier
import (
"errors"
"fmt"
"github.com/andreasavg/Frigatier/utils"
mqtt "github.com/eclipse/paho.mqtt.golang"
"gopkg.in/yaml.v3"
"log"
"os"
)
type Frigatier struct {
config *Config
client mqtt.Client
eventsMap map[string]bool
enabledMessengers []Messenger
}
func NewFrigatier() *Frigatier {
return &Frigatier{}
}
func (f *Frigatier) Run() {
f.sanityChecks()
f.parseConfig()
f.checkConfig()
f.createMapsAndSlices()
f.createClient()
f.subscribeToTopics()
f.createMessengers()
f.runForever()
}
func (f *Frigatier) sanityChecks() {
_, err := os.Stat("config.yml")
if errors.Is(err, os.ErrNotExist) {
log.Fatalln("Could not find config.yml.")
}
}
func (f *Frigatier) parseConfig() {
b, err := os.ReadFile("config.yml")
utils.DieIfErr(err, "Error during reading config file: %s")
c := NewConfig()
if err := yaml.Unmarshal(b, c); err != nil {
log.Fatalf("Error persing config file: %s", err.Error())
}
f.config = c
}
func (f *Frigatier) checkConfig() {
if f.config.Frigate.Host == "" {
log.Fatalln("Please specific the frigate host.")
}
if f.config.Frigate.Port == 0 {
log.Fatalln("Please specific the frigate port.")
}
if f.config.Mqtt.Host == "" {
log.Fatalln("Please specific the mqtt host.")
}
if f.config.Mqtt.Port == 0 {
log.Fatalln("Please specific the MQTT port.")
}
}
func (f *Frigatier) createMapsAndSlices() {
f.eventsMap = make(map[string]bool)
f.enabledMessengers = make([]Messenger, 0)
}
func (f *Frigatier) createClient() {
mqttConfig := f.config.Mqtt
connectionString := fmt.Sprintf("tcp://%s:%d", mqttConfig.Host, mqttConfig.Port)
opts := mqtt.NewClientOptions().AddBroker(connectionString).SetClientID("frigatier")
opts.SetDefaultPublishHandler(f.EventHandler)
if mqttConfig.User != "" {
opts.SetUsername(mqttConfig.User)
}
if mqttConfig.Password != "" {
opts.SetPassword(mqttConfig.Password)
}
c := mqtt.NewClient(opts)
if token := c.Connect(); token.Wait() && token.Error() != nil {
log.Fatalf(token.Error().Error())
}
f.client = c
}
func (f *Frigatier) subscribeToTopics() {
mqttConfig := f.config.Mqtt
topicPrefix := mqttConfig.TopicPrefix
if topicPrefix == "" {
topicPrefix = "frigate"
}
eventsTopic := fmt.Sprintf("%s/events", topicPrefix)
if token := f.client.Subscribe(eventsTopic, 0, nil); token.Wait() && token.Error() != nil {
log.Fatalf(token.Error().Error())
}
}
func (f *Frigatier) createMessengers() {
m := f.config.Messengers
if m.Slack.Enabled {
slack := NewSlackClient(m.Slack)
f.enabledMessengers = append(f.enabledMessengers, slack)
}
if m.Discord.Enabled {
discord := NewDiscordMessenger(m.Discord)
f.enabledMessengers = append(f.enabledMessengers, discord)
}
}
func (f *Frigatier) runForever() {
for {
select {}
}
}