2024-02-08 18:23:46 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-02-19 18:52:40 +00:00
|
|
|
"html/template"
|
2024-02-11 21:48:05 +00:00
|
|
|
"log"
|
2024-02-08 18:23:46 +00:00
|
|
|
"net"
|
2024-02-09 22:32:32 +00:00
|
|
|
"os"
|
2024-03-03 12:54:42 +00:00
|
|
|
"path/filepath"
|
2024-02-09 22:32:32 +00:00
|
|
|
"sync"
|
2024-03-23 09:26:36 +00:00
|
|
|
"sync/atomic"
|
2024-02-09 22:32:32 +00:00
|
|
|
"time"
|
2024-02-08 18:23:46 +00:00
|
|
|
|
2024-03-04 20:11:34 +00:00
|
|
|
"git.sr.ht/~michalr/go-satel"
|
2024-02-08 18:23:46 +00:00
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
|
|
)
|
|
|
|
|
2024-02-09 22:32:32 +00:00
|
|
|
const (
|
2024-03-06 18:39:43 +00:00
|
|
|
PersistenceFilename = "hs_wro_last_seen.bin"
|
2024-03-23 09:26:36 +00:00
|
|
|
retryDelaySec = 25
|
2024-02-09 22:32:32 +00:00
|
|
|
)
|
|
|
|
|
2024-02-11 10:51:41 +00:00
|
|
|
type RealSleeper struct {
|
|
|
|
duration time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self RealSleeper) Sleep(ch chan<- interface{}) {
|
2024-02-11 21:51:33 +00:00
|
|
|
go func() {
|
|
|
|
time.Sleep(self.duration)
|
|
|
|
ch <- nil
|
|
|
|
}()
|
2024-02-11 10:51:41 +00:00
|
|
|
}
|
|
|
|
|
2024-03-06 07:00:39 +00:00
|
|
|
func makeSatel(satelAddr string, poolInterval time.Duration) *satel.Satel {
|
2024-02-11 13:56:03 +00:00
|
|
|
satelConn, err := net.Dial("tcp", satelAddr)
|
2024-02-08 18:23:46 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-03-06 07:00:39 +00:00
|
|
|
return satel.NewConfig(satelConn, satel.Config{EventsQueueSize: 10, PoolingInterval: poolInterval})
|
2024-02-11 13:56:03 +00:00
|
|
|
}
|
|
|
|
|
2024-03-03 12:54:42 +00:00
|
|
|
func getPersistenceFilePath() string {
|
|
|
|
var stateDir = os.Getenv("STATE_DIRECTORY")
|
|
|
|
if len(stateDir) != 0 {
|
|
|
|
return filepath.Join(stateDir, PersistenceFilename)
|
|
|
|
}
|
|
|
|
return PersistenceFilename
|
|
|
|
}
|
|
|
|
|
2024-02-11 13:56:03 +00:00
|
|
|
func main() {
|
|
|
|
var (
|
2024-03-23 09:26:36 +00:00
|
|
|
wg sync.WaitGroup
|
|
|
|
tgEvents = make(chan GenericMessage, 5)
|
|
|
|
logger = log.New(os.Stderr, "Main", log.Lmicroseconds)
|
|
|
|
sleeper = RealSleeper{time.Second * 60}
|
2024-03-30 06:21:37 +00:00
|
|
|
cleanShutdown = atomic.Bool{}
|
2024-02-11 13:56:03 +00:00
|
|
|
)
|
|
|
|
|
2024-03-30 06:21:37 +00:00
|
|
|
cleanShutdown.Store(false)
|
2024-03-24 08:32:03 +00:00
|
|
|
config := MakeConfig(logger)
|
2024-02-11 13:56:03 +00:00
|
|
|
|
2024-03-24 17:39:12 +00:00
|
|
|
s := makeSatel(config.SatelAddr, config.PoolInterval.GetDuration())
|
2024-03-24 15:54:33 +00:00
|
|
|
logger.Printf("Connected to Satel: %s", config.SatelAddr)
|
2024-02-11 13:56:03 +00:00
|
|
|
|
2025-01-03 20:47:07 +00:00
|
|
|
var bot TelegramBotSender = nil
|
|
|
|
if len(config.TelegramApiKey) != 0 {
|
|
|
|
b, err := tgbotapi.NewBotAPI(config.TelegramApiKey)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
logger.Print("Created Telegram Bot API client")
|
|
|
|
bot = b
|
|
|
|
} else {
|
|
|
|
bot = EmptySender{}
|
2024-02-09 22:32:32 +00:00
|
|
|
}
|
|
|
|
|
2024-03-24 15:54:33 +00:00
|
|
|
tgSender := TgSender{bot, s, log.New(os.Stderr, "TgFormatter", log.Lmicroseconds), config.ChatIds}
|
2024-02-18 16:28:32 +00:00
|
|
|
|
2024-12-27 11:28:51 +00:00
|
|
|
tgTpl := template.Must(template.New("TelegramMessage").Parse(TelegramMessageTemplate))
|
|
|
|
ircTpl := template.Must(template.New("IRCMessage").Parse(IRCMessageTemplate))
|
2024-02-19 18:52:40 +00:00
|
|
|
|
2024-03-03 12:54:42 +00:00
|
|
|
dataStore := MakeDataStore(log.New(os.Stderr, "DataStore", log.Lmicroseconds), getPersistenceFilePath())
|
2024-02-28 20:26:20 +00:00
|
|
|
|
2025-01-08 19:24:45 +00:00
|
|
|
collect := CollectFromChannel[GenericMessage]{}
|
|
|
|
notifyViaHttp := MakeNofityViaHTTPSync(config, log.New(os.Stderr, "HTTPNotify", log.Lmicroseconds))
|
|
|
|
throttle := MakeThrottleSync(sleeper, log.New(os.Stderr, "MessageThrottle", log.Lmicroseconds), &wg)
|
|
|
|
sendToTg := MakeSendToTelegramSync(tgSender, log.New(os.Stderr, "SendToTg", log.Lmicroseconds), tgTpl)
|
|
|
|
sendToMatterbridge := MakeSendToMatterbridgeSync(s, config,
|
|
|
|
log.New(os.Stderr, "SendToMatterbridge", log.Lmicroseconds),
|
|
|
|
ircTpl)
|
|
|
|
|
|
|
|
collect.Then(notifyViaHttp).Then(throttle).Then(sendToTg).Then(sendToMatterbridge)
|
|
|
|
collect.Collect(tgEvents, &wg, func() {})
|
2024-02-18 16:28:32 +00:00
|
|
|
|
2024-03-30 06:21:37 +00:00
|
|
|
go CloseSatelOnCtrlC(s, &cleanShutdown)
|
|
|
|
|
2024-04-08 17:27:35 +00:00
|
|
|
closeDebugTools := make(chan interface{})
|
|
|
|
if config.WriteMemoryProfile {
|
|
|
|
WriteMemoryProfilePeriodically(&wg, log.New(os.Stderr, "DebugTools", log.Lmicroseconds), closeDebugTools)
|
|
|
|
}
|
|
|
|
|
2024-03-30 06:21:37 +00:00
|
|
|
for e := range FilterByTypeOrIndex(
|
|
|
|
FilterByLastSeen(s.Events, &wg, &dataStore, log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)),
|
|
|
|
&wg, config.AllowedTypes, config.AllowedIndexes) {
|
|
|
|
logger.Print("Received change from SATEL: ", e)
|
|
|
|
tgEvents <- GenericMessage{e.BasicEvents}
|
2024-02-08 18:23:46 +00:00
|
|
|
}
|
2024-02-09 22:32:32 +00:00
|
|
|
|
2024-03-30 06:21:37 +00:00
|
|
|
logger.Print("Closing...")
|
2024-04-06 10:08:41 +00:00
|
|
|
close(closeDebugTools)
|
2024-02-11 13:56:03 +00:00
|
|
|
close(tgEvents)
|
2024-02-09 22:32:32 +00:00
|
|
|
wg.Wait()
|
2024-03-30 06:21:37 +00:00
|
|
|
if cleanShutdown.Load() {
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
2024-02-08 18:23:46 +00:00
|
|
|
}
|