async def shutdown(self): if not self._run_task: return self._logger.info("Shutting down.") await self._usb_handler.send(to_ascii('C,0\n')) self._run_task.cancel() self._run_task = None self._logger.info("Shutdown complete.")
async def initialize(self, usb_handler: USBHandler): self.logger.debug("Initializing MDB sniffer.") self.usb_handler = usb_handler status = await usb_handler.sendread(to_ascii('X,1\n'), 'x') if status != 'x,ACK': raise RuntimeError(f"Unable to start MDB sniffer, got {status}") self.initialized = True self.logger.debug("Sniffer initialized") self.message_queue = usb_handler.listen('x')
async def shutdown(self): if not self.run_task: return self.logger.info('Shutting down.') self.usb_handler.unlisten('x') await self.usb_handler.send(to_ascii('X,0\n')) self.run_task.cancel() self.run_task = None self.logger.info('Shutdown complete.')
async def initialize(self, usb_handler: USBHandler): self.logger.debug("Initializing MDB sniffer.") self.usb_handler = usb_handler status = await usb_handler.sendread(to_ascii('X,1\n'), 'x') if status != 'x,ACK': self.logger.warning('Got something other than ACK: %s', status) self.initialized = True self.logger.debug("Sniffer initialized") self.message_queue = usb_handler.listen('x')
async def initialize(self, usb_handler: USBHandler): self._logger.info("Initializing") self._usb_handler = usb_handler await usb_handler.sendread(to_ascii('C,0\n'), 'c') self._queue = usb_handler.listen('c') await usb_handler.send(to_ascii('C,SETCONF,mdb-addr=0x10\n')) await usb_handler.send(to_ascii('C,SETCONF,mdb-currency-code=0x1840\n') ) await usb_handler.send(to_ascii('C,1\n')) responses = [] for _ in range(3): responses.append(await self._queue.get()) self._queue.task_done() expected_responses = ['c,SET,OK', 'c,SET,OK', 'c,STATUS,ONLINE'] if responses != expected_responses: raise RuntimeError('Cashless slave did not correctly initialized, ' f'expected {expected_responses}, got ' f'{responses}.') self._logger.info("Finished initializing.") self._initialized = True
async def _run(self): while True: message = await self._queue.get() self._logger.info('Got message: %s', message) if message == 'c,STATUS,ENABLED': self._logger.debug('VMC ready for session, starting.') await self._usb_handler.send(to_ascii('C,START,1.00\n')) elif message == 'c,VEND,SUCCESS': # Vend was successful, tell the websocket? self._logger.info('Vend successful.') elif message.startswith('c,STATUS,VEND'): # Got a request to vend, tell the websocket. product_id, amount = message.split(',')[3:5] self._logger.info('Got vend request for product %s.', product_id) # Just tell it to reject for now. await self._usb_handler.send(to_ascii('C,VEND,0\n')) elif message.startswith('c,ERR'): # This needs some work, need to decide what happens. self._logger.warning('Got an error message: %s', message) else: self._logger.info('Got an unhandled message: %s', message) self._queue.task_done()
async def initialize(self, usb_handler: USBHandler, bill_validator: BillValidator, coin_acceptor: CoinAcceptor, bus_reset=True) -> None: self.logger.info("Initializing MDB Master.") self.usb_handler = usb_handler self.bill_validator = bill_validator self.coin_acceptor = coin_acceptor self.logger.debug('Enabling Master driver.') status = None async with self.lock: status = await self.usb_handler.sendread(to_ascii('M,1\n'), 'm') if status != 'm,ACK': raise RuntimeError('Unable to start master mode on MDB board.') self.initialized = True if bus_reset: self.logger.debug('Bus-reseting peripherals.') await self.send('R,RESET\n') # The extra time is how long the bus reset takes. await asyncio.sleep(SETUP_TIME_SECONDS + 0.1) self.logger.info('Initializing MDB peripherals.') await asyncio.gather(bill_validator.initialize(self, not bus_reset), coin_acceptor.initialize(self, not bus_reset))
async def sendread(self, message: str, prefix: str) -> str: assert self.initialized async with self.lock: return await self.usb_handler.sendread(to_ascii(message), prefix)
async def send(self, message: str) -> None: assert self.initialized async with self.lock: await self.usb_handler.send(to_ascii(message))