Skip to content

Commit

Permalink
RscpError to Enum
Browse files Browse the repository at this point in the history
  • Loading branch information
eikowagenknecht committed Oct 10, 2023
1 parent feb2220 commit 19e19f7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 24 deletions.
6 changes: 3 additions & 3 deletions e3dc/_e3dc_rscp_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from ._RSCPEncryptDecrypt import RSCPEncryptDecrypt
from ._rscpLib import rscpDecode, rscpEncode, rscpFrame
from ._rscpTags import RscpTag, RscpType
from ._rscpTags import RscpError, RscpTag, RscpType

PORT = 5033
BUFFER_SIZE = 1024 * 32
Expand Down Expand Up @@ -99,9 +99,9 @@ def sendRequest(self, plainMsg):

if receive[1] == "Error":
self.disconnect()
if receive[2] == "RSCP_ERR_ACCESS_DENIED":
if receive[2] == RscpError.RSCP_ERR_ACCESS_DENIED.name:
raise RSCPAuthenticationError
elif receive[2] == "RSCP_ERR_NOT_AVAILABLE":
elif receive[2] == RscpError.RSCP_ERR_NOT_AVAILABLE.name:
raise RSCPNotAvailableError
else:
raise CommunicationError(receive[2])
Expand Down
6 changes: 3 additions & 3 deletions e3dc/_rscpLib.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

from ._rscpTags import (
RscpType,
getRscpType,
getErrorcode,
getHexRscpType,
getHexRscpTag,
getStrRscpType,
getHexRscpType,
getRscpType,
getStrRscpTag,
getStrRscpType,
)

DEBUG_DICT = {"print_rscp": False}
Expand Down
44 changes: 27 additions & 17 deletions e3dc/_rscpTags.py
Original file line number Diff line number Diff line change
Expand Up @@ -3592,17 +3592,18 @@ class RscpType(Enum):
Error = 0xFF


rscpErrorCodes = {
0x01: "RSCP_ERR_NOT_HANDLED",
0x02: "RSCP_ERR_ACCESS_DENIED",
0x03: "RSCP_ERR_FORMAT",
0x04: "RSCP_ERR_AGAIN",
0x05: "RSCP_ERR_OUT_OF_BOUNDS",
0x06: "RSCP_ERR_NOT_AVAILABLE",
0x07: "RSCP_ERR_UNKNOWN_TAG",
0x08: "RSCP_ERR_ALREADY_IN_USE",
0xFFFFFFFF: "UNEXPECTED ERROR", # happens for example in get_db_data if time and span is invalid (not available)
}
class RscpError(Enum):
RSCP_ERR_NOT_HANDLED = 0x01
RSCP_ERR_ACCESS_DENIED = 0x02
RSCP_ERR_FORMAT = 0x03
RSCP_ERR_AGAIN = 0x04
RSCP_ERR_OUT_OF_BOUNDS = 0x05
RSCP_ERR_NOT_AVAILABLE = 0x06
RSCP_ERR_UNKNOWN_TAG = 0x07
RSCP_ERR_ALREADY_IN_USE = 0x08
UNKNOWN = 0xFFFFFFFF
"""Catch all for unexpected errors. Happens for example in get_db_data if time and span is invalid (not available)."""


powermeterTypes = {
0x00: "PM_TYPE_UNDEFINED",
Expand Down Expand Up @@ -3754,16 +3755,25 @@ def getStrRscpType(rscptype: int | str | RscpType) -> str:
return rscptype.name


def getErrorcode(error_hex):
"""Get error code as string.
def getStrRscpError(errorcode: int | str | RscpError) -> str:
"""
Convert an error code to its string name representation in RscpError enumeration.
Attributes:
error_hex (int): error as hex
Args:
errorcode (int | str | RscpError): The error code to be converted.
- If int, it's assumed to be the error code value.
- If str, it's assumed to be the error code name.
- If RscpError, it's error code name is used.
Returns:
str: String representation of the given error_hex
str: The name of the error code as a string.
"""
return rscpErrorCodes[error_hex]
if isinstance(errorcode, int):
errorcode = RscpError(errorcode)
elif isinstance(errorcode, str):
errorcode = RscpError[errorcode]

return errorcode.name


def getPowermeterType(powermetertype_hex):
Expand Down
3 changes: 2 additions & 1 deletion tools/convert_rscp_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
# Requirements (just for this script):
# pip install jsbeautifier

import jsbeautifier
import re

import jsbeautifier

TOOLS_DIR = "tools/"
INPUT_SCRIPT_FILE = "rscpLibV0.9.3.min.js"
OUTPUT_PY_FILE = "generated_enum.py"
Expand Down

0 comments on commit 19e19f7

Please sign in to comment.