Example #1
0
 def run(self) -> None:
     os.nice(5)
     globs = [
         FILES_DIRECTORY.rglob(f"*{extension}")
         for extension in get_supported_extensions()
     ]
     for file in chain.from_iterable(globs):
         read_cached_sliced_model_file(file.absolute())
         read_cached_preview(file.absolute())
Example #2
0
 def run(self) -> None:
     os.nice(5)
     globs = [
         config.get_files_directory().rglob(f"*{extension}")
         for extension in get_supported_extensions()
     ]
     for file in chain.from_iterable(globs):
         read_cached_sliced_model_file(file.absolute())
         read_cached_preview(file.absolute())
Example #3
0
def list_files() -> str:
    path_parameter = str(request.args.get("path", "."))
    path = (config.get_files_directory() / path_parameter).resolve()
    if (config.get_files_directory() not in path.parents
            and path != config.get_files_directory()):
        abort(400)
    with os.scandir(path) as dir_entries:
        files = []
        directories = []
        for dir_entry in sorted(dir_entries,
                                key=lambda t: t.stat().st_mtime,
                                reverse=True):
            if dir_entry.is_file():
                sliced_model_file: Optional[SlicedModelFile] = None
                if get_file_extension(
                        dir_entry.name) in get_supported_extensions():
                    if dir_entry.name.startswith("._"):
                        if b"Mac OS X" not in open(dir_entry, "rb").read(32):
                            sliced_model_file = read_cached_sliced_model_file(
                                path / dir_entry.name)
                    else:
                        sliced_model_file = read_cached_sliced_model_file(
                            path / dir_entry.name)

                file_data: Dict[str, Any] = {
                    "filename":
                    dir_entry.name,
                    "path":
                    str((path / dir_entry.name).relative_to(
                        config.get_files_directory())),
                }

                if sliced_model_file:
                    file_data = {
                        "print_time_secs": sliced_model_file.print_time_secs,
                        "can_be_printed": True,
                        **file_data,
                    }
                else:
                    file_data = {
                        "can_be_printed": False,
                        **file_data,
                    }

                files.append(file_data)
            else:
                directories.append({"dirname": dir_entry.name})
        return jsonify({
            "directories": directories,
            "files": files,
        })
Example #4
0
def print_status() -> str:
    with ChiTuPrinter() as printer:
        # the printer sends periodic "ok" responses over serial. this means that
        # sometimes we get an unexpected response from the printer (an "ok" instead of
        # the print status we expected). due to this, we retry at most 3 times here
        # until we have a successful response. see issue #180
        selected_file = retry(
            printer.get_selected_file,
            UnexpectedPrinterResponse,
            num_retries=3,
        )
        print_status = retry(
            printer.get_print_status,
            UnexpectedPrinterResponse,
            num_retries=3,
        )

        if print_status.state == PrinterState.IDLE:
            progress = 0.0
            print_details = {}
        else:
            sliced_model_file = read_cached_sliced_model_file(
                config.get_files_directory() / selected_file)

            if print_status.current_byte == 0:
                current_layer = 1
            else:
                current_layer = (
                    sliced_model_file.end_byte_offset_by_layer.index(
                        print_status.current_byte) + 1)

            progress = (100.0 * none_throws(current_layer - 1) /
                        none_throws(sliced_model_file.layer_count))

            print_details = {
                "current_layer":
                current_layer,
                "layer_count":
                sliced_model_file.layer_count,
                "print_time_secs":
                sliced_model_file.print_time_secs,
                "time_left_secs":
                round(sliced_model_file.print_time_secs * (100.0 - progress) /
                      100.0),
            }

        return jsonify({
            "state": print_status.state.value,
            "selected_file": selected_file,
            "progress": progress,
            **print_details,
        })
Example #5
0
def print_status() -> str:
    with ElegooMars() as elegoo_mars:
        selected_file = elegoo_mars.get_selected_file()
        print_status = elegoo_mars.get_print_status()

        if print_status.state == PrinterState.IDLE:
            progress = 0.0
            print_details = {}
        else:
            sliced_model_file = read_cached_sliced_model_file(
                config.get_files_directory() / selected_file
            )

            if print_status.current_byte == 0:
                current_layer = 1
            else:
                current_layer = (
                    sliced_model_file.end_byte_offset_by_layer.index(
                        print_status.current_byte
                    )
                    + 1
                )

            progress = (
                100.0
                * none_throws(current_layer - 1)
                / none_throws(sliced_model_file.layer_count)
            )

            print_details = {
                "current_layer": current_layer,
                "layer_count": sliced_model_file.layer_count,
                "print_time_secs": sliced_model_file.print_time_secs,
                "time_left_secs": round(
                    sliced_model_file.print_time_secs * (100.0 - progress) / 100.0
                ),
            }

        return jsonify(
            {
                "state": print_status.state.value,
                "selected_file": selected_file,
                "progress": progress,
                **print_details,
            }
        )
Example #6
0
def file_details() -> str:
    filename = str(request.args.get("filename"))
    path = (config.get_files_directory() / filename).resolve()
    if config.get_files_directory() not in path.parents:
        abort(400)
    sliced_model_file = read_cached_sliced_model_file(path)
    return jsonify(
        {
            "filename": sliced_model_file.filename,
            "path": filename,
            "bed_size_mm": list(sliced_model_file.bed_size_mm),
            "height_mm": round(sliced_model_file.height_mm, 4),
            "layer_count": sliced_model_file.layer_count,
            "layer_height_mm": round(sliced_model_file.layer_height_mm, 4),
            "resolution": list(sliced_model_file.resolution),
            "print_time_secs": sliced_model_file.print_time_secs,
        }
    )