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.
 
 
 
 
 
 

107 lines
2.2 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"
"sync"
)
func NewResolver() *Resolver {
return &Resolver{
pointers: make([]*device.Pointer, 0, 128),
}
}
// 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
}
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) resolve(pattern string) []*device.Pointer {
g, err := glob.Compile(pattern, ':')
if err != 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) Resolve(pattern string) []device.Pointer {
g, err := glob.Compile(pattern, ':')
if err != nil {
return []device.Pointer{}
}
r.mu.Lock()
defer r.mu.Unlock()
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.mu.Unlock()
}
}
func (r *Resolver) HandleCommand(_ *lucifer3.EventBus, command lucifer3.Command) {
switch command := command.(type) {
case commands.AddAlias:
r.mu.Lock()
for _, ptr := range r.resolve(command.Match) {
ptr.AddAlias(command.Alias)
}
r.mu.Unlock()
case commands.RemoveAlias:
r.mu.Lock()
for _, ptr := range r.resolve(command.Match) {
ptr.AddAlias(command.Alias)
}
r.mu.Unlock()
}
}