Example #1
0
func readChannels(ch fsm.Channels) {
	buttonChan := getButtons()
	for {
		select {

		case dir := <-ch.MotorDir:
			driver.Elev_set_motor_direction(dir)
		case doorLight := <-ch.DoorLight:
			driver.Elev_set_door_open_lamp(doorLight)
		case floor := <-ch.NewFloor:
			driver.Elev_set_floor_indicator(floor)
		case buttonPressed := <-buttonChan:
			switch buttonPressed.Button {
			case def.BUTTON_COMMAND:
				queue.SetLocalOrder(buttonPressed)
			case def.BUTTON_DOWN, def.BUTTON_UP:
				cost.ChooseElevator(onlineLifts, buttonPressed, true)
			}

		//Incoming messages
		case tcpMessage := <-incomingTcpMsg:
			switch tcpMessage.Type {
			case def.ORDER:
				queue.SetLocalOrder(tcpMessage.Order)
			case def.UPDATEREMOTELIGHT:
				def.SyncLight <- tcpMessage.Order
				ch.OrderTimerReset <- true
			}
		case message := <-incomingMsg:
			handleMsg(ch, message)

		//Outgoing messages
		case button := <-def.SyncRemoteLights:
			for i := range onlineLifts {
				if i == def.LocalElevator.Addr {
					def.SyncLight <- button
				} else {
					go network.TcpConnect(i, def.TcpPort, def.TcpMessage{Type: def.UPDATEREMOTELIGHT, Order: button})
				}
			}

		//Handles if motor stops working
		case <-ch.OrderTimerTimeout:
			for f := 0; f < def.N_FLOORS; f++ {
				for b := 0; b < def.BUTTON_COMMAND; b++ {
					if def.LocalElevator.Orders[f][b] {
						cost.ChooseElevator(onlineLifts, def.ButtonPress{b, f, true, def.LocalElevator.Addr}, false)
					}
				}
			}
		}
	}
}
Example #2
0
func handleDeadElevator(ch fsm.Channels) {
	for {
		//Handles lost connection to remote elevator
		for e := range onlineLifts {
			var temp = onlineLifts[e]
			temp.MissedTics++
			onlineLifts[e] = temp
		}
		for e := range onlineLifts {
			if onlineLifts[e].MissedTics > 1000 {
				var temp = onlineLifts[e]
				delete(onlineLifts, e)
				for f := 0; f < def.N_FLOORS; f++ {
					for b := 0; b < def.BUTTON_COMMAND; b++ {
						if temp.Orders[f][b] {
							cost.ChooseElevator(onlineLifts, def.ButtonPress{b, f, true, def.LocalElevator.Addr}, true)
						}
					}
				}
			}
		}
		time.Sleep(time.Millisecond)
	}
}