refactor: improve _get_data implementation slightly.
This commit is contained in:
@@ -22,6 +22,7 @@ from pyasic.config import MinerConfig
|
|||||||
from pyasic.data import Fan, HashBoard, MinerData
|
from pyasic.data import Fan, HashBoard, MinerData
|
||||||
from pyasic.data.device import DeviceInfo
|
from pyasic.data.device import DeviceInfo
|
||||||
from pyasic.data.error_codes import MinerErrorData
|
from pyasic.data.error_codes import MinerErrorData
|
||||||
|
from pyasic.data.pools import PoolMetrics
|
||||||
from pyasic.device import MinerModel
|
from pyasic.device import MinerModel
|
||||||
from pyasic.device.algorithm import MinerAlgo
|
from pyasic.device.algorithm import MinerAlgo
|
||||||
from pyasic.device.firmware import MinerFirmware
|
from pyasic.device.firmware import MinerFirmware
|
||||||
@@ -29,7 +30,6 @@ from pyasic.device.makes import MinerMake
|
|||||||
from pyasic.errors import APIError
|
from pyasic.errors import APIError
|
||||||
from pyasic.logger import logger
|
from pyasic.logger import logger
|
||||||
from pyasic.miners.data import DataLocations, DataOptions, RPCAPICommand, WebAPICommand
|
from pyasic.miners.data import DataLocations, DataOptions, RPCAPICommand, WebAPICommand
|
||||||
from pyasic.data.pools import PoolMetrics
|
|
||||||
|
|
||||||
|
|
||||||
class MinerProtocol(Protocol):
|
class MinerProtocol(Protocol):
|
||||||
@@ -343,7 +343,7 @@ class MinerProtocol(Protocol):
|
|||||||
return await self._get_uptime()
|
return await self._get_uptime()
|
||||||
|
|
||||||
async def get_pools(self) -> List[PoolMetrics]:
|
async def get_pools(self) -> List[PoolMetrics]:
|
||||||
""" Get the pools information from Miner.
|
"""Get the pools information from Miner.
|
||||||
|
|
||||||
Return:
|
Return:
|
||||||
The pool information of the miner.
|
The pool information of the miner.
|
||||||
@@ -410,50 +410,60 @@ class MinerProtocol(Protocol):
|
|||||||
include: List[Union[str, DataOptions]] = None,
|
include: List[Union[str, DataOptions]] = None,
|
||||||
exclude: List[Union[str, DataOptions]] = None,
|
exclude: List[Union[str, DataOptions]] = None,
|
||||||
) -> dict:
|
) -> dict:
|
||||||
|
# handle include
|
||||||
if include is not None:
|
if include is not None:
|
||||||
include = [str(i) for i in include]
|
include = [str(i) for i in include]
|
||||||
else:
|
else:
|
||||||
# everything
|
# everything
|
||||||
include = [str(enum_value.value) for enum_value in DataOptions]
|
include = [str(enum_value.value) for enum_value in DataOptions]
|
||||||
|
|
||||||
|
# handle exclude
|
||||||
|
# prioritized over include, including x and excluding x will exclude x
|
||||||
if exclude is not None:
|
if exclude is not None:
|
||||||
for item in exclude:
|
for item in exclude:
|
||||||
if str(item) in include:
|
if str(item) in include:
|
||||||
include.remove(str(item))
|
include.remove(str(item))
|
||||||
|
|
||||||
api_multicommand = set()
|
rpc_multicommand = set()
|
||||||
web_multicommand = []
|
web_multicommand = set()
|
||||||
|
# create multicommand
|
||||||
for data_name in include:
|
for data_name in include:
|
||||||
try:
|
try:
|
||||||
|
# get kwargs needed for the _get_xyz function
|
||||||
fn_args = getattr(self.data_locations, data_name).kwargs
|
fn_args = getattr(self.data_locations, data_name).kwargs
|
||||||
|
|
||||||
|
# keep track of which RPC/Web commands need to be sent
|
||||||
for arg in fn_args:
|
for arg in fn_args:
|
||||||
if isinstance(arg, RPCAPICommand):
|
if isinstance(arg, RPCAPICommand):
|
||||||
api_multicommand.add(arg.cmd)
|
rpc_multicommand.add(arg.cmd)
|
||||||
if isinstance(arg, WebAPICommand):
|
if isinstance(arg, WebAPICommand):
|
||||||
if arg.cmd not in web_multicommand:
|
web_multicommand.add(arg.cmd)
|
||||||
web_multicommand.append(arg.cmd)
|
|
||||||
except KeyError as e:
|
except KeyError as e:
|
||||||
logger.error(e, data_name)
|
logger.error(type(e), e, data_name)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if len(api_multicommand) > 0:
|
# create tasks for all commands that need to be sent, or no-op with sleep(0) -> None
|
||||||
api_command_task = asyncio.create_task(
|
if len(rpc_multicommand) > 0:
|
||||||
self.api.multicommand(*api_multicommand, allow_warning=allow_warning)
|
rpc_command_task = asyncio.create_task(
|
||||||
|
self.rpc.multicommand(*rpc_multicommand, allow_warning=allow_warning)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
api_command_task = asyncio.sleep(0)
|
rpc_command_task = asyncio.create_task(asyncio.sleep(0))
|
||||||
if len(web_multicommand) > 0:
|
if len(web_multicommand) > 0:
|
||||||
web_command_task = asyncio.create_task(
|
web_command_task = asyncio.create_task(
|
||||||
self.web.multicommand(*web_multicommand, allow_warning=allow_warning)
|
self.web.multicommand(*web_multicommand, allow_warning=allow_warning)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
web_command_task = asyncio.sleep(0)
|
web_command_task = asyncio.create_task(asyncio.sleep(0))
|
||||||
|
|
||||||
web_command_data = await web_command_task
|
# make sure the tasks complete
|
||||||
|
await asyncio.gather(rpc_command_task, web_command_task)
|
||||||
|
|
||||||
|
# grab data out of the tasks
|
||||||
|
web_command_data = web_command_task.result()
|
||||||
if web_command_data is None:
|
if web_command_data is None:
|
||||||
web_command_data = {}
|
web_command_data = {}
|
||||||
|
api_command_data = rpc_command_task.result()
|
||||||
api_command_data = await api_command_task
|
|
||||||
if api_command_data is None:
|
if api_command_data is None:
|
||||||
api_command_data = {}
|
api_command_data = {}
|
||||||
|
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ class MinersTest(unittest.TestCase):
|
|||||||
"wattage",
|
"wattage",
|
||||||
"voltage",
|
"voltage",
|
||||||
"wattage_limit",
|
"wattage_limit",
|
||||||
|
"pools",
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
warnings.filterwarnings("ignore")
|
warnings.filterwarnings("ignore")
|
||||||
|
|||||||
Reference in New Issue
Block a user