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 others = None
thread = None thread = None
name = "Participant" name = "Participant"
isolated_participant = None
def __init__(self, port=7681, ip_address="0.0.0.0", local_port=0): #, remote=False, udp_socket = None): def __init__(self, port=7681, ip_address=None, local_port=0):
super().__init__(ip_address = "0.0.0.0", port = port) super().__init__(ip_address = "127.0.0.1", port = port)
if local_port == 0: if local_port == 0:
local_port = port local_port = port
@ -50,7 +51,8 @@ class LocalParticipant(Participant):
if self.port != 0: if self.port != 0:
self.isolated = False self.isolated = False
self.remote_site = Participant(ip_address, port) if ip_address is not None:
self.remote_site = Participant(ip_address, port)
self.others = [] self.others = []
self.network_id = 0 self.network_id = 0
@ -66,6 +68,11 @@ class LocalParticipant(Participant):
self.thread = threading.Thread(target = self.Receiver) self.thread = threading.Thread(target = self.Receiver)
self.thread.daemon = True self.thread.daemon = True
self.thread.start() self.thread.start()
def Isolated():
if LocalParticipant.isolated_participant == None:
LocalParticipant.isolated_participant = LocalParticipant(0)
return LocalParticipant.isolated_participant
#region Update #region Update
@ -90,15 +97,16 @@ class LocalParticipant(Participant):
if currentTimeMs is None: if currentTimeMs is None:
currentTimeMs = time.time() * 1000 currentTimeMs = time.time() * 1000
if self.publishInterval > 0 and currentTimeMs > self.nextPublishMe: if self.isolated == False:
msg = ParticipantMsg(self.network_id) if self.publishInterval > 0 and currentTimeMs > self.nextPublishMe:
if self.remote_site is None: msg = ParticipantMsg(self.network_id)
self.Publish(msg) if self.remote_site is None:
else: self.Publish(msg)
self.Send(self.remote_site, msg) else:
self.Send(self.remote_site, msg)
print(f'Publish ParticipantMsg {self.network_id}')
self.nextPublishMe = currentTimeMs + self.publishInterval print(f'Publish ParticipantMsg {self.network_id}')
self.nextPublishMe = currentTimeMs + self.publishInterval
for thing in self.things: for thing in self.things:
thing.Update(currentTimeMs) thing.Update(currentTimeMs)
@ -129,7 +137,7 @@ class LocalParticipant(Participant):
if buffer_size <= 0: if buffer_size <= 0:
return True 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)) self.udp_socket.sendto(self.buffer[:buffer_size], (owner.ip_address, owner.port))
return True return True

View File

@ -17,24 +17,27 @@ class SiteServer(LocalParticipant):
"""! Create a new site server """! Create a new site server
@param port The UDP port on which communication is received @param port The UDP port on which communication is received
""" """
self.ip_address = "0.0.0.0" super().__init__(ip_address = "127.0.0.1", port = port)
self.port = port # self.ip_address = "0.0.0.0"
self.local_port = port # 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.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 = 0 self.publishInterval = 0
self.udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 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.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
self.udp_socket.bind(("0.0.0.0", self.local_port)) # self.udp_socket.bind(("0.0.0.0", self.local_port))
self.thread = threading.Thread(target = self.Receiver) # self.thread = threading.Thread(target = self.Receiver)
self.thread.daemon = True # self.thread.daemon = True
self.thread.start() # self.thread.start()
self.Register(TemperatureSensor, Thing.Type.TemperatureSensor) self.Register(TemperatureSensor, Thing.Type.TemperatureSensor)

View File

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

View File

@ -32,8 +32,8 @@ class ThingTest(unittest.TestCase):
site.Update(milliseconds) site.Update(milliseconds)
def test_site_participant(self): def test_site_participant(self):
site = SiteServer(7681) site = SiteServer(port=7681)
participant = LocalParticipant("127.0.0.1", 7681) participant = LocalParticipant(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("127.0.0.1") participant = LocalParticipant(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"