Announcement

Collapse
No announcement yet.

Moving child to different root

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Moving child to different root

    Anybody have working code example for moving child to different root?

    It should be simple as dev.SetParent(int newRoot) - but it's not...

    spud​​​​​​​ ?

    #2
    dont you have to break the relationship between the root and the child as well. i recall each is related to the other
    Mark

    HS3 Pro 4.2.19.5
    Hardware: Insteon Serial PLM | AD2USB for Vista Alarm | HAI Omnistat2 | 1-Wire HA7E | RFXrec433 | Dahua Cameras | LiftMaster Internet Gateway | Tuya Smart Plugs
    Plugins: Insteon (mine) | Vista Alarm (mine) | Omnistat 3 | Ultra1Wire3 | RFXCOM | HS MyQ | BLRadar | BLDenon | Tuya | Jon00 Charting | Jon00 Links
    Platform: Windows Server 2022 Standard, i5-12600K/3.7GHz/10 core, 16GB RAM, 500GB SSD

    Comment


      #3
      I don't have a working code example, but did you try using AbstractHsDevice.AssociatedDevices or EProperty.AssociatedDevices
      Using this property you should:
      • remove the child ref from the old root
      • add the child ref to the new root
      • change the root ref on the child

      Comment


        #4
        Yeah, that's what I've done, a bit messy

        Code:
        public virtual void SetParent(DeviceBase par)
        {
           // Current root
           CheckIfRoot();
        
           if (rootRef == par.RefId)
              return;
        
           LogWarn($"Changing root device from {rootRef} to {par.RefId}");
        
           if (!par.isRoot)
           {
              LogWarn($"New Parent is not root device");
              return;
           }
        
           // HS AssociatedDevices
           this.Disassociate();
        
           // Add new parent to this association
           this.AddAssocDevice(par.RefId);
        
           // Add this child to new parent association
           par.AddAssocDevice(this.RefId);
        
           // DeviceBase parent/child
           this.Location = par.Location;
           this.Location2 = par.Location2;
        
           root?.RemoveChild(this);
           par.AddChild(this);
        
           // Finally set root
           this.root = par;
        }
        
        
        public List<DeviceBase> AssociatedDevices
        {
           get
           {
              List<DeviceBase> ad = new List<DeviceBase>();
              HashSet<int> refs = AssociatedDeviceRefs;
              foreach (int iref in refs)
              {
                 DeviceBase dev = controller.GetDevice(iref, create: true);
                 ad.Add(dev);
              }
        
              return ad;
           }
        }
        
        
        /// <summary>
        /// Remove AssociatedDevices and this device from other AssociatedDevices
        /// </summary>
        public void Disassociate()
        {
           HashSet<int> assoc = AssociatedDeviceRefs;
           foreach (int dRef in assoc)
           {
              // Remove parent association
              RemoveAssocDevice(dRef);
              // Remove this child from parent association
              controller.GetDevice(dRef)?.RemoveAssocDevice(this.RefId);
           }
        }
        
        
        /// <summary>
        /// RemoveAssocDevice
        /// </summary>
        /// <param name="dRef"></param>
        public void RemoveAssocDevice(int dRef)
        {
           HashSet<int> assoc = AssociatedDeviceRefs;
        
           if (assoc.Contains(dRef))
           {
              Log($"RemoveAssocDevice {dRef}");
              assoc.Remove(dRef);
           }
           AssociatedDeviceRefs = assoc;
        }
        
        
        /// <summary>
        /// AddAssocDevice
        /// </summary>
        /// <param name="dRef"></param>
        public void AddAssocDevice(int dRef)
        {
           HashSet<int> assoc = AssociatedDeviceRefs;
           Log($"AddAssocDevice {dRef}");
           assoc.Add(dRef);
           AssociatedDeviceRefs = assoc;
        }

        Comment

        Working...
        X