Added first ControlCore code
This commit is contained in:
parent
1b000f4e26
commit
e97a69eb9f
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
__pycache__/*
|
61
Messages.py
Normal file
61
Messages.py
Normal file
@ -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)
|
19
Participant.py
Normal file
19
Participant.py
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user