Skip to content

Commit

Permalink
Refactor Identify updating for both Read/Report and Identify command.
Browse files Browse the repository at this point in the history
  • Loading branch information
pwood committed May 21, 2024
1 parent beb0881 commit bd20188
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions implcaps/zcl/identify/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,34 +117,38 @@ func (i *Implementation) ImplName() string {
func (i *Implementation) update(_ zcl.AttributeID, v zcl.AttributeDataTypeValue) {
if v.DataType == zcl.TypeUnsignedInt16 {
if seconds, ok := v.Value.(uint64); ok {
newEndTime := time.Now()

if seconds > 0 {
newEndTime = newEndTime.Add(time.Duration(seconds) * time.Second)
}
i.updateDuration(time.Duration(seconds) * time.Second)
}
}
}

currentEndTimeInMs, _ := i.s.Int(RemainingDurationKey, int(time.Now().UnixMilli()))
currentEndTime := time.UnixMilli(int64(currentEndTimeInMs))
func (i *Implementation) updateDuration(duration time.Duration) {
newEndTime := time.Now()

diffDuration := newEndTime.Sub(currentEndTime)
if duration > 0 {
newEndTime = newEndTime.Add(duration)
}

if diffDuration >= (250*time.Millisecond) || (currentEndTime != newEndTime && diffDuration < 0) {
i.s.Set(RemainingDurationKey, newEndTime.UnixMilli())
i.s.Set(implcaps.LastChangedKey, time.Now().UnixMilli())
currentEndTimeInMs, _ := i.s.Int(RemainingDurationKey, int(time.Now().UnixMilli()))
currentEndTime := time.UnixMilli(int64(currentEndTimeInMs))

if diffDuration < 0 {
diffDuration = 0
}
diffDuration := newEndTime.Sub(currentEndTime)

i.zi.SendEvent(capabilities.IdentifyUpdate{Device: i.d, State: capabilities.IdentifyState{
Identifying: diffDuration > 0,
Remaining: diffDuration,
}})
}
if diffDuration >= (250*time.Millisecond) || (currentEndTime != newEndTime && diffDuration < 0) {
i.s.Set(RemainingDurationKey, newEndTime.UnixMilli())
i.s.Set(implcaps.LastChangedKey, time.Now().UnixMilli())

i.s.Set(implcaps.LastUpdatedKey, time.Now().UnixMilli())
if diffDuration < 0 {
diffDuration = 0
}

i.zi.SendEvent(capabilities.IdentifyUpdate{Device: i.d, State: capabilities.IdentifyState{
Identifying: diffDuration > 0,
Remaining: diffDuration,
}})
}

i.s.Set(implcaps.LastUpdatedKey, time.Now().UnixMilli())
}

func (i *Implementation) LastUpdateTime(_ context.Context) (time.Time, error) {
Expand Down Expand Up @@ -193,12 +197,7 @@ func (i *Implementation) Identify(ctx context.Context, duration time.Duration) e
return err
}

i.s.Set(RemainingDurationKey, time.Now().Add(duration).UnixMilli())

i.zi.SendEvent(capabilities.IdentifyUpdate{Device: i.d, State: capabilities.IdentifyState{
Identifying: true,
Remaining: time.Duration(identifySeconds) * time.Second,
}})
i.updateDuration(time.Duration(identifyTime) * time.Second)

return nil
}

0 comments on commit bd20188

Please sign in to comment.