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.

65 lines
1.2 KiB

  1. package services
  2. import (
  3. lucifer3 "git.aiterp.net/lucifer3/server"
  4. "git.aiterp.net/lucifer3/server/commands"
  5. "git.aiterp.net/lucifer3/server/device"
  6. "sync"
  7. )
  8. func NewSceneMap(resolver device.Resolver) *SceneMap {
  9. return &SceneMap{
  10. resolver: resolver,
  11. sceneMap: make(map[string]string, 64),
  12. }
  13. }
  14. type SceneMap struct {
  15. resolver device.Resolver
  16. mu sync.Mutex
  17. sceneMap map[string]string
  18. }
  19. func (s *SceneMap) SceneID(id string) *string {
  20. s.mu.Lock()
  21. defer s.mu.Unlock()
  22. sceneID, ok := s.sceneMap[id]
  23. if !ok {
  24. return nil
  25. }
  26. return &sceneID
  27. }
  28. func (s *SceneMap) Active() bool {
  29. return true
  30. }
  31. func (s *SceneMap) HandleEvent(*lucifer3.EventBus, lucifer3.Event) {}
  32. func (s *SceneMap) HandleCommand(_ *lucifer3.EventBus, command lucifer3.Command) {
  33. switch command := command.(type) {
  34. case commands.ReplaceScene:
  35. matched := s.resolver.Resolve(command.Match)
  36. if len(matched) > 0 {
  37. s.mu.Lock()
  38. for _, ptr := range matched {
  39. s.sceneMap[ptr.ID] = command.SceneName
  40. }
  41. s.mu.Unlock()
  42. }
  43. case commands.ClearScene:
  44. matched := s.resolver.Resolve(command.Match)
  45. if len(matched) > 0 {
  46. s.mu.Lock()
  47. for _, ptr := range matched {
  48. delete(s.sceneMap, ptr.ID)
  49. }
  50. s.mu.Unlock()
  51. }
  52. }
  53. }