RoboidControl-python/SiteServer.py
2024-12-31 13:09:36 +01:00

53 lines
1.9 KiB
Python

from .Participant import Participant
from .Thing import Thing
from . import Messages
import select
class SiteServer(Participant):
def __init__(self, ipAddress, port):
super().__init__(ipAddress, port)
self.udp_socket.setblocking(0)
def Update(self, currentTime):
ready_to_read, _, _ = select.select([self.udp_socket], [], [], 0.1) # Timeout of 0.1 seconds
while ready_to_read:
data, addr = self.udp_socket.recvfrom(1024)
self.ReceiveData(data)
ready_to_read, _, _ = select.select([self.udp_socket], [], [], 0.1) # Timeout of 0.1 seconds
return super().Update(currentTime)
def ProcessNetworkIdMsg(self, thing_msg):
self.network_id = thing_msg.network_id
# HACK: send the root things first
for thing in Thing.allThings:
if thing is not None and thing.parent_id == 0:
self.SendThingInfo(thing)
# then sent the rest
for thing in Thing.allThings:
if thing is not None and thing.parent_id != 0:
self.SendThingInfo(thing)
def SendThingInfo(self, thing, recurse = False):
if thing is None:
return
thing_msg = Messages.ThingMsg(self.network_id, thing)
thing_msg.SendTo(self)
name_msg = Messages.NameMsg(self.network_id, thing)
name_msg.SendTo(self)
model_msg = Messages.ModelUrlMsg(self.network_id, thing)
model_msg.SendTo(self)
pose_msg = Messages.PoseMsg(self.network_id, thing)
pose_msg.SendTo(self)
if recurse:
for child in thing.children:
self.SendThingInfo(child, True)
def ProcessInvestigateMsg(self, msg: Messages.InvestigateMsg):
thing = Thing.Get(msg.network_id, msg.thing_id)
if thing is not None:
self.SendThingInfo(thing)