1
0
Fork 0

Matterbridge is optional and can appear many times

This commit is contained in:
Michał Rudowicz 2024-12-29 22:28:09 +01:00
parent 94a47b2962
commit 15f6e8bc24
3 changed files with 45 additions and 25 deletions

View File

@ -43,7 +43,7 @@ type AppConfig struct {
DisarmCallbackUrls []string `yaml:"disarm-callback-urls"` DisarmCallbackUrls []string `yaml:"disarm-callback-urls"`
AlarmCallbackUrls []string `yaml:"alarm-callback-urls"` AlarmCallbackUrls []string `yaml:"alarm-callback-urls"`
WriteMemoryProfile bool `yaml:"write-memory-profile"` WriteMemoryProfile bool `yaml:"write-memory-profile"`
Matterbridge MatterbridgeConfig `yaml:"matterbridge"` Matterbridge []MatterbridgeConfig `yaml:"matterbridge"`
} }
func (m *SatelChangeType) UnmarshalYAML(unmarshal func(interface{}) error) error { func (m *SatelChangeType) UnmarshalYAML(unmarshal func(interface{}) error) error {

View File

@ -32,6 +32,15 @@ disarm-callback-urls:
alarm-callback-urls: alarm-callback-urls:
- "test alarm callback url" - "test alarm callback url"
- "second test alarm callback url" - "second test alarm callback url"
matterbridge:
- uri: test_uri_1
token: test_token_1
gateway: test_gateway_1
username: test_username_1
- uri: test_uri_2
token: test_token_2
gateway: test_gateway_2
username: test_username_2
` `
func TestParseYamlConfig(t *testing.T) { func TestParseYamlConfig(t *testing.T) {
@ -47,4 +56,13 @@ func TestParseYamlConfig(t *testing.T) {
a.ElementsMatch([]string{"test arm callback url", "second test arm callback url"}, actualConfig.ArmCallbackUrls) a.ElementsMatch([]string{"test arm callback url", "second test arm callback url"}, actualConfig.ArmCallbackUrls)
a.ElementsMatch([]string{"test disarm callback url", "second test disarm callback url"}, actualConfig.DisarmCallbackUrls) a.ElementsMatch([]string{"test disarm callback url", "second test disarm callback url"}, actualConfig.DisarmCallbackUrls)
a.ElementsMatch([]string{"test alarm callback url", "second test alarm callback url"}, actualConfig.AlarmCallbackUrls) a.ElementsMatch([]string{"test alarm callback url", "second test alarm callback url"}, actualConfig.AlarmCallbackUrls)
a.Equal(actualConfig.Matterbridge[0].URI, "test_uri_1")
a.Equal(actualConfig.Matterbridge[0].Token, "test_token_1")
a.Equal(actualConfig.Matterbridge[0].Gateway, "test_gateway_1")
a.Equal(actualConfig.Matterbridge[0].Username, "test_username_1")
a.Equal(actualConfig.Matterbridge[1].URI, "test_uri_2")
a.Equal(actualConfig.Matterbridge[1].Token, "test_token_2")
a.Equal(actualConfig.Matterbridge[1].Gateway, "test_gateway_2")
a.Equal(actualConfig.Matterbridge[1].Username, "test_username_2")
} }

View File

@ -126,23 +126,25 @@ func SendToMatterbridge(events <-chan GenericMessage, s SatelNameGetter, config
defer wg.Done() defer wg.Done()
for e := range events { for e := range events {
returnEvents <- e returnEvents <- e
for _, matterbridgeConfig := range config.Matterbridge {
body, err := json.Marshal(MatterbridgeMessage{ body, err := json.Marshal(MatterbridgeMessage{
Text: e.Format(tpl, s, logger), Text: e.Format(tpl, s, logger),
Username: config.Matterbridge.Username, Username: matterbridgeConfig.Username,
Gateway: config.Matterbridge.Gateway, Gateway: matterbridgeConfig.Gateway,
}) })
if err != nil { if err != nil {
logger.Fatal("Could not marshal a JSON message: ", err) logger.Fatal("Could not marshal a JSON message: ", err)
} }
req, err := http.NewRequest(http.MethodPost, config.Matterbridge.URI, bytes.NewBuffer(body)) req, err := http.NewRequest(http.MethodPost, matterbridgeConfig.URI, bytes.NewBuffer(body))
req.Header["Authorization"] = []string{fmt.Sprint("Bearer ", config.Matterbridge.Token)} req.Header["Authorization"] = []string{fmt.Sprint("Bearer ", matterbridgeConfig.Token)}
res, err := http.DefaultClient.Do(req) res, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
logger.Print("Could not POST ", config.Matterbridge.URI, ": ", err) logger.Print("Could not POST ", matterbridgeConfig.URI, ": ", err)
return return
} }
logger.Print("Notified via Matterbridge with result ", res.StatusCode) logger.Print("Notified via Matterbridge with result ", res.StatusCode)
} }
}
close(returnEvents) close(returnEvents)
}() }()