//This function returns the next state for the elevator
func Nextstate(myip string, elevators []extra.Elevator, mystate string) (string, []Network.Request) {
	if Elevator.GetElevObstructionSignal() {
		Elevator.SetElevStopLamp(1)
		return "ERROR", nil
	} else if mystate == "ERROR" {
		Elevator.SetElevStopLamp(0)
		return "INIT", nil
	}

	stop := Stop(myip, mystate)
	if len(stop) != 0 {
		return "DOOR_OPEN", stop
	}

	next := Nextrequest(myip, elevators)
	if Elevator.AtFloor() && next.Floor == Elevator.CurrentFloor() {
		return "DOOR_OPEN", append(stop, next)
	}
	if next.Floor > Elevator.CurrentFloor() {
		return "UP", nil
	} else if next.Floor < Elevator.CurrentFloor() && next.Floor != 0 {
		return "DOWN", nil
	} else if Elevator.AtFloor() {
		return "IDLE", nil
	} else {
		return mystate, nil
	}
}
//This function return orders the elevator should stop for
func Stop(myip string, mystate string) []Network.Request {
	var takerequest []Network.Request
	requestlist := Network.GetRequestList()
	for _, request := range requestlist {
		if (request.Direction == Elevator.BUTTON_COMMAND && request.Source == myip) || (request.Direction == Elevator.BUTTON_CALL_UP && mystate == "UP") || (request.Direction == Elevator.BUTTON_CALL_DOWN && mystate == "DOWN") {
			if request.Floor == Elevator.CurrentFloor() && Elevator.AtFloor() {
				takerequest = append(takerequest, request)
			}
		}
	}
	return takerequest
}
Exemple #3
0
func main() {
	var myinfo Network.Info
	var takerequest []Network.Request

	runtime.GOMAXPROCS(runtime.NumCPU())

	myip := Network.GetLocalIP()
	Println(myip)
	myinfo.Source = myip

	conf := extra.LoadConfig("./config/conf.json")

	generatedmessages_c := make(chan Network.Networkmessage, 100)
	go Network.TCPPeerToPeer(conf, myip, generatedmessages_c)

	state := "INIT"
	driver.IoInit()
	Elevator.ElevInit()

	for {
		time.Sleep(10 * time.Millisecond)
		myinfo.State = state
		Elevator.UpdateFloor()
		myinfo.LastFloor = Elevator.CurrentFloor()
		Network.NewInfo(myinfo, generatedmessages_c)
		switch state {
		case "INIT":
			{
				Elevator.SetElevSpeed(-300)
			}
		case "IDLE":
			{
				Elevator.SetElevSpeed(0)
			}
		case "UP":
			{
				Elevator.SetElevSpeed(300)
			}
		case "DOWN":
			{
				Elevator.SetElevSpeed(-300)
			}
		case "DOOR_OPEN":
			{
				Elevator.SetElevDoorOpenLamp(1)
				for _, request := range takerequest {
					request.InOut = 0
					Println("Deleting request: ", request)
					time.Sleep(10 * time.Millisecond)
					Network.Newrequest(generatedmessages_c, request)
				}
				Elevator.SetElevSpeed(0)
				time.Sleep(3000 * time.Millisecond)
				Elevator.etElevDoorOpenLamp(0)
			}
		case "ERROR":
			{
				Elevator.SetElevSpeed(0)
			}
		}
		state, takerequest = ElevatorLogic.Nextstate(myip, conf.Elevators, myinfo.State)
	}
}