func turn_off_lights() { if queue.Get_Orders()[0] < 0 { driver.Set_button_lamp(def.BtnDown, driver.Get_cur_floor(), 0) } else { driver.Set_button_lamp(def.BtnUp, driver.Get_cur_floor(), 0) } }
func Run_elev(outgoingMsg chan def.Message) { for { update_current_floor() //If we have orders and we are not between floors if (len(queue.Get_Orders()) > 0) && (driver.Get_cur_floor() != -1) { set_direction(queue.Get_Orders()[0], driver.Get_cur_floor()) if arrived_at_destination() { driver.Set_motor_dir(0) if handleOrders.ImConnected { send_order_complete_msg(queue.Get_Orders()[0], outgoingMsg) } else { turn_off_lights() } //fmt.Printf("%sOrder to floor %d deleted, current floor = %d\n",def.ColB,queue.Get_Orders()[0],driver.Get_cur_floor()) queue.Remove_first_element_in_orders() queue.Save_backup_to_file() driver.Set_button_lamp(def.BtnInside, driver.Get_cur_floor(), 0) open_and_close_door() } } } }
func send_order_complete_msg(order int, outgoingMsg chan def.Message) { //if order down if order < 0 { msg := def.Message{Category: def.CompleteOrder, Floor: driver.Get_cur_floor(), Button: def.BtnDown, Cost: -1} outgoingMsg <- msg } else { msg := def.Message{Category: def.CompleteOrder, Floor: driver.Get_cur_floor(), Button: def.BtnUp, Cost: -1} outgoingMsg <- msg } }
func Cost(orderlist []int, newOrder int) int { new_orderlist := queue.Update_orderlist(orderlist, newOrder) index := helpFunc.Get_index(new_orderlist, newOrder) // Cost is initially the distance to first floor in list var cost = helpFunc.Difference_abs(driver.Get_cur_floor(), newOrder) // For every order in orderlist, the difference between two orders is // added to the cost if len(orderlist) > 0 { cost = helpFunc.Difference_abs(driver.Get_cur_floor(), orderlist[0]) for i := 0; i < index-1; i++ { cost += helpFunc.Difference_abs(orderlist[i], orderlist[i+1]) } } // The cost is penalized by the number of orders in the queue return int(cost) + 2*len(queue.Get_Orders()) }
func handle_new_order(buttontype, floor int, outgoingMsg chan def.Message) { if external_order(buttontype) && ImConnected { msg := def.Message{Category: def.NewOrder, Floor: floor, Button: buttontype, Cost: -1} outgoingMsg <- msg } else if !external_order(buttontype) { // Internal orders: if the desired floor is under the elevator it is set as a order down if floor == driver.Get_cur_floor() && driver.Get_dir() == 1 { queue.Set_Orders(queue.Update_orderlist(queue.Get_Orders(), -floor)) } else if floor < driver.Get_cur_floor() { queue.Set_Orders(queue.Update_orderlist(queue.Get_Orders(), -floor)) } else if floor == def.NumFloors-1 { queue.Set_Orders(queue.Update_orderlist(queue.Get_Orders(), -floor)) } else { queue.Set_Orders(queue.Update_orderlist(queue.Get_Orders(), floor)) } queue.Save_backup_to_file() } }
func Update_orderlist(orderlist []int, newOrder int) []int { if order_exists(orderlist, newOrder) { return orderlist } tempOrderlist := append_and_sort_list(orderlist, newOrder) // Split up the orderlist in orders up and down ordersDown := get_orders_down(tempOrderlist) ordersUp := get_orders_up(tempOrderlist) var updatedOrderlist []int // These two conditions updates the orders based on the elevators direction and current floor if driver.Get_dir() >= 0 { for _, orderUp := range ordersUp { if orderUp > driver.Get_cur_floor() { updatedOrderlist = append(updatedOrderlist, orderUp) } else { ordersDown = append(ordersDown, orderUp) } } updatedOrderlist = helpFunc.Append_list(updatedOrderlist, ordersDown) } if driver.Get_dir() == -1 { for _, orderDown := range ordersDown { if -orderDown < driver.Get_cur_floor() { updatedOrderlist = append(updatedOrderlist, orderDown) } else { ordersUp = append(ordersUp, orderDown) } } updatedOrderlist = helpFunc.Append_list(updatedOrderlist, ordersUp) } return updatedOrderlist }
func arrived_at_destination() bool { return driver.Get_cur_floor() == int(math.Abs(float64(queue.Get_Orders()[0]))) }