|
| 1 | +import espnow |
| 2 | +import json |
| 3 | + |
| 4 | +class CLIENT: |
| 5 | + def __init__(self, *, timeout: int=5) -> None: |
| 6 | + self.esp = espnow.ESPNow() |
| 7 | + self.timeout = timeout |
| 8 | + |
| 9 | + def configure(self, *, timeout: int=5) -> None: |
| 10 | + self.timeout: int = timeout |
| 11 | + |
| 12 | + def connect(self, peer: str) -> None: |
| 13 | + self.peer: str = peer |
| 14 | + self.esp.active(True) |
| 15 | + self.esp.add_peer(self.peer) |
| 16 | + |
| 17 | + def send_message(self, data: str) -> bool: |
| 18 | + """ |
| 19 | + Send a string |
| 20 | +
|
| 21 | + Args: |
| 22 | +
|
| 23 | + data (str): Data to be sent |
| 24 | +
|
| 25 | + Returns: |
| 26 | +
|
| 27 | + bool: Confirmation flag (`True` if data was received, `False` otherwise) |
| 28 | + """ |
| 29 | + |
| 30 | + ack: bool = self.esp.send(self.peer, data) |
| 31 | + return ack |
| 32 | + |
| 33 | + def receive_message(self, recv_timeout :int= 5) -> list | None: |
| 34 | + """ |
| 35 | + Receive a string |
| 36 | +
|
| 37 | + Args: |
| 38 | +
|
| 39 | + recv_timeout (int, optional): Reception timeout. Defaults to 5. |
| 40 | +
|
| 41 | + Returns: |
| 42 | +
|
| 43 | + list | None: `[<sender's mac address (str)>, <message (str)>]` | `None` if no message is received |
| 44 | + """ |
| 45 | + |
| 46 | + received = self.esp.recv(recv_timeout) |
| 47 | + if received[0] is None: |
| 48 | + return |
| 49 | + return received |
| 50 | + |
| 51 | + def send_txt(self, filename: str) -> bool: |
| 52 | + """ |
| 53 | + Parse and send a `.txt` file as a `string` |
| 54 | +
|
| 55 | + Args: |
| 56 | +
|
| 57 | + filename (str): Filepath of the desired file to be sent with file name and extension |
| 58 | +
|
| 59 | + Returns: |
| 60 | +
|
| 61 | + sent (bool): Confirmation flag (`True` if data was received, `False` otherwise) |
| 62 | + """ |
| 63 | + |
| 64 | + with open(filename, 'r') as f: |
| 65 | + data: str = str(f.readlines()) |
| 66 | + sent: bool = self.send_message(data) |
| 67 | + return sent |
| 68 | + |
| 69 | + def send_json(self, filename: str, *, indent: int=4) -> bool: |
| 70 | + """ |
| 71 | + Parse and send a `.json` file as a `string` |
| 72 | +
|
| 73 | + Args: |
| 74 | +
|
| 75 | + filename (str): Filepath of the desired file to be sent with file name and extension |
| 76 | +
|
| 77 | + indent (int, optional): Desired indent of the resulting parsed `string` (for formatting purposes). Defaults to 4. |
| 78 | +
|
| 79 | + Returns: |
| 80 | +
|
| 81 | + sent (bool): Confirmation flag (`True` if data was received, `False` otherwise) |
| 82 | + """ |
| 83 | + |
| 84 | + with open(filename, 'r') as f: |
| 85 | + unparsed = json.load(f) |
| 86 | + parsed: str = json.dumps(unparsed, indent=indent) |
| 87 | + sent: bool = self.send_message(parsed) |
| 88 | + return sent |
| 89 | + |
| 90 | + def receive_to_txt(self, target_file: str, mode: str='a') -> bool: |
| 91 | + """ |
| 92 | + Write received `string` into a `.txt` file. |
| 93 | +
|
| 94 | + **Will not write or create file if no data is received** |
| 95 | +
|
| 96 | + Args: |
| 97 | +
|
| 98 | + target_file (str): Filepath of the destination file for the received data with file name and extension. |
| 99 | +
|
| 100 | + mode (str, optional): Editing mode |
| 101 | +
|
| 102 | + - `r` - Read only |
| 103 | +
|
| 104 | + - `w` - Write only (truncates file before writing) |
| 105 | +
|
| 106 | + - `x` - Create a new file and open it for writing (raises `FileExistsError` if file already exists) |
| 107 | +
|
| 108 | + - `a` - Append to the end of the file (default) |
| 109 | +
|
| 110 | + - `b` - Binary mode |
| 111 | +
|
| 112 | + - `t` - Text mode |
| 113 | +
|
| 114 | + - `+` - Update (read and write) |
| 115 | +
|
| 116 | + Read `open()`_ for more information |
| 117 | +
|
| 118 | + Returns: |
| 119 | +
|
| 120 | + received (bool): Confirmation flag (`True` if data was received, `False` otherwise) |
| 121 | +
|
| 122 | + .. _open(): https://docs.python.org/3/library/functions.html#open |
| 123 | + """ |
| 124 | + |
| 125 | + if ".txt" not in target_file: |
| 126 | + raise SyntaxError("File format must be .txt") |
| 127 | + try: |
| 128 | + received: bool = False |
| 129 | + data: list | None = self.receive_message() |
| 130 | + if data is None: |
| 131 | + return received |
| 132 | + data_list: list[str] = str(data[-1]).split("\n") |
| 133 | + if data_list[-1] == "": |
| 134 | + data_list = data_list[:-1] |
| 135 | + with open(target_file, mode) as f: |
| 136 | + f.writelines(data_list) |
| 137 | + return not received |
| 138 | + except SyntaxError: |
| 139 | + raise |
| 140 | + |
| 141 | + def receive_to_json(self, target_file: str, mode: str='a') -> bool: |
| 142 | + """ |
| 143 | + Write received `string` into a `.json` file. |
| 144 | +
|
| 145 | + **Will not write or create file if no data is received** |
| 146 | +
|
| 147 | + Args: |
| 148 | +
|
| 149 | + target_file (str): Filepath of the destination file for the received data with file name and extension. |
| 150 | +
|
| 151 | + mode (str, optional): Editing mode |
| 152 | +
|
| 153 | + - `r` - Read only |
| 154 | +
|
| 155 | + - `w` - Write only (truncates file before writing) |
| 156 | +
|
| 157 | + - `x` - Create a new file and open it for writing (raises `FileExistsError` if file already exists) |
| 158 | +
|
| 159 | + - `a` - Append to the end of the file (default) |
| 160 | +
|
| 161 | + - `b` - Binary mode |
| 162 | +
|
| 163 | + - `t` - Text mode |
| 164 | +
|
| 165 | + - `+` - Update (read and write) |
| 166 | +
|
| 167 | + Read `open()`_ for more information |
| 168 | +
|
| 169 | + Returns: |
| 170 | +
|
| 171 | + received (bool): Confirmation flag (`True` if data was received, `False` otherwise) |
| 172 | +
|
| 173 | + .. _open(): https://docs.python.org/3/library/functions.html#open |
| 174 | + """ |
| 175 | + |
| 176 | + if ".json" not in target_file: |
| 177 | + raise SyntaxError("File format must be .json") |
| 178 | + try: |
| 179 | + received: bool = False |
| 180 | + data: list | None = self.receive_message() |
| 181 | + if data is None: |
| 182 | + return received |
| 183 | + mac: str = str(data[0]) |
| 184 | + message = json.loads(str(data[-1])) |
| 185 | + unparsed: dict = { |
| 186 | + "mac": mac, |
| 187 | + "message": message |
| 188 | + } |
| 189 | + with open(target_file, mode) as f: |
| 190 | + json.dump(unparsed, f) |
| 191 | + return not received |
| 192 | + except SyntaxError: |
| 193 | + raise |
| 194 | + |
| 195 | + def receive_to_dict(self) -> dict: |
| 196 | + """ |
| 197 | + Unparses received `string` into a `dict` object |
| 198 | +
|
| 199 | + Args: |
| 200 | +
|
| 201 | + None: |
| 202 | +
|
| 203 | + Returns: |
| 204 | +
|
| 205 | + unparsed (dict): `dictionary` object containing unparsed equivalent of the received `.json` |
| 206 | + """ |
| 207 | + |
| 208 | + data: list | None = self.receive_message() |
| 209 | + if data is None: |
| 210 | + return {} |
| 211 | + mac: str = str(data[0]) |
| 212 | + message = json.loads(str(data[-1])) |
| 213 | + unparsed: dict = { |
| 214 | + "mac": mac, |
| 215 | + "message": message |
| 216 | + } |
| 217 | + return unparsed |
0 commit comments