Files
@ bfee787d7b5c
Branch filter:
Location: light9/light9/collector/output.py
bfee787d7b5c
9.8 KiB
text/x-python
straighten out proxying and nginx+vite mixing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | from rdflib import URIRef
import socket
import struct
import time
import usb.core
import logging
from twisted.internet import threads, reactor, task
from light9.metrics import metrics
log = logging.getLogger('output')
logAllDmx = logging.getLogger('output.allDmx')
class Output(object):
"""
send a binary buffer of values to some output device. Call update
as often as you want- the result will be sent as soon as possible,
and with repeats as needed to outlast hardware timeouts.
This base class doesn't ever call _write. Subclasses below have
strategies for that.
"""
uri: URIRef
def __init__(self, uri: URIRef):
self.uri = uri
self._currentBuffer = b''
if log.isEnabledFor(logging.DEBUG):
self._lastLoggedMsg = ''
task.LoopingCall(self._periodicLog).start(1)
def reconnect(self):
pass
def shortId(self) -> str:
"""short string to distinguish outputs"""
return self.uri.rstrip('/').rsplit('/')[-1]
def update(self, buf: bytes) -> None:
"""caller asks for the output to be this buffer"""
self._currentBuffer = buf
def _periodicLog(self):
msg = '%s: %s' % (self.shortId(), ' '.join(map(str,
self._currentBuffer)))
if msg != self._lastLoggedMsg:
log.debug(msg)
self._lastLoggedMsg = msg
def _write(self, buf: bytes) -> None:
"""
write buffer to output hardware (may be throttled if updates are
too fast, or repeated if they are too slow)
"""
pass
def crash(self):
log.error('unrecoverable- exiting')
reactor.crash()
class DummyOutput(Output):
def __init__(self, uri, **kw):
super().__init__(uri)
class BackgroundLoopOutput(Output):
"""Call _write forever at 20hz in background threads"""
rate: float
def __init__(self, uri, rate=22):
super().__init__(uri)
self.rate = rate
self._currentBuffer = b''
self._loop()
def _loop(self):
start = time.time()
sendingBuffer = self._currentBuffer
def done(worked):
metrics('write_success', output=self.shortId()).incr()
reactor.callLater(max(0, start + 1 / self.rate - time.time()),
self._loop)
def err(e):
metrics('write_fail', output=self.shortId()).incr()
log.error(e)
reactor.callLater(.2, self._loop)
d = threads.deferToThread(self._write, sendingBuffer)
d.addCallbacks(done, err)
class FtdiDmx(BackgroundLoopOutput):
def __init__(self, uri, lastDmxChannel, rate=22):
super().__init__(uri)
self.lastDmxChannel = lastDmxChannel
from .dmx_controller_output import OpenDmxUsb
self.dmx = OpenDmxUsb()
def _write(self, buf):
with metrics('write', output=self.shortId()).time():
if not buf:
logAllDmx.debug('%s: empty buf- no output', self.shortId())
return
# ok to truncate the last channels if they just went
# to 0? No it is not. DMX receivers don't add implicit
# zeros there.
buf = bytes([0]) + buf[:self.lastDmxChannel]
if logAllDmx.isEnabledFor(logging.DEBUG):
# for testing fps, smooth fades, etc
logAllDmx.debug('%s: %s...' %
(self.shortId(), ' '.join(map(str, buf[:32]))))
self.dmx.send_dmx(buf)
class ArtnetDmx(BackgroundLoopOutput):
# adapted from https://github.com/spacemanspiff2007/PyArtNet/blob/master/pyartnet/artnet_node.py (gpl3)
def __init__(self, uri, host, port, rate):
"""sends UDP messages to the given host/port"""
super().__init__(uri, rate)
packet = bytearray()
packet.extend(map(ord, "Art-Net"))
packet.append(0x00) # Null terminate Art-Net
packet.extend([0x00, 0x50]) # Opcode ArtDMX 0x5000 (Little endian)
packet.extend([0x00, 0x0e]) # Protocol version 14
self.base_packet = packet
self.sequence_counter = 255
self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def _write(self, buf):
with metrics('write', output=self.shortId()).time():
if not buf:
logAllDmx.debug('%s: empty buf- no output', self.shortId())
return
if logAllDmx.isEnabledFor(logging.DEBUG):
# for testing fps, smooth fades, etc
logAllDmx.debug('%s: %s...' %
(self.shortId(), ' '.join(map(str, buf[:32]))))
if self.sequence_counter:
self.sequence_counter += 1
if self.sequence_counter > 255:
self.sequence_counter = 1
packet = self.base_packet[:]
packet.append(self.sequence_counter) # Sequence,
packet.append(0x00) # Physical
universe_nr = 0
packet.append(universe_nr & 0xFF) # Universe LowByte
packet.append(universe_nr >> 8 & 0xFF) # Universe HighByte
packet.extend(struct.pack(
'>h', len(buf))) # Pack the number of channels Big endian
packet.extend(buf)
self._socket.sendto(packet, ('127.0.0.1', 6454))
class Udmx(BackgroundLoopOutput):
_reconnections = scales.IntStat('reconnections')
_connected = scales.IntStat('connected')
def __init__(self, uri, bus, address, lastDmxChannel, rate=22):
self.bus = bus
self.address = address
self.lastDmxChannel = lastDmxChannel
self.dev = None
super().__init__(uri, rate=rate)
self._errStats = scales.collection(self.statPath + '/write',
scales.IntStat('overflow'),
scales.IntStat('ioError'),
scales.IntStat('pipeError'))
self.reconnect()
def shortId(self) -> str:
return super().shortId() + f'_bus={self.bus}'
def reconnect(self):
self._connected = 0
from pyudmx import pyudmx
self.dev = pyudmx.uDMXDevice()
if not self.dev.open(bus=self.bus, address=self.address):
raise ValueError("dmx open failed")
log.info(f'opened {self.dev}')
self._connected = 1
self._reconnections += 1
#def update(self, buf:bytes):
# self._write(buf)
#def _loop(self):
# pass
def _write(self, buf):
if not self.dev:
log.info('%s: trying to connect', self.shortId())
raise ValueError()
with metrics('write', output=self.shortId()).time():
try:
if not buf:
logAllDmx.debug('%s: empty buf- no output', self.shortId())
return
# ok to truncate the last channels if they just went
# to 0? No it is not. DMX receivers don't add implicit
# zeros there.
buf = buf[:self.lastDmxChannel]
if logAllDmx.isEnabledFor(logging.DEBUG):
# for testing fps, smooth fades, etc
logAllDmx.debug(
'%s: %s...' %
(self.shortId(), ' '.join(map(str, buf[:32]))))
sent = self.dev.send_multi_value(1, buf)
if sent != len(buf):
raise ValueError("incomplete send")
except ValueError:
self.reconnect()
raise
except usb.core.USBError as e:
# not in main thread
if e.errno == 75:
self._errStats.overflow += 1
return
if e.errno == 5: # i/o err
self._errStats.ioError += 1
return
if e.errno == 32: # pipe err
self._errStats.pipeError += 1
return
msg = 'usb: sending %s bytes to %r; error %r' % (len(buf),
self.uri, e)
log.warn(msg)
if e.errno == 13: # permissions
return self.crash()
if e.errno == 19: # no such dev; usb hw restarted
self.reconnect()
return
raise
'''
# the code used in 2018 and before
class UdmxOld(BackgroundLoopOutput):
def __init__(self, uri, bus):
from light9.io.udmx import Udmx
self._dev = Udmx(bus)
super().__init__(uri)
def _write(self, buf: bytes):
try:
if not buf:
return
self.dev.SendDMX(buf)
except usb.core.USBError as e:
# not in main thread
if e.errno != 75:
msg = 'usb: sending %s bytes to %r; error %r' % (
len(buf), self.uri, e)
log.warn(msg)
raise
# out of date
class EnttecDmx(BackgroundLoopOutput):
stats = scales.collection('/output/enttecDmx', scales.PmfStat('write', recalcPeriod=1),
scales.PmfStat('update', recalcPeriod=1))
def __init__(self, uri, devicePath='/dev/dmx0', numChannels=80):
sys.path.append("dmx_usb_module")
from dmx import Dmx
self.dev = Dmx(devicePath)
super().__init__(uri)
@stats.update.time()
def update(self, values):
# I was outputting on 76 and it was turning on the light at
# dmx75. So I added the 0 byte. No notes explaining the footer byte.
self.currentBuffer = '\x00' + ''.join(map(chr, values)) + "\x00"
@stats.write.time()
def _write(self, buf):
self.dev.write(buf)
'''
|