From ac2785a4401602e6d039b93dd23f0d923fbc5d48 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Wed, 5 Feb 2025 09:34:54 +0100 Subject: [PATCH] Simulation basics --- Participant.cs | 98 ++++++++++++++---------------------- RemoteParticipant.cs | 28 +++++++++++ Sensors/TemperatureSensor.cs | 4 +- SiteServer.cs | 19 +++---- Thing.cs | 67 ++++++++++++------------ 5 files changed, 112 insertions(+), 104 deletions(-) diff --git a/Participant.cs b/Participant.cs index 1d94597..c9cbaad 100644 --- a/Participant.cs +++ b/Participant.cs @@ -25,7 +25,7 @@ namespace Passer.Control.Core { /// Create a porticiapnt /// public Participant() { - others.Add(this); + senders.Add(this); } /// @@ -62,19 +62,22 @@ namespace Passer.Control.Core { this.port = port; } - public List others = new List(); + public List senders = new(); public RemoteParticipant GetParticipant(string ipAddress, int port) { - foreach (RemoteParticipant c in others) { - if (c.ipAddress == ipAddress && c.port == port) - return c; + //Console.WriteLine($"Get Participant {ipAddress}:{port}"); + foreach (RemoteParticipant sender in senders) { + if (sender.ipAddress == ipAddress && sender.port == port) + return sender; } return null; } public RemoteParticipant AddParticipant(string ipAddress, int port) { - RemoteParticipant participant = new(ipAddress, port); - participant.networkId = (byte)this.others.Count; - //others.Add(); + Console.WriteLine($"New Participant {ipAddress}:{port}"); + RemoteParticipant participant = new(ipAddress, port) { + networkId = (byte)this.senders.Count + }; + senders.Add(participant); return participant; } @@ -95,28 +98,6 @@ namespace Passer.Control.Core { 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 #region Update @@ -139,28 +120,24 @@ namespace Passer.Control.Core { private ulong nextPublishMe = 0; - - public virtual void Update(ulong currentTimeMs = 0) { - if (currentTimeMs == 0) { + public virtual void Update(ulong currentTimeMS = 0) { + if (currentTimeMS == 0) { #if UNITY_5_3_OR_NEWER - currentTimeMs = (ulong)(UnityEngine.Time.time * 1000); + currentTimeMS = (ulong)(UnityEngine.Time.time * 1000); #endif } - if (this.publishInterval > 0 && currentTimeMs > this.nextPublishMe) { + if (this.publishInterval > 0 && currentTimeMS > this.nextPublishMe) { this.Publish(new ClientMsg(this.networkId)); - // Console.WriteLine($"{this.name} Sent ClientMsg {this.networkId}"); - this.nextPublishMe = currentTimeMs + this.publishInterval; + // Console.WriteLine($"{this.name} Publish ClientMsg {this.networkId}"); + this.nextPublishMe = currentTimeMS + this.publishInterval; } - // for (int ix = 0; ix < this.others.Count; ix++) { - // Participant client = this.others[ix]; - // if (client == null) - // continue; + foreach (Thing thing in this.things) { + if (thing != null && thing.parent == null) // update only root things + thing.Update(currentTimeMS); + } - // this.ProcessMessages(client); - // } - Thing.UpdateAll(currentTimeMs); } #endregion Update @@ -229,7 +206,6 @@ namespace Passer.Control.Core { public void ReceiveData(byte[] data, RemoteParticipant remoteParticipant) { byte msgId = data[0]; - Console.Write($"received msg {msgId}"); if (msgId == 0xFF) { // Timeout return; @@ -246,19 +222,19 @@ namespace Passer.Control.Core { // result = await InvestigateMsg.Receive(dataStream, client, packetSize); break; case ThingMsg.id: // 0x80 / 128 - this.Process(new ThingMsg(data)); + this.Process(remoteParticipant, new ThingMsg(data)); break; case NameMsg.Id: // 0x91 / 145 - this.Process(new NameMsg(data)); + this.Process(remoteParticipant, new NameMsg(data)); break; case ModelUrlMsg.Id: // 0x90 / 144 - this.Process(new ModelUrlMsg(data)); + this.Process(remoteParticipant, new ModelUrlMsg(data)); break; case PoseMsg.Id: // 0x10 / 16 // result = await PoseMsg.Receive(dataStream, client, packetSize); break; case CustomMsg.Id: // 0xB1 / 177 - this.Process(new CustomMsg(data)); + this.Process(remoteParticipant, new CustomMsg(data)); break; case TextMsg.Id: // 0xB0 / 176 // result = await TextMsg.Receive(dataStream, client, packetSize); @@ -281,33 +257,33 @@ namespace Passer.Control.Core { Console.WriteLine($"{this.name} receive network id {this.networkId} {msg.networkId}"); if (this.networkId != msg.networkId) { this.networkId = msg.networkId; - foreach (Thing thing in Thing.GetAllThings()) + foreach (Thing thing in this.things) //Thing.GetAllThings()) this.SendThingInfo(sender, thing); } } protected virtual void Process(InvestigateMsg msg) { } - protected virtual void Process(ThingMsg msg) { - Console.WriteLine($"received thing {msg.thingId}"); + protected virtual void Process(RemoteParticipant sender, ThingMsg msg) { + Console.WriteLine($"Participant: Process thing [{msg.networkId}/{msg.thingId}]"); } - protected virtual void Process(NameMsg msg) { - Console.WriteLine($"received name {msg.name}"); - Thing thing = Thing.Get(msg.networkId, msg.thingId); + protected virtual void Process(RemoteParticipant sender, NameMsg msg) { + Console.WriteLine($"Participant: Process name [{msg.networkId}/{msg.thingId}] {msg.name}"); + Thing thing = sender.Get(msg.networkId, msg.thingId); if (thing != null) thing.name = msg.name; } - protected virtual void Process(ModelUrlMsg msg) { - Console.WriteLine($"received name {msg.url}"); + protected virtual void Process(RemoteParticipant sender, ModelUrlMsg msg) { + Console.WriteLine($"Participant: Process model [{msg.networkId}/{msg.thingId}] {msg.url}"); } 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); + protected virtual void Process(RemoteParticipant sender, CustomMsg msg) { + Console.WriteLine($"Participant: Process binary [{msg.networkId}/{msg.thingId}]"); + Thing thing = sender.Get(msg.networkId, msg.thingId); thing?.ProcessBinary(msg.bytes); } @@ -316,7 +292,7 @@ namespace Passer.Control.Core { protected virtual void Process(DestroyMsg msg) { } private void ForwardMessage(IMessage msg) { - foreach (Participant client in others) { + foreach (Participant client in senders) { if (client == this) continue; //UnityEngine.Debug.Log($"---> {client.ipAddress}"); diff --git a/RemoteParticipant.cs b/RemoteParticipant.cs index 3477067..9e1e51e 100644 --- a/RemoteParticipant.cs +++ b/RemoteParticipant.cs @@ -1,3 +1,6 @@ +using System; +using System.Collections.Generic; + namespace Passer.Control.Core { public class RemoteParticipant { @@ -13,6 +16,31 @@ namespace Passer.Control.Core { this.ipAddress = ipAddress; this.port = port; } + + protected readonly List things = new(); + + public Thing Get(byte networkId, byte thingId) { + Thing thing = things.Find(aThing => Thing.IsThing(aThing, networkId, thingId)); + if (thing == null) + Console.WriteLine($"Could not find thing {ipAddress}:{port}[{networkId}/{thingId}]"); + return thing; + } + + public void Add(Thing thing, bool invokeEvent = true) { + //Console.WriteLine($"added thing [{thing.networkId}/{thing.id}]"); + 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) + Thing.InvokeNewThing(thing); + Console.Write($"Add thing [{thing.networkId}/{thing.id}] {thing.name}"); + } + } + } } \ No newline at end of file diff --git a/Sensors/TemperatureSensor.cs b/Sensors/TemperatureSensor.cs index 9c16629..b6e61b5 100644 --- a/Sensors/TemperatureSensor.cs +++ b/Sensors/TemperatureSensor.cs @@ -10,8 +10,8 @@ namespace Passer.Control.Core { public override void ProcessBinary(byte[] bytes) { byte ix = 0; - float temp = LowLevelMessages.ReceiveFloat16(bytes, ref ix); - Console.WriteLine($"temperature {this.name} = {temp} C"); + this.temp = LowLevelMessages.ReceiveFloat16(bytes, ref ix); + Console.WriteLine($"temperature {this.name} = {this.temp} C"); } } diff --git a/SiteServer.cs b/SiteServer.cs index 11d6f2c..1d29844 100644 --- a/SiteServer.cs +++ b/SiteServer.cs @@ -18,11 +18,9 @@ namespace Passer.Control.Core { Console.Write($"Prepare receive on port {port}"); this.udpClient = new UdpClient(port); // for receiving - //this.udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, port)); - IPEndPoint receiveEndpoint = new(IPAddress.Any, port); this.udpClient.BeginReceive( - new AsyncCallback(result => ReceiveUDP(result)), - new Tuple(this.udpClient, receiveEndpoint)); + new AsyncCallback(result => ReceiveUDP(result)), + new Tuple(this.udpClient, new(IPAddress.Any, port))); } protected override void Process(RemoteParticipant sender, ClientMsg msg) { @@ -34,13 +32,16 @@ namespace Passer.Control.Core { protected override void Process(RemoteParticipant sender, NetworkIdMsg msg) { } - protected override void Process(ThingMsg msg) { - Thing thing = Thing.Get(msg.networkId, msg.thingId); + protected override void Process(RemoteParticipant sender, ThingMsg msg) { + Console.WriteLine($"SiteServer: Process thing [{msg.networkId}/{msg.thingId}]"); + Thing thing = sender.Get(msg.networkId, msg.thingId); if (thing == null) { + Thing newThing; if (thingMsgProcessors.ContainsKey(msg.thingType)) - thingMsgProcessors[msg.thingType](msg.networkId, msg.thingId); - else - new Thing(this, msg.networkId, msg.thingId, msg.thingType); + newThing = thingMsgProcessors[msg.thingType](msg.networkId, msg.thingId); + else + newThing = new Thing(this, msg.networkId, msg.thingId, msg.thingType); + sender.Add(newThing); } } diff --git a/Thing.cs b/Thing.cs index a42f567..53fc8bc 100644 --- a/Thing.cs +++ b/Thing.cs @@ -130,13 +130,13 @@ namespace Passer.Control.Core { #region Init public virtual void Init(bool invokeEvent = true) { - Thing.Add(this, invokeEvent); + //Thing.Add(this, invokeEvent); } public Thing(bool initialize = true) { if (initialize) { //this.Init(); - Thing.Add(this); + //Thing.Add(this); } } public Thing(Participant client, byte networkId, byte thingId, byte thingType = 0) { @@ -145,7 +145,7 @@ namespace Passer.Control.Core { this.type = thingType; this.networkId = networkId; this.Init(); - Thing.Add(this); + //Thing.Add(this); } public virtual void CreateComponent() {} @@ -193,25 +193,28 @@ namespace Passer.Control.Core { public delegate void ThingHandler(Thing t); public static event ThingHandler OnNewThing; - - public static 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)(allThings.Count + 1); - allThings.Add(thing); - if (invokeEvent) - OnNewThing?.Invoke(thing); - Console.Write($"Add thing [{thing.networkId}/{thing.id}] {thing.name}"); - } + public static void InvokeNewThing(Thing thing) { + OnNewThing?.Invoke(thing); } - public static Thing Get(byte networkId, byte thingId) { - Thing thing = allThings.Find(aThing => IsThing(aThing, networkId, thingId)); - return thing; - } + // public static 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)(allThings.Count + 1); + // allThings.Add(thing); + // if (invokeEvent) + // OnNewThing?.Invoke(thing); + // Console.Write($"Add thing [{thing.networkId}/{thing.id}] {thing.name}"); + // } + // } + + // public static Thing Get(byte networkId, byte thingId) { + // Thing thing = allThings.Find(aThing => IsThing(aThing, networkId, thingId)); + // return thing; + // } public static bool IsThing(Thing thing, byte networkId, byte thingId) { if (thing == null) @@ -219,20 +222,20 @@ namespace Passer.Control.Core { return (thing.networkId == networkId) && (thing.id == thingId); } - public static void Remove(byte networkId, byte thingId) { - allThings.RemoveAll(t => t.networkId == networkId && t.id == thingId); - } + // public static void Remove(byte networkId, byte thingId) { + // allThings.RemoveAll(t => t.networkId == networkId && t.id == thingId); + // } - public static Thing[] GetAllThings() { - return allThings.ToArray(); - } + // public static Thing[] GetAllThings() { + // return allThings.ToArray(); + // } - public static void UpdateAll(ulong currentTimeMS) { - foreach (Thing thing in allThings) { - if (thing.parent == null) // update only root things - thing.Update(currentTimeMS); - } - } + // public static void UpdateAll(ulong currentTimeMS) { + // foreach (Thing thing in allThings) { + // if (thing.parent == null) // update only root things + // thing.Update(currentTimeMS); + // } + // } } }