Skip to content

Commit

Permalink
Update HeartbeatManager
Browse files Browse the repository at this point in the history
Instead of checking for the proper feature entity and feature, use a setter
  • Loading branch information
DerAndereAndi committed Jan 8, 2024
1 parent f9b4fa6 commit a063168
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 21 deletions.
1 change: 1 addition & 0 deletions spine/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ type SubscriptionManager interface {
type HeartbeatManager interface {
IsHeartbeatRunning() bool
UpdateHeartbeatOnSubscriptions()
SetLocalFeature(entity *EntityLocalImpl, feature FeatureLocal)
StartHeartbeat() error
StopHeartbeat()
}
Expand Down
6 changes: 6 additions & 0 deletions spine/entity_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ func (r *EntityLocalImpl) GetOrAddFeature(featureType model.FeatureTypeType, rol
}
f.SetDescriptionString(description)
r.features = append(r.features, f)

if role == model.RoleTypeServer && featureType == model.FeatureTypeTypeDeviceDiagnosis {
// Update HeartbeatManagerImpl
r.device.HeartbeatManager().SetLocalFeature(r, f)
}

return f
}

Expand Down
27 changes: 6 additions & 21 deletions spine/heartbeat_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (c *HeartbeatManagerImpl) IsHeartbeatRunning() bool {
// check if there are any heartbeat subscriptions left, otherwise stop creating new ones
// or start creating heartbeats again if needed
func (c *HeartbeatManagerImpl) UpdateHeartbeatOnSubscriptions() {
if err := c.updateLocal(); err != nil {
if c.localEntity == nil {
return
}

Expand All @@ -66,33 +66,18 @@ func (c *HeartbeatManagerImpl) UpdateHeartbeatOnSubscriptions() {
}
}

func (c *HeartbeatManagerImpl) updateLocal() error {
if c.localFeature == nil {
// search through all entities on the device
entities := c.localDevice.Entities()
for _, entity := range entities {
localFeature := entity.FeatureOfTypeAndRole(model.FeatureTypeTypeDeviceDiagnosis, model.RoleTypeServer)
if localFeature != nil {
c.localFeature = localFeature
c.localEntity = entity
}
}

if c.localFeature == nil {
return errors.New("Local feature for heartbeat sender address could not be found")
}
}

return nil
func (c *HeartbeatManagerImpl) SetLocalFeature(entity *EntityLocalImpl, feature FeatureLocal) {
c.localEntity = entity
c.localFeature = feature
}

// Start setting heartbeat data
// Make sure the a required FeatureTypeTypeDeviceDiagnosis with the role server is present
// otherwise this will end with an error
// Note: Remote features need to have a subscription to get notifications
func (c *HeartbeatManagerImpl) StartHeartbeat() error {
if err := c.updateLocal(); err != nil {
return err
if c.localEntity == nil {
return errors.New("unknown entity")
}

timeout, err := c.heartBeatTimeout.GetTimeDuration()
Expand Down

0 comments on commit a063168

Please sign in to comment.