feat: DMX gateway: Art-Net and serial out, fixtures and a pattern engine
Host gateway that takes led-controller-style commands and fans out DMX512 universes to Art-Net devices and serial ports. Fixtures are patched onto outputs and a led-driver-style pattern engine writes channel roles into them. Includes the vendored microdot/utemplate libs, tests and the README. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
072f95e041
commit
d2ffcb1969
30 files changed
+5490
-1
No files matched your search
+362
@@ -0,0 +1,362 @@
|
||||
# Art-Net 4 (protocol 14). MicroPython and CPython.
|
||||
# UDP 6454. ArtDmx payload[0] is DMX channel 1 (no start code).
|
||||
|
||||
import socket
|
||||
|
||||
try:
|
||||
from time import ticks_diff, ticks_ms, sleep_ms
|
||||
except ImportError:
|
||||
from time import sleep, time
|
||||
|
||||
def ticks_ms():
|
||||
return int(time() * 1000)
|
||||
|
||||
def ticks_diff(a, b):
|
||||
return a - b
|
||||
|
||||
def sleep_ms(ms):
|
||||
sleep(ms / 1000.0)
|
||||
|
||||
PORT = 6454
|
||||
HEADER = b"Art-Net\x00"
|
||||
OP_POLL = 0x2000
|
||||
OP_POLL_REPLY = 0x2100
|
||||
OP_DMX = 0x5000
|
||||
PROT_VER = 14
|
||||
BROADCAST = "255.255.255.255"
|
||||
|
||||
|
||||
def _u16le(n):
|
||||
return bytes((n & 0xFF, (n >> 8) & 0xFF))
|
||||
|
||||
|
||||
def _pad(text, size):
|
||||
raw = text.encode("ascii")[: size - 1]
|
||||
return raw + b"\x00" * (size - len(raw))
|
||||
|
||||
|
||||
def _ip_bytes(ip):
|
||||
if isinstance(ip, (bytes, bytearray)):
|
||||
return bytes(ip[:4])
|
||||
parts = ip.split(".")
|
||||
return bytes(int(p) for p in parts)
|
||||
|
||||
|
||||
def port_lo(universe=0, subnet=0):
|
||||
"""SubUni byte: subnet in high nibble, universe in low nibble."""
|
||||
return ((subnet & 0x0F) << 4) | (universe & 0x0F)
|
||||
|
||||
|
||||
def as_channels(data):
|
||||
"""Normalise to 512-or-fewer channel bytes (ch 1 at index 0)."""
|
||||
if data is None:
|
||||
return b""
|
||||
if len(data) == 513:
|
||||
data = data[1:]
|
||||
if len(data) > 512:
|
||||
data = data[:512]
|
||||
return data
|
||||
|
||||
|
||||
def build_artdmx(channels, universe=0, subnet=0, net=0, sequence=1, physical=0):
|
||||
data = bytearray(as_channels(channels))
|
||||
n = len(data)
|
||||
if n < 2:
|
||||
data.extend(b"\x00" * (2 - n))
|
||||
n = 2
|
||||
if n & 1:
|
||||
data.append(0)
|
||||
n += 1
|
||||
pkt = bytearray(18 + n)
|
||||
pkt[0:8] = HEADER
|
||||
pkt[8:10] = _u16le(OP_DMX)
|
||||
pkt[10] = 0
|
||||
pkt[11] = PROT_VER
|
||||
pkt[12] = sequence
|
||||
pkt[13] = physical
|
||||
pkt[14] = port_lo(universe, subnet)
|
||||
pkt[15] = net & 0x7F
|
||||
pkt[16] = (n >> 8) & 0xFF
|
||||
pkt[17] = n & 0xFF
|
||||
pkt[18 : 18 + n] = data
|
||||
return pkt
|
||||
|
||||
|
||||
def parse(packet):
|
||||
"""Return ('dmx', net, subuni, data), ('poll', addr_unused), or None."""
|
||||
if packet is None or len(packet) < 10 or packet[0:8] != HEADER:
|
||||
return None
|
||||
opcode = packet[8] | (packet[9] << 8)
|
||||
if opcode == OP_DMX:
|
||||
if len(packet) < 18:
|
||||
return None
|
||||
length = (packet[16] << 8) | packet[17]
|
||||
data = packet[18 : 18 + length]
|
||||
return ("dmx", packet[15] & 0x7F, packet[14], data)
|
||||
if opcode == OP_POLL:
|
||||
return ("poll", None)
|
||||
if opcode == OP_POLL_REPLY:
|
||||
return ("poll_reply", None)
|
||||
return None
|
||||
|
||||
|
||||
class ArtNet:
|
||||
"""Send and receive Art-Net on one universe.
|
||||
|
||||
universe/subnet/net match the 15-bit Port-Address
|
||||
(net << 8) | (subnet << 4) | universe.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
universe=0,
|
||||
subnet=0,
|
||||
net=0,
|
||||
bind="0.0.0.0",
|
||||
port=PORT,
|
||||
bind_port=None,
|
||||
name="esp-dmx",
|
||||
ip=None,
|
||||
mac=None,
|
||||
):
|
||||
self.universe = universe & 0x0F
|
||||
self.subnet = subnet & 0x0F
|
||||
self.net = net & 0x7F
|
||||
self.name = name
|
||||
self.ip = ip
|
||||
self.mac = mac or bytes(6)
|
||||
self.sequence = 1
|
||||
self.channels = bytearray(512)
|
||||
self.peers = []
|
||||
self._subuni = port_lo(self.universe, self.subnet)
|
||||
|
||||
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
try:
|
||||
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
except OSError:
|
||||
pass
|
||||
try:
|
||||
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
||||
except OSError:
|
||||
pass
|
||||
self.sock.bind((bind, port if bind_port is None else bind_port))
|
||||
try:
|
||||
self.sock.settimeout(0)
|
||||
except OSError:
|
||||
self.sock.setblocking(False)
|
||||
self._guess_host()
|
||||
|
||||
def _guess_host(self):
|
||||
try:
|
||||
import network
|
||||
|
||||
sta = network.WLAN(network.STA_IF)
|
||||
if sta.active() and sta.isconnected():
|
||||
self.ip = sta.ifconfig()[0]
|
||||
try:
|
||||
self.mac = sta.config("mac")
|
||||
except Exception:
|
||||
pass
|
||||
return
|
||||
|
||||
ap = network.WLAN(network.AP_IF)
|
||||
if ap.active():
|
||||
self.ip = ap.ifconfig()[0]
|
||||
try:
|
||||
self.mac = ap.config("mac")
|
||||
except Exception:
|
||||
pass
|
||||
return
|
||||
except (ImportError, OSError, AttributeError):
|
||||
pass
|
||||
if not self.ip:
|
||||
self.ip = "0.0.0.0"
|
||||
|
||||
def _note_peer(self, addr):
|
||||
"""Remember unicast Art-Net sources (QLC+) so we can reply unicast."""
|
||||
if not addr:
|
||||
return
|
||||
host = addr[0] if isinstance(addr, (tuple, list)) else addr
|
||||
if not host or host in (self.ip, BROADCAST, "0.0.0.0"):
|
||||
return
|
||||
if host.split(".")[-1] == "255":
|
||||
return
|
||||
if host not in self.peers:
|
||||
self.peers.append(host)
|
||||
print("artnet peer", host)
|
||||
self._unicast_poll_reply(host)
|
||||
|
||||
def _announce_addrs(self, extra=None):
|
||||
addrs = [BROADCAST]
|
||||
if extra:
|
||||
host = extra[0] if isinstance(extra, (tuple, list)) else extra
|
||||
if host:
|
||||
addrs.append(host)
|
||||
self._guess_host()
|
||||
parts = (self.ip or "").split(".")
|
||||
if len(parts) == 4 and parts[0] != "0":
|
||||
addrs.append(".".join(parts[:3] + ["255"]))
|
||||
for p in self.peers:
|
||||
addrs.append(p)
|
||||
try:
|
||||
import network
|
||||
|
||||
sta = network.WLAN(network.STA_IF)
|
||||
if sta.active() and sta.isconnected():
|
||||
gw = sta.ifconfig()[2]
|
||||
if gw:
|
||||
addrs.append(gw)
|
||||
except Exception:
|
||||
pass
|
||||
seen = []
|
||||
for a in addrs:
|
||||
if a and a not in seen:
|
||||
seen.append(a)
|
||||
return seen
|
||||
|
||||
def announce(self):
|
||||
"""ArtPoll + ArtPollReply. Unicast to known PCs; broadcast for the rest."""
|
||||
self._guess_host()
|
||||
reply = self._poll_reply_packet()
|
||||
poll = bytearray(14)
|
||||
poll[0:8] = HEADER
|
||||
poll[8:10] = _u16le(OP_POLL)
|
||||
poll[10] = 0
|
||||
poll[11] = PROT_VER
|
||||
poll[12] = 0x02
|
||||
poll[13] = 0
|
||||
for host in self._announce_addrs():
|
||||
try:
|
||||
self.sock.sendto(reply, (host, PORT))
|
||||
self.sock.sendto(poll, (host, PORT))
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
def send(self, data=None, dest=BROADCAST, port=PORT):
|
||||
"""Send ArtDmx. data[0] is channel 1, or pass a 513-byte UART frame."""
|
||||
if data is None:
|
||||
data = self.channels
|
||||
pkt = build_artdmx(
|
||||
data,
|
||||
universe=self.universe,
|
||||
subnet=self.subnet,
|
||||
net=self.net,
|
||||
sequence=self.sequence,
|
||||
)
|
||||
self.sequence += 1
|
||||
if self.sequence > 255:
|
||||
self.sequence = 1
|
||||
self.sock.sendto(pkt, (dest, port))
|
||||
|
||||
def poll(self, dest=BROADCAST, port=PORT):
|
||||
"""Broadcast ArtPoll so other nodes reply."""
|
||||
pkt = bytearray(14)
|
||||
pkt[0:8] = HEADER
|
||||
pkt[8:10] = _u16le(OP_POLL)
|
||||
pkt[10] = 0
|
||||
pkt[11] = PROT_VER
|
||||
pkt[12] = 0
|
||||
pkt[13] = 0
|
||||
self.sock.sendto(pkt, (dest, port))
|
||||
|
||||
def recv(self):
|
||||
"""Read pending UDP. Returns channel bytes for this universe, else None.
|
||||
|
||||
Replies to ArtPoll so QLC+ / consoles can discover this node.
|
||||
If several ArtDmx packets are queued, the latest matching one is kept.
|
||||
"""
|
||||
latest = None
|
||||
while True:
|
||||
packet, addr = self._recvfrom()
|
||||
if packet is None:
|
||||
break
|
||||
kind = parse(packet)
|
||||
if kind is None:
|
||||
continue
|
||||
self._note_peer(addr)
|
||||
if kind[0] == "poll":
|
||||
self._send_poll_reply(addr)
|
||||
elif kind[0] == "dmx":
|
||||
_name, net, subuni, data = kind
|
||||
if net == self.net and subuni == self._subuni:
|
||||
n = len(data)
|
||||
if n > 512:
|
||||
n = 512
|
||||
self.channels[:n] = data[:n]
|
||||
if n < 512:
|
||||
# leave remaining channels as last values
|
||||
pass
|
||||
latest = self.channels
|
||||
return latest
|
||||
|
||||
def apply_frame(self, frame):
|
||||
"""Copy current channels into a UART DMX frame (start code at [0])."""
|
||||
n = len(self.channels)
|
||||
frame[1 : 1 + n] = self.channels
|
||||
|
||||
def serve(self, dmx):
|
||||
"""Block: Art-Net in, UART DMX out. Announces to QLC+ every 2.5s."""
|
||||
self.announce()
|
||||
print("artnet node", self.ip, "universe", self.universe)
|
||||
last_announce = ticks_ms()
|
||||
while True:
|
||||
if self.recv() is not None:
|
||||
self.apply_frame(dmx.frame)
|
||||
dmx.send()
|
||||
now = ticks_ms()
|
||||
if ticks_diff(now, last_announce) >= 2500:
|
||||
self.announce()
|
||||
last_announce = now
|
||||
sleep_ms(20)
|
||||
|
||||
def close(self):
|
||||
self.sock.close()
|
||||
|
||||
def _recvfrom(self):
|
||||
try:
|
||||
return self.sock.recvfrom(2048)
|
||||
except OSError:
|
||||
return None, None
|
||||
|
||||
def _poll_reply_packet(self):
|
||||
pkt = bytearray(239)
|
||||
pkt[0:8] = HEADER
|
||||
pkt[8:10] = _u16le(OP_POLL_REPLY)
|
||||
pkt[10:14] = _ip_bytes(self.ip)
|
||||
pkt[14:16] = _u16le(PORT)
|
||||
pkt[16] = 0
|
||||
pkt[17] = 1
|
||||
pkt[18] = self.net
|
||||
pkt[19] = self.subnet
|
||||
pkt[20] = 0x00
|
||||
pkt[21] = 0xFF
|
||||
pkt[23] = 0xF0
|
||||
pkt[26:44] = _pad(self.name, 18)
|
||||
pkt[44:108] = _pad(self.name + " Art-Net node", 64)
|
||||
pkt[108:172] = _pad("#0001 [00] ok", 64)
|
||||
pkt[173] = 1
|
||||
pkt[174] = 0x80
|
||||
pkt[182] = 0x80
|
||||
pkt[190] = self.universe
|
||||
pkt[200] = 0
|
||||
mac = bytes(self.mac)[:6]
|
||||
pkt[201 : 201 + len(mac)] = mac
|
||||
pkt[207:211] = _ip_bytes(self.ip)
|
||||
pkt[211] = 1
|
||||
pkt[212] = 0x08
|
||||
return pkt
|
||||
|
||||
def _unicast_poll_reply(self, host):
|
||||
try:
|
||||
self.sock.sendto(self._poll_reply_packet(), (host, PORT))
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
def _send_poll_reply(self, addr):
|
||||
pkt = self._poll_reply_packet()
|
||||
dests = self._announce_addrs(addr)
|
||||
for host in dests:
|
||||
try:
|
||||
self.sock.sendto(pkt, (host, PORT))
|
||||
except OSError:
|
||||
pass
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
# DMX512 UART TX. MicroPython (ESP32) and buffer-only on CPython.
|
||||
|
||||
try:
|
||||
from machine import UART, Pin
|
||||
except ImportError:
|
||||
UART = None
|
||||
Pin = None
|
||||
|
||||
|
||||
class DMX:
|
||||
"""One DMX512 universe. UART TX on MicroPython; frame-only on CPython."""
|
||||
|
||||
def __init__(self, uart=1, tx=0, de=None):
|
||||
self.frame = bytearray(513) # start code 0 + 512 slots
|
||||
self.uart = None
|
||||
self._de = None
|
||||
if UART is None:
|
||||
return
|
||||
self.uart = UART(uart, tx=tx)
|
||||
self.uart.init(250000, bits=8, parity=None, stop=2)
|
||||
if de is not None and Pin is not None:
|
||||
self._de = Pin(de, Pin.OUT)
|
||||
self._de.value(0)
|
||||
|
||||
def send(self):
|
||||
if self.uart is None:
|
||||
return
|
||||
if self._de is not None:
|
||||
self._de.value(1)
|
||||
self.uart.sendbreak()
|
||||
self.uart.write(self.frame)
|
||||
if self._de is not None:
|
||||
self._de.value(0)
|
||||
@@ -0,0 +1,2 @@
|
||||
from microdot.microdot import Microdot, Request, Response, abort, redirect, \
|
||||
send_file # noqa: F401
|
||||
@@ -0,0 +1,8 @@
|
||||
try:
|
||||
from functools import wraps
|
||||
except ImportError: # pragma: no cover
|
||||
# MicroPython does not currently implement functools.wraps
|
||||
def wraps(wrapped):
|
||||
def _(wrapper):
|
||||
return wrapper
|
||||
return _
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,70 @@
|
||||
from utemplate import recompile
|
||||
|
||||
_loader = None
|
||||
|
||||
|
||||
class Template:
|
||||
"""A template object.
|
||||
|
||||
:param template: The filename of the template to render, relative to the
|
||||
configured template directory.
|
||||
"""
|
||||
@classmethod
|
||||
def initialize(cls, template_dir='templates',
|
||||
loader_class=recompile.Loader):
|
||||
"""Initialize the templating subsystem.
|
||||
|
||||
:param template_dir: the directory where templates are stored. This
|
||||
argument is optional. The default is to load
|
||||
templates from a *templates* subdirectory.
|
||||
:param loader_class: the ``utemplate.Loader`` class to use when loading
|
||||
templates. This argument is optional. The default
|
||||
is the ``recompile.Loader`` class, which
|
||||
automatically recompiles templates when they
|
||||
change.
|
||||
"""
|
||||
global _loader
|
||||
_loader = loader_class(None, template_dir)
|
||||
|
||||
def __init__(self, template):
|
||||
if _loader is None: # pragma: no cover
|
||||
self.initialize()
|
||||
#: The name of the template
|
||||
self.name = template
|
||||
self.template = _loader.load(template)
|
||||
|
||||
def generate(self, *args, **kwargs):
|
||||
"""Return a generator that renders the template in chunks, with the
|
||||
given arguments."""
|
||||
return self.template(*args, **kwargs)
|
||||
|
||||
def render(self, *args, **kwargs):
|
||||
"""Render the template with the given arguments and return it as a
|
||||
string."""
|
||||
return ''.join(self.generate(*args, **kwargs))
|
||||
|
||||
def generate_async(self, *args, **kwargs):
|
||||
"""Return an asynchronous generator that renders the template in
|
||||
chunks, using the given arguments."""
|
||||
class sync_to_async_iter():
|
||||
def __init__(self, iter):
|
||||
self.iter = iter
|
||||
|
||||
def __aiter__(self):
|
||||
return self
|
||||
|
||||
async def __anext__(self):
|
||||
try:
|
||||
return next(self.iter)
|
||||
except StopIteration:
|
||||
raise StopAsyncIteration
|
||||
|
||||
return sync_to_async_iter(self.generate(*args, **kwargs))
|
||||
|
||||
async def render_async(self, *args, **kwargs):
|
||||
"""Render the template with the given arguments asynchronously and
|
||||
return it as a string."""
|
||||
response = ''
|
||||
async for chunk in self.generate_async(*args, **kwargs):
|
||||
response += chunk
|
||||
return response
|
||||
@@ -0,0 +1,231 @@
|
||||
import binascii
|
||||
import hashlib
|
||||
from microdot import Request, Response
|
||||
from microdot.microdot import MUTED_SOCKET_ERRORS, print_exception
|
||||
from microdot.helpers import wraps
|
||||
|
||||
|
||||
class WebSocketError(Exception):
|
||||
"""Exception raised when an error occurs in a WebSocket connection."""
|
||||
pass
|
||||
|
||||
|
||||
class WebSocket:
|
||||
"""A WebSocket connection object.
|
||||
|
||||
An instance of this class is sent to handler functions to manage the
|
||||
WebSocket connection.
|
||||
"""
|
||||
CONT = 0
|
||||
TEXT = 1
|
||||
BINARY = 2
|
||||
CLOSE = 8
|
||||
PING = 9
|
||||
PONG = 10
|
||||
|
||||
#: Specify the maximum message size that can be received when calling the
|
||||
#: ``receive()`` method. Messages with payloads that are larger than this
|
||||
#: size will be rejected and the connection closed. Set to 0 to disable
|
||||
#: the size check (be aware of potential security issues if you do this),
|
||||
#: or to -1 to use the value set in
|
||||
#: ``Request.max_body_length``. The default is -1.
|
||||
#:
|
||||
#: Example::
|
||||
#:
|
||||
#: WebSocket.max_message_length = 4 * 1024 # up to 4KB messages
|
||||
max_message_length = -1
|
||||
|
||||
def __init__(self, request):
|
||||
self.request = request
|
||||
self.closed = False
|
||||
|
||||
async def handshake(self):
|
||||
response = self._handshake_response()
|
||||
await self.request.sock[1].awrite(
|
||||
b'HTTP/1.1 101 Switching Protocols\r\n')
|
||||
await self.request.sock[1].awrite(b'Upgrade: websocket\r\n')
|
||||
await self.request.sock[1].awrite(b'Connection: Upgrade\r\n')
|
||||
await self.request.sock[1].awrite(
|
||||
b'Sec-WebSocket-Accept: ' + response + b'\r\n\r\n')
|
||||
|
||||
async def receive(self):
|
||||
"""Receive a message from the client."""
|
||||
while True:
|
||||
opcode, payload = await self._read_frame()
|
||||
send_opcode, data = self._process_websocket_frame(opcode, payload)
|
||||
if send_opcode: # pragma: no cover
|
||||
await self.send(data, send_opcode)
|
||||
elif data: # pragma: no branch
|
||||
return data
|
||||
|
||||
async def send(self, data, opcode=None):
|
||||
"""Send a message to the client.
|
||||
|
||||
:param data: the data to send, given as a string or bytes.
|
||||
:param opcode: a custom frame opcode to use. If not given, the opcode
|
||||
is ``TEXT`` or ``BINARY`` depending on the type of the
|
||||
data.
|
||||
"""
|
||||
frame = self._encode_websocket_frame(
|
||||
opcode or (self.TEXT if isinstance(data, str) else self.BINARY),
|
||||
data)
|
||||
await self.request.sock[1].awrite(frame)
|
||||
|
||||
async def close(self):
|
||||
"""Close the websocket connection."""
|
||||
if not self.closed: # pragma: no cover
|
||||
self.closed = True
|
||||
await self.send(b'', self.CLOSE)
|
||||
|
||||
def _handshake_response(self):
|
||||
connection = False
|
||||
upgrade = False
|
||||
websocket_key = None
|
||||
for header, value in self.request.headers.items():
|
||||
h = header.lower()
|
||||
if h == 'connection':
|
||||
connection = True
|
||||
if 'upgrade' not in value.lower():
|
||||
return self.request.app.abort(400)
|
||||
elif h == 'upgrade':
|
||||
upgrade = True
|
||||
if not value.lower() == 'websocket':
|
||||
return self.request.app.abort(400)
|
||||
elif h == 'sec-websocket-key':
|
||||
websocket_key = value
|
||||
if not connection or not upgrade or not websocket_key:
|
||||
return self.request.app.abort(400)
|
||||
d = hashlib.sha1(websocket_key.encode())
|
||||
d.update(b'258EAFA5-E914-47DA-95CA-C5AB0DC85B11')
|
||||
return binascii.b2a_base64(d.digest())[:-1]
|
||||
|
||||
@classmethod
|
||||
def _parse_frame_header(cls, header):
|
||||
fin = header[0] & 0x80
|
||||
opcode = header[0] & 0x0f
|
||||
if fin == 0 or opcode == cls.CONT: # pragma: no cover
|
||||
raise WebSocketError('Continuation frames not supported')
|
||||
has_mask = header[1] & 0x80
|
||||
length = header[1] & 0x7f
|
||||
if length == 126:
|
||||
length = -2
|
||||
elif length == 127:
|
||||
length = -8
|
||||
return fin, opcode, has_mask, length
|
||||
|
||||
def _process_websocket_frame(self, opcode, payload):
|
||||
if opcode == self.TEXT:
|
||||
payload = payload.decode()
|
||||
elif opcode == self.BINARY:
|
||||
pass
|
||||
elif opcode == self.CLOSE:
|
||||
raise WebSocketError('Websocket connection closed')
|
||||
elif opcode == self.PING:
|
||||
return self.PONG, payload
|
||||
elif opcode == self.PONG: # pragma: no branch
|
||||
return None, None
|
||||
return None, payload
|
||||
|
||||
@classmethod
|
||||
def _encode_websocket_frame(cls, opcode, payload):
|
||||
frame = bytearray()
|
||||
frame.append(0x80 | opcode)
|
||||
if opcode == cls.TEXT:
|
||||
payload = payload.encode()
|
||||
if len(payload) < 126:
|
||||
frame.append(len(payload))
|
||||
elif len(payload) < (1 << 16):
|
||||
frame.append(126)
|
||||
frame.extend(len(payload).to_bytes(2, 'big'))
|
||||
else:
|
||||
frame.append(127)
|
||||
frame.extend(len(payload).to_bytes(8, 'big'))
|
||||
frame.extend(payload)
|
||||
return frame
|
||||
|
||||
async def _read_frame(self):
|
||||
header = await self.request.sock[0].read(2)
|
||||
if len(header) != 2: # pragma: no cover
|
||||
raise WebSocketError('Websocket connection closed')
|
||||
fin, opcode, has_mask, length = self._parse_frame_header(header)
|
||||
if length == -2:
|
||||
length = await self.request.sock[0].read(2)
|
||||
length = int.from_bytes(length, 'big')
|
||||
elif length == -8:
|
||||
length = await self.request.sock[0].read(8)
|
||||
length = int.from_bytes(length, 'big')
|
||||
max_allowed_length = Request.max_body_length \
|
||||
if self.max_message_length == -1 else self.max_message_length
|
||||
if length > max_allowed_length:
|
||||
raise WebSocketError('Message too large')
|
||||
if has_mask: # pragma: no cover
|
||||
mask = await self.request.sock[0].read(4)
|
||||
payload = await self.request.sock[0].read(length)
|
||||
if has_mask: # pragma: no cover
|
||||
payload = bytes(x ^ mask[i % 4] for i, x in enumerate(payload))
|
||||
return opcode, payload
|
||||
|
||||
|
||||
async def websocket_upgrade(request):
|
||||
"""Upgrade a request handler to a websocket connection.
|
||||
|
||||
This function can be called directly inside a route function to process a
|
||||
WebSocket upgrade handshake, for example after the user's credentials are
|
||||
verified. The function returns the websocket object::
|
||||
|
||||
@app.route('/echo')
|
||||
async def echo(request):
|
||||
if not authenticate_user(request):
|
||||
abort(401)
|
||||
ws = await websocket_upgrade(request)
|
||||
while True:
|
||||
message = await ws.receive()
|
||||
await ws.send(message)
|
||||
"""
|
||||
ws = WebSocket(request)
|
||||
await ws.handshake()
|
||||
|
||||
@request.after_request
|
||||
async def after_request(request, response):
|
||||
return Response.already_handled
|
||||
|
||||
return ws
|
||||
|
||||
|
||||
def websocket_wrapper(f, upgrade_function):
|
||||
@wraps(f)
|
||||
async def wrapper(request, *args, **kwargs):
|
||||
ws = await upgrade_function(request)
|
||||
try:
|
||||
await f(request, ws, *args, **kwargs)
|
||||
except OSError as exc:
|
||||
if exc.errno not in MUTED_SOCKET_ERRORS: # pragma: no cover
|
||||
raise
|
||||
except WebSocketError:
|
||||
pass
|
||||
except Exception as exc:
|
||||
print_exception(exc)
|
||||
finally: # pragma: no cover
|
||||
try:
|
||||
await ws.close()
|
||||
except Exception:
|
||||
pass
|
||||
return Response.already_handled
|
||||
return wrapper
|
||||
|
||||
|
||||
def with_websocket(f):
|
||||
"""Decorator to make a route a WebSocket endpoint.
|
||||
|
||||
This decorator is used to define a route that accepts websocket
|
||||
connections. The route then receives a websocket object as a second
|
||||
argument that it can use to send and receive messages::
|
||||
|
||||
@app.route('/echo')
|
||||
@with_websocket
|
||||
async def echo(request, ws):
|
||||
while True:
|
||||
message = await ws.receive()
|
||||
await ws.send(message)
|
||||
"""
|
||||
return websocket_wrapper(f, websocket_upgrade)
|
||||
@@ -0,0 +1,72 @@
|
||||
# Host pyserial DMX512 TX (break + 513-byte frame). No-op without pyserial.
|
||||
|
||||
try:
|
||||
from time import sleep
|
||||
except ImportError: # pragma: no cover
|
||||
def sleep(_s):
|
||||
pass
|
||||
|
||||
try:
|
||||
import serial as pyserial
|
||||
except ImportError:
|
||||
pyserial = None
|
||||
|
||||
|
||||
class SerialDMX:
|
||||
"""Raw DMX512 over a serial port (8N2, break, start code 0 + 512 slots)."""
|
||||
|
||||
def __init__(self, path, baud=250000):
|
||||
self.path = path
|
||||
self.baud = int(baud or 250000)
|
||||
self.ser = None
|
||||
self.open()
|
||||
|
||||
def open(self):
|
||||
self.close()
|
||||
if pyserial is None:
|
||||
print("serial_dmx: pyserial not available, skip", self.path)
|
||||
return
|
||||
try:
|
||||
self.ser = pyserial.Serial(
|
||||
self.path,
|
||||
baudrate=self.baud,
|
||||
bytesize=pyserial.EIGHTBITS,
|
||||
parity=pyserial.PARITY_NONE,
|
||||
stopbits=pyserial.STOPBITS_TWO,
|
||||
timeout=0,
|
||||
write_timeout=0.5,
|
||||
)
|
||||
except Exception as e:
|
||||
print("serial_dmx open failed", self.path, e)
|
||||
self.ser = None
|
||||
|
||||
def close(self):
|
||||
if self.ser is None:
|
||||
return
|
||||
try:
|
||||
self.ser.close()
|
||||
except Exception:
|
||||
pass
|
||||
self.ser = None
|
||||
|
||||
def send(self, channels):
|
||||
"""Send one universe. ``channels`` is 512 bytes (ch1 at [0]) or a 513-byte frame."""
|
||||
if self.ser is None:
|
||||
return
|
||||
if channels is None:
|
||||
return
|
||||
if len(channels) == 513:
|
||||
frame = channels
|
||||
else:
|
||||
frame = bytearray(513)
|
||||
n = len(channels)
|
||||
if n > 512:
|
||||
n = 512
|
||||
frame[1 : 1 + n] = channels[:n]
|
||||
try:
|
||||
# DMX break ≥88 µs; pyserial send_break duration is in seconds.
|
||||
self.ser.send_break(duration=0.0001)
|
||||
sleep(0.000012) # mark after break
|
||||
self.ser.write(frame)
|
||||
except Exception as e:
|
||||
print("serial_dmx send failed", self.path, e)
|
||||
Whitespace-only changes.
@@ -0,0 +1,14 @@
|
||||
class Loader:
|
||||
|
||||
def __init__(self, pkg, dir):
|
||||
if dir == ".":
|
||||
dir = ""
|
||||
else:
|
||||
dir = dir.replace("/", ".") + "."
|
||||
if pkg and pkg != "__main__":
|
||||
dir = pkg + "." + dir
|
||||
self.p = dir
|
||||
|
||||
def load(self, name):
|
||||
name = name.replace(".", "_")
|
||||
return __import__(self.p + name, None, None, (name,)).render
|
||||
@@ -0,0 +1,21 @@
|
||||
# (c) 2014-2020 Paul Sokolovsky. MIT license.
|
||||
try:
|
||||
from uos import stat, remove
|
||||
except:
|
||||
from os import stat, remove
|
||||
from . import source
|
||||
|
||||
|
||||
class Loader(source.Loader):
|
||||
|
||||
def load(self, name):
|
||||
o_path = self.pkg_path + self.compiled_path(name)
|
||||
i_path = self.pkg_path + self.dir + "/" + name
|
||||
try:
|
||||
o_stat = stat(o_path)
|
||||
i_stat = stat(i_path)
|
||||
if i_stat[8] > o_stat[8]:
|
||||
# input file is newer, remove output to force recompile
|
||||
remove(o_path)
|
||||
finally:
|
||||
return super().load(name)
|
||||
@@ -0,0 +1,188 @@
|
||||
# (c) 2014-2019 Paul Sokolovsky. MIT license.
|
||||
from . import compiled
|
||||
|
||||
|
||||
class Compiler:
|
||||
|
||||
START_CHAR = "{"
|
||||
STMNT = "%"
|
||||
STMNT_END = "%}"
|
||||
EXPR = "{"
|
||||
EXPR_END = "}}"
|
||||
|
||||
def __init__(self, file_in, file_out, indent=0, seq=0, loader=None):
|
||||
self.file_in = file_in
|
||||
self.file_out = file_out
|
||||
self.loader = loader
|
||||
self.seq = seq
|
||||
self._indent = indent
|
||||
self.stack = []
|
||||
self.in_literal = False
|
||||
self.flushed_header = False
|
||||
self.args = "*a, **d"
|
||||
|
||||
def indent(self, adjust=0):
|
||||
if not self.flushed_header:
|
||||
self.flushed_header = True
|
||||
self.indent()
|
||||
self.file_out.write("def render%s(%s):\n" % (str(self.seq) if self.seq else "", self.args))
|
||||
self.stack.append("def")
|
||||
self.file_out.write(" " * (len(self.stack) + self._indent + adjust))
|
||||
|
||||
def literal(self, s):
|
||||
if not s:
|
||||
return
|
||||
if not self.in_literal:
|
||||
self.indent()
|
||||
self.file_out.write('yield """')
|
||||
self.in_literal = True
|
||||
self.file_out.write(s.replace('"', '\\"'))
|
||||
|
||||
def close_literal(self):
|
||||
if self.in_literal:
|
||||
self.file_out.write('"""\n')
|
||||
self.in_literal = False
|
||||
|
||||
def render_expr(self, e):
|
||||
self.indent()
|
||||
self.file_out.write('yield str(' + e + ')\n')
|
||||
|
||||
def parse_statement(self, stmt):
|
||||
tokens = stmt.split(None, 1)
|
||||
if tokens[0] == "args":
|
||||
if len(tokens) > 1:
|
||||
self.args = tokens[1]
|
||||
else:
|
||||
self.args = ""
|
||||
elif tokens[0] == "set":
|
||||
self.indent()
|
||||
self.file_out.write(stmt[3:].strip() + "\n")
|
||||
elif tokens[0] == "include":
|
||||
if not self.flushed_header:
|
||||
# If there was no other output, we still need a header now
|
||||
self.indent()
|
||||
tokens = tokens[1].split(None, 1)
|
||||
args = ""
|
||||
if len(tokens) > 1:
|
||||
args = tokens[1]
|
||||
if tokens[0][0] == "{":
|
||||
self.indent()
|
||||
# "1" as fromlist param is uPy hack
|
||||
self.file_out.write('_ = __import__(%s.replace(".", "_"), None, None, 1)\n' % tokens[0][2:-2])
|
||||
self.indent()
|
||||
self.file_out.write("yield from _.render(%s)\n" % args)
|
||||
return
|
||||
|
||||
with self.loader.input_open(tokens[0][1:-1]) as inc:
|
||||
self.seq += 1
|
||||
c = Compiler(inc, self.file_out, len(self.stack) + self._indent, self.seq)
|
||||
inc_id = self.seq
|
||||
self.seq = c.compile()
|
||||
self.indent()
|
||||
self.file_out.write("yield from render%d(%s)\n" % (inc_id, args))
|
||||
elif len(tokens) > 1:
|
||||
if tokens[0] == "elif":
|
||||
assert self.stack[-1] == "if"
|
||||
self.indent(-1)
|
||||
self.file_out.write(stmt + ":\n")
|
||||
else:
|
||||
self.indent()
|
||||
self.file_out.write(stmt + ":\n")
|
||||
self.stack.append(tokens[0])
|
||||
else:
|
||||
if stmt.startswith("end"):
|
||||
assert self.stack[-1] == stmt[3:]
|
||||
self.stack.pop(-1)
|
||||
elif stmt == "else":
|
||||
assert self.stack[-1] == "if"
|
||||
self.indent(-1)
|
||||
self.file_out.write("else:\n")
|
||||
else:
|
||||
assert False
|
||||
|
||||
def parse_line(self, l):
|
||||
while l:
|
||||
start = l.find(self.START_CHAR)
|
||||
if start == -1:
|
||||
self.literal(l)
|
||||
return
|
||||
self.literal(l[:start])
|
||||
self.close_literal()
|
||||
sel = l[start + 1]
|
||||
#print("*%s=%s=" % (sel, EXPR))
|
||||
if sel == self.STMNT:
|
||||
end = l.find(self.STMNT_END)
|
||||
assert end > 0
|
||||
stmt = l[start + len(self.START_CHAR + self.STMNT):end].strip()
|
||||
self.parse_statement(stmt)
|
||||
end += len(self.STMNT_END)
|
||||
l = l[end:]
|
||||
if not self.in_literal and l == "\n":
|
||||
break
|
||||
elif sel == self.EXPR:
|
||||
# print("EXPR")
|
||||
end = l.find(self.EXPR_END)
|
||||
assert end > 0
|
||||
expr = l[start + len(self.START_CHAR + self.EXPR):end].strip()
|
||||
self.render_expr(expr)
|
||||
end += len(self.EXPR_END)
|
||||
l = l[end:]
|
||||
else:
|
||||
self.literal(l[start])
|
||||
l = l[start + 1:]
|
||||
|
||||
def header(self):
|
||||
self.file_out.write("# Autogenerated file\n")
|
||||
|
||||
def compile(self):
|
||||
self.header()
|
||||
for l in self.file_in:
|
||||
self.parse_line(l)
|
||||
self.close_literal()
|
||||
return self.seq
|
||||
|
||||
|
||||
class Loader(compiled.Loader):
|
||||
|
||||
def __init__(self, pkg, dir):
|
||||
super().__init__(pkg, dir)
|
||||
self.dir = dir
|
||||
if pkg == "__main__":
|
||||
# if pkg isn't really a package, don't bother to use it
|
||||
# it means we're running from "filesystem directory", not
|
||||
# from a package.
|
||||
pkg = None
|
||||
|
||||
self.pkg_path = ""
|
||||
if pkg:
|
||||
p = __import__(pkg)
|
||||
if isinstance(p.__path__, str):
|
||||
# uPy
|
||||
self.pkg_path = p.__path__
|
||||
else:
|
||||
# CPy
|
||||
self.pkg_path = p.__path__[0]
|
||||
self.pkg_path += "/"
|
||||
|
||||
def input_open(self, template):
|
||||
path = self.pkg_path + self.dir + "/" + template
|
||||
return open(path)
|
||||
|
||||
def compiled_path(self, template):
|
||||
return self.dir + "/" + template.replace(".", "_") + ".py"
|
||||
|
||||
def load(self, name):
|
||||
try:
|
||||
return super().load(name)
|
||||
except (OSError, ImportError):
|
||||
pass
|
||||
|
||||
compiled_path = self.pkg_path + self.compiled_path(name)
|
||||
|
||||
f_in = self.input_open(name)
|
||||
f_out = open(compiled_path, "w")
|
||||
c = Compiler(f_in, f_out, loader=self)
|
||||
c.compile()
|
||||
f_in.close()
|
||||
f_out.close()
|
||||
return super().load(name)
|
||||
Reference in new issue
Block a user