func EventOrderReceived() { for { button := <-ch_order_received var target string if button.Button_type == config.BUTTON_COMMAND { target = config.Laddr } else { target = queue.GetOptimalElev(button) } queue.AddOrder(button, target, ch_order_received) if target == config.Laddr { if config.Local_elev.Door_open { if queue.ShouldStopOnFloor(config.Local_elev.Last_floor) { ch_door_open <- true time.Sleep(time.Millisecond) queue.DeleteOrder(config.Local_elev.Last_floor, ch_outgoing, true) } } else if config.Local_elev.Is_idle { if queue.ShouldStopOnFloor(config.Local_elev.Last_floor) { ch_door_open <- true time.Sleep(time.Millisecond) queue.DeleteOrder(config.Local_elev.Last_floor, ch_outgoing, true) } else { dir := ChooseNewDirection() config.Local_elev.Is_idle = false config.Local_elev.Direction = dir hardware.SetMotorDirection(dir) } } } } }
func MessageServer() { for { msg := <-ch_incoming_msg switch msg.Msg_type { case config.STATE_UPDATE: already_active := false for addr, _ := range config.Active_elevs { if msg.Raddr == addr { already_active = true break } } if !already_active { SetActive(msg.Raddr) } StateCopy(config.Active_elevs[msg.Raddr], &msg.State) config.Active_elevs[msg.Raddr].Timer.Reset(config.TIMEOUT_REMOTE) case config.ADD_ORDER: ch_new_order <- msg.Button ACK_msg := msg ACK_msg.Msg_type = config.ACK ch_outgoing_msg <- ACK_msg case config.DELETE_ORDER: queue.DeleteOrder(msg.Button.Floor, ch_outgoing_msg, false) ACK_msg := msg ACK_msg.Msg_type = config.ACK ch_outgoing_msg <- ACK_msg } } }
func EventReachedFloor(floor int) { config.Local_elev.Last_floor = floor hardware.SetFloorIndicator(floor) if queue.ShouldStopOnFloor(floor) { config.Local_elev.Is_idle = true hardware.SetMotorDirection(config.DIR_STOP) queue.DeleteOrder(floor, ch_outgoing, true) ch_door_open <- true } }