Fixed tests

This commit is contained in:
Pascal Serrarens 2025-03-31 09:22:10 +02:00
parent 4d3d007e57
commit 3ab446219d
4 changed files with 45 additions and 30 deletions

View File

@ -33,9 +33,10 @@ class LocalParticipant(Participant):
others = None
thread = None
name = "Participant"
isolated_participant = None
def __init__(self, port=7681, ip_address="0.0.0.0", local_port=0): #, remote=False, udp_socket = None):
super().__init__(ip_address = "0.0.0.0", port = port)
def __init__(self, port=7681, ip_address=None, local_port=0):
super().__init__(ip_address = "127.0.0.1", port = port)
if local_port == 0:
local_port = port
@ -50,6 +51,7 @@ class LocalParticipant(Participant):
if self.port != 0:
self.isolated = False
if ip_address is not None:
self.remote_site = Participant(ip_address, port)
self.others = []
@ -67,6 +69,11 @@ class LocalParticipant(Participant):
self.thread.daemon = True
self.thread.start()
def Isolated():
if LocalParticipant.isolated_participant == None:
LocalParticipant.isolated_participant = LocalParticipant(0)
return LocalParticipant.isolated_participant
#region Update
def GetParticipant(self, ip_address, port):
@ -90,6 +97,7 @@ class LocalParticipant(Participant):
if currentTimeMs is None:
currentTimeMs = time.time() * 1000
if self.isolated == False:
if self.publishInterval > 0 and currentTimeMs > self.nextPublishMe:
msg = ParticipantMsg(self.network_id)
if self.remote_site is None:
@ -129,7 +137,7 @@ class LocalParticipant(Participant):
if buffer_size <= 0:
return True
# print(f'{self.name} send {self.buffer[0]} to {owner.ip_address} {owner.port}')
print(f'{self.name} send {self.buffer[0]} to {owner.ip_address} {owner.port}')
self.udp_socket.sendto(self.buffer[:buffer_size], (owner.ip_address, owner.port))
return True

View File

@ -17,24 +17,27 @@ class SiteServer(LocalParticipant):
"""! Create a new site server
@param port The UDP port on which communication is received
"""
self.ip_address = "0.0.0.0"
self.port = port
self.local_port = port
super().__init__(ip_address = "127.0.0.1", port = port)
# self.ip_address = "0.0.0.0"
# self.port = port
# self.local_port = port
self.isolated = False # site servers are never isolated
self.remote_site = None # site servers never have remote sites
self.others = []
self.network_id = 0
self.buffer = bytearray(256)
self.thing_msg_processors = {}
self.new_thing_handlers = []
# self.others = []
# self.network_id = 0
# self.buffer = bytearray(256)
# self.thing_msg_processors = {}
# self.new_thing_handlers = []
self.publishInterval = 0
self.udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
self.udp_socket.bind(("0.0.0.0", self.local_port))
# self.udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# self.udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
# self.udp_socket.bind(("0.0.0.0", self.local_port))
self.thread = threading.Thread(target = self.Receiver)
self.thread.daemon = True
self.thread.start()
# self.thread = threading.Thread(target = self.Receiver)
# self.thread.daemon = True
# self.thread.start()
self.Register(TemperatureSensor, Thing.Type.TemperatureSensor)

View File

@ -38,6 +38,10 @@ class Thing:
if parent is not None:
owner = parent.owner
self.SetParent(parent)
elif owner == None:
from LocalParticipant import LocalParticipant
owner = LocalParticipant.Isolated()
self.children = []
## The participant owning this thing

View File

@ -32,8 +32,8 @@ class ThingTest(unittest.TestCase):
site.Update(milliseconds)
def test_site_participant(self):
site = SiteServer(7681)
participant = LocalParticipant("127.0.0.1", 7681)
site = SiteServer(port=7681)
participant = LocalParticipant(port=7681, ip_address="127.0.0.1", local_port=7682)
milliseconds = time.time() * 1000
start_time = milliseconds
@ -46,7 +46,7 @@ class ThingTest(unittest.TestCase):
def test_thing_msg(self):
site = SiteServer()
participant = LocalParticipant("127.0.0.1")
participant = LocalParticipant(ip_address="127.0.0.1", port=7683)
thing = Thing()
thing.name = "First thing"
thing.model_url = "https://passer.life/extras/ant.jpg"