moar stuff.

This commit is contained in:
2023-09-23 14:04:13 +02:00
parent c7d3a3200e
commit 2188d489ea
48 changed files with 1684 additions and 91 deletions
+34 -8
View File
@@ -18,6 +18,7 @@ import (
func NewBridge(host string, client *Client) *Bridge {
return &Bridge{
mu: sync.Mutex{},
client: client,
host: host,
ctx: context.Background(),
@@ -26,6 +27,7 @@ func NewBridge(host string, client *Client) *Bridge {
activeStates: map[string]device.State{},
desiredStates: map[string]device.State{},
colorFlags: map[string]device.ColorFlags{},
reachable: nil,
hasSeen: map[string]bool{},
triggerCongruenceCheckCh: make(chan struct{}, 2),
}
@@ -46,6 +48,7 @@ type Bridge struct {
reachable map[string]bool
hasSeen map[string]bool
lastMotion map[string]time.Time
lastPresence map[string]bool
lastButton map[string]time.Time
triggerCongruenceCheckCh chan struct{}
@@ -115,12 +118,14 @@ func (b *Bridge) RefreshAll() ([]lucifer3.Event, error) {
hasSeen := b.hasSeen
reachable := b.reachable
lastMotion := b.lastMotion
lastPresence := b.lastPresence
b.mu.Unlock()
oldHasSeen := hasSeen
hasSeen = gentools.CopyMap(hasSeen)
reachable = gentools.CopyMap(reachable)
lastMotion = gentools.CopyMap(lastMotion)
lastPresence = gentools.CopyMap(lastPresence)
colorFlags := make(map[string]device.ColorFlags)
activeStates := make(map[string]device.State)
@@ -144,13 +149,17 @@ func (b *Bridge) RefreshAll() ([]lucifer3.Event, error) {
})
lastMotion[b.fullId(*res)] = time.Now()
lastPresence[b.fullId(*res)] = true
} else {
extraEvents = append(extraEvents, events.MotionSensed{
ID: b.fullId(*res),
SecondsSince: 301,
})
if lastMotion[b.fullId(*res)].IsZero() {
extraEvents = append(extraEvents, events.MotionSensed{
ID: b.fullId(*res),
SecondsSince: 301,
})
lastMotion[b.fullId(*res)] = time.Now().Add(-time.Millisecond * 301)
lastMotion[b.fullId(*res)] = time.Now().Add(-time.Millisecond * 301)
}
lastPresence[b.fullId(*res)] = false
}
}
}
@@ -180,6 +189,7 @@ func (b *Bridge) RefreshAll() ([]lucifer3.Event, error) {
b.activeStates = activeStates
b.reachable = reachable
b.lastMotion = lastMotion
b.lastPresence = lastPresence
b.mu.Unlock()
return append(newEvents, extraEvents...), nil
@@ -193,6 +203,7 @@ func (b *Bridge) ApplyPatches(date time.Time, resources []ResourceData) (eventLi
colorFlags := b.colorFlags
lastMotion := b.lastMotion
lastButton := b.lastButton
lastPresence := b.lastPresence
b.mu.Unlock()
mapCopy := gentools.CopyMap(resourceMap)
@@ -201,6 +212,7 @@ func (b *Bridge) ApplyPatches(date time.Time, resources []ResourceData) (eventLi
colorFlagsCopy := gentools.CopyMap(colorFlags)
lastMotionCopy := gentools.CopyMap(lastMotion)
lastButtonCopy := gentools.CopyMap(lastButton)
lastPresenceCopy := gentools.CopyMap(lastPresence)
for _, resource := range resources {
if mapCopy[resource.ID] != nil {
@@ -218,6 +230,8 @@ func (b *Bridge) ApplyPatches(date time.Time, resources []ResourceData) (eventLi
}
for _, resource := range resources {
fullID := b.fullId(resource)
if resource.Owner != nil && resource.Owner.Kind == "device" {
if parent, ok := mapCopy[resource.Owner.ID]; ok && !strings.HasPrefix(parent.Metadata.Archetype, "bridge") {
hwState, _ := parent.GenerateEvent(b.host, mapCopy)
@@ -231,18 +245,24 @@ func (b *Bridge) ApplyPatches(date time.Time, resources []ResourceData) (eventLi
if resource.Temperature != nil {
eventList = append(eventList, events.TemperatureChanged{
ID: b.fullId(resource),
ID: fullID,
Temperature: resource.Temperature.Temperature,
})
}
if resource.Motion != nil {
lastMotionCopy[fullID] = time.Now()
if resource.Motion.Motion {
eventList = append(eventList, events.MotionSensed{
ID: b.fullId(resource),
ID: fullID,
SecondsSince: 0,
})
lastMotionCopy[b.fullId(resource)] = time.Now()
lastMotion[fullID] = time.Now()
lastPresenceCopy[fullID] = true
} else if lastPresenceCopy[fullID] {
lastMotion[fullID] = time.Now()
lastPresenceCopy[fullID] = false
}
}
if resource.Button != nil {
@@ -285,6 +305,7 @@ func (b *Bridge) ApplyPatches(date time.Time, resources []ResourceData) (eventLi
b.colorFlags = colorFlagsCopy
b.lastMotion = lastMotionCopy
b.lastButton = lastButtonCopy
b.lastPresence = lastPresenceCopy
b.mu.Unlock()
return
@@ -382,9 +403,14 @@ func (b *Bridge) Run(ctx context.Context, bus *lucifer3.EventBus) interface{} {
case <-quickStep.C:
b.mu.Lock()
lastMotion := b.lastMotion
lastPresence := b.lastPresence
b.mu.Unlock()
for id, value := range lastMotion {
if lastPresence[id] {
continue
}
since := time.Since(value)
sinceMod := since % (time.Second * 30)
if (since > time.Second*20) && (sinceMod >= time.Second*27 || sinceMod <= time.Second*3) {