@@ -2773,6 +2773,70 @@ - (void)_removeAttributes:(NSSet *)toBeRemovedAttributes fromCluster:(MTRCluster
2773
2773
[self .deviceController.controllerDataStore clearStoredClusterDataForNodeID: self .nodeID endpointID: clusterPathToRemoveAttributesFrom.endpoint clusterID: clusterPathToRemoveAttributesFrom.cluster];
2774
2774
}
2775
2775
2776
+ - (void ) _pruneOrphanedEndpoints : (NSDictionary *)previousPartsListValue
2777
+ newPartsListValue : (NSDictionary *)newPartsListValue
2778
+ {
2779
+ // If the parts list changed and one or more endpoints were removed, remove all the clusters in _persistedClusters, _persistedClusterData and _clusterDataToPersist for all those endpoints.
2780
+ // Also remove it from the data store.
2781
+ NSMutableSet * toBeRemovedEndpoints = [NSMutableSet setWithArray: [self arrayOfNumbersFromAttributeValue: [self _dataValueWithoutDataVersion: previousPartsListValue]]];
2782
+ NSSet * endpointsOnDevice = [NSSet setWithArray: [self arrayOfNumbersFromAttributeValue: newPartsListValue]];
2783
+ [toBeRemovedEndpoints minusSet: endpointsOnDevice];
2784
+
2785
+ for (NSNumber * endpoint in toBeRemovedEndpoints) {
2786
+ NSMutableSet <MTRClusterPath *> * clusterPathsToRemove = [[NSMutableSet alloc ] init ];
2787
+ for (MTRClusterPath * path in _persistedClusters) {
2788
+ if ([path.endpoint isEqualToNumber: endpoint]) {
2789
+ [clusterPathsToRemove addObject: path];
2790
+ }
2791
+ }
2792
+ [self _removeClusters: [clusterPathsToRemove copy ]];
2793
+ [self .deviceController.controllerDataStore removeEndpointFromEndpointIndex: endpoint forNodeID: self .nodeID];
2794
+ }
2795
+ }
2796
+
2797
+ - (void ) _pruneOrphanedClusters : (MTRAttributePath *)attributePath
2798
+ previousServerListValue : (NSDictionary *)previousServerListValue
2799
+ newServerListValue : (NSDictionary *)newServerListValue
2800
+ {
2801
+ // If the server list changed and clusters were removed, remove the clusters from the _persistedClusters, _persistedClusterData and _clusterDataToPersist for all those clusters.
2802
+ // Also remove it from the data store.
2803
+ NSMutableSet <NSNumber *> * toBeRemovedClusters = [NSMutableSet setWithArray: [self arrayOfNumbersFromAttributeValue: [self _dataValueWithoutDataVersion: previousServerListValue]]];
2804
+ NSSet <NSNumber *> * clustersStillOnEndpoint = [NSSet setWithArray: [self arrayOfNumbersFromAttributeValue: newServerListValue]];
2805
+ [toBeRemovedClusters minusSet: clustersStillOnEndpoint];
2806
+
2807
+ NSMutableSet <MTRClusterPath *> * clusterPathsToRemove = [[NSMutableSet alloc ] init ];
2808
+ for (NSNumber * cluster in toBeRemovedClusters) {
2809
+ for (MTRClusterPath * path in _persistedClusters) {
2810
+ if ([path.endpoint isEqualToNumber: attributePath.endpoint] && [path.cluster isEqualToNumber: cluster]) {
2811
+ [clusterPathsToRemove addObject: path];
2812
+ }
2813
+ }
2814
+ }
2815
+ [self _removeClusters: [clusterPathsToRemove copy ]];
2816
+ }
2817
+
2818
+ - (void ) _pruneOrphanedAttributes : (MTRAttributePath *)attributePath
2819
+ newAttributeListValue : (NSDictionary *)newAttributeListValue
2820
+ {
2821
+ // If the attribute list changed and attributes were removed, remove the attributes from the _clusterDataToPersist for that cluster and endpoint.
2822
+ // Clear out the _peristedClusterData and data store cluster data. Update the data storage with updated cluster data.
2823
+ NSMutableSet <NSNumber *> * toBeRemovedAttributes = [NSMutableSet setWithArray: [self arrayOfNumbersFromAttributeValue: [self _cachedAttributeValueForPath: attributePath]]];
2824
+ NSSet <NSNumber *> * attributesStillInCluster = [NSSet setWithArray: [self arrayOfNumbersFromAttributeValue: newAttributeListValue]];
2825
+
2826
+ [toBeRemovedAttributes minusSet: attributesStillInCluster];
2827
+ MTRClusterPath * clusterPathToRemoveAttributesFrom;
2828
+ for (MTRClusterPath * path in _persistedClusters) {
2829
+ if ([path.endpoint isEqualToNumber: attributePath.endpoint] && [path.cluster isEqualToNumber: attributePath.cluster]) {
2830
+ clusterPathToRemoveAttributesFrom = path;
2831
+ break ;
2832
+ }
2833
+ }
2834
+ [self _removeAttributes: [toBeRemovedAttributes copy ] fromCluster: clusterPathToRemoveAttributesFrom];
2835
+ [self .deviceController.controllerDataStore removeAttributes: [toBeRemovedAttributes copy ] fromCluster: clusterPathToRemoveAttributesFrom forNodeID: self .nodeID];
2836
+ }
2837
+
2838
+
2839
+
2776
2840
- (void )_pruneOrphanedEndpointsAndClusters : (MTRAttributePath *)attributePath
2777
2841
previousValue : (NSDictionary *)previousValue
2778
2842
attributeDataValue : (NSDictionary *)attributeDataValue
@@ -2788,60 +2852,14 @@ - (void)_pruneOrphanedEndpointsAndClusters:(MTRAttributePath *)attributePath
2788
2852
// If yes, we might need to prune any deleted endpoints, clusters or attributes from the storage and persisted cluster data.
2789
2853
if (attributePath.cluster .unsignedLongValue == MTRClusterIDTypeDescriptorID) {
2790
2854
if (attributePath.attribute .unsignedLongValue == MTRAttributeIDTypeClusterDescriptorAttributePartsListID && [attributePath.endpoint isEqualToNumber: rootEndpoint]) {
2791
-
2792
- // If the parts list changed and one or more endpoints were removed, remove all the clusters in _persistedClusters, _persistedClusterData and _clusterDataToPersist for all those endpoints.
2793
- // Also remove it from the data store.
2794
- NSMutableSet * toBeRemovedEndpoints = [NSMutableSet setWithArray: [self arrayOfNumbersFromAttributeValue: previousValue]];
2795
- NSSet * endpointsOnDevice = [NSSet setWithArray: [self arrayOfNumbersFromAttributeValue: attributeDataValue]];
2796
- [toBeRemovedEndpoints minusSet: endpointsOnDevice];
2797
-
2798
- for (NSNumber * endpoint in toBeRemovedEndpoints) {
2799
- NSMutableSet <MTRClusterPath *> * clusterPathsToRemove = [[NSMutableSet alloc ] init ];
2800
- for (MTRClusterPath * path in _persistedClusters) {
2801
- if ([path.endpoint isEqualToNumber: endpoint]) {
2802
- [clusterPathsToRemove addObject: path];
2803
- }
2804
- }
2805
- [self _removeClusters: [clusterPathsToRemove copy ]];
2806
- [self .deviceController.controllerDataStore removeEndpointFromEndpointIndex: endpoint forNodeID: self .nodeID];
2807
- }
2855
+ [self _pruneOrphanedEndpoints: previousValue newPartsListValue: attributeDataValue];
2808
2856
} else if (attributePath.attribute .unsignedLongValue == MTRAttributeIDTypeClusterDescriptorAttributeServerListID) {
2809
-
2810
- // If the server list changed and clusters were removed, remove the clusters from the _persistedClusters, _persistedClusterData and _clusterDataToPersist for all those clusters.
2811
- // Also remove it from the data store.
2812
- NSMutableSet <NSNumber *> * toBeRemovedClusters = [NSMutableSet setWithArray: [self arrayOfNumbersFromAttributeValue: [self _dataValueWithoutDataVersion: previousValue]]];
2813
- NSSet <NSNumber *> * clustersStillOnEndpoint = [NSSet setWithArray: [self arrayOfNumbersFromAttributeValue: attributeDataValue]];
2814
- [toBeRemovedClusters minusSet: clustersStillOnEndpoint];
2815
-
2816
- NSMutableSet <MTRClusterPath *> * clusterPathsToRemove = [[NSMutableSet alloc ] init ];
2817
- for (NSNumber * cluster in toBeRemovedClusters) {
2818
- for (MTRClusterPath * path in _persistedClusters) {
2819
- if ([path.endpoint isEqualToNumber: attributePath.endpoint] && [path.cluster isEqualToNumber: cluster]) {
2820
- [clusterPathsToRemove addObject: path];
2821
- }
2822
- }
2823
- }
2824
- [self _removeClusters: [clusterPathsToRemove copy ]];
2857
+ [self _pruneOrphanedClusters: attributePath previousServerListValue: previousValue newServerListValue: attributeDataValue];
2825
2858
}
2826
2859
}
2827
2860
2828
2861
if (attributePath.attribute .unsignedLongValue == MTRAttributeIDTypeGlobalAttributeAttributeListID) {
2829
-
2830
- // If the attribute list changed and attributes were removed, remove the attributes from the _clusterDataToPersist for that cluster and endpoint.
2831
- // Clear out the _peristedClusterData and data store cluster data. Update the data storage with updated cluster data.
2832
- NSMutableSet <NSNumber *> * toBeRemovedAttributes = [NSMutableSet setWithArray: [self arrayOfNumbersFromAttributeValue: [self _cachedAttributeValueForPath: attributePath]]];
2833
- NSSet <NSNumber *> * attributesStillInCluster = [NSSet setWithArray: [self arrayOfNumbersFromAttributeValue: attributeDataValue]];
2834
-
2835
- [toBeRemovedAttributes minusSet: attributesStillInCluster];
2836
- MTRClusterPath * clusterPathToRemoveAttributesFrom;
2837
- for (MTRClusterPath * path in _persistedClusters) {
2838
- if ([path.endpoint isEqualToNumber: attributePath.endpoint] && [path.cluster isEqualToNumber: attributePath.cluster]) {
2839
- clusterPathToRemoveAttributesFrom = path;
2840
- break ;
2841
- }
2842
- }
2843
- [self _removeAttributes: [toBeRemovedAttributes copy ] fromCluster: clusterPathToRemoveAttributesFrom];
2844
- [self .deviceController.controllerDataStore removeAttributes: [toBeRemovedAttributes copy ] fromCluster: clusterPathToRemoveAttributesFrom forNodeID: self .nodeID];
2862
+ [self _pruneOrphanedAttributes: attributePath newAttributeListValue: attributeDataValue];
2845
2863
}
2846
2864
}
2847
2865
0 commit comments