Пример #1
0
func (e *Elevator) Update() {
	for {
		e.location = ElevGetFloorSensorSignal()

		if e.location != -1 && e.currentFloor != e.location {
			e.currentFloor = e.location
			//send reachedfloor
			nextfloor := list.GetValue(e.stopList.Front())
			msg := strconv.Itoa(e.id) + ":ReachedFloor:" + strconv.Itoa(e.currentFloor) + ":" + strconv.Itoa(nextfloor)
			e.out <- msg
		}
		Sleep(Millisecond)
	}
}
Пример #2
0
func (c *Control) FindSuitableElevator(button, floor int) bool {
	//find elevators with correct direction
	var id int
	var e *Elevator
	//result := false
	for id, e = range c.elevators {
		if e.IsIdle() || e.WillPass(floor) || e.IsAt(floor) { //ikkje veldig strenge krav idag
			el := e.stopList.Front()
			if el == nil {
				msg := c.ComposeMessage("NewFloor", strconv.Itoa(id), strconv.Itoa(button), strconv.Itoa(floor))
				c.toNet <- msg
				return true
			}
			for ; el != nil; el = el.Next() {
				if e.IsGoingUp() && button == BUTTON_CALL_DOWN && (list.GetValue(el) < floor || c.GoingDownAbove(floor)) {
					//not suitable, going up after floor, or somebody is going down above us
					break
				} else if e.IsGoingDown() && button == BUTTON_CALL_UP && (list.GetValue(el) > floor || c.GoingUpBelow(floor)) {
					//not suitable, going down after floor, or somebody is going up below us
					break
				}

				if e.stopList.Contains(floor) {
					return true //won't add the floor more than once, maybe reconsider
				}
				msg := c.ComposeMessage("NewFloor", strconv.Itoa(id), strconv.Itoa(button), strconv.Itoa(floor))
				c.toNet <- msg
				return true
			}

		} //not suitable otherwise
	}

	return false

}
Пример #3
0
func (c *Control) AddFloor(button, floor int) { //treng ikkje button? //TEST MED 1 3 2o/n
	/*
		dir := 0
		if (button == BUTTON_CALL_DOWN) {dir = -1}
		if (button == BUTTON_CALL_UP) {dir = 1}
	*/

	e := c.e.stopList.Front()
	if e == nil {
		//tom liste
		c.e.stopList.PushFront(floor)
		return
	}
	i := list.GetValue(e)

	if c.e.IsGoingUp() {
		//elevator going up

		for i < floor && e.Next() != nil {
			// after this loop, e is the first element that is larger than floor, or the last element
			e = e.Next()
			if i > list.GetValue(e) {
				//the elevator is not monotonically increasing, go up before going down TEST MED TO ETASJE 2 3n 4n
				c.e.stopList.InsertAfter(floor, e)
				return
			}
			i = list.GetValue(e)
		}

		if e.Next() == nil {
			//floor is (probably?) larger than any floor already in the list, add to the end
			c.e.stopList.InsertAfter(floor, e)
			return
		} else if i > floor { //stopp før
			c.e.stopList.InsertBefore(floor, e)
		} else { //ned etterpå
			c.e.stopList.InsertAfter(floor, e)
		}

		if list.GetValue(e) != floor {
			c.e.stopList.InsertAfter(floor, e)
			return
		}

	} else if c.e.IsGoingDown() {
		//elevator going down

		for i > floor && e.Next() != nil {
			// after this loop, e is the first element that is larger than floor, or the last element
			e = e.Next()
			if i < list.GetValue(e) {
				//the elevator is not monotonically increasing, go up before going down TEST MED TO ETASJE 2 3n 4n
				c.e.stopList.InsertAfter(floor, e)
				return
			}
			i = list.GetValue(e)
		}

		if e.Next() == nil {
			//floor is (probably?) larger than any floor already in the list, add to the end
			c.e.stopList.InsertAfter(floor, e)
			return
		} else if i < floor { //stopp før
			c.e.stopList.InsertBefore(floor, e)
		} else { //ned etterpå
			c.e.stopList.InsertAfter(floor, e)
		}

		if list.GetValue(e) != floor {
			c.e.stopList.InsertAfter(floor, e)
			return
		}

	} else {
		//elevator standing
		c.e.stopList.PushBack(floor)
	}

	//default
	c.e.stopList.PushBack(floor)
}