Browse Source

add privileged service subscription, which grants first look at all events but no bus access in handling.

beelzebub
Gisle Aune 2 years ago
parent
commit
382111583d
  1. 19
      bus.go
  2. 18
      cmd/bustest/main.go

19
bus.go

@ -26,6 +26,7 @@ type Command interface {
type EventBus struct {
mu sync.Mutex
listeners []*serviceListener
privilegedList []ActiveService
signal chan struct{}
}
@ -51,6 +52,15 @@ func (b *EventBus) Join(service Service) {
b.mu.Unlock()
}
// JoinPrivileged will add the services to a list that gets first look at
// all events, but they are not allowed to issue any commands. This is for
// things that cannot have eventual consistency.
func (b *EventBus) JoinPrivileged(service ActiveService) {
b.mu.Lock()
b.privilegedList = append(b.privilegedList, service)
b.mu.Unlock()
}
func (b *EventBus) RunCommand(command Command) {
fmt.Println("[COMMAND]", command.CommandDescription())
b.send(serviceMessage{command: command})
@ -65,6 +75,15 @@ func (b *EventBus) send(message serviceMessage) {
b.mu.Lock()
defer b.mu.Unlock()
for _, service := range b.privilegedList {
if message.command != nil {
service.HandleCommand(nil, message.command)
}
if message.event != nil {
service.HandleEvent(nil, message.event)
}
}
deleteList := make([]int, 0, 0)
for i, listener := range b.listeners {
if !listener.service.Active() {

18
cmd/bustest/main.go

@ -20,12 +20,10 @@ func main() {
resolver := services.NewResolver()
sceneMap := services.NewSceneMap(resolver)
bus.Join(resolver)
bus.Join(sceneMap)
bus.JoinPrivileged(resolver)
bus.JoinPrivileged(sceneMap)
bus.Join(services.NewEffectEnforcer(resolver, sceneMap))
time.Sleep(time.Millisecond)
bus.RunEvent(events.Connected{Prefix: "nanoleaf:10.80.1.11"})
numbers := []int{5, 2, 3, 1, 4}
@ -39,22 +37,16 @@ func main() {
})
}
time.Sleep(time.Millisecond)
bus.RunCommand(commands.ReplaceScene{
Match: "lucifer:name:Hex*",
SceneID: 7,
})
time.Sleep(time.Millisecond)
bus.RunCommand(commands.AddAlias{
Match: "nanoleaf:10.80.1.{11,7,16,5}:*",
Alias: "lucifer:tag:Magic Lamps",
})
time.Sleep(time.Millisecond)
for i, id := range []string{"40e5", "dead", "beef", "cafe", "1337"} {
bus.RunEvent(events.HardwareState{
ID: "nanoleaf:10.80.1.11:" + id,
@ -65,8 +57,6 @@ func main() {
})
}
time.Sleep(time.Millisecond)
bus.RunEvent(events.ExternalEvent{
Kind: "weather",
Values: map[string]string{
@ -76,8 +66,6 @@ func main() {
},
})
time.Sleep(time.Millisecond)
c1 := gentools.Ptr(color.MustParse("rgb:#ff0000"))
c2 := gentools.Ptr(color.MustParse("rgb:#00ff00"))
bus.RunCommand(commands.Assign{
@ -100,8 +88,6 @@ func main() {
},
})
time.Sleep(time.Millisecond * 100)
log.Println("Search \"**:Hexagon {1,5,6}\"")
for _, dev := range resolver.Resolve("lucifer:name:Hexagon {1,5,6}") {
log.Println("- ID:", dev.ID)

Loading…
Cancel
Save