Refactoring

This commit is contained in:
Pascal Serrarens 2025-01-20 20:09:53 +01:00
parent e218e0ea51
commit fa2895dd81
14 changed files with 91 additions and 80 deletions

View File

@ -1,61 +1,23 @@
from . import LowLevelMessages # from . import LowLevelMessages
from .Thing import Thing # from .Thing import Thing
class IMessage: # class IMessage:
id = 0x00 # id = 0x00
## Serialize the message into the given buffer # ## Serialize the message into the given buffer
# # #
## @returns: the length of the message # ## @returns: the length of the message
def Serialize(buffer): # def Serialize(buffer):
return 0 # return 0
def SendTo(self, participant): # def SendTo(self, participant):
buffer_size = self.Serialize([participant.buffer]) # buffer_size = self.Serialize([participant.buffer])
if buffer_size == 0: # if buffer_size == 0:
return False # return False
return participant.SendBuffer(buffer_size) # return participant.SendBuffer(buffer_size)
def Publish(self, participant): # def Publish(self, participant):
bufferSize = self.Serialize([participant.buffer]) # bufferSize = self.Serialize([participant.buffer])
if bufferSize == 0: # if bufferSize == 0:
return False # return False
return participant.PublishBuffer(bufferSize) # return participant.PublishBuffer(bufferSize)
class InvestigateMsg():
id = 0x81
length = 3
def __init__(self, buffer):
self.network_id = buffer[1]
self.thing_id = buffer[2]
class PoseMsg(IMessage):
id = 0x10
length = 4
def __init__(self, network_id, thing):
self.network_id = network_id
self.thing = thing
def Serialize(self, buffer_ref):
if (self.network_id is None) or (self.thing is None):
return 0
buffer: bytearray = buffer_ref[0]
buffer[0:PoseMsg.length] = [
PoseMsg.id,
self.network_id,
self.thing.id,
self.thing.pose_updated
]
ix = [4]
if self.thing.pose_updated & Thing.Position:
LowLevelMessages.SendSpherical(buffer, ix, self.thing.position)
if self.thing.pose_updated & Thing.Orientation:
LowLevelMessages.SendQuat32(buffer, ix, self.thing.orientation)
if self.thing.pose_updated & Thing.LinearVelocity:
LowLevelMessages.SendSpherical(buffer, ix, self.thing.linearVelocity)
if self.thing.pose_updated & Thing.AngularVelocity:
LowLevelMessages.SendSpherical(buffer, ix, self.thing.angularVelocity)
return ix[0]

View File

@ -1,7 +1,7 @@
from .Messages import IMessage # from ..Messages import IMessage
from .Thing import Thing from ..Thing import Thing
class BinaryMsg(IMessage): class BinaryMsg():
id = 0xB1 id = 0xB1
def __init__(self, buffer): def __init__(self, buffer):

View File

@ -1,10 +1,10 @@
from .Messages import IMessage # from . import IMessage
## A client message announces the presence of a participant ## A client message announces the presence of a participant
# #
## When received by another participant, it can be followed by a NetworkIdMsg ## When received by another participant, it can be followed by a NetworkIdMsg
## to announce that participant to this client such that it can join privately ## to announce that participant to this client such that it can join privately
class ClientMsg(IMessage): class ClientMsg():
id = 0xA0 id = 0xA0
length = 2 length = 2

View File

@ -0,0 +1,8 @@
class InvestigateMsg():
id = 0x81
length = 3
def __init__(self, buffer):
self.network_id = buffer[1]
self.thing_id = buffer[2]

View File

@ -1,5 +1,5 @@
import numpy as np import numpy as np
from .SwingTwist import SwingTwist from ..SwingTwist import SwingTwist
def SendAngle8(buffer, ix_ref, angle): def SendAngle8(buffer, ix_ref, angle):
# Normalize angle # Normalize angle

View File

@ -1,6 +1,6 @@
from .Messages import IMessage # from . import IMessage
class ModelUrlMsg(IMessage): class ModelUrlMsg():
id = 0x90 id = 0x90
length = 4 length = 4

View File

@ -1,6 +1,6 @@
from .Messages import IMessage # from . import IMessage
class NameMsg(IMessage): class NameMsg():
id = 0x91 id = 0x91
length = 4 length = 4

View File

@ -1,9 +1,9 @@
from .Messages import IMessage # from . import IMessage
## A network id message invites another participant to a site ## A network id message invites another participant to a site
# #
## This can be sent in response to a ClientMsg ## This can be sent in response to a ClientMsg
class NetworkIdMsg(IMessage): class NetworkIdMsg():
id = 0xA1 id = 0xA1
length = 2 length = 2

30
Messages/PoseMsg.py Normal file
View File

@ -0,0 +1,30 @@
class PoseMsg():
id = 0x10
length = 4
def __init__(self, network_id, thing):
self.network_id = network_id
self.thing = thing
def Serialize(self, buffer_ref):
if (self.network_id is None) or (self.thing is None):
return 0
buffer: bytearray = buffer_ref[0]
buffer[0:PoseMsg.length] = [
PoseMsg.id,
self.network_id,
self.thing.id,
self.thing.pose_updated
]
ix = [4]
if self.thing.pose_updated & Thing.Position:
LowLevelMessages.SendSpherical(buffer, ix, self.thing.position)
if self.thing.pose_updated & Thing.Orientation:
LowLevelMessages.SendQuat32(buffer, ix, self.thing.orientation)
if self.thing.pose_updated & Thing.LinearVelocity:
LowLevelMessages.SendSpherical(buffer, ix, self.thing.linearVelocity)
if self.thing.pose_updated & Thing.AngularVelocity:
LowLevelMessages.SendSpherical(buffer, ix, self.thing.angularVelocity)
return ix[0]

View File

@ -1,6 +1,6 @@
from .Messages import IMessage # from . import IMessage
class ThingMsg(IMessage): class ThingMsg():
id = 0x80 id = 0x80
length = 5 length = 5

17
Messages/__init__.py Normal file
View File

@ -0,0 +1,17 @@
__all__ = ['BinaryMsg',
'ClientMsg',
'NetworkIdMsg',
'InvestigateMsg',
'ThingMsg',
'NameMsg',
'ModelUrlMsg',
'PoseMsg']
from .BinaryMsg import BinaryMsg
from .ClientMsg import ClientMsg
from .InvestigateMsg import InvestigateMsg
from .ThingMsg import ThingMsg
from .NetworkIdMsg import NetworkIdMsg
from .NameMsg import NameMsg
from .ModelUrlMsg import ModelUrlMsg
from .PoseMsg import PoseMsg

View File

@ -2,12 +2,7 @@ import socket
import threading import threading
import time import time
from .ClientMsg import ClientMsg from .Messages import *
from .NetworkIdMsg import NetworkIdMsg
from .ThingMsg import ThingMsg
from .NameMsg import NameMsg
from .ModelUrlMsg import ModelUrlMsg
from .BinaryMsg import BinaryMsg
from .Thing import Thing from .Thing import Thing
## A participant is device which can communicate with other participants ## A participant is device which can communicate with other participants

View File

@ -1,5 +1,5 @@
from ..Thing import Thing from ..Thing import Thing
from .. import LowLevelMessages from ..Messages import LowLevelMessages
class TemperatureSensor(Thing): class TemperatureSensor(Thing):
def __init__(self, network_id, thing_id): def __init__(self, network_id, thing_id):

View File

@ -1,6 +1,5 @@
from .Participant import Participant from .Participant import Participant
from .ClientMsg import ClientMsg from .Messages import *
from .NetworkIdMsg import NetworkIdMsg
import socket import socket
import threading import threading