Ejemplo n.º 1
0
    def get(self, configuration=None):
        type = self.get_argument("type", "firmware.bin")

        if type == "firmware.bin":
            storage_path = ext_storage_path(settings.config_dir, configuration)
            storage_json = StorageJSON.load(storage_path)
            if storage_json is None:
                self.send_error(404)
                return
            filename = f"{storage_json.name}.bin"
            path = storage_json.firmware_bin_path

        elif type == "firmware-factory.bin":
            storage_path = ext_storage_path(settings.config_dir, configuration)
            storage_json = StorageJSON.load(storage_path)
            if storage_json is None:
                self.send_error(404)
                return
            filename = f"{storage_json.name}-factory.bin"
            path = storage_json.firmware_bin_path.replace(
                "firmware.bin", "firmware-factory.bin")

        else:
            args = ["esphome", "idedata", settings.rel_path(configuration)]
            rc, stdout, _ = run_system_command(*args)

            if rc != 0:
                self.send_error(404 if rc == 2 else 500)
                return

            idedata = platformio_api.IDEData(json.loads(stdout))

            found = False
            for image in idedata.extra_flash_images:
                if image.path.endswith(type):
                    path = image.path
                    filename = type
                    found = True
                    break

            if not found:
                self.send_error(404)
                return

        self.set_header("Content-Type", "application/octet-stream")
        self.set_header("Content-Disposition",
                        f'attachment; filename="{filename}"')
        self.set_header("Cache-Control", "no-cache")
        if not Path(path).is_file():
            self.send_error(404)
            return

        with open(path, "rb") as f:
            while True:
                data = f.read(16384)
                if not data:
                    break
                self.write(data)
        self.finish()
Ejemplo n.º 2
0
def upload_program(config, args, host):
    # if upload is to a serial port use platformio, otherwise assume ota
    if get_port_type(host) == 'SERIAL':
        if CORE.is_esp8266:
            return upload_using_esptool(config, host)
        return platformio_api.run_upload(config, args.verbose, host)

    from esphome.components import ota
    from esphome import espota2

    if args.host_port is not None:
        host_port = args.host_port
    else:
        host_port = int(
            os.getenv('ESPHOME_OTA_HOST_PORT', random.randint(10000, 60000)))

    verbose = args.verbose
    remote_port = ota.get_port(config)
    password = ota.get_auth(config)

    storage = StorageJSON.load(storage_path())
    res = espota2.run_ota(host, remote_port, password, CORE.firmware_bin)
    if res == 0:
        if storage is not None and storage.use_legacy_ota:
            storage.use_legacy_ota = False
            storage.save(storage_path())
        return res
    if storage is not None and not storage.use_legacy_ota:
        return res

    _LOGGER.warning("OTA v2 method failed. Trying with legacy OTA...")
    return espota2.run_legacy_ota(verbose, host_port, host, remote_port,
                                  password, CORE.firmware_bin)
Ejemplo n.º 3
0
 def storage(self):  # type: () -> Optional[StorageJSON]
     if not self._loaded_storage:
         self._storage = StorageJSON.load(
             ext_storage_path(settings.config_dir, self.filename)
         )
         self._loaded_storage = True
     return self._storage
Ejemplo n.º 4
0
def update_storage_json():
    path = storage_path()
    old = StorageJSON.load(path)
    new = StorageJSON.from_esphome_core(CORE, old)
    if old == new:
        return

    if storage_should_clean(old, new):
        _LOGGER.info("Core config or version changed, cleaning build files...")
        clean_build()

    new.save(path)
Ejemplo n.º 5
0
    def post(self, configuration=None):
        config_file = settings.rel_path(configuration)
        storage_path = ext_storage_path(settings.config_dir, configuration)

        trash_path = trash_storage_path(settings.config_dir)
        mkdir_p(trash_path)
        shutil.move(config_file, os.path.join(trash_path, configuration))

        storage_json = StorageJSON.load(storage_path)
        if storage_json is not None:
            # Delete build folder (if exists)
            name = storage_json.name
            build_folder = os.path.join(settings.config_dir, name)
            if build_folder is not None:
                shutil.rmtree(build_folder, os.path.join(trash_path, name))
Ejemplo n.º 6
0
    def post(self, configuration=None):
        config_file = os.path.join(CONFIG_DIR, configuration)
        storage_path = ext_storage_path(CONFIG_DIR, configuration)
        storage_json = StorageJSON.load(storage_path)
        if storage_json is None:
            self.set_status(500)
            return

        name = storage_json.name
        trash_path = trash_storage_path(CONFIG_DIR)
        mkdir_p(trash_path)
        shutil.move(config_file, os.path.join(trash_path, configuration))

        # Delete build folder (if exists)
        build_folder = os.path.join(CONFIG_DIR, name)
        if build_folder is not None:
            shutil.rmtree(build_folder, os.path.join(trash_path, name))
Ejemplo n.º 7
0
    def get(self, configuration=None):
        # pylint: disable=no-value-for-parameter
        storage_path = ext_storage_path(settings.config_dir, configuration)
        storage_json = StorageJSON.load(storage_path)
        if storage_json is None:
            self.send_error()
            return

        path = storage_json.firmware_bin_path
        self.set_header("Content-Type", "application/octet-stream")
        filename = f"{storage_json.name}.bin"
        self.set_header("Content-Disposition", f'attachment; filename="{filename}"')
        with open(path, "rb") as f:
            while True:
                data = f.read(16384)
                if not data:
                    break
                self.write(data)
        self.finish()
Ejemplo n.º 8
0
    def get(self, configuration=None):
        # pylint: disable=no-value-for-parameter
        storage_path = ext_storage_path(CONFIG_DIR, configuration)
        storage_json = StorageJSON.load(storage_path)
        if storage_json is None:
            self.send_error()
            return

        path = storage_json.firmware_bin_path
        self.set_header('Content-Type', 'application/octet-stream')
        filename = '{}.bin'.format(storage_json.name)
        self.set_header("Content-Disposition",
                        'attachment; filename="{}"'.format(filename))
        with open(path, 'rb') as f:
            while True:
                data = f.read(16384)
                if not data:
                    break
                self.write(data)
        self.finish()
Ejemplo n.º 9
0
    def get(self):
        if not self.is_authenticated():
            self.redirect('/login')
            return

        configuration = self.get_argument('configuration')
        storage_path = ext_storage_path(CONFIG_DIR, configuration)
        storage_json = StorageJSON.load(storage_path)
        if storage_json is None:
            self.send_error()
            return

        path = storage_json.firmware_bin_path
        self.set_header('Content-Type', 'application/octet-stream')
        filename = '{}.bin'.format(storage_json.name)
        self.set_header("Content-Disposition", 'attachment; filename="{}"'.format(filename))
        with open(path, 'rb') as f:
            while 1:
                data = f.read(16384)  # or some other nice-sized chunk
                if not data:
                    break
                self.write(data)
        self.finish()
Ejemplo n.º 10
0
 def storage(self):  # type: () -> Optional[StorageJSON]
     if not self._loaded_storage:
         self._storage = StorageJSON.load(
             ext_storage_path(CONFIG_DIR, self.filename))
         self._loaded_storage = True
     return self._storage