pyasic

Miner Web APIs

Each miner has a unique Web API that is used to communicate with it. Each of these API types has commands that differ between them, and some commands have data that others do not. Each miner that is a subclass of BaseMiner may have an API linked to it as Miner.web.

All API implementations inherit from BaseWebAPI, which implements the basic communications protocols.

BaseWebAPI should never be used unless inheriting to create a new miner API class for a new type of miner (which should be exceedingly rare). Use these instead -

AntminerModerNWebAPI

AntminerOldWebAPI

AuradineWebAPI

ePICWebAPI

GoldshellWebAPI

InnosiliconWebAPI

MaraWebAPI

VNishWebAPI


BaseWebAPI

Bases: ABC

Source code in pyasic/web/base.py
class BaseWebAPI(ABC):
    def __init__(self, ip: str) -> None:
        # ip address of the miner
        self.ip = ip
        self.username = None
        self.pwd = None
        self.port = 80

        self.token = None

    def __new__(cls, *args, **kwargs):
        if cls is BaseWebAPI:
            raise TypeError(f"Only children of '{cls.__name__}' may be instantiated")
        return object.__new__(cls)

    def __repr__(self):
        return f"{self.__class__.__name__}: {str(self.ip)}"

    @abstractmethod
    async def send_command(
        self,
        command: str | bytes,
        ignore_errors: bool = False,
        allow_warning: bool = True,
        privileged: bool = False,
        **parameters: Any,
    ) -> dict:
        pass

    @abstractmethod
    async def multicommand(
        self, *commands: str, ignore_errors: bool = False, allow_warning: bool = True
    ) -> dict:
        pass

    def _check_commands(self, *commands):
        allowed_commands = self.get_commands()
        return_commands = []
        for command in [*commands]:
            if command in allowed_commands:
                return_commands.append(command)
            else:
                warnings.warn(
                    f"""Removing incorrect command: {command}
If you are sure you want to use this command please use WebAPI.send_command("{command}", ignore_errors=True) instead.""",
                    APIWarning,
                )
        return return_commands

    @property
    def commands(self) -> list:
        return self.get_commands()

    def get_commands(self) -> list:
        """Get a list of command accessible to a specific type of web API on the miner.

        Returns:
            A list of all web commands that the miner supports.
        """
        return [
            func
            for func in
            # each function in self
            dir(self)
            if not func == "commands"
            if callable(getattr(self, func)) and
            # no __ or _ methods
            not func.startswith("__") and not func.startswith("_") and
            # remove all functions that are in this base class
            func
            not in [
                func for func in dir(BaseWebAPI) if callable(getattr(BaseWebAPI, func))
            ]
        ]

get_commands()

Get a list of command accessible to a specific type of web API on the miner.

Returns:

Type Description
list

A list of all web commands that the miner supports.

Source code in pyasic/web/base.py
def get_commands(self) -> list:
    """Get a list of command accessible to a specific type of web API on the miner.

    Returns:
        A list of all web commands that the miner supports.
    """
    return [
        func
        for func in
        # each function in self
        dir(self)
        if not func == "commands"
        if callable(getattr(self, func)) and
        # no __ or _ methods
        not func.startswith("__") and not func.startswith("_") and
        # remove all functions that are in this base class
        func
        not in [
            func for func in dir(BaseWebAPI) if callable(getattr(BaseWebAPI, func))
        ]
    ]