1
0
Fork 0
hswro-alarm-bot/sender_sync.go

114 lines
2.9 KiB
Go
Raw Normal View History

2025-01-06 20:38:43 +00:00
package main
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
2025-01-06 23:11:03 +00:00
"git.sr.ht/~michalr/go-satel"
2025-01-06 20:38:43 +00:00
)
type SendToTelegramSync struct {
2025-01-06 22:29:54 +00:00
SyncFilterImpl[GenericMessage]
2025-01-06 20:38:43 +00:00
sender Sender
logger *log.Logger
tpl *template.Template
}
2025-01-06 22:29:54 +00:00
func MakeSendToTelegramSync(sender Sender,
logger *log.Logger,
tpl *template.Template) SendToTelegramSync {
return SendToTelegramSync{SyncFilterImpl[GenericMessage]{}, sender, logger, tpl}
}
2025-01-06 20:38:43 +00:00
func (sendToTg *SendToTelegramSync) Call(msg GenericMessage) {
err := sendToTg.sender.Send(msg, sendToTg.tpl)
if err != nil {
// TODO: handle it better
panic(err)
}
sendToTg.CallNext(msg)
}
type SendToMatterbridgeSync struct {
2025-01-06 22:29:54 +00:00
SyncFilterImpl[GenericMessage]
2025-01-06 20:38:43 +00:00
s SatelNameGetter
config AppConfig
logger *log.Logger
tpl *template.Template
}
2025-01-06 23:11:03 +00:00
func MakeSendToMatterbridgeSync(s SatelNameGetter,
config AppConfig,
logger *log.Logger,
tpl *template.Template) SendToMatterbridgeSync {
return SendToMatterbridgeSync{SyncFilterImpl[GenericMessage]{}, s, config, logger, tpl}
}
2025-01-06 20:38:43 +00:00
func (mbridge *SendToMatterbridgeSync) Call(msg GenericMessage) {
for _, matterbridgeConfig := range mbridge.config.Matterbridge {
body, err := json.Marshal(MatterbridgeMessage{
Text: msg.Format(mbridge.tpl, mbridge.s, mbridge.logger),
Username: matterbridgeConfig.Username,
Gateway: matterbridgeConfig.Gateway,
})
if err != nil {
mbridge.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)}
client := http.Client{
Timeout: httpTimeout,
}
res, err := client.Do(req)
if err != nil {
mbridge.logger.Print("Could not POST ", matterbridgeConfig.URI, ": ", err)
return
}
mbridge.logger.Print("Notified via Matterbridge with result ", res.StatusCode)
}
mbridge.CallNext(msg)
}
2025-01-06 23:11:03 +00:00
type NotifyViaHTTPSync struct {
SyncFilterImpl[GenericMessage]
config AppConfig
logger *log.Logger
}
func MakeNofityViaHTTPSync(config AppConfig, logger *log.Logger) NotifyViaHTTPSync {
return NotifyViaHTTPSync{SyncFilterImpl[GenericMessage]{}, config, logger}
}
func (notifyViaHttp *NotifyViaHTTPSync) Call(msg GenericMessage) {
inner_arm:
for _, basicElement := range msg.Messages {
if (basicElement.Index == NotificationPartitionIndex) && (basicElement.Type == satel.ArmedPartition) {
if basicElement.Value == ArmedPartition_Armed {
notifyAllHttp(notifyViaHttp.config.ArmCallbackUrls, notifyViaHttp.logger)
} else {
notifyAllHttp(notifyViaHttp.config.DisarmCallbackUrls, notifyViaHttp.logger)
}
break inner_arm
}
}
inner_alarm:
for _, basicElement := range msg.Messages {
if basicElement.Type == satel.PartitionAlarm {
if basicElement.Value == PartitionAlarm_Alarm {
notifyAllHttp(notifyViaHttp.config.AlarmCallbackUrls, notifyViaHttp.logger)
break inner_alarm
}
}
}
}