add sensor handling.
This commit is contained in:
+135
-11
@@ -3,6 +3,7 @@ package hue
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
lucifer3 "git.aiterp.net/lucifer3/server"
|
||||
"git.aiterp.net/lucifer3/server/device"
|
||||
"git.aiterp.net/lucifer3/server/events"
|
||||
@@ -45,6 +46,8 @@ type Bridge struct {
|
||||
colorFlags map[string]device.ColorFlags
|
||||
reachable map[string]bool
|
||||
hasSeen map[string]bool
|
||||
lastMotion map[string]time.Time
|
||||
lastButton map[string]time.Time
|
||||
|
||||
triggerCongruenceCheckCh chan struct{}
|
||||
|
||||
@@ -112,22 +115,57 @@ func (b *Bridge) RefreshAll() ([]lucifer3.Event, error) {
|
||||
b.mu.Lock()
|
||||
hasSeen := b.hasSeen
|
||||
reachable := b.reachable
|
||||
lastMotion := b.lastMotion
|
||||
b.mu.Unlock()
|
||||
|
||||
oldHasSeen := hasSeen
|
||||
hasSeen = gentools.CopyMap(hasSeen)
|
||||
reachable = gentools.CopyMap(reachable)
|
||||
lastMotion = gentools.CopyMap(lastMotion)
|
||||
|
||||
colorFlags := make(map[string]device.ColorFlags)
|
||||
activeStates := make(map[string]device.State)
|
||||
|
||||
newEvents := make([]lucifer3.Event, 0, 0)
|
||||
extraEvents := make([]lucifer3.Event, 0, 0)
|
||||
for id, res := range resources {
|
||||
if res.Type == "device" && res.Metadata.Archetype != "bridge_v2" {
|
||||
if res.Owner != nil && !oldHasSeen[res.Owner.ID] {
|
||||
if res.Temperature != nil {
|
||||
extraEvents = append(extraEvents, events.TemperatureChanged{
|
||||
ID: b.fullId(*res),
|
||||
Temperature: res.Temperature.Temperature,
|
||||
})
|
||||
}
|
||||
|
||||
if res.Motion != nil {
|
||||
if res.Motion.Motion {
|
||||
extraEvents = append(extraEvents, events.MotionSensed{
|
||||
ID: b.fullId(*res),
|
||||
SecondsSince: 0,
|
||||
})
|
||||
|
||||
lastMotion[b.fullId(*res)] = time.Now()
|
||||
} else {
|
||||
extraEvents = append(extraEvents, events.MotionSensed{
|
||||
ID: b.fullId(*res),
|
||||
SecondsSince: 301,
|
||||
})
|
||||
|
||||
lastMotion[b.fullId(*res)] = time.Now().Add(-time.Millisecond * 301)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if res.Type == "device" {
|
||||
hwState, hwEvent := res.GenerateEvent(b.host, resources)
|
||||
if hwState.SupportFlags == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
newEvents = append(newEvents, hwState)
|
||||
if !hasSeen[id] {
|
||||
newEvents = append(newEvents, hwState, hwEvent, events.DeviceReady{ID: hwState.ID})
|
||||
newEvents = append(newEvents, hwEvent, events.DeviceReady{ID: hwState.ID})
|
||||
hasSeen[id] = true
|
||||
} else {
|
||||
newEvents = append(newEvents, hwState)
|
||||
}
|
||||
|
||||
activeStates[id] = hwState.State
|
||||
@@ -142,29 +180,40 @@ func (b *Bridge) RefreshAll() ([]lucifer3.Event, error) {
|
||||
b.colorFlags = colorFlags
|
||||
b.activeStates = activeStates
|
||||
b.reachable = reachable
|
||||
b.lastMotion = lastMotion
|
||||
b.mu.Unlock()
|
||||
|
||||
return newEvents, nil
|
||||
return append(newEvents, extraEvents...), nil
|
||||
}
|
||||
|
||||
func (b *Bridge) ApplyPatches(resources []ResourceData) (events []lucifer3.Event, shouldRefresh bool) {
|
||||
func (b *Bridge) ApplyPatches(date time.Time, resources []ResourceData) (eventList []lucifer3.Event, shouldRefresh bool) {
|
||||
b.mu.Lock()
|
||||
resourceMap := b.resources
|
||||
activeStates := b.activeStates
|
||||
reachable := b.reachable
|
||||
colorFlags := b.colorFlags
|
||||
lastMotion := b.lastMotion
|
||||
lastButton := b.lastButton
|
||||
b.mu.Unlock()
|
||||
|
||||
mapCopy := gentools.CopyMap(resourceMap)
|
||||
activeStatesCopy := gentools.CopyMap(activeStates)
|
||||
reachableCopy := gentools.CopyMap(reachable)
|
||||
colorFlagsCopy := gentools.CopyMap(colorFlags)
|
||||
lastMotionCopy := gentools.CopyMap(lastMotion)
|
||||
lastButtonCopy := gentools.CopyMap(lastButton)
|
||||
|
||||
for _, resource := range resources {
|
||||
if mapCopy[resource.ID] != nil {
|
||||
mapCopy[resource.ID] = mapCopy[resource.ID].WithPatch(resource)
|
||||
} else {
|
||||
log.Println(resource.ID, resource.Type, "not seen!")
|
||||
eventList = append(eventList, events.Log{
|
||||
ID: b.fullId(resource),
|
||||
Level: "info",
|
||||
Code: "hue_patch_found_unknown_device",
|
||||
Message: "Refresh triggered, because of unknown device",
|
||||
})
|
||||
|
||||
shouldRefresh = true
|
||||
}
|
||||
}
|
||||
@@ -173,13 +222,59 @@ func (b *Bridge) ApplyPatches(resources []ResourceData) (events []lucifer3.Event
|
||||
if resource.Owner != nil && resource.Owner.Kind == "device" {
|
||||
if parent, ok := mapCopy[resource.Owner.ID]; ok {
|
||||
hwState, _ := parent.GenerateEvent(b.host, mapCopy)
|
||||
events = append(events, hwState)
|
||||
eventList = append(eventList, hwState)
|
||||
|
||||
activeStatesCopy[resource.Owner.ID] = hwState.State
|
||||
reachableCopy[resource.Owner.ID] = !hwState.Unreachable
|
||||
if hwState.ColorFlags != 0 {
|
||||
colorFlagsCopy[resource.Owner.ID] = hwState.ColorFlags
|
||||
}
|
||||
|
||||
if resource.Temperature != nil {
|
||||
eventList = append(eventList, events.TemperatureChanged{
|
||||
ID: b.fullId(resource),
|
||||
Temperature: resource.Temperature.Temperature,
|
||||
})
|
||||
}
|
||||
if resource.Motion != nil {
|
||||
if resource.Motion.Motion {
|
||||
eventList = append(eventList, events.MotionSensed{
|
||||
ID: b.fullId(resource),
|
||||
SecondsSince: 0,
|
||||
})
|
||||
|
||||
lastMotionCopy[resource.ID] = time.Now()
|
||||
}
|
||||
}
|
||||
if resource.Button != nil {
|
||||
valid := false
|
||||
if resource.Button.LastEvent == "initial_press" {
|
||||
valid = true
|
||||
} else if resource.Button.LastEvent == "long_release" {
|
||||
valid = false
|
||||
} else if resource.Button.LastEvent == "repeat" {
|
||||
valid = date.Sub(lastButtonCopy[resource.ID]) >= time.Millisecond*500
|
||||
} else {
|
||||
valid = date.Sub(lastButtonCopy[resource.ID]) >= time.Millisecond*990
|
||||
}
|
||||
|
||||
if valid {
|
||||
lastButtonCopy[resource.ID] = date
|
||||
|
||||
owner := resourceMap[resource.Owner.ID]
|
||||
if owner != nil {
|
||||
index := owner.ServiceIndex("button", resource.ID)
|
||||
if index != -1 {
|
||||
eventList = append(eventList, events.ButtonPressed{
|
||||
ID: b.fullId(*owner),
|
||||
Name: []string{"On", "DimUp", "DimDown", "Off"}[index],
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
shouldRefresh = true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -189,6 +284,8 @@ func (b *Bridge) ApplyPatches(resources []ResourceData) (events []lucifer3.Event
|
||||
b.activeStates = activeStatesCopy
|
||||
b.reachable = reachableCopy
|
||||
b.colorFlags = colorFlagsCopy
|
||||
b.lastMotion = lastMotionCopy
|
||||
b.lastButton = lastButtonCopy
|
||||
b.mu.Unlock()
|
||||
|
||||
return
|
||||
@@ -242,6 +339,9 @@ func (b *Bridge) Run(ctx context.Context, bus *lucifer3.EventBus) interface{} {
|
||||
step := time.NewTicker(time.Second * 30)
|
||||
defer step.Stop()
|
||||
|
||||
quickStep := time.NewTicker(time.Second * 5)
|
||||
defer quickStep.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case updates, ok := <-sse:
|
||||
@@ -249,8 +349,12 @@ func (b *Bridge) Run(ctx context.Context, bus *lucifer3.EventBus) interface{} {
|
||||
if !ok {
|
||||
return errors.New("SSE lost connection")
|
||||
}
|
||||
if len(updates) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
newEvents, shouldUpdate := b.ApplyPatches(
|
||||
updates[0].CreationTime,
|
||||
gentools.Flatten(gentools.Map(updates, func(update SSEUpdate) []ResourceData {
|
||||
return update.Data
|
||||
})),
|
||||
@@ -269,6 +373,20 @@ func (b *Bridge) Run(ctx context.Context, bus *lucifer3.EventBus) interface{} {
|
||||
|
||||
b.triggerCongruenceCheck()
|
||||
}
|
||||
case <-quickStep.C:
|
||||
b.mu.Lock()
|
||||
lastMotion := b.lastMotion
|
||||
b.mu.Unlock()
|
||||
|
||||
for id, value := range lastMotion {
|
||||
since := time.Since(value)
|
||||
sinceMod := since % time.Minute
|
||||
if since > time.Second*30 && (sinceMod <= time.Second*5) {
|
||||
bus.RunEvent(events.MotionSensed{ID: id, SecondsSince: since.Seconds()})
|
||||
}
|
||||
}
|
||||
|
||||
b.triggerCongruenceCheck()
|
||||
case <-step.C:
|
||||
hwEvents, err := b.RefreshAll()
|
||||
if err != nil {
|
||||
@@ -307,17 +425,14 @@ func (b *Bridge) makeCongruentLoop(ctx context.Context) {
|
||||
|
||||
updates := make(map[string]ResourceUpdate)
|
||||
for id, desired := range desiredStates {
|
||||
|
||||
active, activeOK := activeStates[id]
|
||||
lightID := resources[id].ServiceID("light")
|
||||
if !reachable[id] || !activeOK || lightID == nil {
|
||||
log.Println("No light", !reachable[id], !activeOK, lightID == nil)
|
||||
continue
|
||||
}
|
||||
|
||||
light := resources[*lightID]
|
||||
if light == nil {
|
||||
log.Println("No light", *lightID)
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -433,6 +548,15 @@ func (b *Bridge) makeCongruentLoop(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bridge) fullId(res ResourceData) string {
|
||||
id := res.ID
|
||||
if res.Owner != nil && res.Owner.Kind == "device" {
|
||||
id = res.Owner.ID
|
||||
}
|
||||
|
||||
return fmt.Sprintf("hue:%s:%s", b.host, id)
|
||||
}
|
||||
|
||||
func (b *Bridge) triggerCongruenceCheck() {
|
||||
select {
|
||||
case b.triggerCongruenceCheckCh <- struct{}{}:
|
||||
|
||||
Reference in New Issue
Block a user