HomeSeer / Plugin-SDK

Plugin development kit for the 4th major edition of the HomeSeer platform.
https://www.nuget.org/packages/HomeSeer-PluginSDK/
GNU Affero General Public License v3.0
20 stars 4 forks source link

Unable to change feature association and delete prior associated device #271

Closed mcsSolutions closed 2 years ago

mcsSolutions commented 2 years ago

HS4 4.2.8.0

Objective is to reassign a feature from Device 1 to Device 2 and since Device 1 no longer has any features then Device 1 is deleted.

What now happens in 4.2.8.0 is that Device 2 does not hold onto the updated association property unless Device 2 is not deleted in the code sequence following the remapping of associations.

It behaves as if the association is being updated in a different thread and when that thread runs it observes that the feature no longer exists because Device 1 has been deleted. The feature object has been lost so cannot be part of Device 2 associations.

Code sequence to showing the issue

                Dim parentList As New Generic.List(Of Integer)
                parentList.Add(iRef)
                'give parent the child list
                hs.UpdatePropertyByRef(iRef, HomeSeer.PluginSdk.Devices.EProperty.AssociatedDevices, childList)
                hs.UpdatePropertyByRef(iRef, HomeSeer.PluginSdk.Devices.EProperty.Relationship, HomeSeer.PluginSdk.Devices.Identification.ERelationship.Device)
                'clean up old associations
                For Each iChildRef As Integer In childList
                    Dim oldList As Generic.HashSet(Of Integer) = hs.GetPropertyByRef(iChildRef, HomeSeer.PluginSdk.Devices.EProperty.AssociatedDevices)
                    Dim iOldParent As Integer = -1
                    For Each ioldRef As Integer In oldList
                        iOldParent = ioldRef
                        Exit For
                    Next
                    If iRef <> iOldParent Then
                        hs.UpdatePropertyByRef(iChildRef, HomeSeer.PluginSdk.Devices.EProperty.AssociatedDevices, parentList)
                        hs.UpdatePropertyByRef(iChildRef, HomeSeer.PluginSdk.Devices.EProperty.Relationship, HomeSeer.PluginSdk.Devices.Identification.ERelationship.Feature)
                        'remove the reassigned child from the old parent
                        Dim oChildList As Generic.HashSet(Of Integer) = hs.GetPropertyByRef(iOldParent, HomeSeer.PluginSdk.Devices.EProperty.AssociatedDevices)
                        If oChildList.Contains(iChildRef) Then
                            oChildList.Remove(iChildRef)
                        End If

                        'If oChildList.Count = 0 Then
                        'hs.DeleteDevice(iOldParent)  'Feature is lost when parent is deleted
                        'Else
                        hs.UpdatePropertyByRef(iOldParent, HomeSeer.PluginSdk.Devices.EProperty.AssociatedDevices, oChildList)
                        hs.UpdatePropertyByRef(iOldParent, HomeSeer.PluginSdk.Devices.EProperty.Relationship, HomeSeer.PluginSdk.Devices.Identification.ERelationship.Device)
                        'End If
                    End If
                Next
mcsSolutions commented 2 years ago

This issue can be closed. It appears that updating the Associations in the parent device before deleting the parent device will overcome the issue reported.