Participant things

This commit is contained in:
Pascal Serrarens 2025-02-04 17:25:51 +01:00
parent 425cd8d6f9
commit b1c324f36c
3 changed files with 27 additions and 10 deletions

View File

@ -90,11 +90,32 @@ namespace Passer.Control.Core {
}
public void Register<ThingClass>(byte thingType) where ThingClass : Thing {
thingMsgProcessors[thingType] = (byte networkId, byte thingId) =>
thingMsgProcessors[thingType] = (byte networkId, byte thingId) =>
Activator.CreateInstance(typeof(ThingClass), networkId, thingId) as ThingClass;
Console.WriteLine($"Registering {typeof(ThingClass)} for thing type {thingType}");
}
private List<Thing> things;
public static event Thing.ThingHandler OnNewThing;
public Thing Get(byte networkId, byte thingId) {
Thing thing = things.Find(aThing => Thing.IsThing(aThing, networkId, thingId));
return thing;
}
public void Add(Thing thing, bool invokeEvent = true) {
Console.WriteLine("added thing");
Thing foundThing = Get(thing.networkId, thing.id);
if (foundThing == null) {
if (thing.id == 0)
thing.id = (byte)(things.Count + 1);
things.Add(thing);
if (invokeEvent)
OnNewThing?.Invoke(thing);
Console.Write($"Add thing [{thing.networkId}/{thing.id}] {thing.name}");
}
}
#endregion Init
@ -272,7 +293,7 @@ namespace Passer.Control.Core {
}
protected virtual void Process(NameMsg msg) {
//Console.WriteLine($"received name {msg.name}");
Console.WriteLine($"received name {msg.name}");
Thing thing = Thing.Get(msg.networkId, msg.thingId);
if (thing != null)
thing.name = msg.name;
@ -285,6 +306,7 @@ namespace Passer.Control.Core {
protected virtual void Process(PoseMsg msg) { }
protected virtual void Process(CustomMsg msg) {
Console.WriteLine($"receive custom msg");
Thing thing = Thing.Get(msg.networkId, msg.thingId);
thing?.ProcessBinary(msg.bytes);
}

View File

@ -35,17 +35,12 @@ namespace Passer.Control.Core {
protected override void Process(RemoteParticipant sender, NetworkIdMsg msg) { }
protected override void Process(ThingMsg msg) {
Console.Write("SiteServer: Processing thing msg");
Thing thing = Thing.Get(msg.networkId, msg.thingId);
if (thing == null) {
if (thingMsgProcessors.ContainsKey(msg.thingType))
thingMsgProcessors[msg.thingType](msg.networkId, msg.thingId);
// if (thingMsgProcessors.TryGetValue(msg.thingType, out ThingConstructor thingConstructor)) {
// thingConstructor(msg.networkId, msg.thingId);
// }
else {
new Thing(this, msg.networkId, msg.thingId, msg.thingType);
}
else
new Thing(this, msg.networkId, msg.thingId, msg.thingType);
}
}

View File

@ -213,7 +213,7 @@ namespace Passer.Control.Core {
return thing;
}
private static bool IsThing(Thing thing, byte networkId, byte thingId) {
public static bool IsThing(Thing thing, byte networkId, byte thingId) {
if (thing == null)
return false;
return (thing.networkId == networkId) && (thing.id == thingId);