|
1 |
| -from typing import Any, List |
| 1 | +from typing import Any, List, Optional, Tuple, Union |
2 | 2 |
|
3 | 3 | from .base import BaseSubAPI
|
4 | 4 | from ..lua import LuaNum
|
5 |
| -from ..rproc import nil, integer, boolean, array_integer |
| 5 | +from ..rproc import nil, integer, option_string, boolean, array_integer, option_integer, fact_option, fact_tuple |
| 6 | + |
| 7 | + |
| 8 | +recv_result = fact_option(fact_tuple( |
| 9 | + integer, |
| 10 | + lambda v: v, |
| 11 | + option_string, |
| 12 | + tail_nils=1, |
| 13 | +)) |
6 | 14 |
|
7 | 15 |
|
8 | 16 | class RednetAPI(BaseSubAPI):
|
| 17 | + CHANNEL_REPEAT = 65533 |
| 18 | + CHANNEL_BROADCAST = 65535 |
| 19 | + |
9 | 20 | async def open(self, side: str):
|
10 | 21 | return nil(await self._send('open', side))
|
11 | 22 |
|
12 |
| - async def close(self, side: str): |
| 23 | + async def close(self, side: str = None): |
13 | 24 | return nil(await self._send('close', side))
|
14 | 25 |
|
15 |
| - async def send(self, receiverID: int, message: Any, protocol: str = None): |
16 |
| - return nil(await self._send('send', receiverID, message, protocol)) |
| 26 | + async def send(self, receiverID: int, message: Any, protocol: str = None) -> bool: |
| 27 | + return boolean(await self._send('send', receiverID, message, protocol)) |
17 | 28 |
|
18 | 29 | async def broadcast(self, message: Any, protocol: str = None):
|
19 | 30 | return nil(await self._send('broadcast', message, protocol))
|
20 | 31 |
|
21 |
| - async def receive(self, protocolFilter: str = None, timeout: LuaNum = None) -> int: |
22 |
| - return integer(await self._send('receive', protocolFilter, timeout)) |
| 32 | + async def receive( |
| 33 | + self, protocolFilter: str = None, timeout: LuaNum = None, |
| 34 | + ) -> Optional[Tuple[int, Any, Optional[str]]]: |
| 35 | + return recv_result(await self._send('receive', protocolFilter, timeout)) |
23 | 36 |
|
24 |
| - async def isOpen(self, side: str) -> bool: |
| 37 | + async def isOpen(self, side: str = None) -> bool: |
25 | 38 | return boolean(await self._send('isOpen', side))
|
26 | 39 |
|
27 | 40 | async def host(self, protocol: str, hostname: str):
|
28 | 41 | return nil(await self._send('host', protocol, hostname))
|
29 | 42 |
|
30 |
| - async def unhost(self, protocol: str, hostname: str): |
31 |
| - return nil(await self._send('unhost', protocol, hostname)) |
32 |
| - |
33 |
| - async def lookup(self, protocol: str, hostname: str) -> List[int]: |
34 |
| - return array_integer(await self._send('lookup', protocol, hostname)) |
| 43 | + async def unhost(self, protocol: str): |
| 44 | + return nil(await self._send('unhost', protocol)) |
| 45 | + |
| 46 | + async def lookup(self, protocol: str, hostname: str = None) -> Union[Optional[int], List[int]]: |
| 47 | + result = await self._send('lookup', protocol, hostname) |
| 48 | + if hostname is None: |
| 49 | + if result is None: |
| 50 | + return [] |
| 51 | + if isinstance(result, list): |
| 52 | + return array_integer(result) |
| 53 | + return [integer(result)] |
| 54 | + else: |
| 55 | + return option_integer(result) |
0 commit comments