From 0d115ee65a17b3e98023db6f2cb020b748f9af76 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Tue, 25 Feb 2025 17:42:54 +0100 Subject: [PATCH] touch sensor is sending correctly --- Participant.cs | 35 ++++++++++++++++++----------------- Sensors/TouchSensor.cs | 22 ++++++++++++++++------ SiteServer.cs | 18 +++++++++--------- Thing.cs | 6 +++--- Unity/TouchSensor.cs | 12 ++++++++---- 5 files changed, 54 insertions(+), 39 deletions(-) diff --git a/Participant.cs b/Participant.cs index ddad6ed..4defa13 100644 --- a/Participant.cs +++ b/Participant.cs @@ -78,14 +78,14 @@ namespace RoboidControl { } public RemoteParticipant AddParticipant(string ipAddress, int port) { Console.WriteLine($"New Participant {ipAddress}:{port}"); - RemoteParticipant participant = new RemoteParticipant(ipAddress, port) { - networkId = (byte)this.senders.Count + RemoteParticipant participant = new(ipAddress, port) { + networkId = (byte)(this.senders.Count + 1) }; senders.Add(participant); return participant; } - protected readonly Dictionary> thingMsgProcessors = new Dictionary>(); + protected readonly Dictionary> thingMsgProcessors = new(); public delegate Thing ThingConstructor(RemoteParticipant sender, byte networkId, byte thingId); public void Register(byte thingType, ThingConstructor constr) { @@ -97,8 +97,8 @@ namespace RoboidControl { } public void Register(byte thingType) where ThingClass : Thing { - thingMsgProcessors[thingType] = (RemoteParticipant sender, byte networkId, byte thingId) => - Activator.CreateInstance(typeof(ThingClass), sender, networkId, thingId) as ThingClass; + thingMsgProcessors[thingType] = (participant, networkId, thingId) => + Activator.CreateInstance(typeof(ThingClass), participant, networkId, thingId) as ThingClass; Console.WriteLine($"Registering {typeof(ThingClass)} for thing type {thingType}"); } @@ -146,9 +146,10 @@ namespace RoboidControl { Thing thing = this.things[ix]; if (thing != null) { thing.Update(currentTimeMS); - BinaryMsg binaryMsg = new(this.networkId, thing); - foreach (RemoteParticipant sender in this.senders) - this.Send(sender, binaryMsg); + BinaryMsg binaryMsg = new(thing.owner.networkId, thing); + this.Send(thing.owner, binaryMsg); + //foreach (RemoteParticipant sender in this.senders) + // this.Send(sender, binaryMsg); } } } @@ -169,15 +170,15 @@ namespace RoboidControl { this.Send(remoteParticipant, new BinaryMsg(this.networkId, thing)); } - public bool Send(IMessage msg) { - int bufferSize = msg.Serialize(ref this.buffer); - if (bufferSize <= 0) - return true; + // public bool Send(IMessage msg) { + // int bufferSize = msg.Serialize(ref this.buffer); + // if (bufferSize <= 0) + // return true; - // Console.WriteLine($"msg to {endPoint.Address.ToString()} {endPoint.Port}"); - this.udpClient?.Send(this.buffer, bufferSize, this.endPoint); - return true; - } + // // Console.WriteLine($"msg to {endPoint.Address.ToString()} {endPoint.Port}"); + // this.udpClient?.Send(this.buffer, bufferSize, this.endPoint); + // return true; + // } public bool Send(RemoteParticipant remoteParticipant, IMessage msg) { int bufferSize = msg.Serialize(ref this.buffer); @@ -185,7 +186,7 @@ namespace RoboidControl { return true; IPEndPoint participantEndpoint = new IPEndPoint(IPAddress.Parse(remoteParticipant.ipAddress), remoteParticipant.port); - Console.WriteLine($"msg to {participantEndpoint.Address.ToString()} {participantEndpoint.Port}"); + // Console.WriteLine($"msg to {participantEndpoint.Address.ToString()} {participantEndpoint.Port}"); this.udpClient?.Send(this.buffer, bufferSize, participantEndpoint); return true; } diff --git a/Sensors/TouchSensor.cs b/Sensors/TouchSensor.cs index a55ad57..ebdc8de 100644 --- a/Sensors/TouchSensor.cs +++ b/Sensors/TouchSensor.cs @@ -1,9 +1,14 @@ +using System; + namespace RoboidControl { /// /// A sensor which can detect touches /// public class TouchSensor : Thing { + + public Participant thisParticipant; + /// /// Value which is true when the sensor is touching something, false otherwise /// @@ -13,21 +18,26 @@ namespace RoboidControl { get { return _touchedSomething; } set { _touchedSomething = value; - //SendBinary(); + BinaryMsg msg = new(networkId, this); + foreach (RemoteParticipant remoteParticipant in thisParticipant.senders) + thisParticipant.Send(remoteParticipant, msg); } } /// /// Create a touch sensor /// - /// The participant for with the sensor is needed + /// The participant for with the sensor is needed /// True when the creation should trigger an event - public TouchSensor(RemoteParticipant participant, bool invokeEvent = true) : base(participant, invokeEvent) { - touchedSomething = false; + public TouchSensor(RemoteParticipant owner, bool invokeEvent = true) : base(owner, invokeEvent) { + //touchedSomething = false; + //thisParticipant = owner; } - public TouchSensor(RemoteParticipant participant, byte networkId, byte thingId) : base(participant, networkId, thingId) { - touchedSomething = false; + public TouchSensor(RemoteParticipant owner, byte networkId, byte thingId) : base(owner, networkId, thingId) { + Console.Write("TouchSensor constructor"); + //touchedSomething = false; + //thisParticipant = participant; } #if UNITY_5_3_OR_NEWER diff --git a/SiteServer.cs b/SiteServer.cs index 178dceb..63ca304 100644 --- a/SiteServer.cs +++ b/SiteServer.cs @@ -42,24 +42,24 @@ namespace RoboidControl { public override void Publish() { } - protected override void Process(RemoteParticipant sender, ParticipantMsg msg) { + protected override void Process(RemoteParticipant remoteParticipant, ParticipantMsg msg) { if (msg.networkId == 0) { - Console.WriteLine($"{this.name} received New Client -> {sender.networkId}"); - this.Send(sender, new NetworkIdMsg(sender.networkId)); + Console.WriteLine($"{this.name} received New Client -> {remoteParticipant.networkId}"); + this.Send(remoteParticipant, new NetworkIdMsg(remoteParticipant.networkId)); } } protected override void Process(RemoteParticipant sender, NetworkIdMsg msg) { } protected override void Process(RemoteParticipant sender, ThingMsg msg) { - // Console.WriteLine($"SiteServer: Process thing [{msg.networkId}/{msg.thingId}]"); + Console.WriteLine($"SiteServer: Process thing [{msg.networkId}/{msg.thingId}]"); Thing thing = sender.Get(msg.networkId, msg.thingId); if (thing == null) { Thing newThing = null; - if (thingMsgProcessors.TryGetValue(msg.thingType, out Func value)) { - // Console.WriteLine("Found thing message processor"); - if (value != null) - newThing = value(sender, msg.networkId, msg.thingId); + if (thingMsgProcessors.TryGetValue(msg.thingType, out Func msgProcessor)) { + Console.WriteLine("Found thing message processor"); + if (msgProcessor != null) + newThing = msgProcessor(sender, msg.networkId, msg.thingId); } if (newThing == null) { newThing = new Thing(sender, msg.networkId, msg.thingId, msg.thingType); @@ -72,7 +72,7 @@ namespace RoboidControl { else newThing.parent = parentThing; } - + Console.WriteLine("Adding to remote sender"); sender.Add(newThing); } } diff --git a/Thing.cs b/Thing.cs index 29fa7c4..1643745 100644 --- a/Thing.cs +++ b/Thing.cs @@ -23,7 +23,7 @@ namespace RoboidControl { /// /// The participant to which this thing belongs /// - public RemoteParticipant participant; + public RemoteParticipant owner; /// /// The network ID of this thing. @@ -209,7 +209,7 @@ namespace RoboidControl { /// The participant for which this thing is created /// True when a new thing event should be triggered public Thing(RemoteParticipant participant, bool invokeEvent = false) { - this.participant = participant; + this.owner = participant; if (invokeEvent) InvokeNewThing(this); } @@ -221,7 +221,7 @@ namespace RoboidControl { /// The ID of the thing /// The type of thing public Thing(RemoteParticipant participant, byte networkId, byte thingId, byte thingType = 0) { - this.participant = participant; + this.owner = participant; this.id = thingId; this.type = thingType; this.networkId = networkId; diff --git a/Unity/TouchSensor.cs b/Unity/TouchSensor.cs index 6e6ce44..847513d 100644 --- a/Unity/TouchSensor.cs +++ b/Unity/TouchSensor.cs @@ -8,6 +8,7 @@ namespace RoboidControl.Unity { /// public class TouchSensor : Thing { + public SiteServer participant; /// /// The core touch sensor /// @@ -18,10 +19,10 @@ namespace RoboidControl.Unity { /// /// Start the Unity represention /// - protected virtual void Start() { + protected virtual void Start() { if (core == null) { - SiteServer siteServer = FindAnyObjectByType(); - SetCoreThing(new RoboidControl.TouchSensor(siteServer.site)); + participant = FindAnyObjectByType(); + SetCoreThing(new RoboidControl.TouchSensor(participant.site)); } } @@ -43,6 +44,9 @@ namespace RoboidControl.Unity { collider.isTrigger = true; component.core = core; + component.participant = FindAnyObjectByType(); + core.thisParticipant = component.participant.site; + if (core.parent != null && core.parent.component != null) gameObj.transform.SetParent(core.parent.component.transform, false); @@ -57,7 +61,7 @@ namespace RoboidControl.Unity { return; if (this.transform.root == other.transform.root) return; - + // Debug.Log($"touched {other.gameObject.name}"); this.coreSensor.touchedSomething = true; }