Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defining types in return dictionaries #1923

Merged
merged 5 commits into from
Aug 31, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions python/ccp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,30 @@
import struct
from enum import IntEnum, Enum

from typing import TypedDict, Optional

class ExchangeStationIdsReturn(TypedDict):
id_length: int
MarinkoMagla marked this conversation as resolved.
Show resolved Hide resolved
data_type: int
available: int
protected: int

class GetDaqListSizeReturn(TypedDict):
list_size: int
first_pid: int

class GetSessionStatusReturn(TypedDict):
status: int
info: Optional[int]

class DiagnosticServiceReturn(TypedDict):
length: int
type: int

class ActionServiceReturn(TypedDict):
length: int
type: int
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we're gonna do this, might as well just return the class instead of a dict. what do you think @gregjhogan?


class COMMAND_CODE(IntEnum):
CONNECT = 0x01
SET_MTA = 0x02
Expand Down Expand Up @@ -140,10 +164,10 @@ def connect(self, station_addr: int) -> None:
self._send_cro(COMMAND_CODE.CONNECT, struct.pack("<H", station_addr))
self._recv_dto(0.025)

def exchange_station_ids(self, device_id_info: bytes = b"") -> dict:
def exchange_station_ids(self, device_id_info: bytes = b"") -> ExchangeStationIdsReturn:
self._send_cro(COMMAND_CODE.EXCHANGE_ID, device_id_info)
resp = self._recv_dto(0.025)
return { # TODO: define a type
return {
"id_length": resp[0],
"data_type": resp[1],
"available": resp[2],
Expand Down Expand Up @@ -211,12 +235,12 @@ def select_calibration_page(self) -> None:
self._send_cro(COMMAND_CODE.SELECT_CAL_PAGE)
self._recv_dto(0.025)

def get_daq_list_size(self, list_num: int, can_id: int = 0) -> dict:
def get_daq_list_size(self, list_num: int, can_id: int = 0) -> GetDaqListSizeReturn:
if list_num > 255:
raise ValueError("list number must be less than 256")
self._send_cro(COMMAND_CODE.GET_DAQ_SIZE, bytes([list_num, 0]) + struct.pack(f"{self.byte_order.value}I", can_id))
resp = self._recv_dto(0.025)
return { # TODO: define a type
return {
"list_size": resp[0],
"first_pid": resp[1],
}
Expand Down Expand Up @@ -266,10 +290,10 @@ def set_session_status(self, status: int) -> None:
self._send_cro(COMMAND_CODE.SET_S_STATUS, bytes([status]))
self._recv_dto(0.025)

def get_session_status(self) -> dict:
def get_session_status(self) -> GetSessionStatusReturn:
self._send_cro(COMMAND_CODE.GET_S_STATUS)
resp = self._recv_dto(0.025)
return { # TODO: define a type
return {
"status": resp[0],
"info": resp[2] if resp[1] else None,
}
Expand Down Expand Up @@ -310,26 +334,26 @@ def move_memory_block(self, size: int) -> None:
self._send_cro(COMMAND_CODE.MOVE, struct.pack(f"{self.byte_order.value}I", size))
self._recv_dto(0.025)

def diagnostic_service(self, service_num: int, data: bytes = b"") -> dict:
def diagnostic_service(self, service_num: int, data: bytes = b"") -> DiagnosticServiceReturn:
if service_num > 65535:
raise ValueError("service number must be less than 65536")
if len(data) > 4:
raise ValueError("max data size is 4 bytes")
self._send_cro(COMMAND_CODE.DIAG_SERVICE, struct.pack(f"{self.byte_order.value}H", service_num) + data)
resp = self._recv_dto(0.025)
return { # TODO: define a type
return {
"length": resp[0],
"type": resp[1],
}

def action_service(self, service_num: int, data: bytes = b"") -> dict:
def action_service(self, service_num: int, data: bytes = b"") -> ActionServiceReturn:
if service_num > 65535:
raise ValueError("service number must be less than 65536")
if len(data) > 4:
raise ValueError("max data size is 4 bytes")
self._send_cro(COMMAND_CODE.ACTION_SERVICE, struct.pack(f"{self.byte_order.value}H", service_num) + data)
resp = self._recv_dto(0.025)
return { # TODO: define a type
return {
"length": resp[0],
"type": resp[1],
}
Expand Down