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.

145 lines
3.4 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/events"
  6. "git.aiterp.net/lucifer3/server/internal/gentools"
  7. "reflect"
  8. "strings"
  9. )
  10. func Resolver() lucifer3.Service {
  11. return &resolver{
  12. tags: make(map[string][]string),
  13. names: make(map[string][]string),
  14. }
  15. }
  16. type resolver struct {
  17. tags map[string][]string
  18. names map[string][]string
  19. }
  20. func (r *resolver) Active() bool {
  21. return true
  22. }
  23. func (r *resolver) HandleEvent(_ *lucifer3.EventBus, event lucifer3.Event) {
  24. switch event := event.(type) {
  25. case events.HardwareState:
  26. // On HardwareState, use the internal name if (and only if) it is not already named.
  27. if event.InternalName != "" {
  28. found := false
  29. for _, ids := range r.names {
  30. i := gentools.IndexOf(ids, event.ID)
  31. if i != -1 {
  32. found = true
  33. break
  34. }
  35. }
  36. if !found {
  37. r.names[event.InternalName] = append(r.names[event.InternalName], event.ID)
  38. }
  39. }
  40. }
  41. }
  42. func (r *resolver) rerun(bus *lucifer3.EventBus, resolveList *[]string, command interface{}) {
  43. newList := make([]string, 0, 16)
  44. for _, id := range *resolveList {
  45. switch {
  46. case strings.HasPrefix(id, "lucifer:tag:"):
  47. tag := id[12:]
  48. if len(newList) == 0 {
  49. newList = append(newList, r.tags[tag]...)
  50. } else {
  51. gentools.AddUniques(&newList, r.tags[tag]...)
  52. }
  53. case strings.HasPrefix(id, "lucifer:name:"):
  54. name := id[13:]
  55. if strings.HasSuffix(name, "*") {
  56. prefix := name[:len(name)-1]
  57. for name, ids := range r.names {
  58. if strings.HasPrefix(name, prefix) {
  59. gentools.AddUniques(&newList, ids...)
  60. }
  61. }
  62. } else if strings.HasPrefix(name, "*") {
  63. suffix := name[1:]
  64. for name, ids := range r.names {
  65. if strings.HasSuffix(name, suffix) {
  66. gentools.AddUniques(&newList, ids...)
  67. }
  68. }
  69. } else {
  70. gentools.AddUniques(&newList, r.names[name]...)
  71. }
  72. }
  73. }
  74. if len(newList) > 0 {
  75. *resolveList = newList
  76. bus.RunCommand(reflect.Indirect(reflect.ValueOf(command)).Interface().(lucifer3.Command))
  77. }
  78. }
  79. func (r *resolver) HandleCommand(bus *lucifer3.EventBus, command lucifer3.Command) {
  80. switch command := command.(type) {
  81. case commands.Assign:
  82. r.rerun(bus, &command.IDs, &command)
  83. case commands.ReplaceScene:
  84. r.rerun(bus, &command.IDs, &command)
  85. case commands.ClearScene:
  86. r.rerun(bus, &command.IDs, &command)
  87. case commands.SetName:
  88. if !strings.HasPrefix(command.ID, "lucifer:") {
  89. for name := range r.names {
  90. i := gentools.IndexOf(r.names[name], command.ID)
  91. if i != -1 {
  92. r.names[name] = append(r.names[name][:i], r.names[name][i+1:]...)
  93. }
  94. }
  95. r.names[command.Name] = append(r.names[command.Name], command.ID)
  96. }
  97. case commands.AddTag:
  98. for _, id := range command.IDs {
  99. if strings.HasPrefix(id, "lucifer:") {
  100. continue
  101. }
  102. found := false
  103. for _, id2 := range r.tags[command.Tag] {
  104. if id == id2 {
  105. found = true
  106. break
  107. }
  108. }
  109. if !found {
  110. r.tags[command.Tag] = append(r.tags[command.Tag], id)
  111. }
  112. }
  113. r.rerun(bus, &command.IDs, &command)
  114. case commands.RemoveTag:
  115. for _, id := range command.IDs {
  116. if strings.HasPrefix(id, "lucifer:") {
  117. continue
  118. }
  119. for i, id2 := range r.tags[command.Tag] {
  120. if id == id2 {
  121. r.tags[command.Tag] = append(r.tags[command.Tag][:i], r.tags[command.Tag][i+1:]...)
  122. break
  123. }
  124. }
  125. }
  126. r.rerun(bus, &command.IDs, &command)
  127. }
  128. }