esp-artnet-http-to-dmx/artnetserver.py

38 lines
1.0 KiB
Python

try:
import usocket as socket
except:
import socket
class ArtNetServer():
def __init__(self, ip, port, universe=0):
self.ip = ip
self.port = port
self.universe = universe
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.socket.setblocking(False)
self.socket.bind((ip, port))
def __del__(self):
self.close()
def close(self):
self.socket.close()
def checkdata(self, dmx):
try:
data, address = self.socket.recvfrom(1024)
except OSError as e:
err = e.args[0]
if err != 11:
raise e
else:
if self.validate_header(data):
universe = int.from_bytes(data[14:16], 'little')
if universe != self.universe:
return -1
dmx.set_channels(list(data)[18:])
return 0
def validate_header(self, header):
return header[:12] == b'Art-Net\x00\x00P\x00\x0e'