示例#1
0
    def _load_services(self):
        """Load Home Automation Services Helpers"""
        product_class = self._db.get("Device.DeviceInfo.ProductClass")
        self._logger.info("Loading Services for Product Class [%s]",
                          product_class)

        if product_class == "RPi_Motion":
            default_cfg = {GPIO_PIN: "4"}
            cfg_mgr = utils.ConfigMgr(self._cfg_file_name, default_cfg)
            gpio_pin = int(cfg_mgr.get_cfg_item(GPIO_PIN))
            target_class = self._get_class(product_class, "agent.motion",
                                           "PersistDetectedMotion")
            self._service_map[product_class] = target_class(gpio_pin, self._db)
        elif product_class == "RPi_Camera" or product_class == "RPiZero_Camera":
            default_cfg = {CAMERA_IMAGE_DIR: "pictures"}
            cfg_mgr = utils.ConfigMgr(self._cfg_file_name, default_cfg)
            camera_image_dir = cfg_mgr.get_cfg_item(CAMERA_IMAGE_DIR)
            target_class = self._get_class(product_class, "agent.camera",
                                           "PersistRecordedImage")
            self._service_map[product_class] = target_class(
                camera_image_dir, "image", self._db)
            # Also create and start the Camera Web UI
            target_ui_class = self._get_class(product_class, "agent.camera_ui",
                                              "ThreadedCameraWebUI")
            camera_ui = target_ui_class(host="0.0.0.0",
                                        directory=camera_image_dir)
            camera_ui.start()
        else:
            self._logger.warning("No Services to load for Product Class [%s]",
                                 product_class)
示例#2
0
def test_no_file():
    cfg_file_defaults = {"key1": "defaultValue1", "key2": "defaultValue2"}

    cfg_mgr = utils.ConfigMgr("config_file_that_doesnt_exist.cfg",
                              cfg_file_defaults)
    value1 = cfg_mgr.get_cfg_item("key1")
    value2 = cfg_mgr.get_cfg_item("key2")

    assert value1 == "defaultValue1"
    assert value2 == "defaultValue2"
示例#3
0
def test_item_not_in_config_file_no_default():
    mock_cfg_file = "{\"key1\" : \"value1\"}"
    cfg_file_defaults = {"key2": "defaultValue2"}

    with mock.patch("builtins.open",
                    mock.mock_open(read_data=mock_cfg_file)) as my_mock:
        cfg_mgr = utils.ConfigMgr("mock.cfg", cfg_file_defaults)
        try:
            value = cfg_mgr.get_cfg_item("key3")
            assert value == "Expected a config_mgr.MissingConfigError to be raised"
        except utils.MissingConfigError:
            pass
示例#4
0
def test_empty_file():
    mock_cfg_file = ""
    cfg_file_defaults = {"key1": "defaultValue1", "key2": "defaultValue2"}

    with mock.patch("builtins.open",
                    mock.mock_open(read_data=mock_cfg_file)) as my_mock:
        cfg_mgr = utils.ConfigMgr("mock.cfg", cfg_file_defaults)
        value1 = cfg_mgr.get_cfg_item("key1")
        value2 = cfg_mgr.get_cfg_item("key2")

    my_mock.assert_called_once_with("mock.cfg", "r")
    assert value1 == "defaultValue1"
    assert value2 == "defaultValue2"