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.thingId = thingId;
this.bytes = bytes; this.bytes = bytes;
} }
public CustomMsg(byte networkId, Thing thing) : base() {
this.networkId = networkId;
this.thingId = thing.id;
}
public override byte Serialize(ref byte[] buffer) { public override byte Serialize(ref byte[] buffer) {
if (bytes == null)
return 0;
byte ix = 0; byte ix = 0;
buffer[ix++] = CustomMsg.Id; buffer[ix++] = CustomMsg.Id;
buffer[ix++] = this.networkId; buffer[ix++] = this.networkId;

View File

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

View File

@ -21,8 +21,8 @@ namespace Passer.Control.Core {
public Thing Get(byte networkId, byte thingId) { public Thing Get(byte networkId, byte thingId) {
Thing thing = things.Find(aThing => Thing.IsThing(aThing, networkId, thingId)); Thing thing = things.Find(aThing => Thing.IsThing(aThing, networkId, thingId));
if (thing == null) // if (thing == null)
Console.WriteLine($"Could not find thing {ipAddress}:{port}[{networkId}/{thingId}]"); // Console.WriteLine($"Could not find thing {ipAddress}:{port}[{networkId}/{thingId}]");
return thing; return thing;
} }
@ -37,7 +37,7 @@ namespace Passer.Control.Core {
if (invokeEvent) if (invokeEvent)
Thing.InvokeNewThing(thing); 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) { public SiteServer(int port = 7681) {
this.name = "Site Server"; this.name = "Site Server";
this.publishInterval = 0;
this.ipAddress = "0.0.0.0"; this.ipAddress = "0.0.0.0";
this.port = port; this.port = port;
@ -23,6 +22,9 @@ namespace Passer.Control.Core {
new Tuple<UdpClient, IPEndPoint>(this.udpClient, new(IPAddress.Any, port))); new Tuple<UdpClient, IPEndPoint>(this.udpClient, new(IPAddress.Any, port)));
} }
public override void Publish() {
}
protected override void Process(RemoteParticipant sender, ClientMsg msg) { protected override void Process(RemoteParticipant sender, ClientMsg 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 -> {sender.networkId}");
@ -33,14 +35,14 @@ namespace Passer.Control.Core {
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; Thing newThing;
if (thingMsgProcessors.ContainsKey(msg.thingType)) if (thingMsgProcessors.ContainsKey(msg.thingType))
newThing = thingMsgProcessors[msg.thingType](msg.networkId, msg.thingId); newThing = thingMsgProcessors[msg.thingType](msg.networkId, msg.thingId);
else else
newThing = new Thing(this, msg.networkId, msg.thingId, msg.thingType); newThing = new Thing(sender, msg.networkId, msg.thingId, msg.thingType);
sender.Add(newThing); sender.Add(newThing);
} }
} }

View File

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