1
0
Fork 0

Configurable pooling interval

This commit is contained in:
Michał Rudowicz 2024-03-06 08:00:39 +01:00
parent 8237eda13d
commit 7a19f0b5fd
3 changed files with 10 additions and 7 deletions

2
go.mod
View File

@ -3,7 +3,7 @@ module alarm_bot
go 1.19 go 1.19
require ( require (
git.sr.ht/~michalr/go-satel v0.0.0-20240305205259-053187c5d1a0 git.sr.ht/~michalr/go-satel v0.0.0-20240306065228-8979c2dd1ed8
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
github.com/stretchr/testify v1.8.4 github.com/stretchr/testify v1.8.4
) )

2
go.sum
View File

@ -6,6 +6,8 @@ git.sr.ht/~michalr/go-satel v0.0.0-20240304210948-c93d9db530fe h1:huAwK9SgnCqgrb
git.sr.ht/~michalr/go-satel v0.0.0-20240304210948-c93d9db530fe/go.mod h1:J/Bnb8xBRmuEq03dvJKLf3eCwizIhGuomUY4lVGa/6U= git.sr.ht/~michalr/go-satel v0.0.0-20240304210948-c93d9db530fe/go.mod h1:J/Bnb8xBRmuEq03dvJKLf3eCwizIhGuomUY4lVGa/6U=
git.sr.ht/~michalr/go-satel v0.0.0-20240305205259-053187c5d1a0 h1:E5gMgMCgaZltNC9nM5s8gouzbO/X51zymT7+S4LNvvM= git.sr.ht/~michalr/go-satel v0.0.0-20240305205259-053187c5d1a0 h1:E5gMgMCgaZltNC9nM5s8gouzbO/X51zymT7+S4LNvvM=
git.sr.ht/~michalr/go-satel v0.0.0-20240305205259-053187c5d1a0/go.mod h1:J/Bnb8xBRmuEq03dvJKLf3eCwizIhGuomUY4lVGa/6U= git.sr.ht/~michalr/go-satel v0.0.0-20240305205259-053187c5d1a0/go.mod h1:J/Bnb8xBRmuEq03dvJKLf3eCwizIhGuomUY4lVGa/6U=
git.sr.ht/~michalr/go-satel v0.0.0-20240306065228-8979c2dd1ed8 h1:edwd27GRcof9fC93rBv0yTolaEcs5AlFmWyVquYKRK0=
git.sr.ht/~michalr/go-satel v0.0.0-20240306065228-8979c2dd1ed8/go.mod h1:J/Bnb8xBRmuEq03dvJKLf3eCwizIhGuomUY4lVGa/6U=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

13
main.go
View File

@ -60,12 +60,13 @@ func (self RealSleeper) Sleep(ch chan<- interface{}) {
}() }()
} }
func getCmdLineParams(logger *log.Logger) (string, []int64, []satel.ChangeType, []int) { func getCmdLineParams(logger *log.Logger) (string, []int64, []satel.ChangeType, []int, time.Duration) {
satelApiAddr := flag.String("satel-addr", "", "Address that should be used to connect to the SATEL device") satelApiAddr := flag.String("satel-addr", "", "Address that should be used to connect to the SATEL device")
satelApiPort := flag.String("satel-port", "7094", "Port that should be used to connect to the SATEL device") satelApiPort := flag.String("satel-port", "7094", "Port that should be used to connect to the SATEL device")
chatIdRaw := flag.String("tg-chat-id", "", "Telegram Chat ID where to send updates. Use \",\" to specify multiple IDs.") chatIdRaw := flag.String("tg-chat-id", "", "Telegram Chat ID where to send updates. Use \",\" to specify multiple IDs.")
allowedTypesRaw := flag.String("allowed-types", "", "Satel change types that are allowed. All other types will be discarded. By default all are allowed. Use \",\" to specify multiple types.") allowedTypesRaw := flag.String("allowed-types", "", "Satel change types that are allowed. All other types will be discarded. By default all are allowed. Use \",\" to specify multiple types.")
allowedIndexesRaw := flag.String("allowed-indexes", "", "Satel indexes (zones?) that are allowed. All other indexes will be discarded. By default all are allowed. Use \",\" to specify multiple indexes.") allowedIndexesRaw := flag.String("allowed-indexes", "", "Satel indexes (zones?) that are allowed. All other indexes will be discarded. By default all are allowed. Use \",\" to specify multiple indexes.")
satelPoolInterval := flag.Duration("pool-interval", 5*time.Second, "How often should the SATEL device be pooled for changes? Default: 5 seconds.")
flag.Parse() flag.Parse()
if len(*satelApiAddr) == 0 || len(*satelApiPort) == 0 || len(*chatIdRaw) == 0 { if len(*satelApiAddr) == 0 || len(*satelApiPort) == 0 || len(*chatIdRaw) == 0 {
@ -106,15 +107,15 @@ func getCmdLineParams(logger *log.Logger) (string, []int64, []satel.ChangeType,
} }
satelAddr := fmt.Sprintf("%s:%s", *satelApiAddr, *satelApiPort) satelAddr := fmt.Sprintf("%s:%s", *satelApiAddr, *satelApiPort)
return satelAddr, chatIds, allowedTypes, allowedIndexes return satelAddr, chatIds, allowedTypes, allowedIndexes, *satelPoolInterval
} }
func makeSatel(satelAddr string) *satel.Satel { func makeSatel(satelAddr string, poolInterval time.Duration) *satel.Satel {
satelConn, err := net.Dial("tcp", satelAddr) satelConn, err := net.Dial("tcp", satelAddr)
if err != nil { if err != nil {
panic(err) panic(err)
} }
return satel.NewConfig(satelConn, satel.Config{EventsQueueSize: 10}) return satel.NewConfig(satelConn, satel.Config{EventsQueueSize: 10, PoolingInterval: poolInterval})
} }
func getPersistenceFilePath() string { func getPersistenceFilePath() string {
@ -132,9 +133,9 @@ func main() {
logger = log.New(os.Stderr, "Main", log.Lmicroseconds) logger = log.New(os.Stderr, "Main", log.Lmicroseconds)
) )
satelAddr, chatIds, allowedTypes, allowedIndexes := getCmdLineParams(logger) satelAddr, chatIds, allowedTypes, allowedIndexes, poolInterval := getCmdLineParams(logger)
s := makeSatel(satelAddr) s := makeSatel(satelAddr, poolInterval)
logger.Printf("Connected to Satel: %s", satelAddr) logger.Printf("Connected to Satel: %s", satelAddr)
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN")) bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN"))