Refactoring
This commit is contained in:
parent
e218e0ea51
commit
fa2895dd81
76
Messages.py
76
Messages.py
@ -1,61 +1,23 @@
|
||||
from . import LowLevelMessages
|
||||
from .Thing import Thing
|
||||
# from . import LowLevelMessages
|
||||
# from .Thing import Thing
|
||||
|
||||
class IMessage:
|
||||
id = 0x00
|
||||
# class IMessage:
|
||||
# id = 0x00
|
||||
|
||||
## Serialize the message into the given buffer
|
||||
#
|
||||
## @returns: the length of the message
|
||||
def Serialize(buffer):
|
||||
return 0
|
||||
# ## Serialize the message into the given buffer
|
||||
# #
|
||||
# ## @returns: the length of the message
|
||||
# def Serialize(buffer):
|
||||
# return 0
|
||||
|
||||
def SendTo(self, participant):
|
||||
buffer_size = self.Serialize([participant.buffer])
|
||||
if buffer_size == 0:
|
||||
return False
|
||||
return participant.SendBuffer(buffer_size)
|
||||
# def SendTo(self, participant):
|
||||
# buffer_size = self.Serialize([participant.buffer])
|
||||
# if buffer_size == 0:
|
||||
# return False
|
||||
# return participant.SendBuffer(buffer_size)
|
||||
|
||||
def Publish(self, participant):
|
||||
bufferSize = self.Serialize([participant.buffer])
|
||||
if bufferSize == 0:
|
||||
return False
|
||||
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]
|
||||
# def Publish(self, participant):
|
||||
# bufferSize = self.Serialize([participant.buffer])
|
||||
# if bufferSize == 0:
|
||||
# return False
|
||||
# return participant.PublishBuffer(bufferSize)
|
||||
|
@ -1,7 +1,7 @@
|
||||
from .Messages import IMessage
|
||||
from .Thing import Thing
|
||||
# from ..Messages import IMessage
|
||||
from ..Thing import Thing
|
||||
|
||||
class BinaryMsg(IMessage):
|
||||
class BinaryMsg():
|
||||
id = 0xB1
|
||||
|
||||
def __init__(self, buffer):
|
@ -1,10 +1,10 @@
|
||||
from .Messages import IMessage
|
||||
# from . import IMessage
|
||||
|
||||
## A client message announces the presence of a participant
|
||||
#
|
||||
## 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
|
||||
class ClientMsg(IMessage):
|
||||
class ClientMsg():
|
||||
id = 0xA0
|
||||
length = 2
|
||||
|
8
Messages/InvestigateMsg.py
Normal file
8
Messages/InvestigateMsg.py
Normal 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]
|
@ -1,5 +1,5 @@
|
||||
import numpy as np
|
||||
from .SwingTwist import SwingTwist
|
||||
from ..SwingTwist import SwingTwist
|
||||
|
||||
def SendAngle8(buffer, ix_ref, angle):
|
||||
# Normalize angle
|
@ -1,6 +1,6 @@
|
||||
from .Messages import IMessage
|
||||
# from . import IMessage
|
||||
|
||||
class ModelUrlMsg(IMessage):
|
||||
class ModelUrlMsg():
|
||||
id = 0x90
|
||||
length = 4
|
||||
|
@ -1,6 +1,6 @@
|
||||
from .Messages import IMessage
|
||||
# from . import IMessage
|
||||
|
||||
class NameMsg(IMessage):
|
||||
class NameMsg():
|
||||
id = 0x91
|
||||
length = 4
|
||||
|
@ -1,9 +1,9 @@
|
||||
from .Messages import IMessage
|
||||
# from . import IMessage
|
||||
|
||||
## A network id message invites another participant to a site
|
||||
#
|
||||
## This can be sent in response to a ClientMsg
|
||||
class NetworkIdMsg(IMessage):
|
||||
class NetworkIdMsg():
|
||||
id = 0xA1
|
||||
length = 2
|
||||
|
30
Messages/PoseMsg.py
Normal file
30
Messages/PoseMsg.py
Normal 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]
|
@ -1,6 +1,6 @@
|
||||
from .Messages import IMessage
|
||||
# from . import IMessage
|
||||
|
||||
class ThingMsg(IMessage):
|
||||
class ThingMsg():
|
||||
id = 0x80
|
||||
length = 5
|
||||
|
17
Messages/__init__.py
Normal file
17
Messages/__init__.py
Normal 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
|
@ -2,12 +2,7 @@ import socket
|
||||
import threading
|
||||
import time
|
||||
|
||||
from .ClientMsg import ClientMsg
|
||||
from .NetworkIdMsg import NetworkIdMsg
|
||||
from .ThingMsg import ThingMsg
|
||||
from .NameMsg import NameMsg
|
||||
from .ModelUrlMsg import ModelUrlMsg
|
||||
from .BinaryMsg import BinaryMsg
|
||||
from .Messages import *
|
||||
from .Thing import Thing
|
||||
|
||||
## A participant is device which can communicate with other participants
|
||||
|
@ -1,5 +1,5 @@
|
||||
from ..Thing import Thing
|
||||
from .. import LowLevelMessages
|
||||
from ..Messages import LowLevelMessages
|
||||
|
||||
class TemperatureSensor(Thing):
|
||||
def __init__(self, network_id, thing_id):
|
||||
|
@ -1,6 +1,5 @@
|
||||
from .Participant import Participant
|
||||
from .ClientMsg import ClientMsg
|
||||
from .NetworkIdMsg import NetworkIdMsg
|
||||
from .Messages import *
|
||||
|
||||
import socket
|
||||
import threading
|
||||
|
Loading…
x
Reference in New Issue
Block a user