1
0
Fork 0

Some more ground work for command handler

This commit is contained in:
Michał Rudowicz 2025-01-11 23:35:07 +01:00
parent cbc3d68c95
commit 732d173d0d
3 changed files with 35 additions and 9 deletions

View File

@ -6,11 +6,12 @@ import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
) )
func getCurrentStatus(dataStore *DataStore) string { func getCurrentStatus(dataStore *DataStore, config AppConfig) string {
// elements := FilterBasicElementsByTypeOrIndex(dataStore.GetBasicEventElements(), config.AllowedTypes, config.AllowedIndexes)
return "" return ""
} }
func HandleTelegramCommands(bot tgbotapi.BotAPI, logger *log.Logger, dataStore *DataStore) { func HandleTelegramCommands(bot tgbotapi.BotAPI, logger *log.Logger, dataStore *DataStore, config AppConfig) {
u := tgbotapi.NewUpdate(0) u := tgbotapi.NewUpdate(0)
u.Timeout = 60 u.Timeout = 60
@ -32,7 +33,7 @@ func HandleTelegramCommands(bot tgbotapi.BotAPI, logger *log.Logger, dataStore *
// Extract the command from the Message. // Extract the command from the Message.
switch update.Message.Command() { switch update.Message.Command() {
case "status": case "status":
msg.Text = getCurrentStatus(dataStore) msg.Text = getCurrentStatus(dataStore, config)
} }
if _, err := bot.Send(msg); err != nil { if _, err := bot.Send(msg); err != nil {

View File

@ -118,6 +118,21 @@ func (self *DataStore) GetSystemState() map[EventKey]EventValue {
return copiedMap return copiedMap
} }
func (self *DataStore) GetBasicEventElements() []satel.BasicEventElement {
self.mtx.Lock()
defer self.mtx.Unlock()
retval := make([]satel.BasicEventElement, 0)
for key, value := range self.lastSeen {
retval = append(retval, satel.BasicEventElement{
Type: key.ChangeType,
Index: key.Index,
Value: value.Value,
})
}
return retval
}
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

@ -197,14 +197,24 @@ func (filter *FilterByTypeOrIndex) Call(ev satel.Event) {
// no allowed types == all types are allowed // no allowed types == all types are allowed
filter.CallNext(ev) filter.CallNext(ev)
} else { } else {
retEv := satel.Event{BasicEvents: make([]satel.BasicEventElement, 0)} retEv := satel.Event{BasicEvents: FilterBasicElementsByTypeOrIndex(ev.BasicEvents,
for _, basicEventElement := range ev.BasicEvents { filter.allowedTypes, filter.allowedIndexes)}
if isBasicEventElementOkay(basicEventElement, filter.allowedTypes, filter.allowedIndexes) {
retEv.BasicEvents = append(retEv.BasicEvents, basicEventElement)
}
}
if len(retEv.BasicEvents) != 0 { if len(retEv.BasicEvents) != 0 {
filter.CallNext(retEv) filter.CallNext(retEv)
} }
} }
} }
func FilterBasicElementsByTypeOrIndex(input []satel.BasicEventElement, allowedTypes []SatelChangeType, allowedIndexes []int) []satel.BasicEventElement {
if (len(allowedTypes) == 0) && (len(allowedIndexes) == 0) {
return input
}
retval := make([]satel.BasicEventElement, 0)
for _, basicEventElement := range input {
if isBasicEventElementOkay(basicEventElement, allowedTypes, allowedIndexes) {
retval = append(retval, basicEventElement)
}
}
return retval
}