From b1c324f36c71b7c13bafc722198fe24f52682932 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Tue, 4 Feb 2025 17:25:51 +0100 Subject: [PATCH] Participant things --- Participant.cs | 26 ++++++++++++++++++++++++-- SiteServer.cs | 9 ++------- Thing.cs | 2 +- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/Participant.cs b/Participant.cs index 7168ba7..1d94597 100644 --- a/Participant.cs +++ b/Participant.cs @@ -90,11 +90,32 @@ namespace Passer.Control.Core { } public void Register(byte thingType) where ThingClass : Thing { - thingMsgProcessors[thingType] = (byte networkId, byte thingId) => + thingMsgProcessors[thingType] = (byte networkId, byte thingId) => Activator.CreateInstance(typeof(ThingClass), networkId, thingId) as ThingClass; Console.WriteLine($"Registering {typeof(ThingClass)} for thing type {thingType}"); } + private List things; + public static event Thing.ThingHandler OnNewThing; + + public Thing Get(byte networkId, byte thingId) { + Thing thing = things.Find(aThing => Thing.IsThing(aThing, networkId, thingId)); + return thing; + } + + public void Add(Thing thing, bool invokeEvent = true) { + Console.WriteLine("added thing"); + Thing foundThing = Get(thing.networkId, thing.id); + + if (foundThing == null) { + if (thing.id == 0) + thing.id = (byte)(things.Count + 1); + things.Add(thing); + if (invokeEvent) + OnNewThing?.Invoke(thing); + Console.Write($"Add thing [{thing.networkId}/{thing.id}] {thing.name}"); + } + } #endregion Init @@ -272,7 +293,7 @@ namespace Passer.Control.Core { } protected virtual void Process(NameMsg msg) { - //Console.WriteLine($"received name {msg.name}"); + Console.WriteLine($"received name {msg.name}"); Thing thing = Thing.Get(msg.networkId, msg.thingId); if (thing != null) thing.name = msg.name; @@ -285,6 +306,7 @@ namespace Passer.Control.Core { protected virtual void Process(PoseMsg msg) { } protected virtual void Process(CustomMsg msg) { + Console.WriteLine($"receive custom msg"); Thing thing = Thing.Get(msg.networkId, msg.thingId); thing?.ProcessBinary(msg.bytes); } diff --git a/SiteServer.cs b/SiteServer.cs index 77d8fce..11d6f2c 100644 --- a/SiteServer.cs +++ b/SiteServer.cs @@ -35,17 +35,12 @@ namespace Passer.Control.Core { protected override void Process(RemoteParticipant sender, NetworkIdMsg msg) { } protected override void Process(ThingMsg msg) { - Console.Write("SiteServer: Processing thing msg"); Thing thing = Thing.Get(msg.networkId, msg.thingId); if (thing == null) { if (thingMsgProcessors.ContainsKey(msg.thingType)) thingMsgProcessors[msg.thingType](msg.networkId, msg.thingId); - // if (thingMsgProcessors.TryGetValue(msg.thingType, out ThingConstructor thingConstructor)) { - // thingConstructor(msg.networkId, msg.thingId); - // } - else { - new Thing(this, msg.networkId, msg.thingId, msg.thingType); - } + else + new Thing(this, msg.networkId, msg.thingId, msg.thingType); } } diff --git a/Thing.cs b/Thing.cs index d275014..a42f567 100644 --- a/Thing.cs +++ b/Thing.cs @@ -213,7 +213,7 @@ namespace Passer.Control.Core { return thing; } - private static bool IsThing(Thing thing, byte networkId, byte thingId) { + public static bool IsThing(Thing thing, byte networkId, byte thingId) { if (thing == null) return false; return (thing.networkId == networkId) && (thing.id == thingId);