def test_submit_courier_assigned(self): """Test to verify how a user submits and order and doesn't cancel since a courier is assigned""" # Constants random.seed(666) # Services env = Environment(initial_time=hour_to_sec(12)) dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy()) # Create a user, have it submit an order immediately and after some minutes, assign a courier user = User(cancellation_policy=self.cancellation_policy, dispatcher=dispatcher, env=env) user.submit_order_event( order_id=self.order_id, pick_up_at=self.pick_up_at, drop_off_at=self.drop_off_at, placement_time=self.placement_time, expected_drop_off_time=self.expected_drop_off_time, preparation_time=self.preparation_time, ready_time=self.ready_time) env.process(TestsDispatcher.assign_courier(user, env, dispatcher)) env.run(until=hour_to_sec(13)) # Verify order is created but not canceled because a courier was assigned self.assertTrue(user.order) self.assertIsNotNone(user.order.courier_id) self.assertIsNone(user.order.cancellation_time) self.assertEqual(dispatcher.assigned_orders, {self.order_id: user.order}) self.assertEqual(dispatcher.unassigned_orders, {}) self.assertEqual(user.condition, 'waiting')
def test_submit_wait_for_order(self, *args): """Test to verify how a user submits an order but doesn't cancel even without courier, deciding to wait""" # Constants random.seed(157) # Services env = Environment(initial_time=hour_to_sec(12)) dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy()) # Create a user and have it submit an order immediately user = User(cancellation_policy=self.cancellation_policy, dispatcher=dispatcher, env=env) user.submit_order_event( order_id=self.order_id, pick_up_at=self.pick_up_at, drop_off_at=self.drop_off_at, placement_time=self.placement_time, expected_drop_off_time=self.expected_drop_off_time, preparation_time=self.preparation_time, ready_time=self.ready_time) env.run(until=hour_to_sec(13)) # Verify order is created but not canceled, disregarding the lack of a courier self.assertTrue(user.order) self.assertIsNone(user.order.courier_id) self.assertIsNone(user.order.cancellation_time) self.assertEqual(dispatcher.unassigned_orders, {self.order_id: user.order}) self.assertEqual(user.condition, 'waiting')
def __init__(self): self.out = voiceoutput.OsXVoiceOutput() self.inp = voiceinput.GoogleVoiceInput(self.out) self.voost = boost.Boost(MY_MOVEHUB_ADD, mode=MY_MOVEHUB_MODE, hci=MY_BTCTRLR_HCI) self.user = User(self.out, self.inp) self.boost_inited = self.init_boost() if (self.boost_inited): self.user_inited = self.init_user() else: self.user_inited = False
def test_orders_dropped_off_event(self): """Test to verify the mechanics of orders being dropped off""" # Constants initial_time = hour_to_sec(14) on_time = time(14, 0, 0) off_time = time(16, 0, 0) # Services env = Environment(initial_time=initial_time) dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy()) # Creates an order and sends the picked up event order = Order(order_id=45, user=User(env=env)) dispatcher.assigned_orders[order.order_id] = order courier = Courier(on_time=on_time, off_time=off_time) dispatcher.orders_dropped_off_event(orders={order.order_id: order}, courier=courier) env.run(until=initial_time + hour_to_sec(1)) # Verify order properties are modified and it is allocated correctly self.assertEqual(order.state, 'dropped_off') self.assertEqual(order.drop_off_time, sec_to_time(initial_time)) self.assertIn(order.order_id, dispatcher.fulfilled_orders.keys()) self.assertEqual(dispatcher.assigned_orders, {}) self.assertIn(order.order_id, courier.fulfilled_orders)
def _new_users_procedure(self, orders_info: List[Dict[str, Any]]): """Method to establish how a new user is created in the World""" for order_info in orders_info: user = User(env=self.env, dispatcher=self.dispatcher, cancellation_policy=USER_CANCELLATION_POLICIES_MAP[ settings.USER_CANCELLATION_POLICY], user_id=order_info['order_id']) user.submit_order_event( order_id=order_info['order_id'], pick_up_at=Location(lat=order_info['pick_up_lat'], lng=order_info['pick_up_lng']), drop_off_at=Location(lat=order_info['drop_off_lat'], lng=order_info['drop_off_lng']), placement_time=order_info['placement_time'], expected_drop_off_time=order_info['expected_drop_off_time'], preparation_time=order_info['preparation_time'], ready_time=order_info['ready_time']) self.users.append(user)
def test_cancel_order_event(self): """Test to verify how the dispatcher cancels an order after certain time""" random.seed(741) # Services env = Environment(initial_time=hour_to_sec(12)) dispatcher = Dispatcher( env=env, cancellation_policy=self.dispatcher_cancellation_policy, matching_policy=DummyMatchingPolicy() ) # Create a user and have it submit an order immediately, avoiding user cancellation user = User(cancellation_policy=self.cancellation_policy, dispatcher=dispatcher, env=env) user.submit_order_event( order_id=self.order_id, pick_up_at=self.pick_up_at, drop_off_at=self.drop_off_at, placement_time=self.placement_time, expected_drop_off_time=self.expected_drop_off_time, preparation_time=self.preparation_time, ready_time=self.ready_time ) env.run(until=hour_to_sec(14)) # Verify order is canceled by the dispatcher self.assertTrue(user.order) self.assertIsNone(user.order.courier_id) self.assertIsNotNone(user.order.cancellation_time) self.assertEqual(dispatcher.unassigned_orders, {}) self.assertIn(self.order_id, dispatcher.canceled_orders.keys()) self.assertEqual( user.order.cancellation_time, ( datetime.combine(date.today(), self.preparation_time) + timedelta(seconds=settings.DISPATCHER_WAIT_TO_CANCEL) ).time() ) self.assertEqual(user.condition, 'canceled')
def authenticate(self): usrs = User.get_users_data() if request.method == "POST" or True: result = request.form usr = result['username'] passwd = result['password'] for e in usrs: if e["User"] == usr and e["Pass"] == passwd: self.add_url_rule('/viz', view_func=self.visualizer) return str(e["id"]) return str(-1)
def test_order_not_canceled(self): """ Test to verify that the dispatcher doesn't cancel an order if it has a courier assigned. The user doesn't see a courier but decides not to cancel. """ random.seed(192) # Services env = Environment(initial_time=hour_to_sec(12)) dispatcher = Dispatcher( env=env, cancellation_policy=self.dispatcher_cancellation_policy, matching_policy=DummyMatchingPolicy() ) # Create a user, have it submit an order immediately and after some minutes, assign a courier. # Courier is assigned after user cancellation time has expired user = User(cancellation_policy=self.cancellation_policy, dispatcher=dispatcher, env=env) user.submit_order_event( order_id=self.order_id, pick_up_at=self.pick_up_at, drop_off_at=self.drop_off_at, placement_time=self.placement_time, expected_drop_off_time=self.expected_drop_off_time, preparation_time=self.preparation_time, ready_time=self.ready_time ) env.process(self.assign_courier(user, env, dispatcher)) env.run(until=hour_to_sec(13)) # Verify order is created but not canceled because a courier was assigned self.assertTrue(user.order) self.assertIsNotNone(user.order.courier_id) self.assertIsNone(user.order.cancellation_time) self.assertEqual(dispatcher.assigned_orders, {self.order_id: user.order}) self.assertEqual(dispatcher.unassigned_orders, {}) self.assertEqual(user.condition, 'waiting')
def test_submit_cancel_order(self): """Test to verify how a user submits and decides to cancel an order""" # Constants random.seed(666) initial_time = hour_to_sec(12) time_delta = min_to_sec(10) # Services env = Environment(initial_time=initial_time) dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy()) # Create a user and have it submit an order immediately user = User(cancellation_policy=self.cancellation_policy, dispatcher=dispatcher, env=env) user.submit_order_event( order_id=self.order_id, pick_up_at=self.pick_up_at, drop_off_at=self.drop_off_at, placement_time=self.placement_time, expected_drop_off_time=self.expected_drop_off_time, preparation_time=self.preparation_time, ready_time=self.ready_time) env.run(until=initial_time + time_delta) # Verify order is created and canceled due to a courier not being assigned self.assertTrue(user.order) self.assertIsNone(user.order.courier_id) self.assertIsNotNone(user.order.cancellation_time) self.assertEqual(dispatcher.unassigned_orders, {}) self.assertIn(self.order_id, dispatcher.canceled_orders.keys()) self.assertEqual( user.order.cancellation_time, (datetime.combine(date.today(), self.placement_time) + timedelta(seconds=settings.USER_WAIT_TO_CANCEL)).time()) self.assertEqual(user.condition, 'canceled')
def test_notify_event_reject_picking_up(self, osrm): """Test to evaluate how a courier handles a notification while picking up and rejects it""" # Constants random.seed(4747474) on_time = time(12, 0, 0) off_time = time(15, 0, 0) # Services env = Environment(initial_time=hour_to_sec(12) + min_to_sec(12)) dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy()) # Creates a courier with low acceptance rate, an active route and in state of picking up. # Sends a new instruction, composed of a single new order active_order = Order( order_id=self.order_id, drop_off_at=self.drop_off_at, pick_up_at=self.pick_up_at, placement_time=self.placement_time, expected_drop_off_time=self.expected_drop_off_time, preparation_time=self.preparation_time, ready_time=self.ready_time, courier_id=self.courier_id, user=User(env=env)) dispatcher.assigned_orders[active_order.order_id] = active_order new_order = Order(order_id=17, drop_off_at=Location(lat=4.694627, lng=-74.038886), pick_up_at=self.pick_up_at, placement_time=self.placement_time, expected_drop_off_time=self.expected_drop_off_time, preparation_time=self.preparation_time, ready_time=self.ready_time, user=User(env=env)) dispatcher.unassigned_orders[new_order.order_id] = new_order courier = Courier( acceptance_policy=self.acceptance_policy, dispatcher=dispatcher, env=env, movement_evaluation_policy=self.movement_evaluation_policy, movement_policy=self.movement_policy, courier_id=self.courier_id, vehicle=self.vehicle, location=active_order.pick_up_at, acceptance_rate=0.01, active_route=Route(orders={self.order_id: active_order}, stops=[ Stop(location=self.pick_up_at, position=0, orders={self.order_id: active_order}, type=StopType.PICK_UP, visited=False), Stop(location=self.drop_off_at, position=1, orders={self.order_id: active_order}, type=StopType.DROP_OFF, visited=False) ]), on_time=on_time, off_time=off_time) instruction = Stop(location=new_order.drop_off_at, position=1, orders={new_order.order_id: new_order}, type=StopType.DROP_OFF, visited=False) notification = Notification(courier=courier, instruction=instruction) courier.state.interrupt() courier.active_stop = courier.active_route.stops[0] courier.state = env.process( courier._picking_up_state( orders={active_order.order_id: active_order})) env.process(courier.notification_event(notification)) env.run(until=hour_to_sec(14)) # Asserts: # - the courier didn't fulfill the new order, # - fulfilled the active order and # - is at a different start location. self.assertIsNone(new_order.pick_up_time) self.assertIsNone(new_order.drop_off_time) self.assertIsNone(new_order.courier_id) self.assertIn(courier.courier_id, new_order.rejected_by) self.assertIn(new_order.order_id, courier.rejected_orders) self.assertEqual(new_order.state, 'unassigned') self.assertIsNotNone(active_order.pick_up_time) self.assertIsNotNone(active_order.drop_off_time) self.assertEqual(active_order.courier_id, courier.courier_id) self.assertTrue(active_order.pick_up_time < active_order.drop_off_time) self.assertEqual(active_order.state, 'dropped_off') self.assertIsNone(courier.active_route) self.assertIsNone(courier.active_stop) self.assertNotEqual(courier.location, self.start_location) self.assertEqual(dispatcher.fulfilled_orders, {active_order.order_id: active_order}) self.assertEqual(dispatcher.unassigned_orders, {new_order.order_id: new_order}) self.assertEqual(courier.condition, 'idle') self.assertIn(courier.courier_id, dispatcher.idle_couriers.keys())
def test_notify_event_accept_idle(self, osrm): """Test to evaluate how a courier handles a notification while being idle and accepts it""" # Constants random.seed(126) initial_time = hour_to_sec(12) time_delta = min_to_sec(40) on_time = time(12, 0, 0) off_time = time(13, 0, 0) # Services env = Environment(initial_time=initial_time) dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy()) # Creates a courier with high acceptance rate and immediately send a new instruction, composed of a single order courier = Courier( acceptance_policy=self.acceptance_policy, dispatcher=dispatcher, env=env, movement_evaluation_policy=self.movement_evaluation_policy, movement_policy=self.movement_policy, courier_id=self.courier_id, vehicle=self.vehicle, location=self.start_location, acceptance_rate=0.99, on_time=on_time, off_time=off_time) order = Order(order_id=self.order_id, drop_off_at=self.drop_off_at, pick_up_at=self.pick_up_at, placement_time=self.placement_time, expected_drop_off_time=self.expected_drop_off_time, preparation_time=self.preparation_time, ready_time=self.ready_time, user=User(env=env)) dispatcher.unassigned_orders[order.order_id] = order instruction = Route(orders={self.order_id: order}, stops=[ Stop(location=self.pick_up_at, position=0, orders={self.order_id: order}, type=StopType.PICK_UP, visited=False), Stop(location=self.drop_off_at, position=1, orders={self.order_id: order}, type=StopType.DROP_OFF, visited=False) ]) notification = Notification(courier=courier, instruction=instruction) env.process(courier.notification_event(notification)) env.run(until=initial_time + time_delta) # Asserts that the courier fulfilled the route and is at a different start location self.assertIsNotNone(order.pick_up_time) self.assertIsNotNone(order.drop_off_time) self.assertEqual(order.courier_id, courier.courier_id) self.assertTrue(order.pick_up_time < order.drop_off_time) self.assertIsNone(courier.active_route) self.assertIsNone(courier.active_stop) self.assertEqual(dispatcher.fulfilled_orders, {order.order_id: order}) self.assertEqual(order.state, 'dropped_off') self.assertNotEqual(courier.location, self.start_location) self.assertEqual(courier.condition, 'idle') self.assertIn(courier.courier_id, dispatcher.idle_couriers.keys())
class VoostTalk: def __init__(self): self.out = voiceoutput.OsXVoiceOutput() self.inp = voiceinput.GoogleVoiceInput(self.out) self.voost = boost.Boost(MY_MOVEHUB_ADD, mode=MY_MOVEHUB_MODE, hci=MY_BTCTRLR_HCI) self.user = User(self.out, self.inp) self.boost_inited = self.init_boost() if (self.boost_inited): self.user_inited = self.init_user() else: self.user_inited = False def init_boost(self): self.out.output("Will initialize lego boost. Please turn on lego") init_status = self.voost.init_hub() if (init_status is None): self.out.output("Unable to start Lego Boost. Unknown Error") return False elif (init_status.get('errStr', None) is not None): self.out.output("Unable to start Lego Boost. Error: {}".format( init_status['errStr'])) return False self.out.output("Voost device initialized.") return True def init_user(self): time.sleep(1) self.user.init(prompt=True) if (self.user.username is not None): self.output_instructions() self.listen_to_voice_and_act() return True def output_instructions(self): if (not EXPERT_MODE): self.out.output("I can act on the following commands") time.sleep(0.5) self.out.output("First command is {}".format(robot.GO_FORWARD_STR)) time.sleep(0.5) self.out.output("Second command is {}".format(robot.GO_BACK_STR)) time.sleep(0.5) self.out.output("Third command is {}".format(robot.STOP_LEGO_STR)) def process_voice_command(self, text_said): retval = -2 if (text_said is not None): if (text_said.lower().startswith(robot.GO_FORWARD_STR.lower())): self.voost.go_forward_command() retval = 0 elif (text_said.lower().startswith(robot.GO_BACK_STR.lower())): self.voost.go_back_command() retval = 0 elif (text_said.lower().startswith(robot.STOP_LEGO_STR.lower())): self.out.output("Stopping Lego") self.voost.try_stop_movehub() self.out.output("Stoped Lego. Good bye {}".format( self.user.username)) retval = -1 else: self.out.output( "Sorry did not understand the command. Please try again") retval = 0 return retval def listen_to_voice_and_act(self): while (True): self.out.output("Hey {}. Tell me what to do".format( self.user.username)) text_said = self.inp.get_voice_command() ret = self.process_voice_command(text_said) if (ret < 0): break time.sleep(5)