iqrfpy.iresponse
Response message class abstraction.
1"""Response message class abstraction.""" 2 3from abc import ABC, abstractmethod 4from typing import List, Optional, Union 5from iqrfpy.enums.commands import Command 6from iqrfpy.enums.message_types import MessageType 7from iqrfpy.enums.peripherals import Peripheral 8import iqrfpy.utils.dpa as dpa_constants 9from iqrfpy.utils.validators import DpaValidator 10 11__all__ = ['IResponse', 'IResponseGetterMixin'] 12 13 14class IResponse(ABC): 15 """This class serves as an interface for embedded peripherals and standards response messages. 16 17 This class does not provide any deserialization logic in its factory methods as they simply serve as abstracts. 18 19 Once a factory method is implemented, it should be used to parse responses and create response message objects. 20 """ 21 22 ASYNC_MSGID = 'async' 23 24 def __init__(self, nadr: int, pnum: Union[Peripheral, int], pcmd: Union[Command, int], 25 hwpid: int = dpa_constants.HWPID_MAX, rcode: int = 0, dpa_value: int = 0, 26 pdata: Optional[List[int]] = None, m_type: Optional[Union[MessageType, str]] = None, 27 msgid: Optional[str] = None, result: Optional[dict] = None): 28 self._nadr = nadr 29 self._pnum = pnum 30 self._pcmd = pcmd 31 self._mtype = m_type 32 self._hwpid = hwpid 33 self._rcode = rcode 34 self._dpa_value = dpa_value 35 self._pdata = pdata 36 self._msgid = msgid 37 self._result = result 38 39 @property 40 @abstractmethod 41 def nadr(self) -> int: 42 """:obj:`int`: Device address. 43 44 Getter only. 45 """ 46 return self._nadr 47 48 @property 49 @abstractmethod 50 def pnum(self) -> Union[Peripheral, int]: 51 """:obj:`Peripheral` or :obj:`int`: Peripheral number. 52 53 Getter only. 54 """ 55 return self._pnum 56 57 @property 58 @abstractmethod 59 def pcmd(self) -> Union[Command, int]: 60 """:obj:`Command` or :obj:`int`: Peripheral command. 61 62 Getter only. 63 """ 64 return self._pcmd 65 66 @property 67 @abstractmethod 68 def mtype(self) -> Optional[Union[MessageType, str]]: 69 """:obj:`MessageType` or :obj:`str` or :obj:`None`: Message type. 70 71 Getter only. 72 """ 73 return self._mtype 74 75 @property 76 @abstractmethod 77 def hwpid(self) -> int: 78 """:obj:`int`: Hardware profile ID. 79 80 Getter only. 81 """ 82 return self._hwpid 83 84 @property 85 @abstractmethod 86 def rcode(self) -> int: 87 """:obj:`int`: DPA error code. 88 89 Getter only. 90 """ 91 return self._rcode 92 93 @property 94 @abstractmethod 95 def dpa_value(self) -> int: 96 """:obj:`int`: DPA value. 97 98 Getter only. 99 """ 100 return self._dpa_value 101 102 @property 103 @abstractmethod 104 def pdata(self) -> Optional[List[int]]: 105 """:obj:`list` of :obj:`int`: DPA response data. 106 107 Getter only. 108 """ 109 return self._pdata 110 111 @property 112 @abstractmethod 113 def result(self) -> Optional[dict]: 114 """:obj:`dict` or :obj:`None`: JSON API response data. 115 116 Getter only. 117 """ 118 return self._result 119 120 @property 121 @abstractmethod 122 def msgid(self) -> Optional[str]: 123 """:obj:`str` or :obj:`None`: Message ID. 124 125 Getter only. 126 """ 127 return self._msgid 128 129 @staticmethod 130 def validate_dpa_response(data: bytes) -> None: 131 """Validate DPA response for base response length. 132 133 Args: 134 data (bytes): DPA response. 135 """ 136 DpaValidator.base_response_length(data) 137 138 @classmethod 139 @abstractmethod 140 def from_dpa(cls, dpa: bytes) -> 'IResponse': 141 """Factory method. Parse DPA response into Response object. 142 143 Args: 144 dpa (bytes): DPA response. 145 """ 146 147 @classmethod 148 @abstractmethod 149 def from_json(cls, json: dict) -> 'IResponse': 150 """Factory method. Parse JSON response into Response object. 151 152 Args: 153 json (dict): JSON API response. 154 """ 155 156 157class IResponseGetterMixin(IResponse): 158 """Response mixin.""" 159 160 @property 161 def nadr(self) -> int: 162 """:obj:`int`: Device address. 163 164 Getter only. 165 """ 166 return super().nadr 167 168 @property 169 def pnum(self) -> Union[Peripheral, int]: 170 """:obj:`Peripheral` or :obj:`int`: Peripheral number. 171 172 Getter only. 173 """ 174 return super().pnum 175 176 @property 177 def pcmd(self) -> Union[Command, int]: 178 """:obj:`Command` or :obj:`int`: Peripheral command. 179 180 Getter only. 181 """ 182 return super().pcmd 183 184 @property 185 def mtype(self) -> Union[MessageType, str]: 186 """:obj:`MessageType` or :obj:`str` or :obj:`None`: Message type. 187 188 Getter only. 189 """ 190 return super().mtype 191 192 @property 193 def hwpid(self) -> int: 194 """:obj:`int`: Hardware profile ID. 195 196 Getter only. 197 """ 198 return super().hwpid 199 200 @property 201 def rcode(self) -> int: 202 """:obj:`int`: DPA error code. 203 204 Getter only. 205 """ 206 return super().rcode 207 208 def get_rcode_as_string(self) -> str: 209 """Return rcode as string. 210 211 Returns: 212 str: String representation of rcode. 213 """ 214 return dpa_constants.ResponseCodes.to_string(self.rcode) 215 216 @property 217 def dpa_value(self) -> int: 218 """:obj:`int`: DPA value. 219 220 Getter only. 221 """ 222 return super().dpa_value 223 224 @property 225 def pdata(self) -> Optional[List[int]]: 226 """:obj:`list` of :obj:`int`: DPA response data. 227 228 Getter only. 229 """ 230 return super().pdata 231 232 @property 233 def result(self) -> Optional[dict]: 234 """:obj:`dict` or :obj:`None`: JSON API response data. 235 236 Getter only. 237 """ 238 return super().result 239 240 @property 241 def msgid(self) -> Optional[str]: 242 """:obj:`str` or :obj:`None`: Message ID. 243 244 Getter only. 245 """ 246 return super().msgid 247 248 @classmethod 249 def from_dpa(cls, dpa: bytes) -> IResponse: 250 """Factory method. Parse DPA response into Response object. 251 252 Args: 253 dpa (bytes): DPA response. 254 255 Raises: 256 NotImplementedError: Factory method for mixin not implemented. 257 """ 258 raise NotImplementedError('from_dpa() method not implemented.') 259 260 @classmethod 261 def from_json(cls, json: dict) -> IResponse: 262 """Factory method. Parse JSON response into Response object. 263 264 Args: 265 json (dict): JSON API response. 266 267 Raises: 268 NotImplementedError: Factory method for mixin not implemented. 269 """ 270 raise NotImplementedError('from_json() method not implemented.')
15class IResponse(ABC): 16 """This class serves as an interface for embedded peripherals and standards response messages. 17 18 This class does not provide any deserialization logic in its factory methods as they simply serve as abstracts. 19 20 Once a factory method is implemented, it should be used to parse responses and create response message objects. 21 """ 22 23 ASYNC_MSGID = 'async' 24 25 def __init__(self, nadr: int, pnum: Union[Peripheral, int], pcmd: Union[Command, int], 26 hwpid: int = dpa_constants.HWPID_MAX, rcode: int = 0, dpa_value: int = 0, 27 pdata: Optional[List[int]] = None, m_type: Optional[Union[MessageType, str]] = None, 28 msgid: Optional[str] = None, result: Optional[dict] = None): 29 self._nadr = nadr 30 self._pnum = pnum 31 self._pcmd = pcmd 32 self._mtype = m_type 33 self._hwpid = hwpid 34 self._rcode = rcode 35 self._dpa_value = dpa_value 36 self._pdata = pdata 37 self._msgid = msgid 38 self._result = result 39 40 @property 41 @abstractmethod 42 def nadr(self) -> int: 43 """:obj:`int`: Device address. 44 45 Getter only. 46 """ 47 return self._nadr 48 49 @property 50 @abstractmethod 51 def pnum(self) -> Union[Peripheral, int]: 52 """:obj:`Peripheral` or :obj:`int`: Peripheral number. 53 54 Getter only. 55 """ 56 return self._pnum 57 58 @property 59 @abstractmethod 60 def pcmd(self) -> Union[Command, int]: 61 """:obj:`Command` or :obj:`int`: Peripheral command. 62 63 Getter only. 64 """ 65 return self._pcmd 66 67 @property 68 @abstractmethod 69 def mtype(self) -> Optional[Union[MessageType, str]]: 70 """:obj:`MessageType` or :obj:`str` or :obj:`None`: Message type. 71 72 Getter only. 73 """ 74 return self._mtype 75 76 @property 77 @abstractmethod 78 def hwpid(self) -> int: 79 """:obj:`int`: Hardware profile ID. 80 81 Getter only. 82 """ 83 return self._hwpid 84 85 @property 86 @abstractmethod 87 def rcode(self) -> int: 88 """:obj:`int`: DPA error code. 89 90 Getter only. 91 """ 92 return self._rcode 93 94 @property 95 @abstractmethod 96 def dpa_value(self) -> int: 97 """:obj:`int`: DPA value. 98 99 Getter only. 100 """ 101 return self._dpa_value 102 103 @property 104 @abstractmethod 105 def pdata(self) -> Optional[List[int]]: 106 """:obj:`list` of :obj:`int`: DPA response data. 107 108 Getter only. 109 """ 110 return self._pdata 111 112 @property 113 @abstractmethod 114 def result(self) -> Optional[dict]: 115 """:obj:`dict` or :obj:`None`: JSON API response data. 116 117 Getter only. 118 """ 119 return self._result 120 121 @property 122 @abstractmethod 123 def msgid(self) -> Optional[str]: 124 """:obj:`str` or :obj:`None`: Message ID. 125 126 Getter only. 127 """ 128 return self._msgid 129 130 @staticmethod 131 def validate_dpa_response(data: bytes) -> None: 132 """Validate DPA response for base response length. 133 134 Args: 135 data (bytes): DPA response. 136 """ 137 DpaValidator.base_response_length(data) 138 139 @classmethod 140 @abstractmethod 141 def from_dpa(cls, dpa: bytes) -> 'IResponse': 142 """Factory method. Parse DPA response into Response object. 143 144 Args: 145 dpa (bytes): DPA response. 146 """ 147 148 @classmethod 149 @abstractmethod 150 def from_json(cls, json: dict) -> 'IResponse': 151 """Factory method. Parse JSON response into Response object. 152 153 Args: 154 json (dict): JSON API response. 155 """
This class serves as an interface for embedded peripherals and standards response messages.
This class does not provide any deserialization logic in its factory methods as they simply serve as abstracts.
Once a factory method is implemented, it should be used to parse responses and create response message objects.
Peripheral
or int
: Peripheral number.
Getter only.
MessageType
or str
or None
: Message type.
Getter only.
130 @staticmethod 131 def validate_dpa_response(data: bytes) -> None: 132 """Validate DPA response for base response length. 133 134 Args: 135 data (bytes): DPA response. 136 """ 137 DpaValidator.base_response_length(data)
Validate DPA response for base response length.
Arguments:
- data (bytes): DPA response.
139 @classmethod 140 @abstractmethod 141 def from_dpa(cls, dpa: bytes) -> 'IResponse': 142 """Factory method. Parse DPA response into Response object. 143 144 Args: 145 dpa (bytes): DPA response. 146 """
Factory method. Parse DPA response into Response object.
Arguments:
- dpa (bytes): DPA response.
148 @classmethod 149 @abstractmethod 150 def from_json(cls, json: dict) -> 'IResponse': 151 """Factory method. Parse JSON response into Response object. 152 153 Args: 154 json (dict): JSON API response. 155 """
Factory method. Parse JSON response into Response object.
Arguments:
- json (dict): JSON API response.
158class IResponseGetterMixin(IResponse): 159 """Response mixin.""" 160 161 @property 162 def nadr(self) -> int: 163 """:obj:`int`: Device address. 164 165 Getter only. 166 """ 167 return super().nadr 168 169 @property 170 def pnum(self) -> Union[Peripheral, int]: 171 """:obj:`Peripheral` or :obj:`int`: Peripheral number. 172 173 Getter only. 174 """ 175 return super().pnum 176 177 @property 178 def pcmd(self) -> Union[Command, int]: 179 """:obj:`Command` or :obj:`int`: Peripheral command. 180 181 Getter only. 182 """ 183 return super().pcmd 184 185 @property 186 def mtype(self) -> Union[MessageType, str]: 187 """:obj:`MessageType` or :obj:`str` or :obj:`None`: Message type. 188 189 Getter only. 190 """ 191 return super().mtype 192 193 @property 194 def hwpid(self) -> int: 195 """:obj:`int`: Hardware profile ID. 196 197 Getter only. 198 """ 199 return super().hwpid 200 201 @property 202 def rcode(self) -> int: 203 """:obj:`int`: DPA error code. 204 205 Getter only. 206 """ 207 return super().rcode 208 209 def get_rcode_as_string(self) -> str: 210 """Return rcode as string. 211 212 Returns: 213 str: String representation of rcode. 214 """ 215 return dpa_constants.ResponseCodes.to_string(self.rcode) 216 217 @property 218 def dpa_value(self) -> int: 219 """:obj:`int`: DPA value. 220 221 Getter only. 222 """ 223 return super().dpa_value 224 225 @property 226 def pdata(self) -> Optional[List[int]]: 227 """:obj:`list` of :obj:`int`: DPA response data. 228 229 Getter only. 230 """ 231 return super().pdata 232 233 @property 234 def result(self) -> Optional[dict]: 235 """:obj:`dict` or :obj:`None`: JSON API response data. 236 237 Getter only. 238 """ 239 return super().result 240 241 @property 242 def msgid(self) -> Optional[str]: 243 """:obj:`str` or :obj:`None`: Message ID. 244 245 Getter only. 246 """ 247 return super().msgid 248 249 @classmethod 250 def from_dpa(cls, dpa: bytes) -> IResponse: 251 """Factory method. Parse DPA response into Response object. 252 253 Args: 254 dpa (bytes): DPA response. 255 256 Raises: 257 NotImplementedError: Factory method for mixin not implemented. 258 """ 259 raise NotImplementedError('from_dpa() method not implemented.') 260 261 @classmethod 262 def from_json(cls, json: dict) -> IResponse: 263 """Factory method. Parse JSON response into Response object. 264 265 Args: 266 json (dict): JSON API response. 267 268 Raises: 269 NotImplementedError: Factory method for mixin not implemented. 270 """ 271 raise NotImplementedError('from_json() method not implemented.')
Response mixin.
Peripheral
or int
: Peripheral number.
Getter only.
MessageType
or str
or None
: Message type.
Getter only.
209 def get_rcode_as_string(self) -> str: 210 """Return rcode as string. 211 212 Returns: 213 str: String representation of rcode. 214 """ 215 return dpa_constants.ResponseCodes.to_string(self.rcode)
Return rcode as string.
Returns:
str: String representation of rcode.
249 @classmethod 250 def from_dpa(cls, dpa: bytes) -> IResponse: 251 """Factory method. Parse DPA response into Response object. 252 253 Args: 254 dpa (bytes): DPA response. 255 256 Raises: 257 NotImplementedError: Factory method for mixin not implemented. 258 """ 259 raise NotImplementedError('from_dpa() method not implemented.')
Factory method. Parse DPA response into Response object.
Arguments:
- dpa (bytes): DPA response.
Raises:
- NotImplementedError: Factory method for mixin not implemented.
261 @classmethod 262 def from_json(cls, json: dict) -> IResponse: 263 """Factory method. Parse JSON response into Response object. 264 265 Args: 266 json (dict): JSON API response. 267 268 Raises: 269 NotImplementedError: Factory method for mixin not implemented. 270 """ 271 raise NotImplementedError('from_json() method not implemented.')
Factory method. Parse JSON response into Response object.
Arguments:
- json (dict): JSON API response.
Raises:
- NotImplementedError: Factory method for mixin not implemented.