2024-02-08 18:23:46 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"flag"
|
2024-02-08 19:57:16 +00:00
|
|
|
"strings"
|
|
|
|
"strconv"
|
2024-02-08 18:23:46 +00:00
|
|
|
|
|
|
|
"github.com/probakowski/go-satel"
|
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
satel_api_addr := flag.String("satel_addr", "", "Address that should be used to connect to the SATEL device")
|
|
|
|
satel_api_port := flag.String("satel_port", "7094", "Port that should be used to connect to the SATEL device")
|
2024-02-08 19:57:16 +00:00
|
|
|
chat_id_raw := flag.String("tg_chat_id", "", "Telegram Chat ID where to send updates. Use \",\" to specify multiple IDs.")
|
2024-02-08 18:23:46 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2024-02-08 19:57:16 +00:00
|
|
|
if len(*satel_api_addr) == 0 || len(*satel_api_port) == 0 || len(*chat_id_raw) == 0 {
|
2024-02-08 18:23:46 +00:00
|
|
|
fmt.Println("Use --satel_addr=ADDR, --satel_port=PORT and --tg_chat_id=CHAT_ID command line flags to continue.")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-02-08 19:57:16 +00:00
|
|
|
chat_ids_strings := strings.Split(*chat_id_raw, ",")
|
|
|
|
var chat_ids []int64
|
|
|
|
for _, chat_id_str := range chat_ids_strings {
|
|
|
|
chat_id, err := strconv.ParseInt(chat_id_str, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Tried to use a non-int value for one of tg_chat_ids: %s. That's bad.", chat_id_str)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
chat_ids = append(chat_ids, chat_id)
|
|
|
|
}
|
2024-02-08 18:23:46 +00:00
|
|
|
|
|
|
|
satel_addr := fmt.Sprintf("%s:%s", *satel_api_addr, *satel_api_port)
|
|
|
|
satel_conn, err := net.Dial("tcp", satel_addr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
s := satel.NewConfig(satel_conn, satel.Config{EventsQueueSize: 1000})
|
|
|
|
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN"))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for e, ok := <-s.Events; ok; e, ok = <-s.Events {
|
|
|
|
fmt.Println("Change from SATEL: ", "type", e.Type, "index", e.Index, "value", e.Value)
|
2024-02-08 19:57:16 +00:00
|
|
|
for _, chat_id := range chat_ids {
|
|
|
|
msg := tgbotapi.NewMessage(chat_id, fmt.Sprintf("Change from SATEL: Zone: %d Type: %s Value: %t",
|
|
|
|
e.Index, e.Type, e.Value))
|
2024-02-08 18:23:46 +00:00
|
|
|
|
2024-02-08 19:57:16 +00:00
|
|
|
if _, err := bot.Send(msg); err != nil {
|
|
|
|
// TODO: retry sending later in case of problems
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2024-02-08 18:23:46 +00:00
|
|
|
}
|
|
|
|
}
|