diff --git a/command_handler.go b/command_handler.go index 7c72a14..3f5f2d1 100644 --- a/command_handler.go +++ b/command_handler.go @@ -6,11 +6,12 @@ import ( 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 "" } -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.Timeout = 60 @@ -32,7 +33,7 @@ func HandleTelegramCommands(bot tgbotapi.BotAPI, logger *log.Logger, dataStore * // Extract the command from the Message. switch update.Message.Command() { case "status": - msg.Text = getCurrentStatus(dataStore) + msg.Text = getCurrentStatus(dataStore, config) } if _, err := bot.Send(msg); err != nil { diff --git a/data_store.go b/data_store.go index bf73071..b311cd2 100644 --- a/data_store.go +++ b/data_store.go @@ -118,6 +118,21 @@ func (self *DataStore) GetSystemState() map[EventKey]EventValue { 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) { self.mtx.Lock() self.lastSeen[key] = value diff --git a/filters.go b/filters.go index 678314c..c6eec35 100644 --- a/filters.go +++ b/filters.go @@ -197,14 +197,24 @@ func (filter *FilterByTypeOrIndex) Call(ev satel.Event) { // no allowed types == all types are allowed filter.CallNext(ev) } else { - retEv := satel.Event{BasicEvents: make([]satel.BasicEventElement, 0)} - for _, basicEventElement := range ev.BasicEvents { - if isBasicEventElementOkay(basicEventElement, filter.allowedTypes, filter.allowedIndexes) { - retEv.BasicEvents = append(retEv.BasicEvents, basicEventElement) - } - } + retEv := satel.Event{BasicEvents: FilterBasicElementsByTypeOrIndex(ev.BasicEvents, + filter.allowedTypes, filter.allowedIndexes)} if len(retEv.BasicEvents) != 0 { 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 +}