using System;
using System.Collections.Generic;
namespace Passer.RoboidControl {
///
/// A reference to a participant, possible on a remote location
///
public class RemoteParticipant {
///
/// The internet address of the participant
///
public string ipAddress = "0.0.0.0";
///
/// The UDP port on which the participant can be reached
///
public int port = 0;
///
/// The network ID of the participant
///
public byte networkId;
///
/// Default constructor
///
public RemoteParticipant() {}
///
/// Create a new remote participant
///
/// The ip address of the participant
/// The UDP port of the participant
public RemoteParticipant(string ipAddress, int port) {
this.ipAddress = ipAddress;
this.port = port;
}
///
/// The things reported by this participant
///
protected readonly List things = new List();
///
/// Get a thing with the given ids
///
/// The networkId of the thing
/// The ID of the thing
/// The thing when it is found, null in other cases.
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}]");
return thing;
}
///
/// Add a new thing for this participant
///
/// The thing to add
/// Invoke an notification event when the thing has been added
public void Add(Thing thing, bool invokeEvent = true) {
// Console.WriteLine($"added thing [{thing.networkId}/{thing.id}]");
Thing foundThing = Get(thing.networkId, thing.id);
if (foundThing == null) {
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}]");
}
}
}
}
}