Some more ground work for command handler
This commit is contained in:
parent
cbc3d68c95
commit
732d173d0d
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
|
22
filters.go
22
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue