def __init__(self, settings: Settings): log_values = bool(settings.get(LOG_SENSOR_DATA)) DustSensor.__init__(self, settings) CyclicComponent.__init__(self, None, settings) self._sensor_driver = None self._start_update_loop(self._init_sensor, self._read_sensor)
def testStandbyInDayMode(base_fixture, target_mockup_fixture): settings = Settings(base_fixture.testdata_path / "integration") settings.set(MOTION_SENSOR_ENABLED, True) settings.set(NIGHT_MODE_BEGIN, 22) settings.set(NIGHT_MODE_END, 5) settings.set(BRIGHTNESS, 70) settings.set(DAY_STANDBY_TIMEOUT, 10) energy_saver = None comps = ComponentRegistry(settings) disp = comps.display # day with freeze_time("2019-01-01 12:59:59"): energy_saver = comps.energy_saver time.sleep(energy_saver.INIT_WAIT_TIME) time.sleep(energy_saver.UPDATE_TIME + 1) assert energy_saver.night_mode_active == False assert disp.get_brightness() == STANDBY_BRIGHTNESS # switch to wake comps.motion_detection_sensor._motion_detected = 1 with freeze_time("2019-01-01 13:00:10"): time.sleep(energy_saver.UPDATE_TIME + 2) assert disp.get_brightness() == settings.get(BRIGHTNESS) time.sleep(10 - energy_saver.UPDATE_TIME) # switch to standby comps.motion_detection_sensor._motion_detected = 0 time.sleep(energy_saver.UPDATE_TIME * 2) assert disp.get_brightness() == STANDBY_BRIGHTNESS energy_saver.stop()
def __init__(self, settings: Settings): log_values = bool(settings.get(LOG_SENSOR_DATA)) CO2Sensor.__init__(self, log_values) CyclicComponent.__init__(self) self._start_time = datetime.datetime.now() self._readings_stabilized = False self._start_update_loop(self._init_sensor, self._read_sensor)
def __init__(self, settings: Settings): MEASURE_POINTS = 2 log_values = bool(settings.get(LOG_SENSOR_DATA)) LightSensor.__init__(self, log_values, MEASURE_POINTS) CyclicComponent.__init__(self) self._sensor_driver = None self._start_update_loop(self._init_sensor, self._read_sensor)
def __init__(self, components: ComponentRegistry, settings: Settings): log_values = bool(settings.get(LOG_SENSOR_DATA)) TempSensor.__init__(self, log_values) BarometricSensor.__init__(self, log_values) CyclicComponent.__init__(self, components, settings) self._sensor_driver = None self._start_update_loop(self._init_sensor, self._read_sensor)
def main(settings_path: Optional[Path] = None): """ Main function, calling setup, loading components and safe shutdown. :param settings_path: Only used for testing to load a settings file. """ # Create user config dir if not config.user_config_dir.exists(): os.makedirs(config.user_config_dir) # System is first, is_target_system is the most basic check runtime_system = RuntimeSystem() if not runtime_system.is_target_system: setup_on_non_target_system() # All other classes depend on settings if not settings_path: settings_path = config.user_config_dir settings = Settings(ini_folder=settings_path) handle_cmd_args(settings) # cmd args set Debug level for logger # to be able to remote debug as much as possible, this call is being done early start_remote_debug() Logger( output_path=config.user_config_dir) # singleton, no assigment needed comp_ctrl = ComponentController(settings) config.comp_ctrl = comp_ctrl #if config.DEBUG_LEVEL > 0: # disable startup sound # comp_ctrl.components.tts.say_internal("startup", [WAQD_VERSION]) display_type = settings.get(DISPLAY_TYPE) if display_type in [DISP_TYPE_RPI, DISP_TYPE_WAVESHARE_5_LCD]: config.qt_app = qt_app_setup(settings) # main_ui must be held in this context, otherwise the gc will destroy the gui loading_sequence(comp_ctrl, settings) try: config.qt_app.exec_() except: # pylint:disable=bare-except trace_back = traceback.format_exc() Logger().error("Application crashed: \n%s", trace_back) elif display_type == DISP_TYPE_WAVESHARE_EPAPER_2_9: pass elif display_type == DISP_TYPE_HEADLESS: comp_ctrl.init_all() comp_ctrl._stop_event.wait() # unload modules - wait for every thread to quit if runtime_system.is_target_system: Logger().info("Prepare to exit...") if comp_ctrl: comp_ctrl.unload_all() while not comp_ctrl.all_unloaded: time.sleep(.1)
def set_ui_language(qt_app: QtWidgets.QApplication, settings: Settings): """ Set the ui language. Retranslate must be called afterwards.""" if config.translator: qt_app.removeTranslator(config.translator) if settings.get(LANG) == LANG_ENGLISH: # default case, ui is written in english return if not config.translator: config.translator = QtCore.QTranslator(qt_app) tr_file = Path("NULL") if settings.get(LANG) == LANG_GERMAN: tr_file = config.base_path / "ui/qt/german.qm" if settings.get(LANG) == LANG_HUNGARIAN: tr_file = config.base_path / "ui/qt/hungarian.qm" if not tr_file.exists(): logger.error("Cannot find %s translation file.", str(tr_file)) config.translator.load(str(tr_file)) qt_app.installTranslator(config.translator)
def __init__(self, components: ComponentRegistry, settings: Settings): MEASURE_POINTS = 3 log_values = bool(settings.get(LOG_SENSOR_DATA)) CO2Sensor.__init__(self, log_values, MEASURE_POINTS) TvocSensor.__init__(self, log_values, MEASURE_POINTS) CyclicComponent.__init__(self, components) self._start_time = datetime.datetime.now() self._reload_forbidden = True self._sensor_driver: "CCS811" = None self._error_num = 0 self._start_update_loop(self._init_sensor, self._read_sensor)
def get_localized_date(date_time: datetime.datetime, settings: Settings) -> str: """ Returns a formatted date of a day conforming to the actual locale. Contains weekday name, month and day. """ # switch locale to selected language - needs reboot on linux if settings.get(LANG) != LANG_ENGLISH: locale_name = "" try: if platform.system() == "Windows": if settings.get(LANG) == LANG_GERMAN: locale_name = "de_DE" elif settings.get(LANG) == LANG_HUNGARIAN: locale_name = "hu-HU" elif platform.system() == "Linux": if settings.get(LANG) == LANG_GERMAN: locale_name = "de_DE.UTF8" elif settings.get(LANG) == LANG_HUNGARIAN: locale_name = "hu_HU.UTF8" locale.setlocale(locale.LC_ALL, locale_name) except Exception as error: logger.error("Cannot set language to %s: %s", settings.get(LANG), str(error)) # "sudo apt-get install language-pack-id" is needed... # or sudo locale-gen else: locale.setlocale(locale.LC_ALL, 'C') local_date = time.strftime("%a, %x", date_time.timetuple()) # remove year - twice once with following . and once for none local_date = local_date.replace(str(date_time.year) + ".", "") local_date = local_date.replace(str(date_time.year), "") return local_date
def __init__(self, pin: int, components: ComponentRegistry, settings: Settings): log_values = bool(settings.get(LOG_SENSOR_DATA)) TempSensor.__init__(self, log_values) HumiditySensor.__init__(self, log_values) CyclicComponent.__init__(self, components, log_values) self._pin = pin if not self._pin: self._logger.error("DHT22: No pin, disabled") self._disabled = True return self._sensor_driver = None self._error_num = 0 self._start_update_loop(self._init_sensor, self._read_sensor)
def __init__(self, components: ComponentRegistry, settings: Settings): super().__init__(components, settings) if not settings.get(EVENTS_ENABLED): return self.gui_background_update_sig: Optional[pyqtBoundSignal] = None self._config_events_file = config.user_config_dir / "events.json" self._events = parse_event_file(self._config_events_file) self._scheduler: Optional[BackgroundScheduler] = None self._init_thread = threading.Thread(name="StartScheduler", target=self._init_scheduler, daemon=True) self._init_thread.start()
def testNoStandbyIfSensorIsDisabled(base_fixture): settings = Settings(base_fixture.testdata_path / "integration") settings.set(MOTION_SENSOR_ENABLED, False) settings.set(NIGHT_MODE_BEGIN, 23) settings.set(NIGHT_MODE_END, 5) settings.set(BRIGHTNESS, 70) energy_saver = None comps = ComponentRegistry(settings) disp = comps.display # night mode - no wakeup with freeze_time("2019-01-01 12:00:00"): # energy_saver needs to be initalized in freeze time, otherwise testing time will have an impact energy_saver = ESaver(comps, settings) time.sleep(energy_saver.INIT_WAIT_TIME) time.sleep(energy_saver.UPDATE_TIME + 1) assert disp.get_brightness() == settings.get(BRIGHTNESS) energy_saver.stop()