1
0
Fork 0

Initial, not building command handler braindump

This commit is contained in:
Michał Rudowicz 2024-03-10 16:01:25 +01:00
parent 0c873ff77d
commit 99008a21a6
6 changed files with 116 additions and 5 deletions

54
command_handler.go Normal file
View File

@ -0,0 +1,54 @@
package main
import (
"html/template"
"log"
"sync"
"git.sr.ht/~michalr/go-satel"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
func handleStatusCommand(dataStore *DataStore, msg tgbotapi.MessageConfig, bot *tgbotapi.BotAPI, tpl *template.Template,
s SatelNameGetter, logger *log.Logger) {
data := dataStore.GetGenericMessageOfAllowedPartitions(satel.ArmedPartition)
msg.Text = data.Format(tpl, s, logger)
if _, err := bot.Send(msg); err != nil {
log.Panic(err)
}
}
func HandleIncomingCommands(dataStore *DataStore, tgChan tgbotapi.UpdatesChannel, bot *tgbotapi.BotAPI, wg *sync.WaitGroup,
tpl *template.Template, s SatelNameGetter, quitChan <-chan interface{}, logger *log.Logger) {
wg.Add(1)
defer wg.Done()
for {
select {
case update, ok := <-tgChan:
if ok == false { // quit when tgChan closes
return
}
if update.Message == nil { // ignore any non-Message updates
continue
}
if !update.Message.IsCommand() { // ignore any non-command Messages
continue
}
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "")
msg.ReplyToMessageID = update.Message.MessageID
switch update.Message.Command() {
case "status":
handleStatusCommand(dataStore, msg, bot, tpl, s, logger)
default:
logger.Print("Received an unknown command: ", update.Message.Command())
}
case <-quitChan:
return
}
}
}

12
command_handler_test.go Normal file
View File

@ -0,0 +1,12 @@
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestCommandHandler_Status(t *testing.T) {
a := assert.New(t)
a.True(true)
}

View File

@ -37,6 +37,7 @@ type DataStore struct {
mtx sync.Mutex mtx sync.Mutex
logger *log.Logger logger *log.Logger
persistenceFilePath string persistenceFilePath string
allowedPartitions []int
lastSeen map[EventKey]EventValue lastSeen map[EventKey]EventValue
} }
@ -99,7 +100,7 @@ func (self *DataStore) saveSystemState() {
} }
} }
func MakeDataStore(logger *log.Logger, persistenceFilePath string) DataStore { func MakeDataStore(logger *log.Logger, persistenceFilePath string, allowedPartitions []int) DataStore {
return DataStore{ return DataStore{
logger: logger, logger: logger,
persistenceFilePath: persistenceFilePath, persistenceFilePath: persistenceFilePath,
@ -118,6 +119,44 @@ func (self *DataStore) GetSystemState() map[EventKey]EventValue {
return copiedMap return copiedMap
} }
func (self *DataStore) isMessageAllowed(key EventKey, allowedType satel.ChangeType) bool {
if key.ChangeType != allowedType {
return false
}
if len(self.allowedPartitions) == 0 {
// all partitions are allowed
return true
}
for _, allowedIndex := range self.allowedPartitions {
if key.Index == allowedIndex {
return true
}
}
return false
}
func (self *DataStore) GetGenericMessageOfAllowedPartitions(allowedType satel.ChangeType) GenericMessage {
self.mtx.Lock()
defer self.mtx.Unlock()
messages := make([]satel.BasicEventElement, 0)
for key, value := range self.lastSeen {
if self.isMessageAllowed(key, allowedType) {
messages = append(messages, satel.BasicEventElement{
Type: key.ChangeType,
Index: key.Index,
Value: value.Value,
})
}
}
return GenericMessage{
ChatIds: EmptyChatId{},
Messages: messages,
}
}
func (self *DataStore) SetSystemState(key EventKey, value EventValue) { func (self *DataStore) SetSystemState(key EventKey, value EventValue) {
self.mtx.Lock() self.mtx.Lock()
self.lastSeen[key] = value self.lastSeen[key] = value

View File

@ -130,7 +130,7 @@ func TestSatelLastSeenFiltering(t *testing.T) {
receivedEvents := make([]satel.Event, 0) receivedEvents := make([]satel.Event, 0)
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
fakeLog := log.New(io.Discard, "", log.Ltime) fakeLog := log.New(io.Discard, "", log.Ltime)
ds := MakeDataStore(fakeLog, tempFileName) ds := MakeDataStore(fakeLog, tempFileName, []int{})
go func() { go func() {
wg.Add(1) wg.Add(1)
@ -166,7 +166,7 @@ func TestSatelLastSeenFilteringWithPersistence(t *testing.T) {
receivedEvents := make([]satel.Event, 0) receivedEvents := make([]satel.Event, 0)
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
fakeLog := log.New(io.Discard, "", log.Ltime) fakeLog := log.New(io.Discard, "", log.Ltime)
ds := MakeDataStore(fakeLog, tempFileName) ds := MakeDataStore(fakeLog, tempFileName, []int{})
go func() { go func() {
wg.Add(1) wg.Add(1)
@ -193,7 +193,7 @@ func TestSatelLastSeenFilteringWithPersistence(t *testing.T) {
testEvents = make(chan satel.Event) testEvents = make(chan satel.Event)
receivedEvents = make([]satel.Event, 0) receivedEvents = make([]satel.Event, 0)
ds = MakeDataStore(fakeLog, tempFileName) ds = MakeDataStore(fakeLog, tempFileName, []int{})
go func() { go func() {
wg.Add(1) wg.Add(1)
for e := range FilterByLastSeen(testEvents, &wg, &ds, fakeLog) { for e := range FilterByLastSeen(testEvents, &wg, &ds, fakeLog) {

View File

@ -148,7 +148,7 @@ func main() {
tpl := template.Must(template.New("TelegramMessage").Parse(TelegramMessageTemplate)) tpl := template.Must(template.New("TelegramMessage").Parse(TelegramMessageTemplate))
dataStore := MakeDataStore(log.New(os.Stderr, "DataStore", log.Lmicroseconds), getPersistenceFilePath()) dataStore := MakeDataStore(log.New(os.Stderr, "DataStore", log.Lmicroseconds), getPersistenceFilePath(), allowedIndexes)
Consume( Consume(
SendToTg(Throttle(NotifyViaHTTP(tgEvents, &wg, log.New(os.Stderr, "HTTPNotify", log.Lmicroseconds)), SendToTg(Throttle(NotifyViaHTTP(tgEvents, &wg, log.New(os.Stderr, "HTTPNotify", log.Lmicroseconds)),

View File

@ -30,10 +30,16 @@ type TgChatId struct {
tgChatId int64 tgChatId int64
} }
type EmptyChatId struct{}
func (self TgChatId) GetTgIds() *[]int64 { func (self TgChatId) GetTgIds() *[]int64 {
return &[]int64{self.tgChatId} return &[]int64{self.tgChatId}
} }
func (self EmptyChatId) GetTgIds() *[]int64 {
return nil
}
func Consume(events <-chan GenericMessage) { func Consume(events <-chan GenericMessage) {
go func() { go func() {
for range events { for range events {