def set_amount_of_pulse_counters(self, amount): # type: (int) -> int _ = self # This does not make a lot of sense in an ORM driven implementation, but is for legacy purposes. # The legacy implementation heavily depends on the number (legacy id) and the fact that there should be no # gaps between them. If there are gaps, legacy upstream code will most likely break. # TODO: Fix legacy implementation once the upstream can manage this better amount_of_master_pulse_counters = PulseCounter.select().where(PulseCounter.source == 'master').count() if amount < amount_of_master_pulse_counters: raise ValueError('Amount should be >= {0}'.format(amount_of_master_pulse_counters)) # Assume amount is 27: # - This means n master driven PulseCounters # - This means 27-n gateway driven PulseCounters # The `number` field will contain 0-(n-1) (zero-based counting), this means that any # number above or equal to the amount can be removed (>= n) PulseCounter.delete().where(PulseCounter.number >= amount).execute() for number in range(amount_of_master_pulse_counters, amount): pulse_counter = PulseCounter.get_or_none(number=number) if pulse_counter is None: pulse_counter = PulseCounter(number=number, name='PulseCounter {0}'.format(number), source='gateway', persistent=False) pulse_counter.save() return amount
def load_pulse_counter(self, pulse_counter_id): # type: (int) -> PulseCounterDTO pulse_counter = PulseCounter.select(PulseCounter, Room) \ .join_from(PulseCounter, Room, join_type=JOIN.LEFT_OUTER) \ .where(PulseCounter.number == pulse_counter_id) \ .get() # type: PulseCounter # TODO: Load dict if pulse_counter.source == 'master': pulse_counter_dto = self._master_controller.load_pulse_counter(pulse_counter_id=pulse_counter_id) pulse_counter_dto.room = pulse_counter.room.number if pulse_counter.room is not None else None else: pulse_counter_dto = PulseCounterMapper.orm_to_dto(pulse_counter) return pulse_counter_dto
def load_pulse_counters(self): # type: () -> List[PulseCounterDTO] pulse_counter_dtos = [] for pulse_counter in list(PulseCounter.select(PulseCounter, Room) .join_from(PulseCounter, Room, join_type=JOIN.LEFT_OUTER)): # TODO: Load dicts if pulse_counter.source == 'master': pulse_counter_dto = self._master_controller.load_pulse_counter(pulse_counter_id=pulse_counter.number) pulse_counter_dto.room = pulse_counter.room.number if pulse_counter.room is not None else None pulse_counter_dto.name = pulse_counter.name # Use longer ORM name else: pulse_counter_dto = PulseCounterMapper.orm_to_dto(pulse_counter) pulse_counter_dtos.append(pulse_counter_dto) return pulse_counter_dtos
def get_persistence(self): # type: () -> Dict[int, bool] _ = self return {pulse_counter.number: pulse_counter.persistent for pulse_counter in PulseCounter.select()}
def get_values(self): # type: () -> Dict[int, Optional[int]] pulse_counter_values = {} # type: Dict[int, Optional[int]] pulse_counter_values.update(self._master_controller.get_pulse_counter_values()) for pulse_counter in PulseCounter.select().where(PulseCounter.source == 'gateway'): pulse_counter_values[pulse_counter.number] = self._counts.get(pulse_counter.number) return pulse_counter_values
def get_amount_of_pulse_counters(self): # type: () -> int _ = self return PulseCounter.select(fn.Max(PulseCounter.number)).scalar() + 1