updated some comments and improved general readability

This commit is contained in:
UpstreamData
2021-11-03 14:45:31 -06:00
parent e7ea0629a4
commit da8d45a9b1
3 changed files with 9 additions and 3 deletions

View File

@@ -25,6 +25,7 @@ class BaseMinerAPI:
self.ip = ipaddress.ip_address(ip) self.ip = ipaddress.ip_address(ip)
def get_commands(self) -> list: def get_commands(self) -> list:
"""Get a list of command accessible to a specific type of API on the miner."""
return [func for func in return [func for func in
# each function in self # each function in self
dir(self) if callable(getattr(self, func)) and dir(self) if callable(getattr(self, func)) and
@@ -38,6 +39,7 @@ class BaseMinerAPI:
] ]
async def multicommand(self, *commands: str) -> dict: async def multicommand(self, *commands: str) -> dict:
"""Creates and sends multiple commands as one command to the miner."""
# split the commands into a proper list # split the commands into a proper list
commands = [*commands] commands = [*commands]
@@ -54,6 +56,7 @@ class BaseMinerAPI:
return await self.send_command(command) return await self.send_command(command)
async def send_command(self, command: str, parameters: str or int or bool = None) -> dict: async def send_command(self, command: str, parameters: str or int or bool = None) -> dict:
"""Send an API command to the miner and return the result."""
try: try:
# get reader and writer streams # get reader and writer streams
reader, writer = await asyncio.open_connection(str(self.ip), self.port) reader, writer = await asyncio.open_connection(str(self.ip), self.port)
@@ -110,7 +113,8 @@ class BaseMinerAPI:
return data return data
@staticmethod @staticmethod
def validate_command_output(data): def validate_command_output(data: dict) -> bool:
"""Check if the returned command output is correctly formatted."""
# check if the data returned is correct or an error # check if the data returned is correct or an error
# if status isn't a key, it is a multicommand # if status isn't a key, it is a multicommand
if "STATUS" not in data.keys(): if "STATUS" not in data.keys():
@@ -131,7 +135,8 @@ class BaseMinerAPI:
return True return True
@staticmethod @staticmethod
def load_api_data(data): def load_api_data(data: bytes) -> None:
"""Convert API data from JSON to dict"""
try: try:
# some json from the API returns with a null byte (\x00) on the end # some json from the API returns with a null byte (\x00) on the end
if data.endswith(b"\x00"): if data.endswith(b"\x00"):

View File

@@ -40,6 +40,7 @@ class MinerFactory:
async def _get_version_data(ip: ipaddress.ip_address) -> dict or None: async def _get_version_data(ip: ipaddress.ip_address) -> dict or None:
for i in range(3): for i in range(3):
try: try:
# open a connection to the miner
fut = asyncio.open_connection(str(ip), 4028) fut = asyncio.open_connection(str(ip), 4028)
# get reader and writer streams # get reader and writer streams
reader, writer = await asyncio.wait_for(fut, timeout=5) reader, writer = await asyncio.wait_for(fut, timeout=5)