예제 #1
0
    def get(self, configuration=None):
        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))

        firmware_offset = "0x10000" if idedata.extra_flash_images else "0x0"
        flash_images = [
            {
                "path": f"./download.bin?configuration={configuration}&type=firmware.bin",
                "offset": firmware_offset,
            }
        ] + [
            {
                "path": f"./download.bin?configuration={configuration}&type={os.path.basename(image.path)}",
                "offset": image.offset,
            }
            for image in idedata.extra_flash_images
        ]

        self.set_header("Content-Type", "application/json")
        self.write(json.dumps(flash_images))
        self.finish()
예제 #2
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()