From bb01f02bb76bdef9b0290c52c0de11c6b45b7a0e Mon Sep 17 00:00:00 2001 From: Andreas Linde Date: Sun, 28 Jan 2024 19:55:56 +0100 Subject: [PATCH] Only send heartbeats if the feature supports it --- spine/heartbeat_manager.go | 14 +++++++++++--- spine/heartbeat_manager_test.go | 14 +++++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/spine/heartbeat_manager.go b/spine/heartbeat_manager.go index 81fa624..1c298fe 100644 --- a/spine/heartbeat_manager.go +++ b/spine/heartbeat_manager.go @@ -52,12 +52,20 @@ func (c *HeartbeatManager) UpdateHeartbeatOnSubscriptions() { return } - featureAddr := c.localEntity.FeatureOfTypeAndRole(model.FeatureTypeTypeDeviceDiagnosis, model.RoleTypeServer) - if featureAddr == nil { + feature := c.localEntity.FeatureOfTypeAndRole(model.FeatureTypeTypeDeviceDiagnosis, model.RoleTypeServer) + if feature == nil { return } - subscriptions := c.subscriptionManager.SubscriptionsOnFeature(*featureAddr.Address()) + // check if the local device diagnosis server feature, supports the heartbeat function + ops, ok := feature.Operations()[model.FunctionTypeDeviceDiagnosisHeartbeatData] + if !ok || !ops.Read() { + return + } + + subscriptions := c.subscriptionManager.SubscriptionsOnFeature(*feature.Address()) + // check if any subscription address supports Heartbeat function + if len(subscriptions) == 0 { // stop creating heartbeats c.StopHeartbeat() diff --git a/spine/heartbeat_manager_test.go b/spine/heartbeat_manager_test.go index 29a3100..d4df151 100644 --- a/spine/heartbeat_manager_test.go +++ b/spine/heartbeat_manager_test.go @@ -47,6 +47,7 @@ func (suite *HeartBeatManagerSuite) Test_HeartbeatSuccess() { suite.localDevice.AddEntity(entity) localFeature := entity.GetOrAddFeature(model.FeatureTypeTypeDeviceDiagnosis, model.RoleTypeServer) + localFeature.AddFunctionType(model.FunctionTypeDeviceDiagnosisHeartbeatData, false, false) entity.AddFeature(localFeature) remoteEntity := NewEntityRemote(suite.remoteDevice, model.EntityTypeTypeEVSE, []model.AddressEntityType{1}) @@ -94,7 +95,18 @@ func (suite *HeartBeatManagerSuite) Test_HeartbeatSuccess() { assert.Nil(suite.T(), data) running := suite.sut.IsHeartbeatRunning() - assert.Equal(suite.T(), true, running) + assert.Equal(suite.T(), false, running) + + suite.localDevice.RemoveEntity(entity) + entity = NewEntityLocal(suite.localDevice, model.EntityTypeTypeCEM, []model.AddressEntityType{1}) + suite.localDevice.AddEntity(entity) + + localFeature = entity.GetOrAddFeature(model.FeatureTypeTypeDeviceDiagnosis, model.RoleTypeServer) + localFeature.AddFunctionType(model.FunctionTypeDeviceDiagnosisHeartbeatData, true, false) + entity.AddFeature(localFeature) + + err = suite.localDevice.ProcessCmd(datagram, suite.remoteDevice) + assert.Nil(suite.T(), err) err = suite.sut.StartHeartbeat() assert.Nil(suite.T(), err)