From ecf6fd5e11372664f2186a563e41d93b3ce0aa7c Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Fri, 6 Jun 2025 12:37:04 +0200 Subject: [PATCH 1/2] Non-sites can now process & handle (site) things --- src/Participant.cs | 1 + src/Participants/ParticipantUDP.cs | 35 +++++++++++++++++++++- src/Participants/SiteServer.cs | 48 +++++++++++++++--------------- 3 files changed, 59 insertions(+), 25 deletions(-) diff --git a/src/Participant.cs b/src/Participant.cs index b1071b3..079c70c 100644 --- a/src/Participant.cs +++ b/src/Participant.cs @@ -244,6 +244,7 @@ namespace RoboidControl { /// The added participant public static Participant AddParticipant(string ipAddress, int port, Participant localParticipant = null) { Console.WriteLine($"New Participant {ipAddress}:{port}"); + // This code is only valid for site, because those can distribute networkIds..... Participant participant = new(ipAddress, port, localParticipant) { networkId = (byte)(Participant.participants.Count + 1) }; diff --git a/src/Participants/ParticipantUDP.cs b/src/Participants/ParticipantUDP.cs index 40b81b5..5640756 100644 --- a/src/Participants/ParticipantUDP.cs +++ b/src/Participants/ParticipantUDP.cs @@ -97,7 +97,7 @@ namespace RoboidControl { /// /// Isolated participants do not communicate with other participants public bool isIsolated = false; - + /// /// The remote site when this participant is connected to a site /// @@ -379,6 +379,16 @@ namespace RoboidControl { this.networkId = msg.networkId; foreach (Thing thing in this.things) //Thing.GetAllThings()) sender.SendThingInfo(thing); + + // HACK to get the networkId for sites corrected to 0. + // This is needed because AddParticipant assigns a networkId for non-sites + sender.networkId = 0; + UpdateEvent e = new() { + messageId = NetworkIdMsg.Id, + participant = sender + }; + this.updateQueue.Enqueue(e); + } } @@ -392,6 +402,29 @@ namespace RoboidControl { #if DEBUG Console.WriteLine($"{this.name}: Process ThingMsg [{msg.networkId}/{msg.thingId}] {msg.thingType} {msg.parentId}"); #endif + Thing thing = sender.Get(msg.thingId); + thing ??= ProcessNewThing(sender, msg); + + if (msg.parentId != 0) { + thing.parent = sender.Get(msg.parentId); + if (thing.parent == null) + Console.WriteLine($"Could not find parent [{msg.networkId}/{msg.parentId}]"); + } + else { + // Console.Write($"Dropped {thing.id}"); + thing.parent = null; + } + } + + protected virtual Thing ProcessNewThing(Participant sender, ThingMsg msg) { + Thing newThing = msg.thingType switch { + Thing.Type.TouchSensor => new TouchSensor(sender.root), + Thing.Type.DifferentialDrive => new DifferentialDrive(sender.root), + _ => new Thing(sender.root) + }; + newThing.id = msg.thingId; + newThing.type = msg.thingType; + return newThing; } protected virtual void Process(Participant sender, NameMsg msg) { diff --git a/src/Participants/SiteServer.cs b/src/Participants/SiteServer.cs index 2fc6e2a..11053b3 100644 --- a/src/Participants/SiteServer.cs +++ b/src/Participants/SiteServer.cs @@ -113,33 +113,33 @@ namespace RoboidControl { protected override void Process(Participant sender, NetworkIdMsg msg) { } - protected override void Process(Participant sender, ThingMsg msg) { - Console.WriteLine($"{this.name}: Process thing [{msg.networkId}/{msg.thingId}] {msg.thingType} {msg.parentId} "); + // protected override void Process(Participant sender, ThingMsg msg) { + // Console.WriteLine($"{this.name}: Process thing [{msg.networkId}/{msg.thingId}] {msg.thingType} {msg.parentId} "); - Thing thing = sender.Get(msg.thingId); - thing ??= ProcessNewThing(sender, msg); + // Thing thing = sender.Get(msg.thingId); + // thing ??= ProcessNewThing(sender, msg); - if (msg.parentId != 0) { - thing.parent = sender.Get(msg.parentId); - if (thing.parent == null) - Console.WriteLine($"Could not find parent [{msg.networkId}/{msg.parentId}]"); - } - else { - // Console.Write($"Dropped {thing.id}"); - thing.parent = null; - } - } + // if (msg.parentId != 0) { + // thing.parent = sender.Get(msg.parentId); + // if (thing.parent == null) + // Console.WriteLine($"Could not find parent [{msg.networkId}/{msg.parentId}]"); + // } + // else { + // // Console.Write($"Dropped {thing.id}"); + // thing.parent = null; + // } + // } - protected virtual Thing ProcessNewThing(Participant sender, ThingMsg msg) { - Thing newThing = msg.thingType switch { - Thing.Type.TouchSensor => new TouchSensor(sender.root), - Thing.Type.DifferentialDrive => new DifferentialDrive(sender.root), - _ => new Thing(sender.root) - }; - newThing.id = msg.thingId; - newThing.type = msg.thingType; - return newThing; - } + // protected virtual Thing ProcessNewThing(Participant sender, ThingMsg msg) { + // Thing newThing = msg.thingType switch { + // Thing.Type.TouchSensor => new TouchSensor(sender.root), + // Thing.Type.DifferentialDrive => new DifferentialDrive(sender.root), + // _ => new Thing(sender.root) + // }; + // newThing.id = msg.thingId; + // newThing.type = msg.thingType; + // return newThing; + // } #endregion Receive From 6dab2b70c98970cc9987342564466f6bb3932e48 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Fri, 6 Jun 2025 12:42:39 +0200 Subject: [PATCH 2/2] Correct typo --- Unity/Thing.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Unity/Thing.cs b/Unity/Thing.cs index ebacc63..c1597ab 100644 --- a/Unity/Thing.cs +++ b/Unity/Thing.cs @@ -128,7 +128,7 @@ namespace RoboidControl.Unity { this.gameObject.name = core.name; break; case ModelUrlMsg.Id: - Debug.Log("{this.id} Handle Model URL"); + Debug.Log($"{this.core.id} Handle Model URL"); string extension = core.modelUrl[core.modelUrl.LastIndexOf(".")..]; if (extension == ".jpg" || extension == ".png") StartCoroutine(LoadJPG());