Acc is publishing right values

This commit is contained in:
Pascal Serrarens 2025-02-05 11:37:22 +01:00
parent ac2785a440
commit 40d542fdc7
5 changed files with 37 additions and 13 deletions

View File

@ -20,8 +20,14 @@ namespace Passer.Control.Core {
this.thingId = thingId;
this.bytes = bytes;
}
public CustomMsg(byte networkId, Thing thing) : base() {
this.networkId = networkId;
this.thingId = thing.id;
}
public override byte Serialize(ref byte[] buffer) {
if (bytes == null)
return 0;
byte ix = 0;
buffer[ix++] = CustomMsg.Id;
buffer[ix++] = this.networkId;

View File

@ -109,6 +109,8 @@ namespace Passer.Control.Core {
byte[] data = udpClient.EndReceive(result, ref this.endPoint);
// This does not yet take multi-packet messages into account!
// We can receive our own publish (broadcast) packages. How do we recognize them????
// It is hard to determine our source port
RemoteParticipant remoteParticipant = this.GetParticipant(endPoint.Address.ToString(), endPoint.Port);
if (remoteParticipant == null)
remoteParticipant = this.AddParticipant(endPoint.Address.ToString(), endPoint.Port);
@ -118,7 +120,7 @@ namespace Passer.Control.Core {
udpClient.BeginReceive(new AsyncCallback(result => ReceiveUDP(result)), null);
}
private ulong nextPublishMe = 0;
protected ulong nextPublishMe = 0;
public virtual void Update(ulong currentTimeMS = 0) {
if (currentTimeMS == 0) {
@ -128,7 +130,7 @@ namespace Passer.Control.Core {
}
if (this.publishInterval > 0 && currentTimeMS > this.nextPublishMe) {
this.Publish(new ClientMsg(this.networkId));
Publish();
// Console.WriteLine($"{this.name} Publish ClientMsg {this.networkId}");
this.nextPublishMe = currentTimeMS + this.publishInterval;
}
@ -137,7 +139,10 @@ namespace Passer.Control.Core {
if (thing != null && thing.parent == null) // update only root things
thing.Update(currentTimeMS);
}
}
public virtual void Publish() {
this.Publish(new ClientMsg(this.networkId));
}
#endregion Update
@ -149,6 +154,7 @@ namespace Passer.Control.Core {
this.Send(remoteParticipant, new ThingMsg(this.networkId, thing));
this.Send(remoteParticipant, new NameMsg(this.networkId, thing));
this.Send(remoteParticipant, new ModelUrlMsg(this.networkId, thing));
this.Send(remoteParticipant, new CustomMsg(this.networkId, thing));
}
public bool Send(IMessage msg) {
@ -172,6 +178,14 @@ namespace Passer.Control.Core {
return true;
}
public void PublishThingInfo(Thing thing) {
//Console.WriteLine("Publish thing info");
this.Publish(new ThingMsg(this.networkId, thing));
this.Publish(new NameMsg(this.networkId, thing));
this.Publish(new ModelUrlMsg(this.networkId, thing));
this.Publish(new CustomMsg(this.networkId, thing));
}
public bool Publish(IMessage msg) {
int bufferSize = msg.Serialize(ref this.buffer);
if (bufferSize <= 0)
@ -269,7 +283,7 @@ namespace Passer.Control.Core {
}
protected virtual void Process(RemoteParticipant sender, NameMsg msg) {
Console.WriteLine($"Participant: Process name [{msg.networkId}/{msg.thingId}] {msg.name}");
// 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;
@ -282,7 +296,7 @@ namespace Passer.Control.Core {
protected virtual void Process(PoseMsg msg) { }
protected virtual void Process(RemoteParticipant sender, CustomMsg msg) {
Console.WriteLine($"Participant: Process binary [{msg.networkId}/{msg.thingId}]");
// Console.WriteLine($"Participant: Process binary [{msg.networkId}/{msg.thingId}]");
Thing thing = sender.Get(msg.networkId, msg.thingId);
thing?.ProcessBinary(msg.bytes);
}

View File

@ -21,8 +21,8 @@ namespace Passer.Control.Core {
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}]");
// if (thing == null)
// Console.WriteLine($"Could not find thing {ipAddress}:{port}[{networkId}/{thingId}]");
return thing;
}
@ -37,7 +37,7 @@ namespace Passer.Control.Core {
if (invokeEvent)
Thing.InvokeNewThing(thing);
Console.Write($"Add thing [{thing.networkId}/{thing.id}] {thing.name}");
Console.Write($"Add thing {ipAddress}:{port}[{networkId}/{thing.id}]");
}
}

View File

@ -9,7 +9,6 @@ namespace Passer.Control.Core {
public SiteServer(int port = 7681) {
this.name = "Site Server";
this.publishInterval = 0;
this.ipAddress = "0.0.0.0";
this.port = port;
@ -23,6 +22,9 @@ namespace Passer.Control.Core {
new Tuple<UdpClient, IPEndPoint>(this.udpClient, new(IPAddress.Any, port)));
}
public override void Publish() {
}
protected override void Process(RemoteParticipant sender, ClientMsg msg) {
if (msg.networkId == 0) {
Console.WriteLine($"{this.name} received New Client -> {sender.networkId}");
@ -33,14 +35,14 @@ namespace Passer.Control.Core {
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;
if (thingMsgProcessors.ContainsKey(msg.thingType))
newThing = thingMsgProcessors[msg.thingType](msg.networkId, msg.thingId);
else
newThing = new Thing(this, msg.networkId, msg.thingId, msg.thingType);
newThing = new Thing(sender, msg.networkId, msg.thingId, msg.thingType);
sender.Add(newThing);
}
}

View File

@ -32,7 +32,7 @@ namespace Passer.Control.Core {
#region Properties
public Participant participant;
public RemoteParticipant participant;
public delegate void ChangeHandler();
public delegate void SphericalHandler(Spherical v);
@ -138,13 +138,15 @@ namespace Passer.Control.Core {
//this.Init();
//Thing.Add(this);
}
//OnNewThing?.Invoke(this);
}
public Thing(Participant client, byte networkId, byte thingId, byte thingType = 0) {
this.participant = client;
public Thing(RemoteParticipant sender, byte networkId, byte thingId, byte thingType = 0) {
this.participant = sender;
this.id = thingId;
this.type = thingType;
this.networkId = networkId;
this.Init();
//OnNewThing?.Invoke(this);
//Thing.Add(this);
}