From e97a69eb9fbb5cbcea22205d058fc3af55449806 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Fri, 13 Dec 2024 17:58:43 +0100 Subject: [PATCH] Added first ControlCore code --- .gitignore | 1 + Messages.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ Participant.py | 19 ++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 .gitignore create mode 100644 Messages.py create mode 100644 Participant.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96403d3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__/* diff --git a/Messages.py b/Messages.py new file mode 100644 index 0000000..6067483 --- /dev/null +++ b/Messages.py @@ -0,0 +1,61 @@ +from controlcore_python.Participant import Participant + +class IMessage: + def SendMsg(client: Participant, msg): + bufferSize =msg.Serialize(client.buffer) + return client.SendBuffer(bufferSize) + + def Serialize(buffer): + return 0 + +class NetworkIdMsg(IMessage): + id = 0xA1 + length = 2 + + def __init__(self, networkId): + self.networkId = networkId + + def Serialize(self, buffer): + ix = 0 + buffer[ix] = NetworkIdMsg.id + ix+=1 + buffer[ix] = self.networkId + ix+=1 + return ix + + @staticmethod + def Send(participant: Participant, networkId): + msg = NetworkIdMsg(networkId) + return IMessage.SendMsg(participant, msg) + +class ModelUrlMsg(IMessage): + id = 0x90 + + def __init__(self, networkId, thingId, url): + self.networkId = networkId + self.thingId = thingId + self.url = url + + def Serialize(self, buffer): + ix = 0 + buffer[ix] = ModelUrlMsg.id + ix+=1 + buffer[ix] = self.networkId + ix+=1 + buffer[ix] = self.thingId + ix+=1 + buffer[ix] = 0x3D # Dummy float16 value 1 + ix+=1 + buffer[ix] = 0x00 + ix+=1 + buffer[ix] = len(self.url) + ix+=1 + for c in self.url: + buffer[ix] = ord(c) + ix+=1 + return ix + + @staticmethod + def Send(participant, networkId, thingId, modelUrl): + msg = ModelUrlMsg(networkId, thingId, modelUrl) + return IMessage.SendMsg(participant, msg) diff --git a/Participant.py b/Participant.py new file mode 100644 index 0000000..d79d4cc --- /dev/null +++ b/Participant.py @@ -0,0 +1,19 @@ +import socket + +class Participant: + def __init__(self, ipAddress, port): + self.buffer = bytearray(256) + self.ipAddress = ipAddress + self.port = port + self.udpSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + self.udpSocket.bind(("0.0.0.0", 7681)) + + def SendBuffer(self, bufferSize): + if self.ipAddress is None: + return False + + if bufferSize <= 0: + return True + + self.udpSocket.sendto(self.buffer[:bufferSize], (self.ipAddress, self.port)) + return True \ No newline at end of file