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. 25
      bus.go
  2. 18
      cmd/bustest/main.go

25
bus.go

@ -24,9 +24,10 @@ type Command interface {
} }
type EventBus struct { type EventBus struct {
mu sync.Mutex
listeners []*serviceListener
signal chan struct{}
mu sync.Mutex
listeners []*serviceListener
privilegedList []ActiveService
signal chan struct{}
} }
// JoinCallback joins the event bus for a moment. // JoinCallback joins the event bus for a moment.
@ -51,6 +52,15 @@ func (b *EventBus) Join(service Service) {
b.mu.Unlock() 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) { func (b *EventBus) RunCommand(command Command) {
fmt.Println("[COMMAND]", command.CommandDescription()) fmt.Println("[COMMAND]", command.CommandDescription())
b.send(serviceMessage{command: command}) b.send(serviceMessage{command: command})
@ -65,6 +75,15 @@ func (b *EventBus) send(message serviceMessage) {
b.mu.Lock() b.mu.Lock()
defer b.mu.Unlock() 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) deleteList := make([]int, 0, 0)
for i, listener := range b.listeners { for i, listener := range b.listeners {
if !listener.service.Active() { if !listener.service.Active() {

18
cmd/bustest/main.go

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

Loading…
Cancel
Save