Compare commits
2 Commits
e0c5d86fab
...
052620c0f0
Author | SHA1 | Date | |
---|---|---|---|
052620c0f0 | |||
68b0622a1f |
@ -11,10 +11,6 @@ namespace RoboidControl {
|
||||
/// It also maintains the communcation information to contact the participant.
|
||||
/// It is used as a basis for the local participant, but also as a reference to remote participants.
|
||||
public class Participant {
|
||||
/// <summary>
|
||||
/// Default constructor
|
||||
/// </summary>
|
||||
public Participant() { }
|
||||
/// <summary>
|
||||
/// Create a new participant with the given communcation info
|
||||
/// </summary>
|
||||
@ -35,7 +31,7 @@ namespace RoboidControl {
|
||||
public int port = 0;
|
||||
|
||||
/// <summary>
|
||||
/// The network ID of the participant
|
||||
/// he network Id to identify the participant
|
||||
/// </summary>
|
||||
public byte networkId = 0;
|
||||
|
||||
@ -44,6 +40,48 @@ namespace RoboidControl {
|
||||
/// </summary>
|
||||
public readonly List<Thing> things = new();
|
||||
|
||||
/// <summary>
|
||||
/// Get the thing with the given properties
|
||||
/// </summary>
|
||||
/// <param name="thingId">The ID of the thing</param>
|
||||
/// <returns>The thing when it is found, null in other cases.</returns>
|
||||
public Thing Get(byte thingId) {
|
||||
Thing thing = things.Find(aThing => aThing.id == thingId);
|
||||
//Thing thing = things.Find(aThing => Thing.IsThing(aThing, networkId, thingId));
|
||||
// if (thing == null)
|
||||
// Console.WriteLine($"Could not find thing {ipAddress}:{port}[{networkId}/{thingId}]");
|
||||
return thing;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a new thing for this participant
|
||||
/// </summary>
|
||||
/// <param name="thing">The thing to add</param>
|
||||
/// <param name="checkId">If true, the thing.id is regenerated if it is zero
|
||||
public void Add(Thing thing, bool checkId = true) {
|
||||
if (checkId && thing.id == 0) {
|
||||
thing.id = (byte)(this.things.Count + 1);
|
||||
this.things.Add(thing);
|
||||
}
|
||||
else {
|
||||
Thing foundThing = Get(thing.id);
|
||||
if (foundThing == null)
|
||||
this.things.Add(thing);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove a thing for this participant
|
||||
/// </summary>
|
||||
/// <param name="thing">The thing to remove</param>
|
||||
public void Remove(Thing thing) {
|
||||
this.things.Remove(thing);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update all things for this participant
|
||||
/// </summary>
|
||||
/// <param name="currentTimeMS">The current time in milliseconds (optional)</param>
|
||||
public virtual void Update(ulong currentTimeMS = 0) {
|
||||
int n = this.things.Count;
|
||||
for (int ix = 0; ix < n; ix++) {
|
||||
@ -111,58 +149,6 @@ namespace RoboidControl {
|
||||
Participant.participants.Add(participant);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a thing with the given ids
|
||||
/// </summary>
|
||||
/// <param name="networkId">The network ID of the thing</param>
|
||||
/// <param name="thingId">The ID of the thing</param>
|
||||
/// <returns>The thing when it is found, null in other cases.</returns>
|
||||
public Thing Get(byte networkId, byte thingId) {
|
||||
Thing thing = things.Find(aThing => aThing.id == thingId);
|
||||
//Thing thing = things.Find(aThing => Thing.IsThing(aThing, networkId, thingId));
|
||||
// if (thing == null)
|
||||
// Console.WriteLine($"Could not find thing {ipAddress}:{port}[{networkId}/{thingId}]");
|
||||
return thing;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a new thing for this participant
|
||||
/// </summary>
|
||||
/// <param name="thing">The thing to add</param>
|
||||
/// <param name="invokeEvent">Invoke an notification event when the thing has been added</param>
|
||||
public void Add(Thing thing, bool checkId = true, bool invokeEvent = true) {
|
||||
if (checkId && thing.id == 0) {
|
||||
thing.id = (byte)(this.things.Count + 1);
|
||||
this.things.Add(thing);
|
||||
}
|
||||
// Console.WriteLine($"added thing [{thing.networkId}/{thing.id}]");
|
||||
Thing foundThing = Get(thing.networkId, thing.id);
|
||||
|
||||
if (foundThing == null) {
|
||||
this.things.Add(thing);
|
||||
|
||||
// if (invokeEvent)
|
||||
// Thing.InvokeNewThing(thing);
|
||||
// Console.Write($"Add thing {ipAddress}:{port}[{networkId}/{thing.id}]");
|
||||
}
|
||||
// else {
|
||||
// if (thing != foundThing) {
|
||||
// // should be: find first non-existing id...
|
||||
// thing.id = (byte)this.things.Count;
|
||||
// things.Add(thing);
|
||||
// // Console.Write($"Add thing, updated thing id to [{thing.networkId}/{thing.id}]");
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove a thing for this participant
|
||||
/// </summary>
|
||||
/// <param name="thing">The thing to remove</param>
|
||||
public void Remove(Thing thing) {
|
||||
this.things.Remove(thing);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -365,7 +365,7 @@ namespace RoboidControl {
|
||||
Console.WriteLine($"Participant: Process NameMsg [{msg.networkId}/{msg.thingId}] {msg.nameLength} {msg.name}");
|
||||
#endif
|
||||
|
||||
Thing thing = sender.Get(msg.networkId, msg.thingId);
|
||||
Thing thing = sender.Get(msg.thingId);
|
||||
if (thing != null)
|
||||
thing.name = msg.name;
|
||||
}
|
||||
@ -375,7 +375,7 @@ namespace RoboidControl {
|
||||
Console.WriteLine($"Participant: Process ModelUrlMsg [{msg.networkId}/{msg.thingId}] {msg.urlLength} {msg.url}");
|
||||
#endif
|
||||
|
||||
Thing thing = sender.Get(msg.networkId, msg.thingId);
|
||||
Thing thing = sender.Get(msg.thingId);
|
||||
if (thing != null)
|
||||
thing.modelUrl = msg.url;
|
||||
}
|
||||
@ -384,7 +384,7 @@ namespace RoboidControl {
|
||||
#if DEBUG
|
||||
Console.WriteLine($"Participant: Process PoseMsg [{msg.networkId}/{msg.thingId}] {msg.poseType}");
|
||||
#endif
|
||||
Thing thing = sender.Get(msg.networkId, msg.thingId);
|
||||
Thing thing = sender.Get(msg.thingId);
|
||||
if (thing != null) {
|
||||
if ((msg.poseType & PoseMsg.Pose_Position) != 0)
|
||||
thing.position = msg.position;
|
||||
@ -405,7 +405,7 @@ namespace RoboidControl {
|
||||
#if DEBUG
|
||||
Console.WriteLine($"Participant: Process BinaryMsg [{msg.networkId}/{msg.thingId}] {msg.dataLength}");
|
||||
#endif
|
||||
Thing thing = sender.Get(msg.networkId, msg.thingId);
|
||||
Thing thing = sender.Get(msg.thingId);
|
||||
thing?.ProcessBinary(msg.data);
|
||||
}
|
||||
|
||||
@ -420,10 +420,12 @@ namespace RoboidControl {
|
||||
#if DEBUG
|
||||
Console.WriteLine($"Participant: Process Destroy Msg [{msg.networkId}/{msg.thingId}]");
|
||||
#endif
|
||||
Thing thing = sender.Get(msg.networkId, msg.thingId);
|
||||
Thing thing = sender.Get(msg.thingId);
|
||||
if (thing != null)
|
||||
this.Remove(thing);
|
||||
#if UNITY_5_3_OR_NEWER
|
||||
thing.component.core = null;
|
||||
#endif
|
||||
}
|
||||
|
||||
private void ForwardMessage(IMessage msg) {
|
||||
|
@ -105,7 +105,7 @@ namespace RoboidControl {
|
||||
protected override void Process(Participant sender, ThingMsg msg) {
|
||||
Console.WriteLine($"SiteServer: Process thing [{msg.networkId}/{msg.thingId}] {msg.thingType} {msg.parentId} ");
|
||||
|
||||
Thing thing = sender.Get(msg.networkId, msg.thingId);
|
||||
Thing thing = sender.Get(msg.thingId);
|
||||
if (thing == null) {
|
||||
switch (msg.thingType) {
|
||||
case (byte)Thing.Type.TouchSensor:
|
||||
@ -118,7 +118,7 @@ namespace RoboidControl {
|
||||
thing = new Thing(sender, msg.networkId, msg.thingId, msg.thingType);
|
||||
|
||||
if (msg.parentId != 0) {
|
||||
thing.parent = sender.Get(msg.networkId, msg.parentId);
|
||||
thing.parent = sender.Get(msg.parentId);
|
||||
if (thing.parent == null)
|
||||
Console.WriteLine($"Could not find parent [{msg.networkId}/{msg.parentId}]");
|
||||
}
|
||||
|
@ -357,6 +357,8 @@ namespace RoboidControl {
|
||||
public static ulong GetTimeMs() {
|
||||
#if UNITY_5_3_OR_NEWER
|
||||
return (ulong)(UnityEngine.Time.time * 1000);
|
||||
#else
|
||||
return TimeManager.GetCurrentTimeMilliseconds();
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -365,7 +367,7 @@ namespace RoboidControl {
|
||||
/// </summary>
|
||||
/// <param name="recursively">When true, this will Update the descendants recursively</param>
|
||||
public void Update(bool recursively = false) {
|
||||
Update(TimeManager.GetCurrentTimeMilliseconds(), recursively);
|
||||
Update(GetTimeMs(), recursively);
|
||||
}
|
||||
// #endif
|
||||
/// <summary>
|
||||
|
Loading…
x
Reference in New Issue
Block a user