touch sensor is sending correctly

This commit is contained in:
Pascal Serrarens 2025-02-25 17:42:54 +01:00
parent 6dfbdc7316
commit 0d115ee65a
5 changed files with 54 additions and 39 deletions

View File

@ -78,14 +78,14 @@ namespace RoboidControl {
} }
public RemoteParticipant AddParticipant(string ipAddress, int port) { public RemoteParticipant AddParticipant(string ipAddress, int port) {
Console.WriteLine($"New Participant {ipAddress}:{port}"); Console.WriteLine($"New Participant {ipAddress}:{port}");
RemoteParticipant participant = new RemoteParticipant(ipAddress, port) { RemoteParticipant participant = new(ipAddress, port) {
networkId = (byte)this.senders.Count networkId = (byte)(this.senders.Count + 1)
}; };
senders.Add(participant); senders.Add(participant);
return participant; return participant;
} }
protected readonly Dictionary<byte, Func<RemoteParticipant, byte, byte, Thing>> thingMsgProcessors = new Dictionary<byte, Func<RemoteParticipant, byte, byte, Thing>>(); protected readonly Dictionary<byte, Func<RemoteParticipant, byte, byte, Thing>> thingMsgProcessors = new();
public delegate Thing ThingConstructor(RemoteParticipant sender, byte networkId, byte thingId); public delegate Thing ThingConstructor(RemoteParticipant sender, byte networkId, byte thingId);
public void Register(byte thingType, ThingConstructor constr) { public void Register(byte thingType, ThingConstructor constr) {
@ -97,8 +97,8 @@ namespace RoboidControl {
} }
public void Register<ThingClass>(byte thingType) where ThingClass : Thing { public void Register<ThingClass>(byte thingType) where ThingClass : Thing {
thingMsgProcessors[thingType] = (RemoteParticipant sender, byte networkId, byte thingId) => thingMsgProcessors[thingType] = (participant, networkId, thingId) =>
Activator.CreateInstance(typeof(ThingClass), sender, networkId, thingId) as ThingClass; Activator.CreateInstance(typeof(ThingClass), participant, networkId, thingId) as ThingClass;
Console.WriteLine($"Registering {typeof(ThingClass)} for thing type {thingType}"); Console.WriteLine($"Registering {typeof(ThingClass)} for thing type {thingType}");
} }
@ -146,9 +146,10 @@ namespace RoboidControl {
Thing thing = this.things[ix]; Thing thing = this.things[ix];
if (thing != null) { if (thing != null) {
thing.Update(currentTimeMS); thing.Update(currentTimeMS);
BinaryMsg binaryMsg = new(this.networkId, thing); BinaryMsg binaryMsg = new(thing.owner.networkId, thing);
foreach (RemoteParticipant sender in this.senders) this.Send(thing.owner, binaryMsg);
this.Send(sender, 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)); this.Send(remoteParticipant, new BinaryMsg(this.networkId, thing));
} }
public bool Send(IMessage msg) { // public bool Send(IMessage msg) {
int bufferSize = msg.Serialize(ref this.buffer); // int bufferSize = msg.Serialize(ref this.buffer);
if (bufferSize <= 0) // if (bufferSize <= 0)
return true; // return true;
// Console.WriteLine($"msg to {endPoint.Address.ToString()} {endPoint.Port}"); // // Console.WriteLine($"msg to {endPoint.Address.ToString()} {endPoint.Port}");
this.udpClient?.Send(this.buffer, bufferSize, this.endPoint); // this.udpClient?.Send(this.buffer, bufferSize, this.endPoint);
return true; // return true;
} // }
public bool Send(RemoteParticipant remoteParticipant, IMessage msg) { public bool Send(RemoteParticipant remoteParticipant, IMessage msg) {
int bufferSize = msg.Serialize(ref this.buffer); int bufferSize = msg.Serialize(ref this.buffer);
@ -185,7 +186,7 @@ namespace RoboidControl {
return true; return true;
IPEndPoint participantEndpoint = new IPEndPoint(IPAddress.Parse(remoteParticipant.ipAddress), remoteParticipant.port); 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); this.udpClient?.Send(this.buffer, bufferSize, participantEndpoint);
return true; return true;
} }

View File

@ -1,9 +1,14 @@
using System;
namespace RoboidControl { namespace RoboidControl {
/// <summary> /// <summary>
/// A sensor which can detect touches /// A sensor which can detect touches
/// </summary> /// </summary>
public class TouchSensor : Thing { public class TouchSensor : Thing {
public Participant thisParticipant;
/// <summary> /// <summary>
/// Value which is true when the sensor is touching something, false otherwise /// Value which is true when the sensor is touching something, false otherwise
/// </summary> /// </summary>
@ -13,21 +18,26 @@ namespace RoboidControl {
get { return _touchedSomething; } get { return _touchedSomething; }
set { set {
_touchedSomething = value; _touchedSomething = value;
//SendBinary(); BinaryMsg msg = new(networkId, this);
foreach (RemoteParticipant remoteParticipant in thisParticipant.senders)
thisParticipant.Send(remoteParticipant, msg);
} }
} }
/// <summary> /// <summary>
/// Create a touch sensor /// Create a touch sensor
/// </summary> /// </summary>
/// <param name="participant">The participant for with the sensor is needed</param> /// <param name="owner">The participant for with the sensor is needed</param>
/// <param name="invokeEvent">True when the creation should trigger an event</param> /// <param name="invokeEvent">True when the creation should trigger an event</param>
public TouchSensor(RemoteParticipant participant, bool invokeEvent = true) : base(participant, invokeEvent) { public TouchSensor(RemoteParticipant owner, bool invokeEvent = true) : base(owner, invokeEvent) {
touchedSomething = false; //touchedSomething = false;
//thisParticipant = owner;
} }
public TouchSensor(RemoteParticipant participant, byte networkId, byte thingId) : base(participant, networkId, thingId) { public TouchSensor(RemoteParticipant owner, byte networkId, byte thingId) : base(owner, networkId, thingId) {
touchedSomething = false; Console.Write("TouchSensor constructor");
//touchedSomething = false;
//thisParticipant = participant;
} }
#if UNITY_5_3_OR_NEWER #if UNITY_5_3_OR_NEWER

View File

@ -42,24 +42,24 @@ namespace RoboidControl {
public override void Publish() { public override void Publish() {
} }
protected override void Process(RemoteParticipant sender, ParticipantMsg msg) { protected override void Process(RemoteParticipant remoteParticipant, ParticipantMsg msg) {
if (msg.networkId == 0) { if (msg.networkId == 0) {
Console.WriteLine($"{this.name} received New Client -> {sender.networkId}"); Console.WriteLine($"{this.name} received New Client -> {remoteParticipant.networkId}");
this.Send(sender, new NetworkIdMsg(sender.networkId)); this.Send(remoteParticipant, new NetworkIdMsg(remoteParticipant.networkId));
} }
} }
protected override void Process(RemoteParticipant sender, NetworkIdMsg msg) { } protected override void Process(RemoteParticipant sender, NetworkIdMsg msg) { }
protected override void Process(RemoteParticipant sender, ThingMsg 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); Thing thing = sender.Get(msg.networkId, msg.thingId);
if (thing == null) { if (thing == null) {
Thing newThing = null; Thing newThing = null;
if (thingMsgProcessors.TryGetValue(msg.thingType, out Func<RemoteParticipant, byte, byte, Thing> value)) { if (thingMsgProcessors.TryGetValue(msg.thingType, out Func<RemoteParticipant, byte, byte, Thing> msgProcessor)) {
// Console.WriteLine("Found thing message processor"); Console.WriteLine("Found thing message processor");
if (value != null) if (msgProcessor != null)
newThing = value(sender, msg.networkId, msg.thingId); newThing = msgProcessor(sender, msg.networkId, msg.thingId);
} }
if (newThing == null) { if (newThing == null) {
newThing = new Thing(sender, msg.networkId, msg.thingId, msg.thingType); newThing = new Thing(sender, msg.networkId, msg.thingId, msg.thingType);
@ -72,7 +72,7 @@ namespace RoboidControl {
else else
newThing.parent = parentThing; newThing.parent = parentThing;
} }
Console.WriteLine("Adding to remote sender");
sender.Add(newThing); sender.Add(newThing);
} }
} }

View File

@ -23,7 +23,7 @@ namespace RoboidControl {
/// <summary> /// <summary>
/// The participant to which this thing belongs /// The participant to which this thing belongs
/// </summary> /// </summary>
public RemoteParticipant participant; public RemoteParticipant owner;
/// <summary> /// <summary>
/// The network ID of this thing. /// The network ID of this thing.
@ -209,7 +209,7 @@ namespace RoboidControl {
/// <param name="participant">The participant for which this thing is created</param> /// <param name="participant">The participant for which this thing is created</param>
/// <param name="invokeEvent">True when a new thing event should be triggered</param> /// <param name="invokeEvent">True when a new thing event should be triggered</param>
public Thing(RemoteParticipant participant, bool invokeEvent = false) { public Thing(RemoteParticipant participant, bool invokeEvent = false) {
this.participant = participant; this.owner = participant;
if (invokeEvent) if (invokeEvent)
InvokeNewThing(this); InvokeNewThing(this);
} }
@ -221,7 +221,7 @@ namespace RoboidControl {
/// <param name="thingId">The ID of the thing</param> /// <param name="thingId">The ID of the thing</param>
/// <param name="thingType">The type of thing</param> /// <param name="thingType">The type of thing</param>
public Thing(RemoteParticipant participant, byte networkId, byte thingId, byte thingType = 0) { public Thing(RemoteParticipant participant, byte networkId, byte thingId, byte thingType = 0) {
this.participant = participant; this.owner = participant;
this.id = thingId; this.id = thingId;
this.type = thingType; this.type = thingType;
this.networkId = networkId; this.networkId = networkId;

View File

@ -8,6 +8,7 @@ namespace RoboidControl.Unity {
/// </summary> /// </summary>
public class TouchSensor : Thing { public class TouchSensor : Thing {
public SiteServer participant;
/// <summary> /// <summary>
/// The core touch sensor /// The core touch sensor
/// </summary> /// </summary>
@ -18,10 +19,10 @@ namespace RoboidControl.Unity {
/// <summary> /// <summary>
/// Start the Unity represention /// Start the Unity represention
/// </summary> /// </summary>
protected virtual void Start() { protected virtual void Start() {
if (core == null) { if (core == null) {
SiteServer siteServer = FindAnyObjectByType<SiteServer>(); participant = FindAnyObjectByType<SiteServer>();
SetCoreThing(new RoboidControl.TouchSensor(siteServer.site)); SetCoreThing(new RoboidControl.TouchSensor(participant.site));
} }
} }
@ -43,6 +44,9 @@ namespace RoboidControl.Unity {
collider.isTrigger = true; collider.isTrigger = true;
component.core = core; component.core = core;
component.participant = FindAnyObjectByType<SiteServer>();
core.thisParticipant = component.participant.site;
if (core.parent != null && core.parent.component != null) if (core.parent != null && core.parent.component != null)
gameObj.transform.SetParent(core.parent.component.transform, false); gameObj.transform.SetParent(core.parent.component.transform, false);
@ -57,7 +61,7 @@ namespace RoboidControl.Unity {
return; return;
if (this.transform.root == other.transform.root) if (this.transform.root == other.transform.root)
return; return;
// Debug.Log($"touched {other.gameObject.name}"); // Debug.Log($"touched {other.gameObject.name}");
this.coreSensor.touchedSomething = true; this.coreSensor.touchedSomething = true;
} }