diff --git a/config.go b/config.go index 9ddb4c5..56c6270 100644 --- a/config.go +++ b/config.go @@ -34,16 +34,16 @@ type MatterbridgeConfig struct { } type AppConfig struct { - SatelAddr string `yaml:"satel-addr"` - ChatIds []int64 `yaml:"tg-chat-ids"` - AllowedTypes []SatelChangeType `yaml:"allowed-types"` - AllowedIndexes []int `yaml:"allowed-indexes"` - PoolInterval OwnDuration `yaml:"pool-interval"` - ArmCallbackUrls []string `yaml:"arm-callback-urls"` - DisarmCallbackUrls []string `yaml:"disarm-callback-urls"` - AlarmCallbackUrls []string `yaml:"alarm-callback-urls"` - WriteMemoryProfile bool `yaml:"write-memory-profile"` - Matterbridge MatterbridgeConfig `yaml:"matterbridge"` + SatelAddr string `yaml:"satel-addr"` + ChatIds []int64 `yaml:"tg-chat-ids"` + AllowedTypes []SatelChangeType `yaml:"allowed-types"` + AllowedIndexes []int `yaml:"allowed-indexes"` + PoolInterval OwnDuration `yaml:"pool-interval"` + ArmCallbackUrls []string `yaml:"arm-callback-urls"` + DisarmCallbackUrls []string `yaml:"disarm-callback-urls"` + AlarmCallbackUrls []string `yaml:"alarm-callback-urls"` + WriteMemoryProfile bool `yaml:"write-memory-profile"` + Matterbridge []MatterbridgeConfig `yaml:"matterbridge"` } func (m *SatelChangeType) UnmarshalYAML(unmarshal func(interface{}) error) error { diff --git a/config_test.go b/config_test.go index 5007a99..46ecf40 100644 --- a/config_test.go +++ b/config_test.go @@ -32,6 +32,15 @@ disarm-callback-urls: alarm-callback-urls: - "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) { @@ -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 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.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") } diff --git a/sender_worker.go b/sender_worker.go index 85d2d98..91daee5 100644 --- a/sender_worker.go +++ b/sender_worker.go @@ -126,22 +126,24 @@ func SendToMatterbridge(events <-chan GenericMessage, s SatelNameGetter, config defer wg.Done() for e := range events { returnEvents <- e - body, err := json.Marshal(MatterbridgeMessage{ - Text: e.Format(tpl, s, logger), - Username: config.Matterbridge.Username, - Gateway: config.Matterbridge.Gateway, - }) - if err != nil { - logger.Fatal("Could not marshal a JSON message: ", err) + for _, matterbridgeConfig := range config.Matterbridge { + body, err := json.Marshal(MatterbridgeMessage{ + Text: e.Format(tpl, s, logger), + Username: matterbridgeConfig.Username, + Gateway: matterbridgeConfig.Gateway, + }) + if err != nil { + logger.Fatal("Could not marshal a JSON message: ", err) + } + req, err := http.NewRequest(http.MethodPost, matterbridgeConfig.URI, bytes.NewBuffer(body)) + req.Header["Authorization"] = []string{fmt.Sprint("Bearer ", matterbridgeConfig.Token)} + res, err := http.DefaultClient.Do(req) + if err != nil { + logger.Print("Could not POST ", matterbridgeConfig.URI, ": ", err) + return + } + logger.Print("Notified via Matterbridge with result ", res.StatusCode) } - req, err := http.NewRequest(http.MethodPost, config.Matterbridge.URI, bytes.NewBuffer(body)) - req.Header["Authorization"] = []string{fmt.Sprint("Bearer ", config.Matterbridge.Token)} - res, err := http.DefaultClient.Do(req) - if err != nil { - logger.Print("Could not POST ", config.Matterbridge.URI, ": ", err) - return - } - logger.Print("Notified via Matterbridge with result ", res.StatusCode) } close(returnEvents) }()