diff --git a/Messages.py b/Messages.py index 5206a60..9bf0f93 100644 --- a/Messages.py +++ b/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) diff --git a/BinaryMsg.py b/Messages/BinaryMsg.py similarity index 89% rename from BinaryMsg.py rename to Messages/BinaryMsg.py index ad9248d..e7b6d24 100644 --- a/BinaryMsg.py +++ b/Messages/BinaryMsg.py @@ -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): diff --git a/ClientMsg.py b/Messages/ClientMsg.py similarity index 97% rename from ClientMsg.py rename to Messages/ClientMsg.py index 0b51580..ca19a39 100644 --- a/ClientMsg.py +++ b/Messages/ClientMsg.py @@ -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 diff --git a/Messages/InvestigateMsg.py b/Messages/InvestigateMsg.py new file mode 100644 index 0000000..bd7bfd0 --- /dev/null +++ b/Messages/InvestigateMsg.py @@ -0,0 +1,8 @@ + +class InvestigateMsg(): + id = 0x81 + length = 3 + + def __init__(self, buffer): + self.network_id = buffer[1] + self.thing_id = buffer[2] diff --git a/LowLevelMessages.py b/Messages/LowLevelMessages.py similarity index 97% rename from LowLevelMessages.py rename to Messages/LowLevelMessages.py index 114107d..b02616c 100644 --- a/LowLevelMessages.py +++ b/Messages/LowLevelMessages.py @@ -1,5 +1,5 @@ import numpy as np -from .SwingTwist import SwingTwist +from ..SwingTwist import SwingTwist def SendAngle8(buffer, ix_ref, angle): # Normalize angle diff --git a/ModelUrlMsg.py b/Messages/ModelUrlMsg.py similarity index 95% rename from ModelUrlMsg.py rename to Messages/ModelUrlMsg.py index 349729a..ba7648c 100644 --- a/ModelUrlMsg.py +++ b/Messages/ModelUrlMsg.py @@ -1,6 +1,6 @@ -from .Messages import IMessage +# from . import IMessage -class ModelUrlMsg(IMessage): +class ModelUrlMsg(): id = 0x90 length = 4 diff --git a/NameMsg.py b/Messages/NameMsg.py similarity index 95% rename from NameMsg.py rename to Messages/NameMsg.py index 4908748..db2ebb9 100644 --- a/NameMsg.py +++ b/Messages/NameMsg.py @@ -1,6 +1,6 @@ -from .Messages import IMessage +# from . import IMessage -class NameMsg(IMessage): +class NameMsg(): id = 0x91 length = 4 diff --git a/NetworkIdMsg.py b/Messages/NetworkIdMsg.py similarity index 95% rename from NetworkIdMsg.py rename to Messages/NetworkIdMsg.py index 524baf6..f1549dc 100644 --- a/NetworkIdMsg.py +++ b/Messages/NetworkIdMsg.py @@ -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 diff --git a/Messages/PoseMsg.py b/Messages/PoseMsg.py new file mode 100644 index 0000000..a23ec51 --- /dev/null +++ b/Messages/PoseMsg.py @@ -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] diff --git a/ThingMsg.py b/Messages/ThingMsg.py similarity index 94% rename from ThingMsg.py rename to Messages/ThingMsg.py index eb72008..34e1160 100644 --- a/ThingMsg.py +++ b/Messages/ThingMsg.py @@ -1,6 +1,6 @@ -from .Messages import IMessage +# from . import IMessage -class ThingMsg(IMessage): +class ThingMsg(): id = 0x80 length = 5 diff --git a/Messages/__init__.py b/Messages/__init__.py new file mode 100644 index 0000000..b809675 --- /dev/null +++ b/Messages/__init__.py @@ -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 \ No newline at end of file diff --git a/Participant.py b/Participant.py index 5261c7d..960d2eb 100644 --- a/Participant.py +++ b/Participant.py @@ -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 diff --git a/Sensors/TemperatureSensor.py b/Sensors/TemperatureSensor.py index ef34910..12d13bc 100644 --- a/Sensors/TemperatureSensor.py +++ b/Sensors/TemperatureSensor.py @@ -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): diff --git a/SiteServer.py b/SiteServer.py index 861ee95..5d67ddc 100644 --- a/SiteServer.py +++ b/SiteServer.py @@ -1,6 +1,5 @@ from .Participant import Participant -from .ClientMsg import ClientMsg -from .NetworkIdMsg import NetworkIdMsg +from .Messages import * import socket import threading