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) {
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<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 void Register(byte thingType, ThingConstructor constr) {
@ -97,8 +97,8 @@ namespace RoboidControl {
}
public void Register<ThingClass>(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;
}

View File

@ -1,9 +1,14 @@
using System;
namespace RoboidControl {
/// <summary>
/// A sensor which can detect touches
/// </summary>
public class TouchSensor : Thing {
public Participant thisParticipant;
/// <summary>
/// Value which is true when the sensor is touching something, false otherwise
/// </summary>
@ -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);
}
}
/// <summary>
/// Create a touch sensor
/// </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>
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

View File

@ -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<RemoteParticipant, byte, byte, Thing> value)) {
// Console.WriteLine("Found thing message processor");
if (value != null)
newThing = value(sender, msg.networkId, msg.thingId);
if (thingMsgProcessors.TryGetValue(msg.thingType, out Func<RemoteParticipant, byte, byte, Thing> 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);
}
}

View File

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

View File

@ -8,6 +8,7 @@ namespace RoboidControl.Unity {
/// </summary>
public class TouchSensor : Thing {
public SiteServer participant;
/// <summary>
/// The core touch sensor
/// </summary>
@ -20,8 +21,8 @@ namespace RoboidControl.Unity {
/// </summary>
protected virtual void Start() {
if (core == null) {
SiteServer siteServer = FindAnyObjectByType<SiteServer>();
SetCoreThing(new RoboidControl.TouchSensor(siteServer.site));
participant = FindAnyObjectByType<SiteServer>();
SetCoreThing(new RoboidControl.TouchSensor(participant.site));
}
}
@ -43,6 +44,9 @@ namespace RoboidControl.Unity {
collider.isTrigger = true;
component.core = core;
component.participant = FindAnyObjectByType<SiteServer>();
core.thisParticipant = component.participant.site;
if (core.parent != null && core.parent.component != null)
gameObj.transform.SetParent(core.parent.component.transform, false);