1
0
Fork 0

Ability to select HTTP Method in callbacks

This commit is contained in:
Michał Rudowicz 2025-01-27 19:58:14 +01:00
parent f2e5684477
commit 4c50b33e1c
4 changed files with 66 additions and 38 deletions

View File

@ -33,15 +33,21 @@ allowed-indexes:
- 1337 - 1337
pool-interval: 5m pool-interval: 5m
telegram-api-key: "telegram api key" telegram-api-key: "telegram api key"
arm-callback-urls: arm-callbacks:
- "http://192.168.1.10/hello" - uri: "http://192.168.1.10/hello"
- "http://example.com/api" method: "POST"
disarm-callback-urls: - uri: "http://example.com/api"
- "http://192.168.1.10/bye" method: "GET"
- "http://example.com/api2" disarm-callbacks:
alarm-callback-urls: - uri: "http://192.168.1.10/bye"
- "http://192.168.1.10/ohno" method: "POST"
- "http://example.com/api3" - uri: "http://example.com/api2"
method: "GET"
alarm-callbacks:
- uri: "http://192.168.1.10/ohno"
method: "POST"
- uri: "http://example.com/api3"
method: "GET"
matterbridge: matterbridge:
- uri: "http://localhost:4242/api/message" - uri: "http://localhost:4242/api/message"
token: "test_token_1" token: "test_token_1"
@ -70,9 +76,9 @@ To configure that set the `matterbridge` part of the config file with the follow
Set the values in following parts of the config file: Set the values in following parts of the config file:
- `arm-callback-urls` - for an URL that will be POST when partition **0** is armed - `arm-callbacks` - for an URL that will be requested when partition **0** is armed. Use `uri` to specify the URI that will be requested, and method to select which HTTP Method will be used (allowed values: `GET`, `POST`)
- `disarm-callback-urls` - for an URL that will be POST when partition **0** is unarmed - `disarm-callbacks` - for an URL that will be requested when partition **0** is unarmed. Use `uri` to specify the URI that will be requested, and method to select which HTTP Method will be used (allowed values: `GET`, `POST`)
- `alarm-callback-urls` - for an URL that will be POST when **any** partition alarm is activated - `alarm-callbacks` - for an URL that will be requested when **any** partition alarm is activated. Use `uri` to specify the URI that will be requested, and method to select which HTTP Method will be used (allowed values: `GET`, `POST`)
### Filtering events by change type ### Filtering events by change type

View File

@ -33,15 +33,20 @@ type MatterbridgeConfig struct {
Username string `yaml:"username"` Username string `yaml:"username"`
} }
type HttpCallbackConfig struct {
URI string `yaml:"uri"`
Method string `yaml:"method"`
}
type AppConfig struct { type AppConfig struct {
SatelAddr string `yaml:"satel-addr"` SatelAddr string `yaml:"satel-addr"`
ChatIds []int64 `yaml:"tg-chat-ids"` ChatIds []int64 `yaml:"tg-chat-ids"`
AllowedTypes []SatelChangeType `yaml:"allowed-types"` AllowedTypes []SatelChangeType `yaml:"allowed-types"`
AllowedIndexes []int `yaml:"allowed-indexes"` AllowedIndexes []int `yaml:"allowed-indexes"`
PoolInterval OwnDuration `yaml:"pool-interval"` PoolInterval OwnDuration `yaml:"pool-interval"`
ArmCallbackUrls []string `yaml:"arm-callback-urls"` ArmCallbacks []HttpCallbackConfig `yaml:"arm-callbacks"`
DisarmCallbackUrls []string `yaml:"disarm-callback-urls"` DisarmCallbacks []HttpCallbackConfig `yaml:"disarm-callbacks"`
AlarmCallbackUrls []string `yaml:"alarm-callback-urls"` AlarmCallbacks []HttpCallbackConfig `yaml:"alarm-callbacks"`
WriteMemoryProfile bool `yaml:"write-memory-profile"` WriteMemoryProfile bool `yaml:"write-memory-profile"`
Matterbridge []MatterbridgeConfig `yaml:"matterbridge"` Matterbridge []MatterbridgeConfig `yaml:"matterbridge"`
TelegramApiKey string `yaml:"telegram-api-key"` TelegramApiKey string `yaml:"telegram-api-key"`
@ -167,13 +172,13 @@ func MakeConfig(logger *log.Logger) AppConfig {
config.WriteMemoryProfile = false config.WriteMemoryProfile = false
if len(os.Getenv("NOTIFY_URL_ARM")) != 0 { if len(os.Getenv("NOTIFY_URL_ARM")) != 0 {
config.ArmCallbackUrls = append(config.ArmCallbackUrls, os.Getenv("NOTIFY_URL_ARM")) logger.Fatal("NOTIFY_URL_ARM is not supported anymore. Please use the config file instead.")
} }
if len(os.Getenv("NOTIFY_URL_DISARM")) != 0 { if len(os.Getenv("NOTIFY_URL_DISARM")) != 0 {
config.DisarmCallbackUrls = append(config.DisarmCallbackUrls, os.Getenv("NOTIFY_URL_DISARM")) logger.Fatal("NOTIFY_URL_DISARM is not supported anymore. Please use the config file instead.")
} }
if len(os.Getenv("ALARM_URL_ARM")) != 0 { if len(os.Getenv("ALARM_URL_ARM")) != 0 {
config.AlarmCallbackUrls = append(config.AlarmCallbackUrls, os.Getenv("ALARM_URL_ARM")) logger.Fatal("ALARM_URL_ARM is not supported anymore. Please use the config file instead.")
} }
getCmdLineParams(&config, logger) getCmdLineParams(&config, logger)

View File

@ -23,15 +23,21 @@ allowed-indexes:
- 5678 - 5678
- 1337 - 1337
pool-interval: 5m pool-interval: 5m
arm-callback-urls: arm-callbacks:
- "test arm callback url" - uri: "test arm callback url"
- "second test arm callback url" method: "POST"
disarm-callback-urls: - uri: "second test arm callback url"
- "test disarm callback url" method: "GET"
- "second test disarm callback url" disarm-callbacks:
alarm-callback-urls: - uri: "test disarm callback url"
- "test alarm callback url" method: "POST"
- "second test alarm callback url" - uri: "second test disarm callback url"
method: "GET"
alarm-callbacks:
- uri: "test alarm callback url"
method: "POST"
- uri: "second test alarm callback url"
method: "GET"
telegram-api-key: "test api key" telegram-api-key: "test api key"
matterbridge: matterbridge:
- uri: test_uri_1 - uri: test_uri_1
@ -54,9 +60,12 @@ func TestParseYamlConfig(t *testing.T) {
a.ElementsMatch([]int{5678, 1337}, actualConfig.AllowedIndexes) a.ElementsMatch([]int{5678, 1337}, actualConfig.AllowedIndexes)
a.ElementsMatch([]SatelChangeType{{satel.ZoneIsolate}, {satel.ZoneAlarm}}, actualConfig.AllowedTypes) a.ElementsMatch([]SatelChangeType{{satel.ZoneIsolate}, {satel.ZoneAlarm}}, actualConfig.AllowedTypes)
a.Equal(5*time.Minute, actualConfig.PoolInterval.GetDuration()) a.Equal(5*time.Minute, actualConfig.PoolInterval.GetDuration())
a.ElementsMatch([]string{"test arm callback url", "second test arm callback url"}, actualConfig.ArmCallbackUrls) a.ElementsMatch([]HttpCallbackConfig{{"test arm callback url", "POST"}, {"second test arm callback url", "GET"}},
a.ElementsMatch([]string{"test disarm callback url", "second test disarm callback url"}, actualConfig.DisarmCallbackUrls) actualConfig.ArmCallbacks)
a.ElementsMatch([]string{"test alarm callback url", "second test alarm callback url"}, actualConfig.AlarmCallbackUrls) a.ElementsMatch([]HttpCallbackConfig{{"test disarm callback url", "POST"}, {"second test disarm callback url", "GET"}},
actualConfig.DisarmCallbacks)
a.ElementsMatch([]HttpCallbackConfig{{"test alarm callback url", "POST"}, {"second test alarm callback url", "GET"}},
actualConfig.AlarmCallbacks)
a.Equal("test api key", actualConfig.TelegramApiKey) a.Equal("test api key", actualConfig.TelegramApiKey)
a.Equal(actualConfig.Matterbridge[0].URI, "test_uri_1") a.Equal(actualConfig.Matterbridge[0].URI, "test_uri_1")

View File

@ -88,25 +88,33 @@ func MakeNofityViaHTTPSync(config AppConfig, logger *log.Logger) *NotifyViaHTTPS
return &NotifyViaHTTPSync{SyncFilterImpl[GenericMessage]{}, config, logger} return &NotifyViaHTTPSync{SyncFilterImpl[GenericMessage]{}, config, logger}
} }
func doHttpNotification(url string, logger *log.Logger) { func doHttpNotification(callbackConfig HttpCallbackConfig, logger *log.Logger) {
if len(url) == 0 { if len(callbackConfig.URI) == 0 {
return return
} }
req, err := http.NewRequest(http.MethodPost, url, nil) var method = http.MethodPost
if callbackConfig.Method == "GET" {
method = http.MethodGet
} else if callbackConfig.Method == "POST" {
method = http.MethodPost
} else {
logger.Print("Unknown method ", callbackConfig.Method, " , using POST instead.")
}
req, err := http.NewRequest(method, callbackConfig.URI, nil)
client := http.Client{ client := http.Client{
Timeout: httpTimeout, Timeout: httpTimeout,
} }
res, err := client.Do(req) res, err := client.Do(req)
if err != nil { if err != nil {
logger.Print("Could not POST ", url, ": ", err) logger.Print("Could not ", callbackConfig.Method, " ", callbackConfig.URI, ": ", err)
return return
} }
logger.Print("Notified via HTTP with result ", res.StatusCode) logger.Print("Notified via HTTP with result ", res.StatusCode)
} }
func notifyAllHttp(urls []string, logger *log.Logger) { func notifyAllHttp(urls []HttpCallbackConfig, logger *log.Logger) {
for _, uri := range urls { for _, uri := range urls {
doHttpNotification(uri, logger) doHttpNotification(uri, logger)
} }
@ -117,9 +125,9 @@ inner_arm:
for _, basicElement := range msg.Messages { for _, basicElement := range msg.Messages {
if (basicElement.Index == NotificationPartitionIndex) && (basicElement.Type == satel.ArmedPartition) { if (basicElement.Index == NotificationPartitionIndex) && (basicElement.Type == satel.ArmedPartition) {
if basicElement.Value == ArmedPartition_Armed { if basicElement.Value == ArmedPartition_Armed {
notifyAllHttp(notifyViaHttp.config.ArmCallbackUrls, notifyViaHttp.logger) notifyAllHttp(notifyViaHttp.config.ArmCallbacks, notifyViaHttp.logger)
} else { } else {
notifyAllHttp(notifyViaHttp.config.DisarmCallbackUrls, notifyViaHttp.logger) notifyAllHttp(notifyViaHttp.config.DisarmCallbacks, notifyViaHttp.logger)
} }
break inner_arm break inner_arm
} }
@ -128,7 +136,7 @@ inner_alarm:
for _, basicElement := range msg.Messages { for _, basicElement := range msg.Messages {
if basicElement.Type == satel.PartitionAlarm { if basicElement.Type == satel.PartitionAlarm {
if basicElement.Value == PartitionAlarm_Alarm { if basicElement.Value == PartitionAlarm_Alarm {
notifyAllHttp(notifyViaHttp.config.AlarmCallbackUrls, notifyViaHttp.logger) notifyAllHttp(notifyViaHttp.config.AlarmCallbacks, notifyViaHttp.logger)
break inner_alarm break inner_alarm
} }
} }