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.
 
 
 
 
 
 

67 lines
1.2 KiB

package services
import (
lucifer3 "git.aiterp.net/lucifer3/server"
"git.aiterp.net/lucifer3/server/commands"
"git.aiterp.net/lucifer3/server/device"
"log"
"sync"
)
func NewSceneMap(resolver device.Resolver) *SceneMap {
return &SceneMap{
resolver: resolver,
sceneMap: make(map[string]int64, 64),
}
}
type SceneMap struct {
resolver device.Resolver
mu sync.Mutex
sceneMap map[string]int64
}
func (s *SceneMap) SceneID(id string) *int64 {
s.mu.Lock()
defer s.mu.Unlock()
sceneID, ok := s.sceneMap[id]
if !ok {
return nil
}
return &sceneID
}
func (s *SceneMap) Active() bool {
return true
}
func (s *SceneMap) HandleEvent(*lucifer3.EventBus, lucifer3.Event) {}
func (s *SceneMap) HandleCommand(_ *lucifer3.EventBus, command lucifer3.Command) {
switch command := command.(type) {
case commands.ReplaceScene:
matched := s.resolver.Resolve(command.Match)
if len(matched) > 0 {
s.mu.Lock()
for _, ptr := range matched {
s.sceneMap[ptr.ID] = command.SceneID
}
s.mu.Unlock()
}
case commands.ClearScene:
matched := s.resolver.Resolve(command.Match)
if len(matched) > 0 {
s.mu.Lock()
for _, ptr := range matched {
log.Println("Got")
delete(s.sceneMap, ptr.ID)
}
s.mu.Unlock()
}
}
}