138 lines
3.4 KiB
Go
138 lines
3.4 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"html/template"
|
|
"log"
|
|
"net/http"
|
|
|
|
"git.sr.ht/~michalr/go-satel"
|
|
)
|
|
|
|
type SendToTelegramSync struct {
|
|
SyncFilterImpl[GenericMessage]
|
|
|
|
sender Sender
|
|
logger *log.Logger
|
|
tpl *template.Template
|
|
}
|
|
|
|
func MakeSendToTelegramSync(sender Sender,
|
|
logger *log.Logger,
|
|
tpl *template.Template) *SendToTelegramSync {
|
|
return &SendToTelegramSync{SyncFilterImpl[GenericMessage]{}, sender, logger, tpl}
|
|
}
|
|
|
|
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 {
|
|
SyncFilterImpl[GenericMessage]
|
|
|
|
s SatelNameGetter
|
|
config AppConfig
|
|
logger *log.Logger
|
|
tpl *template.Template
|
|
}
|
|
|
|
func MakeSendToMatterbridgeSync(s SatelNameGetter,
|
|
config AppConfig,
|
|
logger *log.Logger,
|
|
tpl *template.Template) *SendToMatterbridgeSync {
|
|
return &SendToMatterbridgeSync{SyncFilterImpl[GenericMessage]{}, s, config, logger, tpl}
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
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 doHttpNotification(url string, logger *log.Logger) {
|
|
|
|
if len(url) == 0 {
|
|
return
|
|
}
|
|
req, err := http.NewRequest(http.MethodPost, url, nil)
|
|
|
|
client := http.Client{
|
|
Timeout: httpTimeout,
|
|
}
|
|
res, err := client.Do(req)
|
|
if err != nil {
|
|
logger.Print("Could not POST ", url, ": ", err)
|
|
return
|
|
}
|
|
logger.Print("Notified via HTTP with result ", res.StatusCode)
|
|
}
|
|
|
|
func notifyAllHttp(urls []string, logger *log.Logger) {
|
|
for _, uri := range urls {
|
|
doHttpNotification(uri, 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
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|