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 ?
It should be simple as dev.SetParent(int newRoot) - but it's not...
spud ?
If this is your first visit, be sure to check out the FAQ. You must register before you can post.
IMPORTANT: Your first post will be checked for appropriate content. This may take a bit of time.
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