def __init__(self, bus=None, port_numbers=None, interface=0): self.backend = USBListener(self, HASSEB_USB_VENDOR, HASSEB_USB_PRODUCT, bus=bus, port_numbers=port_numbers, interface=interface)
class AsyncHassebDALIUSBDriver(HassebDALIUSBDriver, AsyncDALIDriver): """Asynchronous ``DALIDriver`` implementation for Hasseb DALI USB device. This driver is FAKE, since Hasseb does NOT support async communication. """ _pending = None def __init__(self, bus=None, port_numbers=None, interface=0): self.backend = USBListener(self, HASSEB_USB_VENDOR, HASSEB_USB_PRODUCT, bus=bus, port_numbers=port_numbers, interface=interface) def send(self, command, callback=None, **kw): data = self.construct(command) if command.response is not None: self._pending = command, callback, kw else: self._pending = None self.backend.write(data) def receive(self, data): frame = self.extract(data) if isinstance(frame, HassebDALIUSBNoDataAvailable): return elif isinstance(frame, BackwardFrame): if self._pending: command, callback, kw = self._pending callback(command.response(frame), **kw) else: logger.error("Received frame for no pending command") else: logger.error("Received frame is not BackwardFrame")
def __init__(self, bus=None, port_numbers=None, interface=0): self.backend = USBListener( self, DALI_USB_VENDOR, DALI_USB_PRODUCT, bus=bus, port_numbers=port_numbers, interface=interface )
class AsyncTridonicDALIUSBDriver(TridonicDALIUSBDriver, AsyncDALIDriver): """Asynchronous ``DALIDriver`` implementation for Tridonic DALI USB device. """ # transaction mapping _transactions = dict() def __init__(self, bus=None, port_numbers=None, interface=0): self.backend = USBListener(self, DALI_USB_VENDOR, DALI_USB_PRODUCT, bus=bus, port_numbers=port_numbers, interface=interface) def send(self, command, callback=None, **kw): data = self.construct(command) sn = struct.unpack('B', data[1])[0] self._transactions[sn] = { 'command': command, 'callback': callback, 'kw': kw } self.backend.write(data) def receive(self, data): frame = self.extract(data) sn = data[8] if isinstance(frame, ForwardFrame): self._handle_dispatch(frame) elif isinstance(frame, BackwardFrame): self._handle_response(sn, frame) elif frame is DALI_USB_NO_RESPONSE: self._handle_response(sn, None) def _handle_dispatch(self, frame): command = from_frame(frame) if self.debug: self.logger.info(str(command)) if self.dispatcher is None: if self.debug: msg = 'Ignore received command: {}'.format(command) self.logger.info(msg) return self.dispatcher(command) def _handle_response(self, sn, frame): request = self._transactions.get(sn) if not request: if self.debug: msg = 'Received response to unknown request: {}'.format(sn) self.logger.error(msg) return del self._transactions[sn] callback = request['callback'] if not callback: if self.debug: self.logger.info('No callback given for received response') return command = request['command'] if command.response: callback(command._response(frame), **request['kw']) else: callback(frame, **request['kw'])
class AsyncTridonicDALIUSBDriver(TridonicDALIUSBDriver, AsyncDALIDriver): """Asynchronous ``DALIDriver`` implementation for Tridonic DALI USB device. """ # transaction mapping _transactions = dict() def __init__(self, bus=None, port_numbers=None, interface=0): self.backend = USBListener( self, DALI_USB_VENDOR, DALI_USB_PRODUCT, bus=bus, port_numbers=port_numbers, interface=interface ) def send(self, command, callback=None, **kw): data = self.construct(command) sn = struct.unpack('B', data[1])[0] self._transactions[sn] = { 'command': command, 'callback': callback, 'kw': kw } self.backend.write(data) def receive(self, data): frame = self.extract(data) sn = data[8] if isinstance(frame, ForwardFrame): self._handle_dispatch(frame) elif isinstance(frame, BackwardFrame): self._handle_response(sn, frame) elif frame is DALI_USB_NO_RESPONSE: self._handle_response(sn, None) def _handle_dispatch(self, frame): command = from_frame(frame) if self.debug: self.logger.info(str(command)) if self.dispatcher is None: if self.debug: msg = 'Ignore received command: {}'.format(command) self.logger.info(msg) return self.dispatcher(command) def _handle_response(self, sn, frame): request = self._transactions.get(sn) if not request: if self.debug: msg = 'Received response to unknown request: {}'.format(sn) self.logger.error(msg) return del self._transactions[sn] callback = request['callback'] if not callback: if self.debug: self.logger.info('No callback given for received response') return command = request['command'] if command.response: callback(command._response(frame), **request['kw']) else: callback(frame, **request['kw'])