158 lines
4.0 KiB
Go
158 lines
4.0 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 SendNowhere struct {
|
|
SyncFilterImpl[GenericMessage]
|
|
}
|
|
|
|
func MakeSendNowhere() *SendNowhere {
|
|
return &SendNowhere{SyncFilterImpl[GenericMessage]{}}
|
|
}
|
|
|
|
func (sendNowhere *SendNowhere) Call(msg GenericMessage) {
|
|
sendNowhere.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(callbackConfig HttpCallbackConfig, logger *log.Logger) {
|
|
|
|
if len(callbackConfig.URI) == 0 {
|
|
return
|
|
}
|
|
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{
|
|
Timeout: httpTimeout,
|
|
}
|
|
res, err := client.Do(req)
|
|
if err != nil {
|
|
logger.Print("Could not ", callbackConfig.Method, " ", callbackConfig.URI, ": ", err)
|
|
return
|
|
}
|
|
logger.Print("Notified via HTTP with result ", res.StatusCode)
|
|
}
|
|
|
|
func notifyAllHttp(urls []HttpCallbackConfig, 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.ArmCallbacks, notifyViaHttp.logger)
|
|
} else {
|
|
notifyAllHttp(notifyViaHttp.config.DisarmCallbacks, 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.AlarmCallbacks, notifyViaHttp.logger)
|
|
break inner_alarm
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|