You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

214 lines
4.1 KiB

package services
import (
lucifer3 "git.aiterp.net/lucifer3/server"
"git.aiterp.net/lucifer3/server/commands"
"git.aiterp.net/lucifer3/server/device"
"git.aiterp.net/lucifer3/server/events"
"github.com/gobwas/glob"
"sort"
"strings"
"sync"
)
func NewResolver() *Resolver {
return &Resolver{
pointers: make([]*device.Pointer, 0, 128),
patternCache: make(map[string]glob.Glob),
}
}
// Resolver implements both device.Resolver and lucifer3.Service. There should be only one of them as it keeps its
// state from event-sourcery.
type Resolver struct {
mu sync.Mutex
pointers []*device.Pointer
patternCache map[string]glob.Glob
}
func (r *Resolver) resolve(pattern string) []*device.Pointer {
g := r.patternToGlob(pattern)
if g == nil {
return []*device.Pointer{}
}
res := make([]*device.Pointer, 0, 32)
for _, ptr := range r.pointers {
if ptr.Matches(g) {
res = append(res, ptr)
}
}
return res
}
func (r *Resolver) GetByID(id string) *device.Pointer {
r.mu.Lock()
defer r.mu.Unlock()
for _, ptr := range r.pointers {
if ptr.ID == id {
return ptr
}
}
return nil
}
func (r *Resolver) Resolve(pattern string) []device.Pointer {
if pattern == "" {
return []device.Pointer{}
}
r.mu.Lock()
defer r.mu.Unlock()
g := r.patternToGlob(pattern)
if g == nil {
return []device.Pointer{}
}
res := make([]device.Pointer, 0, 32)
for _, ptr := range r.pointers {
if ptr.Matches(g) {
res = append(res, *ptr)
}
}
return res
}
func (r *Resolver) Active() bool {
return true
}
func (r *Resolver) HandleEvent(_ *lucifer3.EventBus, event lucifer3.Event) {
switch event := event.(type) {
// On HardwareState, use the internal name if (and only if) it is not already named.
case events.HardwareState:
r.mu.Lock()
ptr := r.ensure(event.ID)
if event.InternalName != "" && ptr.Name() == "" {
ptr.AddAlias("lucifer:name:" + event.InternalName)
r.sortPointers()
}
r.mu.Unlock()
}
}
func (r *Resolver) HandleCommand(bus *lucifer3.EventBus, command lucifer3.Command) {
var aliasEvents []lucifer3.Event
switch command := command.(type) {
case commands.AddAlias:
r.mu.Lock()
for _, ptr := range r.resolve(command.Match) {
added, removed := ptr.AddAlias(command.Alias)
if added != nil {
aliasEvents = append(aliasEvents, events.AliasAdded{
ID: ptr.ID,
Alias: *added,
})
}
if removed != nil {
aliasEvents = append(aliasEvents, events.AliasRemoved{
ID: ptr.ID,
Alias: *removed,
})
}
}
if strings.HasPrefix(command.Alias, "lucifer:name:") {
r.sortPointers()
}
r.mu.Unlock()
case commands.RemoveAlias:
r.mu.Lock()
for _, ptr := range r.resolve(command.Match) {
if ptr.RemoveAlias(command.Alias) {
aliasEvents = append(aliasEvents, events.AliasRemoved{
ID: ptr.ID,
Alias: command.Alias,
})
}
}
if strings.HasPrefix(command.Alias, "lucifer:name:") {
r.sortPointers()
}
r.mu.Unlock()
}
if len(aliasEvents) > 0 {
go bus.RunEvents(aliasEvents)
}
}
func (r *Resolver) ensure(id string) *device.Pointer {
for _, ptr := range r.pointers {
if ptr.ID == id {
return ptr
}
}
r.pointers = append(r.pointers, &device.Pointer{
ID: id,
})
return r.pointers[len(r.pointers)-1]
}
func (r *Resolver) CompileMatcher(pattern string) device.PointerMatcher {
r.mu.Lock()
defer r.mu.Unlock()
return r.patternToGlob(pattern)
}
func (r *Resolver) patternToGlob(pattern string) glob.Glob {
if cached, ok := r.patternCache[pattern]; ok {
return cached
}
g, err := glob.Compile(pattern, ':')
if err != nil {
return nil
}
// Keep this in bounds. Random cache deletions after 128 length.
if len(r.patternCache) > 128 {
i := 64
for k := range r.patternCache {
if i == 0 {
break
}
delete(r.patternCache, k)
i -= 1
}
}
// Cache it
r.patternCache[pattern] = g
return g
}
func (r *Resolver) sortPointers() {
sort.Slice(r.pointers, func(i, j int) bool {
ni := r.pointers[i].Name()
nj := r.pointers[j].Name()
if ni == nj {
return r.pointers[i].ID < r.pointers[j].ID
}
if ni == "" {
return false
}
if nj == "" {
return true
}
return ni < nj
})
}