Skip to content

Commit 1cdb9f7

Browse files
committed
TypeAlias for RscpMessage
1 parent 8b2ada6 commit 1cdb9f7

File tree

4 files changed

+30
-51
lines changed

4 files changed

+30
-51
lines changed

e3dc/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from ._e3dc import E3DC, AuthenticationError, NotAvailableError, PollError, SendError
99
from ._e3dc_rscp_local import CommunicationError, RSCPAuthenticationError, RSCPKeyError
1010
from ._e3dc_rscp_web import RequestTimeoutError, SocketNotReady
11-
from ._rscpLib import FrameError
11+
from ._rscpLib import FrameError, RscpMessage
1212
from ._rscpLib import set_debug as set_rscp_debug
1313

1414
__all__ = [
@@ -23,6 +23,7 @@
2323
"RequestTimeoutError",
2424
"SocketNotReady",
2525
"FrameError",
26+
"RscpMessage",
2627
"set_rscp_debug",
2728
]
2829
__version__ = "0.9.3"

e3dc/_e3dc_rscp_local.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
# Licensed under a MIT license. See LICENSE for details
66

77
import socket
8-
from typing import Any
98

109
from ._RSCPEncryptDecrypt import RSCPEncryptDecrypt
11-
from ._rscpLib import rscpDecode, rscpEncode, rscpFrame
10+
from ._rscpLib import RscpMessage, rscpDecode, rscpEncode, rscpFrame
1211
from ._rscpTags import RscpError, RscpTag, RscpType
1312

1413
PORT = 5033
@@ -64,9 +63,7 @@ def __init__(
6463
self.encdec: RSCPEncryptDecrypt
6564
self.processedData = None
6665

67-
def _send(
68-
self, plainMsg: tuple[str | int | RscpTag, str | int | RscpType, Any]
69-
) -> None:
66+
def _send(self, plainMsg: RscpMessage) -> None:
7067
sendData = rscpFrame(rscpEncode(plainMsg))
7168
encData = self.encdec.encrypt(sendData)
7269
self.socket.send(encData)
@@ -78,19 +75,15 @@ def _receive(self):
7875
decData = rscpDecode(self.encdec.decrypt(data))[0]
7976
return decData
8077

81-
def sendCommand(
82-
self, plainMsg: tuple[str | int | RscpTag, str | int | RscpType, Any]
83-
) -> None:
78+
def sendCommand(self, plainMsg: RscpMessage) -> None:
8479
"""Sending RSCP command.
8580
8681
Args:
8782
plainMsg (tuple): plain message
8883
"""
8984
self.sendRequest(plainMsg) # same as sendRequest but doesn't return a value
9085

91-
def sendRequest(
92-
self, plainMsg: tuple[str | int | RscpTag, str | int | RscpType, Any]
93-
) -> tuple[str | int | RscpTag, str | int | RscpType, Any]:
86+
def sendRequest(self, plainMsg: RscpMessage) -> RscpMessage:
9487
"""Sending RSCP request.
9588
9689
Args:

e3dc/_e3dc_rscp_web.py

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
import struct
99
import threading
1010
import time
11-
from typing import Any, Callable
11+
from typing import Callable
1212

1313
import tzlocal
1414
from websocket import ABNF, WebSocketApp
1515

1616
from ._rscpLib import (
17+
RscpMessage,
1718
rscpDecode,
1819
rscpEncode,
1920
rscpFindTag,
@@ -132,11 +133,9 @@ def reset(self):
132133
self.virtConId = None
133134
self.virtAuthLevel = None
134135
self.webSerialno = None
135-
self.responseCallback: Callable[
136-
[tuple[str | int | RscpTag, str | int | RscpType, Any]], None
137-
]
136+
self.responseCallback: Callable[[RscpMessage], None]
138137
self.responseCallbackCalled = False
139-
self.requestResult: tuple[str | int | RscpTag, str | int | RscpType, Any]
138+
self.requestResult: RscpMessage
140139

141140
def buildVirtualConn(self):
142141
"""Method to create Virtual Connection."""
@@ -161,9 +160,7 @@ def buildVirtualConn(self):
161160
# print("--------------------- Sending virtual conn")
162161
self.ws.send(virtualConn, ABNF.OPCODE_BINARY)
163162

164-
def respondToINFORequest(
165-
self, decoded: tuple[str | int | RscpTag, str | int | RscpType, Any]
166-
):
163+
def respondToINFORequest(self, decoded: RscpMessage):
167164
"""Create Response to INFO request."""
168165
TIMEZONE_STR, utcDiffS = calcTimeZone()
169166

@@ -228,9 +225,7 @@ def respondToINFORequest(
228225
return ""
229226
return None # this is no standard request
230227

231-
def registerConnectionHandler(
232-
self, decodedMsg: tuple[str | int | RscpTag, str | int | RscpType, Any]
233-
):
228+
def registerConnectionHandler(self, decodedMsg: RscpMessage):
234229
"""Registering Connection Handler."""
235230
if self.conId == 0:
236231
self.conId = rscpFindTagIndex(decodedMsg, RscpTag.SERVER_CONNECTION_ID)
@@ -324,14 +319,10 @@ def on_message(self, message: bytes):
324319
ABNF.OPCODE_BINARY,
325320
)
326321

327-
def _defaultRequestCallback(
328-
self, msg: tuple[str | int | RscpTag, str | int | RscpType, Any]
329-
):
322+
def _defaultRequestCallback(self, msg: RscpMessage):
330323
self.requestResult = msg
331324

332-
def sendRequest(
333-
self, message: tuple[str | int | RscpTag, str | int | RscpType, Any]
334-
) -> tuple[str | int | RscpTag, str | int | RscpType, Any]:
325+
def sendRequest(self, message: RscpMessage) -> RscpMessage:
335326
"""Send a request and wait for a response."""
336327
self._sendRequest_internal(rscpFrame(rscpEncode(message)))
337328
for _ in range(self.TIMEOUT * 10):
@@ -343,24 +334,19 @@ def sendRequest(
343334

344335
return self.requestResult
345336

346-
def sendCommand(
347-
self, message: tuple[str | int | RscpTag, str | int | RscpType, Any]
348-
):
337+
def sendCommand(self, message: RscpMessage):
349338
"""Send a command."""
350339
return self._sendRequest_internal(rscpFrame(rscpEncode(message)))
351340

352341
def _sendRequest_internal(
353342
self,
354-
innerFrame: bytes | tuple[str | int | RscpTag, str | int | RscpType, Any],
355-
callback: (
356-
Callable[[tuple[str | int | RscpTag, str | int | RscpType, Any]], None]
357-
| None
358-
) = None,
343+
innerFrame: bytes | RscpMessage,
344+
callback: Callable[[RscpMessage], None] | None = None,
359345
):
360346
"""Internal send request method.
361347
362348
Args:
363-
innerFrame (Union[tuple, <RSCP encoded frame>]): inner frame
349+
innerFrame (tuple | bytes): inner frame
364350
callback (str): callback method
365351
synchronous (bool): If True, the method waits for a response (i.e. exits after calling callback).
366352
If True and callback = None, the method returns the (last) response message

e3dc/_rscpLib.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import struct
99
import time
1010
import zlib
11-
from typing import Any, cast
11+
from typing import Any, TypeAlias, cast
1212

1313
from ._rscpTags import (
1414
RscpTag,
@@ -21,6 +21,9 @@
2121
getStrRscpType,
2222
)
2323

24+
# Type alias for RSCP messages
25+
RscpMessage: TypeAlias = tuple[str | int | RscpTag, str | int | RscpType, Any]
26+
2427
DEBUG_DICT = {"print_rscp": False}
2528

2629

@@ -60,9 +63,9 @@ def set_debug(debug: bool):
6063

6164

6265
def rscpFindTag(
63-
decodedMsg: tuple[str | int | RscpTag, str | int | RscpType, Any] | None,
66+
decodedMsg: RscpMessage | None,
6467
tag: int | str | RscpTag,
65-
) -> tuple[str | int | RscpTag, str | int | RscpType, Any] | None:
68+
) -> RscpMessage | None:
6669
"""Finds a submessage with a specific tag.
6770
6871
Args:
@@ -83,9 +86,7 @@ def rscpFindTag(
8386
if decodedMsg[0] == tagStr:
8487
return decodedMsg
8588
if isinstance(decodedMsg[2], list):
86-
msgList: list[tuple[str | int | RscpTag, str | int | RscpType, Any]] = cast(
87-
list[tuple[str | int | RscpTag, str | int | RscpType, Any]], decodedMsg[2]
88-
)
89+
msgList: list[RscpMessage] = cast(list[RscpMessage], decodedMsg[2])
8990
for msg in msgList:
9091
msgValue = rscpFindTag(msg, tag)
9192
if msgValue is not None:
@@ -94,7 +95,7 @@ def rscpFindTag(
9495

9596

9697
def rscpFindTagIndex(
97-
decodedMsg: tuple[str | int | RscpTag, str | int | RscpType, Any] | None,
98+
decodedMsg: RscpMessage | None,
9899
tag: int | str | RscpTag,
99100
index: int = 2,
100101
) -> Any:
@@ -127,7 +128,7 @@ class FrameError(Exception):
127128

128129

129130
def rscpEncode(
130-
tag: int | str | RscpTag | tuple[str | int | RscpTag, str | int | RscpType, Any],
131+
tag: int | str | RscpTag | RscpMessage,
131132
rscptype: int | str | RscpType | None = None,
132133
data: Any = None,
133134
) -> bytes:
@@ -172,9 +173,7 @@ def rscpEncode(
172173
elif rscptype == RscpType.Container:
173174
if isinstance(data, list):
174175
newData = b""
175-
dataList: list[tuple[str | int | RscpTag, str | int | RscpType, Any]] = (
176-
cast(list[tuple[str | int | RscpTag, str | int | RscpType, Any]], data)
177-
)
176+
dataList: list[RscpMessage] = cast(list[RscpMessage], data)
178177
for dataChunk in dataList:
179178
newData += rscpEncode(
180179
dataChunk[0], dataChunk[1], dataChunk[2]
@@ -248,7 +247,7 @@ def rscpFrameDecode(frameData: bytes, returnFrameLen: bool = False):
248247

249248
def rscpDecode(
250249
data: bytes,
251-
) -> tuple[tuple[str | int | RscpTag, str | int | RscpType, Any], int]:
250+
) -> tuple[RscpMessage, int]:
252251
"""Decodes RSCP data."""
253252
headerFmt = (
254253
"<IBH" # format of header: little-endian, Uint32 tag, Uint8 type, Uint16 length
@@ -273,7 +272,7 @@ def rscpDecode(
273272

274273
if type_ == RscpType.Container:
275274
# this is a container: parse the inside
276-
dataList: list[tuple[str | int | RscpTag, str | int | RscpType, Any]] = []
275+
dataList: list[RscpMessage] = []
277276
curByte = headerSize
278277
while curByte < headerSize + length:
279278
innerData, usedLength = rscpDecode(data[curByte:])

0 commit comments

Comments
 (0)