Skip to content

Commit

Permalink
connect option for loading all error codes (#134)
Browse files Browse the repository at this point in the history
* connect option for loading all error codes

* add MK39 option which copies the MK4 codes
  • Loading branch information
sarkafa authored Mar 7, 2024
1 parent 5081240 commit 320b26f
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ __pycache__
.coverage
*.egg-info
build
venv
3 changes: 3 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
include prusaerrors/sl1/errors.yaml
include prusaerrors/mmu/errors.yaml
include prusaerrors/buddy/errors.yaml

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Example: 12201
dialogs to the server. If there's an error in one of the above categories, it
is possible to reuse that error code directly, no need to create a duplicate
one in this category.
9. Other

More information about the error codes can be found at:
[prusa.io/error-codes](https://prusa.io/error-codes)
Expand Down
1 change: 1 addition & 0 deletions prusaerrors/buddy/errors.yaml
Empty file added prusaerrors/connect/__init__.py
Empty file.
105 changes: 105 additions & 0 deletions prusaerrors/connect/codes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# This file is part of the SL1 firmware
# Copyright (C) 2020 Prusa Research a.s. - www.prusa3d.com
# SPDX-License-Identifier: GPL-3.0-or-later

"""
All printer type error/attention codes
Warning: The codes might not have yet been officially approved.
"""
import re
from pathlib import Path
from typing import Optional

import yaml

from prusaerrors.shared.codes import unique_codes, Codes, Printer, Code, Category


BUDDY = ['MINI', 'MK4', 'IX', 'XL', 'MK35', 'MK39']


class PrinterCode(Code):
"""
Code class holds error code information
"""

@property
def code(self) -> str:
"""
Get error code
:return: Error code
"""
return f"x{self.raw_code}"


def yaml_codes(src_path: Path):
"""
Add code definitions from YAML source
"""

def decor(cls):
with src_path.open("r") as src_file:
data = yaml.safe_load(src_file)
assert "Errors" in data

code_re = re.compile(
r"^(?P<printer>([0-9][0-9]|XX))"
r"(?P<category>[0-9])"
r"(?P<error>[0-9][0-9])$")
for entry in data["Errors"]:
code_parts = code_re.match(entry["code"]).groupdict()
category = Category(int(code_parts["category"]))
error = int(code_parts["error"])

# code is common for printers defined in printers attribute
if code_parts["printer"] == 'XX':
if printers := entry.get("printers"):
if 'MK4' in printers:
printers.append('MK39')
else: # if no printers specified code is valid for all buddy
printers = BUDDY

for printer in printers:
printer = Printer[printer.upper().replace(".", "")]
code = PrinterCode(
printer, category, error, entry["title"],
entry["text"], entry.get("approved", False))
setattr(cls, str(code), code)

# code contains printer number
else:
printer = Printer(int(code_parts["printer"]))
code = PrinterCode(printer, category, error, entry["title"],
entry["text"], entry.get("approved", False))
setattr(cls, str(code), code)
return cls

return decor


@unique_codes
@yaml_codes(Path(__file__).parent.parent / "sl1" / "errors.yaml")
@yaml_codes(Path(__file__).parent.parent / "mmu" / "errors.yaml")
@yaml_codes(Path(__file__).parent.parent / "buddy" / "errors.yaml")
class PrinterCodes(Codes):
"""
Load all the printer type error, exception and warning identification codes
Content is loaded by yaml_codes decorators.
"""

@classmethod
def get(cls, code: str) -> Optional[Code]:
"""
Get Code by its number
:param code: Code number as string
:return: Code instance
"""
try:
return super().get(f"x{code}")
except KeyError:
return None
1 change: 1 addition & 0 deletions prusaerrors/mmu/errors.yaml
9 changes: 9 additions & 0 deletions prusaerrors/shared/codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ class Printer(IntEnum):
"""

UNKNOWN = 0
MMU = 0x0004
SL1 = 0x000A
MINI = 0x000C
MK4 = 0x000D
IX = 0x0010
XL = 0x0011
MK35 = 0x0017
MK39 = 0x0015


@unique
Expand All @@ -42,6 +49,8 @@ class Category(IntEnum):
SYSTEM = 5 # System - BSOD, ...
BOOTLOADER = 6 #
WARNINGS = 7 # Category-less warnings
DIALOGS = 8 # Remote Dialogs
UNKNOWN = 9


@functools.total_ordering
Expand Down
31 changes: 31 additions & 0 deletions tests/test_connect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This file is part of the SL1 firmware
# Copyright (C) 2020 Prusa Research a.s. - www.prusa3d.com
# SPDX-License-Identifier: GPL-3.0-or-later

# pylint: disable = missing-function-docstring
# pylint: disable = missing-class-docstring
# pylint: disable = missing-module-docstring

import unittest

from prusaerrors.shared.codes import Printer, Category
from prusaerrors.connect.codes import PrinterCodes


class TestErrors(unittest.TestCase):

def test_code_lookup(self):
code = PrinterCodes.get("17505")
assert code.printer == Printer(17)
assert code.category == Category(5)
assert code.error == 5
assert code.title
assert code.message

def test_unknown_code(self):
code = PrinterCodes.get("unknown_code")
assert code is None


if __name__ == "__main__":
unittest.main()

0 comments on commit 320b26f

Please sign in to comment.