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