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

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