62 lines
2.4 KiB
Python
62 lines
2.4 KiB
Python
class Participant:
|
|
"""! A participant is a device which manages things.
|
|
|
|
It can communicate with other participant to synchronise the state of things.
|
|
This class is used to register the things the participant is managing.
|
|
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.
|
|
"""
|
|
def __init__(self, ip_address, port):
|
|
"""! Create a new participant with the given communcation info
|
|
"""
|
|
|
|
## The Ip Address of a participant. When the participant is local, this contains 0.0.0.0
|
|
self.ip_addres = ip_address
|
|
## The port number for UDP communication with the participant. This is 0 for isolated participants.
|
|
self.port = port
|
|
## The network Id to identify the participant.
|
|
## @note This field is likely to disappear in future versions
|
|
self.networkId = 0
|
|
|
|
## The things managed by this participant
|
|
self.things = set({ None })
|
|
|
|
def Add(self, thing, check_id):
|
|
"""! Add a new thing for this participant.
|
|
@param thing The thing to add
|
|
@param check_id When true, the thing ID of the thing is checked. If it is 0, a new thing Id will be assigned.
|
|
"""
|
|
if check_id and thing.id == 0:
|
|
thing.id = len(self.things)
|
|
self.things.add(thing)
|
|
else:
|
|
found_thing = self.Get(thing.network_id, thing.id)
|
|
if found_thing == None:
|
|
self.things.add(thing)
|
|
|
|
def Get(self, network_id, thing_id):
|
|
"""! Find a thing managed by this participant
|
|
|
|
@param network_id The network ID for the thing
|
|
@param thing_id The ID of the thing
|
|
@return The thing if found or nullptr when no thing has been found
|
|
@note The use of the network ID is likely to disappear in future versions.
|
|
"""
|
|
for thing in self.things:
|
|
if thing is not None:
|
|
if thing.network_id == network_id and thing.id == thing_id:
|
|
return thing
|
|
return None
|
|
|
|
def Remove(self, thing):
|
|
"""! Remove a thig for this participant
|
|
@param thing The thing to remove
|
|
"""
|
|
self.things.remove(thing)
|
|
|
|
## Update all things
|
|
def Update(self, currentTimeMs):
|
|
for thing in list(self.things):
|
|
if thing is not None:
|
|
thing.update(currentTimeMs)
|