Renamed LocalParticipant, removed Register

This commit is contained in:
Pascal Serrarens 2025-04-06 12:00:42 +02:00
parent a56533ffbc
commit 93b512885e
7 changed files with 35 additions and 35 deletions

View File

@ -3,14 +3,14 @@ from os import path
sys.path.append(path.abspath(path.join(path.dirname(__file__), '..'))) sys.path.append(path.abspath(path.join(path.dirname(__file__), '..')))
import time import time
from LocalParticipant import LocalParticipant from ParticipantUDP import ParticipantUDP
from Thing import * from Thing import *
from Things.DifferentialDrive import DifferentialDrive from Things.DifferentialDrive import DifferentialDrive
from Things.TouchSensor import TouchSensor from Things.TouchSensor import TouchSensor
# Create a local participant for handling communcation # Create a local participant for handling communcation
# using default settings (UDP communciation over port 7681) # using default settings (UDP communciation over port 7681)
participant = LocalParticipant(port=7681, local_port=7682, ip_address="127.0.0.1") participant = ParticipantUDP(port=7681, local_port=7682, ip_address="127.0.0.1")
# The robot's propulsion is a differential drive # The robot's propulsion is a differential drive
bb2b = DifferentialDrive(participant) bb2b = DifferentialDrive(participant)
bb2b.name = "BB2B" bb2b.name = "BB2B"

View File

@ -17,7 +17,7 @@ micropython = 'micropython' in sys.modules
if micropython: if micropython:
from MicroPython.uPythonParticipant import Bla from MicroPython.uPythonParticipant import Bla
class LocalParticipant(Participant): class ParticipantUDP(Participant):
"""! A local participant is the local device which can communicate with other participants. """! A local participant is the local device which can communicate with other participants.
It manages all local things and communcation with other participants. Each application has a local participant which is usually explicit in the code. It manages all local things and communcation with other participants. Each application has a local participant which is usually explicit in the code.
@ -46,19 +46,18 @@ class LocalParticipant(Participant):
# Isolated participants do not communicate with other participants # Isolated participants do not communicate with other participants
self.isolated = True self.isolated = True
## The remote site when this participant is connected to a site ## The other participants communicating with this participant
self.remote_site = None self.others = []
if self.port != 0: if self.port != 0:
self.isolated = False self.isolated = False
if ip_address is not None: if ip_address is not None:
self.remote_site = Participant(ip_address, port) self.others.append(Participant(ip_address, port))
self.others = [] # self.network_id = 0
self.network_id = 0
self.buffer = bytearray(256) self.buffer = bytearray(256)
self.thing_msg_processors = {} # self.thing_msg_processors = {}
self.new_thing_handlers = [] # self.new_thing_handlers = []
self.publishInterval = 3000 # 3 seconds self.publishInterval = 3000 # 3 seconds
self.udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
@ -70,9 +69,9 @@ class LocalParticipant(Participant):
self.thread.start() self.thread.start()
def Isolated(): def Isolated():
if LocalParticipant.isolated_participant == None: if ParticipantUDP.isolated_participant == None:
LocalParticipant.isolated_participant = LocalParticipant(0) ParticipantUDP.isolated_participant = ParticipantUDP(0)
return LocalParticipant.isolated_participant return ParticipantUDP.isolated_participant
#region Update #region Update
@ -100,10 +99,11 @@ class LocalParticipant(Participant):
if self.isolated == False: if self.isolated == False:
if self.publishInterval > 0 and currentTimeMs > self.nextPublishMe: if self.publishInterval > 0 and currentTimeMs > self.nextPublishMe:
msg = ParticipantMsg(self.network_id) msg = ParticipantMsg(self.network_id)
if self.remote_site is None: if self.others.count == 0:
self.Publish(msg) self.Publish(msg)
else: else:
self.Send(self.remote_site, msg) for other_participant in self.others:
self.Send(other_participant, msg)
print(f'Publish ParticipantMsg {self.network_id}') print(f'Publish ParticipantMsg {self.network_id}')
self.nextPublishMe = currentTimeMs + self.publishInterval self.nextPublishMe = currentTimeMs + self.publishInterval
@ -203,15 +203,15 @@ class LocalParticipant(Participant):
def ProcessThingMsg(self, msg: ThingMsg): def ProcessThingMsg(self, msg: ThingMsg):
print(f'received thing {msg.network_id} {msg.thing_id}') print(f'received thing {msg.network_id} {msg.thing_id}')
if msg.thing_type in self.thing_msg_processors: # if msg.thing_type in self.thing_msg_processors:
constructor = self.thing_msg_processors[msg.thing_type] # constructor = self.thing_msg_processors[msg.thing_type]
constructor(msg.network_id, msg.thing_id) # constructor(msg.network_id, msg.thing_id)
# not really 'new' thing, but it is a start # # not really 'new' thing, but it is a start
thing = Thing.Get(msg.network_id, msg.thing_id) # thing = Thing.Get(msg.network_id, msg.thing_id)
if thing is not None: # if thing is not None:
for handler in self.new_thing_handlers: # for handler in self.new_thing_handlers:
handler(thing) # handler(thing)
def ProcessNameMsg(self, msg: NameMsg): def ProcessNameMsg(self, msg: NameMsg):
print(f'received name {msg.name}') print(f'received name {msg.name}')

View File

@ -1,4 +1,4 @@
from LocalParticipant import LocalParticipant from ParticipantUDP import ParticipantUDP
from Messages.ParticipantMsg import ParticipantMsg from Messages.ParticipantMsg import ParticipantMsg
from Messages.NetworkIdMsg import NetworkIdMsg from Messages.NetworkIdMsg import NetworkIdMsg
from Things.TemperatureSensor import TemperatureSensor from Things.TemperatureSensor import TemperatureSensor
@ -7,7 +7,7 @@ from Thing import Thing
import socket import socket
import threading import threading
class SiteServer(LocalParticipant): class SiteServer(ParticipantUDP):
"""! A site server is a participant which provides a shared simulated environment """! A site server is a participant which provides a shared simulated environment
""" """

View File

@ -39,8 +39,8 @@ class Thing:
owner = parent.owner owner = parent.owner
self.SetParent(parent) self.SetParent(parent)
elif owner == None: elif owner == None:
from LocalParticipant import LocalParticipant from ParticipantUDP import ParticipantUDP
owner = LocalParticipant.Isolated() owner = ParticipantUDP.Isolated()
self.children = [] self.children = []

View File

@ -1,9 +1,9 @@
__all__ = ['Thing', __all__ = ['Thing',
'LocalParticipant', 'ParticipantUDP',
'SiteServer'] 'SiteServer']
from .LinearAlgebra.Direction import Direction from .LinearAlgebra.Direction import Direction
from .LocalParticipant import LocalParticipant from .ParticipantUDP import ParticipantUDP
from .Thing import Thing from .Thing import Thing
from .LinearAlgebra.Spherical import Spherical from .LinearAlgebra.Spherical import Spherical
from .LinearAlgebra.SwingTwist import SwingTwist from .LinearAlgebra.SwingTwist import SwingTwist

View File

@ -5,7 +5,7 @@ sys.path.append(str(Path(__file__).resolve().parent.parent))
import time import time
from SiteServer import SiteServer from SiteServer import SiteServer
from LocalParticipant import LocalParticipant from ParticipantUDP import ParticipantUDP
from Thing import * from Thing import *
from Things.DifferentialDrive import DifferentialDrive from Things.DifferentialDrive import DifferentialDrive
from Things.TouchSensor import TouchSensor from Things.TouchSensor import TouchSensor
@ -15,7 +15,7 @@ site = SiteServer(port=7691)
# Create a local participant for handling communcation # Create a local participant for handling communcation
# using default settings (UDP communciation over port 7681) # using default settings (UDP communciation over port 7681)
participant = LocalParticipant(port=7691, local_port=7683, ip_address="127.0.0.1") participant = ParticipantUDP(port=7691, local_port=7683, ip_address="127.0.0.1")
# The robot's propulsion is a differential drive # The robot's propulsion is a differential drive
bb2b = DifferentialDrive(participant) bb2b = DifferentialDrive(participant)
bb2b.name = "BB2B" bb2b.name = "BB2B"

View File

@ -8,13 +8,13 @@ sys.path.append(str(Path(__file__).resolve().parent.parent))
import unittest import unittest
from Thing import Thing from Thing import Thing
from LocalParticipant import LocalParticipant from ParticipantUDP import ParticipantUDP
from SiteServer import SiteServer from SiteServer import SiteServer
class ThingTest(unittest.TestCase): class ThingTest(unittest.TestCase):
def test_participant(self): def test_participant(self):
participant: LocalParticipant = LocalParticipant() participant: ParticipantUDP = ParticipantUDP()
milliseconds = time.time() * 1000 milliseconds = time.time() * 1000
start_time = milliseconds start_time = milliseconds
@ -33,7 +33,7 @@ class ThingTest(unittest.TestCase):
def test_site_participant(self): def test_site_participant(self):
site = SiteServer(port=7681) site = SiteServer(port=7681)
participant = LocalParticipant(port=7681, ip_address="127.0.0.1", local_port=7682) participant = ParticipantUDP(port=7681, ip_address="127.0.0.1", local_port=7682)
milliseconds = time.time() * 1000 milliseconds = time.time() * 1000
start_time = milliseconds start_time = milliseconds
@ -46,7 +46,7 @@ class ThingTest(unittest.TestCase):
def test_thing_msg(self): def test_thing_msg(self):
site = SiteServer() site = SiteServer()
participant = LocalParticipant(ip_address="127.0.0.1", port=7683) participant = ParticipantUDP(ip_address="127.0.0.1", port=7683)
thing = Thing() thing = Thing()
thing.name = "First thing" thing.name = "First thing"
thing.model_url = "https://passer.life/extras/ant.jpg" thing.model_url = "https://passer.life/extras/ant.jpg"