def check_floor(floor):
  
  if (floor < 0):     #There is no order on a floor below 0
    return False
    
  if (orderlist_empty()):   #If there is no orders in the orderlist
    return False
    
  else:  #If there are orders in the orderlist
    for key in shared.order_map:
      order = shared.order_map[key]
      
      if (order.completed):     #If the order is completed, do nothing
        continue
      
      if not (order.assigned):  #If the order is not assigned to the specific elevator
        continue
      
      if (order.assigned_to_id != shared.get_local_elevator_ID()):  #If the order assigned to an ID is not the same as the local_elevator ID
        continue
      
      if (order.direction == shared.NODIR) and (order.creatorID != shared.get_local_elevator_ID()): #If there is a button_command and its not the local_elevator ID that took the order
        continue
      
      if (order.floor == floor):
        return True
    return False
Beispiel #2
0
def main():
  initialization.Initialization()
  
  
  
  Io_thread = threading.Thread(target = Io)     #Creates thread for IO
  Io_thread.daemon = True
  Io_thread.start()
  
  Statemachine_thread = threading.Thread(target = Statemachine)     #Creates thread for Statemachine
  Statemachine_thread.daemon = True
  Statemachine_thread.start()
  
  
  network.network_threads()  

  orderlist.clean_thread()

  while True:
    cmd = raw_input("Type a command")
    if cmd == "list_orders":
      for key in shared.order_map:
        print "Order:", shared.order_map[key].__dict__
    elif cmd == "list_elevators":
      for key in shared.elevators:
        print "Elevator:", shared.elevators[key].__dict__
    elif cmd == "my_id":
      print "My id is", shared.get_local_elevator_ID()
  return
def controller(floor, direction):
  shared.target_floor = -1
  
  best_cost = 999999999
  best_order = None
  for key in shared.order_map:  #goes through the dictionary with all the orders
    order = shared.order_map[key]
    if (order.completed): #if order completed, continue
      continue
    if not (order.assigned) or (order.assigned_to_id != shared.get_local_elevator_ID()):  #if the order is not assigned or the local elevator id is not equal to the assigned id, continue
      continue
    
    if (order.floor == floor) and not (orderlist.should_complete(order, floor)):  #if the floor is correct but the orderlist.should_complete(,) returns False, then continue
      continue
      
    cost = orderlist.cost_func(order, shared.local_elevator)  #set cost by calling the cost function with right order and local elevator
    
    if (cost < best_cost):  #if the cost is smallers then best_cost we set both best_cost and best_order
      best_cost = cost
      best_order = order
      
  if (best_order == None):  #if best_order equals to None then retruns False
    return False
  
  shared.target_floor = best_order.floor  #set target floor to the best_order's floor

  if (shared.target_floor < 0) or (shared.target_floor >= shared.N_FLOORS): #checking for something that should not happen, target floor < 0 or target floor > 4, elevator out of reach
    print "Should not be reached"
    set_speed(0)
    shared.target_dir = shared.NODIR
    return False
  
  if (shared.target_floor > floor): #checks if we have reached target floor, if not we set the target direction in the right direction
    set_speed(300)
    shared.target_dir = shared.UP
    return True
    
  
  elif (shared.target_floor < floor): #checks if we have reached target floor, if not we set the target direction in the right direction
    set_speed(-300)
    shared.target_dir = shared.DOWN
    return True
    
  elif (shared.target_floor == floor):  #target floor equals to floor
    if (driver.elev.elev_get_floor_sensor_signal() == -1):  #if jet not reached floor drive down to floor
      set_speed(-300)
      shared.target_dir = shared.NODIR  #sets target direction to no direction
      return True
    else: #in right floor
      set_speed(0)  #shall stop
      shared.target_dir = shared.NODIR  #sets target direction to no direction
      return True
  
  print "Should never reach this"
  return False
def add_order(floor, direction):
  for key in shared.order_map:
    order = shared.order_map[key]
    if (order.completed):
      continue
    
    if (order.floor == floor) and (order.direction == direction): #If the order is already in the list
      return
    
  new_order = shared.Order(shared.get_local_elevator_ID(), floor, direction, False, -1, False, 0)
  assign_order(new_order)     #Calls assign to find the best elevator
  
  shared.order_map[new_order.ID] = new_order
  print "New order with floor:",new_order.floor," and direction:",new_order.direction
  return
def set_lights():
  found_commands = {}
  found_up = {}
  found_down = {}
  
  for key in shared.order_map:
    order = shared.order_map[key]
    if (order.completed):
      continue
    
    if (order.direction == shared.NODIR) and (order.creatorID != shared.get_local_elevator_ID()):   #Should not set_lights if there is button_commands on another elevator
      continue
    
    if (order.direction == shared.NODIR):
      found_commands[order.floor] = True
      
    elif (order.direction == shared.UP) and (order.floor < shared.N_FLOORS-1):
      found_up[order.floor] = True
      
    elif (order.direction == shared.DOWN) and (order.floor > 0):
      found_down[order.floor] = True    
   
  for floor in range(shared.N_FLOORS):
    if (floor in found_commands):
      driver.elev.elev_set_button_lamp(shared.BUTTON_COMMAND, floor, 1)
    else:
      driver.elev.elev_set_button_lamp(shared.BUTTON_COMMAND, floor, 0)
  
  for floor in range(shared.N_FLOORS-1):
    if (floor in found_up):
      driver.elev.elev_set_button_lamp(shared.BUTTON_CALL_UP, floor, 1)
    else:
      driver.elev.elev_set_button_lamp(shared.BUTTON_CALL_UP, floor, 0)
      
  for floor in range(1, shared.N_FLOORS):
    if (floor in found_down):
      driver.elev.elev_set_button_lamp(shared.BUTTON_CALL_DOWN, floor, 1)
    else:
      driver.elev.elev_set_button_lamp(shared.BUTTON_CALL_DOWN, floor, 0)
  
  driver.elev.elev_set_floor_indicator(shared.local_elevator.last_floor)
  return
def connection_validator(): #checks if we lost a connection woth one of the elevators
  lost_ids = []
  for ID in shared.elevators:
    if (ID == shared.get_local_elevator_ID()):  #checks the local elevator
      continue  #can't loose the local elevator
    elevator_state = shared.elevators[ID]
    
    if ((time.time() - elevator_state.last_ping) > 4.0): #if we don't hear from a elevator for over 4.0 seconds we have lost it, by shut down or bad internet connection or no internet connection at all
      print "Lost elevator with id:", ID, "which is ", (time.time() - elevator_state.last_ping), " seconds old"
      lost_ids.append(ID) #puts all the lost elevators ips in one list
  for ID in lost_ids:
    del shared.elevators[ID]  #deletes the lost elevators
    
  for ID in lost_ids: #checks through the lost elevators if we have to reassign some of the orders the lost elevator had
    for key in orderlist.get_order_map():
        order = orderlist.get_order_map()[key]
        if (order.completed): #if the order is completed we shall ont reassign that order
          continue
        if not (order.assigned):  #the order is not given to anyone
          continue
        if (order.direction == shared.NODIR): #checks if the we have a local command, "BUTTON_COMMANDS"
          continue  #Can't reassign command orders
        if (order.assigned_to_id == ID):  #if the assigned ID and the elevator ID is the same 
          orderlist.assign_order(order) #we reassign the orders