Exemplo n.º 1
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()
			}
		}
	}
}
Exemplo n.º 2
0
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)
	}
}
Exemplo n.º 3
0
func add_order_to_best_elev(rcvList map[rcvOrder][]rcvCost, newOrder rcvOrder) {
	if this_elevator_has_the_lowest_cost(rcvList[newOrder]) {
		//fmt.Printf("%sNew order added to floor = %d with cost = %d\n%s",def.ColB,helpFunc.Order_dir(newOrder.floor,newOrder.button),Cost(queue.Get_Orders(),helpFunc.Order_dir(newOrder.floor,newOrder.button)),def.ColN)
		queue.Set_Orders(queue.Update_orderlist(queue.Get_Orders(), queue.Order_direction(newOrder.floor, newOrder.button)))
		//fmt.Printf("%sUpdated orders = %v\n%s\n",def.ColB,queue.Get_Orders(),def.ColN)
		queue.Save_backup_to_file()
	}
}
Exemplo n.º 4
0
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()
	}
}
Exemplo n.º 5
0
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())
}
Exemplo n.º 6
0
func Handle_msg(incomingMsg, outgoingMsg, costMsg, orderCompleted chan def.Message) {

	for {

		msg := <-incomingMsg
		const aliveTimeout = 6 * time.Second

		switch msg.Category {
		case def.Alive:
			//if connection exists
			if connection, exist := onlineElevs[msg.Addr]; exist {
				connection.Timer.Reset(aliveTimeout)
			} else {
				add_new_connection(msg.Addr, aliveTimeout)
			}

		case def.NewOrder:
			//fmt.Printf("%sNew external order recieved to floor %d %s \n", def.ColM, helpFunc.Order_dir(msg.Floor, msg.Button), def.ColN)
			driver.Set_button_lamp(msg.Button, msg.Floor, 1)
			costMsg := def.Message{Category: def.Cost, Floor: msg.Floor, Button: msg.Button, Cost: handleOrders.Cost(queue.Get_Orders(), queue.Order_direction(msg.Floor, msg.Button))}
			outgoingMsg <- costMsg

		case def.CompleteOrder:
			driver.Set_button_lamp(msg.Button, msg.Floor, 0)
			orderCompleted <- msg

		case def.Cost:
			//see handleOrders.assign_external_order
			costMsg <- msg
		}
	}
}
Exemplo n.º 7
0
func arrived_at_destination() bool {
	return driver.Get_cur_floor() == int(math.Abs(float64(queue.Get_Orders()[0])))
}