def track_eject(self, eject_tracker: EjectTracker, already_left): """Remove one ball from count.""" ball_left = self._wait_for_ball_to_leave( ) if not already_left else None ball_activity = self.wait_for_ball_activity() # we are stable from here on eject_tracker.set_ready() count = self._entrance_count while True: if ball_left: futures = [ball_activity, ball_left] else: futures = [ball_activity] yield from Util.any(futures, loop=self.machine.clock.loop) if ball_left and ball_left.done(): ball_left = False eject_tracker.track_ball_left() self.debug_log( "Device ejected a ball. Reducing ball count by one.") self._entrance_count -= 1 count -= 1 if self._entrance_count < 0: self._entrance_count = 0 self.ball_device.log.warning("Entrance count went below 0") if ball_activity.done() and self._entrance_count > count: for _ in range(self._entrance_count - count): yield from eject_tracker.track_ball_entrance() count = self._entrance_count ball_activity = self.wait_for_ball_activity()
def _handle_late_confirm_or_missing(self, eject_request: OutgoingBall, ball_eject_process: EjectTracker, incoming_ball_at_target: IncomingBall, eject_try: int) -> Generator[int, None, bool]: ball_return_future = Util.ensure_future(ball_eject_process.wait_for_ball_return(), loop=self.machine.clock.loop) unknown_balls_future = Util.ensure_future(ball_eject_process.wait_for_ball_unknown_ball(), loop=self.machine.clock.loop) eject_success_future = incoming_ball_at_target.wait_for_confirm() timeout = self.ball_device.config['ball_missing_timeouts'][eject_request.target] / 1000 # if ball_eject_process.is_jammed(): # # ball returned. eject failed # eject_request.already_left = False # incoming_ball_at_target.did_not_arrive() # return False # assume that the ball may have skipped the target device by now incoming_ball_at_target.set_can_skip() if not eject_request.target.is_playfield(): yield from eject_request.target.ball_count_handler.wait_for_count_is_valid() if eject_success_future.done(): self.debug_log("Got eject confirm (after recounting)") yield from self._handle_eject_success(eject_request) return True else: if (yield from self._handle_playfield_timeout_confirm( eject_request, ball_return_future, unknown_balls_future, incoming_ball_at_target)): return True try: event = yield from Util.first([ball_return_future, unknown_balls_future, eject_success_future], timeout=timeout, loop=self.machine.clock.loop) except asyncio.TimeoutError: # handle lost ball incoming_ball_at_target.did_not_arrive() yield from self._failed_eject(eject_request, eject_try, True) yield from self.ball_device.lost_ejected_ball(target=eject_request.target) # ball is lost but the eject is finished -> return true return True else: if event == eject_success_future: # we eventually got eject success yield from self._handle_eject_success(eject_request) return True elif event == ball_return_future: # ball returned. eject failed self.debug_log("Ball returned. Eject failed.") eject_request.already_left = False incoming_ball_at_target.did_not_arrive() return False elif event == unknown_balls_future: # TODO: this may be an option self.debug_log("Got unknown balls. Assuming a ball returned.") incoming_ball_at_target.did_not_arrive() return False # throw an error if we got here raise AssertionError("Invalid state")
def _handle_eject_success(self, ball_eject_process: EjectTracker, eject_request: OutgoingBall): self.debug_log("Eject successful") if ball_eject_process: ball_eject_process.eject_success() yield from self.machine.events.post_async( 'balldevice_' + self.ball_device.name + '_ball_eject_success', balls=1, target=eject_request.target) '''event: balldevice_(name)_ball_eject_success
def track_eject(self, eject_tracker: EjectTracker, already_left): """Return eject_process dict.""" # count active switches while True: waiter = self.wait_for_ball_activity() try: active_switches = self._count_switches_sync() waiter.cancel() break except ValueError: yield from waiter ball_left_future = Util.ensure_future( self._wait_for_ball_to_leave(active_switches), loop=self.machine.clock.loop) if not already_left else None # all switches are stable. we are ready now eject_tracker.set_ready() jam_active_before_eject = self.is_jammed() jam_active_after_eject = False active_switches = active_switches count = len(active_switches) while True: ball_count_change = Util.ensure_future( self.wait_for_ball_count_changes(count), loop=self.machine.clock.loop) if ball_left_future: futures = [ball_count_change, ball_left_future] else: futures = [ball_count_change] yield from Util.any(futures, loop=self.machine.clock.loop) if ball_left_future and ball_left_future.done(): ball_left_future = None eject_tracker.track_ball_left() count -= 1 ball_count_change.cancel() elif ball_count_change.done(): new_count = ball_count_change.result() # check jam first if not jam_active_after_eject and not jam_active_before_eject and self.is_jammed( ): eject_tracker.track_ball_returned() jam_active_after_eject = True count += 1 if new_count > count: # TODO: add some magic to detect entrances pass if new_count > count: eject_tracker.track_unknown_balls(new_count - count) elif count > new_count: eject_tracker.track_lost_balls(count - new_count) count = new_count
def _handle_late_confirm_or_missing(self, eject_request: OutgoingBall, ball_eject_process: EjectTracker, incoming_ball_at_target: IncomingBall) -> Generator[int, None, bool]: ball_return_future = Util.ensure_future(ball_eject_process.wait_for_ball_return(), loop=self.machine.clock.loop) unknown_balls_future = Util.ensure_future(ball_eject_process.wait_for_ball_unknown_ball(), loop=self.machine.clock.loop) eject_success_future = incoming_ball_at_target.wait_for_confirm() timeout = self.ball_device.config['ball_missing_timeouts'][eject_request.target] / 1000 # assume that the ball may have skipped the target device by now incoming_ball_at_target.set_can_skip() if not eject_request.target.is_playfield(): yield from eject_request.target.ball_count_handler.wait_for_count_is_valid() if eject_success_future.done(): self.debug_log("Got eject confirm (after recounting)") yield from self._handle_eject_success(ball_eject_process, eject_request) return True else: # TODO: remove hack when moving code below yield from asyncio.sleep(0.1, loop=self.machine.clock.loop) # TODO: move this to a better location if not ball_return_future.done() and not unknown_balls_future.done() and \ eject_request.target.is_playfield(): # if target is playfield mark eject as confirmed self.debug_log("Confirming eject because target is playfield and ball did not return.") incoming_ball_at_target.ball_arrived() yield from self._handle_eject_success(ball_eject_process, eject_request) return True try: event = yield from Util.first([ball_return_future, unknown_balls_future, eject_success_future], timeout=timeout, loop=self.machine.clock.loop) except asyncio.TimeoutError: # handle lost ball incoming_ball_at_target.did_not_arrive() ball_eject_process.ball_lost() yield from self.ball_device.lost_ejected_ball(target=eject_request.target) # ball is lost but the eject is finished -> return true return True else: if event == eject_success_future: # we eventually got eject success yield from self._handle_eject_success(ball_eject_process, eject_request) return True elif event == ball_return_future: # ball returned. eject failed eject_request.already_left = False incoming_ball_at_target.did_not_arrive() ball_eject_process.ball_returned() return False elif event == unknown_balls_future: # TODO: this may be an option self.debug_log("Got unknown balls. Assuming a ball returned.") incoming_ball_at_target.did_not_arrive() ball_eject_process.ball_returned() return False else: raise AssertionError("Invalid state")