当前位置: 首页>>代码示例>>Python>>正文


Python Driver.getAccordingLight方法代码示例

本文整理汇总了Python中driver.Driver.getAccordingLight方法的典型用法代码示例。如果您正苦于以下问题:Python Driver.getAccordingLight方法的具体用法?Python Driver.getAccordingLight怎么用?Python Driver.getAccordingLight使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在driver.Driver的用法示例。


在下文中一共展示了Driver.getAccordingLight方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from driver import Driver [as 别名]
# 或者: from driver.Driver import getAccordingLight [as 别名]
class Elevator:
    DIRECTION_UP = 1
    DIRECTION_DOWN = -1
    DIRECTION_STAND = 0
    LEAVE_DOORS_OPEN = 2

    def __init__(self):
        self.driver = Driver()
        self.previous_floor = 1
        self.direction = self.DIRECTION_UP
        self.queue = []  # A Queue of orders
        self.task = []  # The current order that is being taken care of
        self.motor_speed = 1000
        self.elevator = None  # *** hva brukes denne til og hvor settes den til en verdi? ***

    def go_to_floor(self, floor, lock):
        """does what its name implies"""
        current_floor = self.driver.getCurrentFloor()
        if not current_floor:
            current_floor = self.previous_floor
        if current_floor < floor:
            self.direction = self.DIRECTION_UP
            self.driver.move(OUTPUT.MOTOR_UP, self.motor_speed)
        elif current_floor == floor:
            self.driver.stop()
            self.pop_task(lock)
        elif current_floor > floor:
            self.driver.move(OUTPUT.MOTOR_DOWN, self.motor_speed)
            self.direction = self.DIRECTION_DOWN

    def pop_task(self, lock):
        """removes a task from the list of tasks and preforms cleanup"""
        lock.acquire()
        floor = self.task.pop()
        lock.release()
        light = self.driver.getAccordingLight(INPUT.IN_BUTTONS, floor)
        self.driver.setChannel(light, 0)
        self.open_doors()

    def open_doors(self):
        """simulates opening the doors of the elevator and waiting"""
        start = time.clock()
        while time.clock() < self.LEAVE_DOORS_OPEN + start:
            self.driver.stop()

    def add_job(self, floor, direction, lock):
        """adds a job to the queue or task"""
        if task:
            # check whether this could be added as a subtask
            if (
                self.direction == self.DIRECTION_UP == direction
                and self.previous_floor < floor <= self.task[len(self.task) - 1][0]
            ):
                self.add_subtask(floor, direction, lock)
            elif (
                self.direction == self.DIRECTION_DOWN == direction
                and self.previous_floor > floor >= self.task[len(self.task) - 1][0]
            ):
                self.add_subtask(floor, direction, lock)
            else:
                lock.acquire()
                self.queue.append((floor, direction))
                lock.release()
        else:
            lock.acquire()
            task.append((floor, direction))
            lock.release()

    def add_subtask(self, floor, direction, lock):
        """adds a subtask to the task in correct order"""
        lock.acquire()
        for i, sub in enumerate(self.task):
            if direction == self.DIRECTION_DOWN:
                if sub >= self.task[i]:
                    self.task.insert(sub, i)
            else:
                if sub <= self.task[i]:
                    self.task.insert(sub, i)
        lock.release()

    def read_inputs(self, lock):
        """reads inputs and processes them"""
        for sig in INPUT.BUTTONS:
            if self.driver.readChannel(sig):
                (floor, type) = self.driver.channelToFloor(sig)
                light = self.driver.getAccordingLight(type, floor)
                self.driver.setChannel(light, 1)
                if type == INPUT.IN_BUTTONS:
                    self.add_subtask(floor, self.direction, lock)

                elif type == INPUT.DOWN_BUTTONS:
                    job = (floor, self.DIRECTION_DOWN)
                    if self.check_job(job):
                        self.add_job(floor, self.DIRECTION_DOWN, lock)
                    else:
                        ElevatorProcess(target=self.elevator.send_job, args=(self.elevator, job)).start()
                elif type == INPUT.UP_BUTTONS:
                    job = (floor, self.DIRECTION_UP)
                    if self.check_job(job):
                        self.add_job(floor, self.DIRECTION_UP, lock)
#.........这里部分代码省略.........
开发者ID:metareven,项目名称:Elevator,代码行数:103,代码来源:elevator.py


注:本文中的driver.Driver.getAccordingLight方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。