Beispiel #1
0
    def slave_machine_power_on(self, room_id: str,
                               current_temp: float) -> Tuple[float, int]:
        """
        开启指定从机

        Returns:
            要创建的服务的目标温度, 目标风速
        """
        if current_temp is None:
            logger.error('当前温度不合法')
            raise RuntimeError('当前温度不合法')
        room = self.__master_machine.get_room(room_id)
        room.current_temp = current_temp
        if room.status == room_status.CLOSED:
            # 按照默认温度风速创建服务
            room.target_temp = self.__master_machine.default_target_temp
            room.current_speed = self.__master_machine.default_speed
            target_temp, speed = self.__master_machine.default_target_temp, self.__master_machine.default_speed
        elif room.status == room_status.STANDBY:
            # 按照先前温度和风速创建服务
            target_temp, speed = room.target_temp, room.current_speed
        else:
            logger.error('房间已开机或未入住')
            raise RuntimeError('房间已开机或入住')
        room.power_on(current_temp)
        DBFacade.exec(Log.objects.create,
                      room_id=room_id,
                      operation=operations.POWER_ON,
                      op_time=datetime.datetime.now())
        return target_temp, speed
 def get_report(self, room_id: str, start_time: datetime.datetime,
                finish_time: datetime.datetime):
     """获取指定房间的报表"""
     details = DBFacade.exec(DetailModel.objects.filter,
                             room_id=room_id,
                             start_time__gte=start_time,
                             finish_time__lte=finish_time)
     logs = DBFacade.exec(Log.objects.filter,
                          room_id=room_id,
                          op_time__gte=start_time,
                          op_time__lte=finish_time)
     duration = 0
     fee = 0.0
     for d in details:
         duration += (d.finish_time - d.start_time).seconds
         fee += d.fee
     times_of_on_off = 0
     times_of_dispatch = 0
     times_of_change_temp = 0
     times_of_change_speed = 0
     for l in logs:
         times_of_on_off += 1 if l.operation == operations.POWER_ON or l.operation == operations.POWER_OFF else 0
         times_of_dispatch += 1 if l.operation == operations.DISPATCH else 0
         times_of_change_temp += 1 if l.operation == operations.CHANGE_TEMP else 0
         times_of_change_speed += 1 if l.operation == operations.CHANGE_SPEED else 0
     return Report(room_id, start_time, finish_time, duration,
                   times_of_on_off, times_of_dispatch, times_of_change_temp,
                   times_of_change_speed, len(details), round(fee, 2))
Beispiel #3
0
 def slave_machine_power_off(self, room_id):
     """关闭指定从机"""
     room = self.__master_machine.get_room(room_id)
     if room.status == room_status.CLOSED:
         logger.error('房间已关机')
         raise RuntimeError('房间已关机')
     self.__service_queue.remove(room_id)
     self.__wait_queue.remove(room_id)
     room.close_up()
     DBFacade.exec(Log.objects.create,
                   room_id=room_id,
                   operation=operations.POWER_OFF,
                   op_time=datetime.datetime.now())
Beispiel #4
0
 def finish(self):
     """服务结束"""
     detail = Detail(
         None, self.room.room_id, self.start_time,
         self.start_time + datetime.timedelta(seconds=self.duration),
         self.target_speed, self.fee_rate, self.fee_since_start)
     DBFacade.exec(DetailModel.objects.create,
                   room_id=detail.room_id,
                   start_time=detail.start_time,
                   finish_time=detail.finish_time,
                   speed=detail.target_speed,
                   fee_rate=detail.fee_rate,
                   fee=detail.fee)
     self.__start_time = ...
     logger.info('房间' + self.room.room_id + '停止服务')
Beispiel #5
0
    def change_speed(self, room_id: str, target_speed: int):
        """
        改变目标风速

        Args:
            room_id: 房间号
            target_speed: 目标风速
        """
        if target_speed not in (fan_speed.LOW, fan_speed.NORMAL,
                                fan_speed.HIGH):
            logger.error('目标风速不合法')
            raise RuntimeError('目标风速不合法')
        room = self.__master_machine.get_room(room_id)
        if room.status == room_status.CLOSED or room.status == room_status.AVAILABLE:
            logger.error('未入住或未开机')
            raise RuntimeError('未入住或未开机')
        room.current_speed = target_speed
        air_conditioner_service = self.__service_queue.get_service(room_id)
        if air_conditioner_service is not None:  # 在服务队列中
            self.__service_queue.remove(room_id)
            air_conditioner_service.target_speed = target_speed
            air_conditioner_service.fee_rate = self.__master_machine.fee_rate[
                target_speed]
            while air_conditioner_service.target_speed < self.__wait_queue.max_speed:
                service_to_serve = self.__wait_queue.pop()
                if service_to_serve is not None:
                    self.__update_service.push_service(service_to_serve)
                else:
                    break
            self.__update_service.push_service(air_conditioner_service)
        else:
            air_conditioner_service = self.__wait_queue.get_service(room_id)
            if air_conditioner_service is not None:  # 在等待队列中
                self.__wait_queue.remove(room_id)
                air_conditioner_service.target_speed = target_speed
                air_conditioner_service.fee_rate = self.__master_machine.fee_rate[
                    target_speed]
                self.__update_service.push_service(air_conditioner_service)
        DBFacade.exec(Log.objects.create,
                      room_id=room_id,
                      operation=operations.CHANGE_SPEED,
                      op_time=datetime.datetime.now())
        logger.info('房间' + room_id + '改变目标风速为' + str(target_speed))
Beispiel #6
0
    def change_temp(self, room_id: str, target_temp: float):
        """
        改变目标温度

        Args:
            room_id: 房间号
            target_temp: 目标温度
        """
        if not self.__master_machine.temp_low_limit <= target_temp <= self.__master_machine.temp_high_limit:
            logger.error('目标温度不合法')
            raise RuntimeError('目标温度不合法')
        room = self.__master_machine.get_room(room_id)
        if room.status == room_status.CLOSED or room.status == room_status.AVAILABLE:
            logger.error('未入住或未开机')
            raise RuntimeError('未入住或未开机')
        room.target_temp = target_temp
        DBFacade.exec(Log.objects.create,
                      room_id=room_id,
                      operation=operations.CHANGE_TEMP,
                      op_time=datetime.datetime.now())
        logger.info('房间' + room_id + '改变目标温度为' + str(target_temp))
Beispiel #7
0
 def _task(self):
     """周期定时任务"""
     reach_temp_services = self.__service_queue.update(
         self.__master_machine.mode)
     for service in reach_temp_services:
         self.__service_queue.remove(service.room.room_id)
         logger.info('房间' + service.room.room_id + '到达设定温度')
         service.room.status = room_status.STANDBY
     timeout_services = self.__wait_queue.update(self.__master_machine.mode)
     for service in timeout_services:
         if self.push_service(service) is True:
             DBFacade.exec(Log.objects.create,
                           room_id=service.room.room_id,
                           operation=operations.DISPATCH,
                           op_time=datetime.datetime.now())
     while self.__service_queue.has_space():
         service = self.__wait_queue.pop()
         if service is not None:
             self.push_service(service)
         else:
             break
Beispiel #8
0
    def push(
        self, service: AirConditionerService
    ) -> Tuple[bool, Optional[AirConditionerService]]:
        """
        将服务对象加入服务队列

        Returns:
            如果房间能被加入服务队列, 则返回(True, 被换出的服务对象)
            如果房间不能被加入服务队列, 则返回(False, service)
        """
        if len(self.queue) < self.__MAX_NUM:
            self.queue[service.room.room_id] = service
            self.__update_max_min_speed()
            service.start()
            return True, None
        elif service.target_speed > self.__min_speed:
            services = [
                s for s in list(self.queue.values())
                if s.target_speed == self.__min_speed
            ]
            if len(services) == 1:
                service_to_pop = services[0]
            else:
                service_to_pop = max(services, key=lambda x: x.duration)
            self.queue.pop(service_to_pop.room.room_id)
            self.queue[service.room.room_id] = service
            service_to_pop.finish()
            self.__update_max_min_speed()
            service.start()
            DBFacade.exec(Log.objects.create,
                          room_id=service.room.room_id,
                          operation=operations.DISPATCH,
                          op_time=datetime.datetime.now())
            return True, service_to_pop
        elif service.target_speed == self.__min_speed and service.target_speed == self.__max_speed:
            service.wait_time = 120
        else:
            service.wait_time = 86400  # 无限等待
        return False, service
 def get_detail(self, room_id: str):
     """获取指定房间的详单"""
     room = self.get_room(room_id)
     if room.status != room_status.AVAILABLE:
         logger.error('需先退房')
         raise RuntimeError('需先退房')
     details = DBFacade.exec(DetailModel.objects.filter,
                             room_id=room_id,
                             start_time__gte=room.check_in_time,
                             finish_time__lte=room.check_out_time)
     return room.check_in_time, [
         Detail(d.detail_id, d.room_id, d.start_time, d.finish_time,
                d.speed, d.fee_rate, d.fee) for d in details
     ]