add sensor handling.
This commit is contained in:
@@ -16,6 +16,9 @@ func New(addr string) (lucifer3.Service, error) {
|
||||
svc := &service{}
|
||||
|
||||
e := echo.New()
|
||||
e.HideBanner = true
|
||||
e.HidePort = true
|
||||
|
||||
e.GET("/state", func(c echo.Context) error {
|
||||
svc.mu.Lock()
|
||||
data := svc.data
|
||||
|
||||
+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{}{}:
|
||||
|
||||
@@ -47,6 +47,14 @@ func (d *Data) WithPatch(patches ...Patch) Data {
|
||||
if patch.Device.ClearActiveColorRGB {
|
||||
pd.ActiveColorRGB = nil
|
||||
}
|
||||
if patch.Device.Sensors != nil {
|
||||
if patch.Device.Sensors.LastMotion != nil {
|
||||
pd.Sensors.LastMotion = gentools.ShallowCopy(patch.Device.Sensors.LastMotion)
|
||||
}
|
||||
if patch.Device.Sensors.Temperature != nil {
|
||||
pd.Sensors.Temperature = gentools.ShallowCopy(patch.Device.Sensors.Temperature)
|
||||
}
|
||||
}
|
||||
|
||||
if patch.Device.Delete {
|
||||
delete(newData.Devices, pd.ID)
|
||||
@@ -115,6 +123,12 @@ type Device struct {
|
||||
DesiredState *device.State `json:"desiredState"`
|
||||
Aliases []string `json:"aliases"`
|
||||
Assignment *uuid.UUID `json:"assignment"`
|
||||
Sensors DeviceSensors `json:"sensors"`
|
||||
}
|
||||
|
||||
type DeviceSensors struct {
|
||||
LastMotion *float64 `json:"lastMotion,omitempty"`
|
||||
Temperature *float64 `json:"temperature,omitempty"`
|
||||
}
|
||||
|
||||
type Assignment struct {
|
||||
|
||||
@@ -14,13 +14,30 @@ type Patch struct {
|
||||
Device *DevicePatch `json:"device,omitempty"`
|
||||
}
|
||||
|
||||
func (e Patch) VerboseKey() string {
|
||||
return "uistate.Patch"
|
||||
}
|
||||
|
||||
func (e Patch) EventDescription() string {
|
||||
if e.Device != nil {
|
||||
switch {
|
||||
case e.Device.Name != nil:
|
||||
return fmt.Sprintf("uistate.Patch(device=%s, name=%s)", e.Device.ID, e.Device.Name)
|
||||
case e.Device.DesiredState != nil:
|
||||
return fmt.Sprintf("uistate.Patch(device=%s, desired state)", e.Device.ID)
|
||||
return fmt.Sprintf("uistate.Patch(device=%s, desiredState=%s)", e.Device.ID, e.Device.DesiredState.String())
|
||||
case e.Device.HWState != nil:
|
||||
return fmt.Sprintf("uistate.Patch(device=%s, hwState)", e.Device.ID)
|
||||
case e.Device.HWMetadata != nil:
|
||||
return fmt.Sprintf("uistate.Patch(device=%s, hwMetadata)", e.Device.ID)
|
||||
case e.Device.AddAlias != nil:
|
||||
return fmt.Sprintf("uistate.Patch(device=%s, addAlias=%s)", e.Device.ID, *e.Device.AddAlias)
|
||||
case e.Device.RemoveAlias != nil:
|
||||
return fmt.Sprintf("uistate.Patch(device=%s, removeAlias=%s)", e.Device.ID, *e.Device.RemoveAlias)
|
||||
case e.Device.ActiveColorRGB != nil:
|
||||
col := color.Color{RGB: e.Device.ActiveColorRGB}
|
||||
return fmt.Sprintf("uistate.Patch(device=%s, activeColorRgb=%s)", e.Device.ID, col.String())
|
||||
default:
|
||||
return fmt.Sprintf("uistate.Patch(device=%s)", e.Device.ID)
|
||||
return fmt.Sprintf("uistate.Patch(device=%s, ...other)", e.Device.ID)
|
||||
}
|
||||
} else if e.Assignment != nil {
|
||||
return fmt.Sprintf("uistate.Patch(assignment=%s)", e.Assignment.ID)
|
||||
@@ -40,8 +57,9 @@ type DevicePatch struct {
|
||||
RemoveAlias *string `json:"removeAlias,omitempty"`
|
||||
Assignment *uuid.UUID `json:"assignment,omitempty"`
|
||||
ClearAssignment bool `json:"clearAssignment,omitempty"`
|
||||
ActiveColorRGB *color.RGB `json:"activeColorRgb"`
|
||||
ClearActiveColorRGB bool `json:"clearActiveColorRGB"`
|
||||
ActiveColorRGB *color.RGB `json:"activeColorRgb,omitempty"`
|
||||
ClearActiveColorRGB bool `json:"clearActiveColorRGB,omitempty"`
|
||||
Sensors *DeviceSensors `json:"sensors,omitempty"`
|
||||
Delete bool `json:"delete,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
@@ -80,6 +80,16 @@ func (s *service) HandleEvent(bus *lucifer3.EventBus, event lucifer3.Event) {
|
||||
ID: event.ID,
|
||||
Delete: true,
|
||||
}}}
|
||||
case events.MotionSensed:
|
||||
patches = []Patch{{Device: &DevicePatch{
|
||||
ID: event.ID,
|
||||
Sensors: &DeviceSensors{LastMotion: gentools.Ptr(event.SecondsSince)},
|
||||
}}}
|
||||
case events.TemperatureChanged:
|
||||
patches = []Patch{{Device: &DevicePatch{
|
||||
ID: event.ID,
|
||||
Sensors: &DeviceSensors{Temperature: gentools.Ptr(event.Temperature)},
|
||||
}}}
|
||||
case events.DeviceAssigned:
|
||||
// Un-assign from current assignment (if any)
|
||||
if d, ok := s.data.Devices[event.DeviceID]; ok && d.Assignment != nil {
|
||||
|
||||
Reference in New Issue
Block a user