package main import ( "bytes" "encoding/json" "fmt" "html/template" "log" "net/http" "sync" "time" "git.sr.ht/~michalr/go-satel" ) const ( NotificationPartitionIndex = 0 httpTimeout = 1 * time.Second ) type Sender interface { Send(msg GenericMessage, tpl *template.Template) error } type Sleeper interface { Sleep(ch chan<- interface{}) } func Consume(events <-chan GenericMessage) { go func() { for range events { } }() } func SendToTg(events <-chan GenericMessage, s Sender, wg *sync.WaitGroup, logger *log.Logger, tpl *template.Template) <-chan GenericMessage { returnEvents := make(chan GenericMessage) wg.Add(1) go func() { defer wg.Done() defer close(returnEvents) for e := range events { returnEvents <- e err := s.Send(e, tpl) if err != nil { // TODO: handle it better panic(err) } } }() return returnEvents } func doHttpNotification(url string, logger *log.Logger, wg *sync.WaitGroup) { wg.Add(1) defer wg.Done() 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, wg *sync.WaitGroup) { for _, uri := range urls { go doHttpNotification(uri, logger, wg) } } func NotifyViaHTTP(events <-chan GenericMessage, config AppConfig, wg *sync.WaitGroup, logger *log.Logger) <-chan GenericMessage { returnEvents := make(chan GenericMessage) wg.Add(1) go func() { defer wg.Done() defer close(returnEvents) for e := range events { returnEvents <- e inner_arm: for _, basicElement := range e.Messages { if (basicElement.Index == NotificationPartitionIndex) && (basicElement.Type == satel.ArmedPartition) { if basicElement.Value == ArmedPartition_Armed { notifyAllHttp(config.ArmCallbackUrls, logger, wg) } else { notifyAllHttp(config.DisarmCallbackUrls, logger, wg) } break inner_arm } } inner_alarm: for _, basicElement := range e.Messages { if basicElement.Type == satel.PartitionAlarm { if basicElement.Value == PartitionAlarm_Alarm { notifyAllHttp(config.AlarmCallbackUrls, logger, wg) break inner_alarm } } } } }() return returnEvents } type MatterbridgeMessage struct { Text string `json:"text"` Username string `json:"username"` Gateway string `json:"gateway"` } func SendToMatterbridge(events <-chan GenericMessage, s SatelNameGetter, config AppConfig, wg *sync.WaitGroup, logger *log.Logger, tpl *template.Template) <-chan GenericMessage { returnEvents := make(chan GenericMessage) wg.Add(1) go func() { defer wg.Done() defer close(returnEvents) for e := range events { returnEvents <- e 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)} client := http.Client{ Timeout: httpTimeout, } res, err := client.Do(req) if err != nil { logger.Print("Could not POST ", matterbridgeConfig.URI, ": ", err) return } logger.Print("Notified via Matterbridge with result ", res.StatusCode) } } }() return returnEvents }