added some docstrings to API, and added to miner types

This commit is contained in:
UpstreamData
2021-12-20 11:55:30 -07:00
parent b58d3cbb9b
commit 449bdfae69
10 changed files with 220 additions and 9 deletions

View File

@@ -6,145 +6,207 @@ class BMMinerAPI(BaseMinerAPI):
super().__init__(ip, port) super().__init__(ip, port)
async def version(self) -> dict: async def version(self) -> dict:
"""
API 'version' command.
Returns a dict containing version information.
"""
return await self.send_command("version") return await self.send_command("version")
async def config(self) -> dict: async def config(self) -> dict:
"""
API 'config' command.
Returns some miner configuration information:
ASC Count <- the number of ASCs
PGA Count <- the number of PGAs
Pool Count <- the number of Pools
Strategy <- the current pool strategy
Log Interval <- the interval of logging
Device Code <- list of compiled device drivers
OS <- the current operating system
Failover-Only <- failover-only setting
Scan Time <- scan-time setting
Queue <- queue setting
Expiry <- expiry setting
"""
return await self.send_command("config") return await self.send_command("config")
async def summary(self) -> dict: async def summary(self) -> dict:
"""API 'summary' command."""
return await self.send_command("summary") return await self.send_command("summary")
async def pools(self) -> dict: async def pools(self) -> dict:
"""API 'pools' command."""
return await self.send_command("pools") return await self.send_command("pools")
async def devs(self) -> dict: async def devs(self) -> dict:
"""API 'devs' command."""
return await self.send_command("devs") return await self.send_command("devs")
async def edevs(self, old: bool = False) -> dict: async def edevs(self, old: bool = False) -> dict:
"""API 'edevs' command."""
if old: if old:
return await self.send_command("edevs", parameters="old") return await self.send_command("edevs", parameters="old")
else: else:
return await self.send_command("edevs") return await self.send_command("edevs")
async def pga(self, n: int) -> dict: async def pga(self, n: int) -> dict:
"""API 'pga' command."""
return await self.send_command("pga", parameters=n) return await self.send_command("pga", parameters=n)
async def pgacount(self) -> dict: async def pgacount(self) -> dict:
"""API 'pgacount' command."""
return await self.send_command("pgacount") return await self.send_command("pgacount")
async def switchpool(self, n: int) -> dict: async def switchpool(self, n: int) -> dict:
"""API 'switchpool' command."""
return await self.send_command("switchpool", parameters=n) return await self.send_command("switchpool", parameters=n)
async def enablepool(self, n: int) -> dict: async def enablepool(self, n: int) -> dict:
"""API 'enablepool' command."""
return await self.send_command("enablepool", parameters=n) return await self.send_command("enablepool", parameters=n)
async def addpool(self, url: str, username: str, password: str) -> dict: async def addpool(self, url: str, username: str, password: str) -> dict:
"""API 'addpool' command."""
return await self.send_command("addpool", parameters=f"{url}, {username}, {password}") return await self.send_command("addpool", parameters=f"{url}, {username}, {password}")
async def poolpriority(self, *n: int) -> dict: async def poolpriority(self, *n: int) -> dict:
"""API 'poolpriority' command."""
return await self.send_command("poolpriority", parameters=f"{','.join([str(item) for item in n])}") return await self.send_command("poolpriority", parameters=f"{','.join([str(item) for item in n])}")
async def poolquota(self, n: int, q: int) -> dict: async def poolquota(self, n: int, q: int) -> dict:
"""API 'poolquota' command."""
return await self.send_command("poolquota", parameters=f"{n}, {q}") return await self.send_command("poolquota", parameters=f"{n}, {q}")
async def disablepool(self, n: int) -> dict: async def disablepool(self, n: int) -> dict:
"""API 'disablepool' command."""
return await self.send_command("disablepool", parameters=n) return await self.send_command("disablepool", parameters=n)
async def removepool(self, n: int) -> dict: async def removepool(self, n: int) -> dict:
"""API 'removepool' command."""
return await self.send_command("removepool", parameters=n) return await self.send_command("removepool", parameters=n)
async def save(self, filename: str = None) -> dict: async def save(self, filename: str = None) -> dict:
"""API 'save' command."""
if filename: if filename:
return await self.send_command("save", parameters=filename) return await self.send_command("save", parameters=filename)
else: else:
return await self.send_command("save") return await self.send_command("save")
async def quit(self) -> dict: async def quit(self) -> dict:
"""quit' command."""
return await self.send_command("quit") return await self.send_command("quit")
async def notify(self) -> dict: async def notify(self) -> dict:
"""API 'notify' command."""
return await self.send_command("notify") return await self.send_command("notify")
async def privileged(self) -> dict: async def privileged(self) -> dict:
"""API 'privileged' command."""
return await self.send_command("privileged") return await self.send_command("privileged")
async def pgaenable(self, n: int) -> dict: async def pgaenable(self, n: int) -> dict:
"""API 'pgaenable' command."""
return await self.send_command("pgaenable", parameters=n) return await self.send_command("pgaenable", parameters=n)
async def pgadisable(self, n: int) -> dict: async def pgadisable(self, n: int) -> dict:
"""API 'pgadisable' command."""
return await self.send_command("pgadisable", parameters=n) return await self.send_command("pgadisable", parameters=n)
async def pgaidentify(self, n: int) -> dict: async def pgaidentify(self, n: int) -> dict:
"""API 'pgaidentify' command."""
return await self.send_command("pgaidentify", parameters=n) return await self.send_command("pgaidentify", parameters=n)
async def devdetails(self) -> dict: async def devdetails(self) -> dict:
"""API 'devdetails' command."""
return await self.send_command("devdetails") return await self.send_command("devdetails")
async def restart(self) -> dict: async def restart(self) -> dict:
"""API 'restart' command."""
return await self.send_command("restart") return await self.send_command("restart")
async def stats(self) -> dict: async def stats(self) -> dict:
"""API 'stats' command."""
return await self.send_command("stats") return await self.send_command("stats")
async def estats(self, old: bool = False) -> dict: async def estats(self, old: bool = False) -> dict:
"""API 'estats' command."""
if old: if old:
return await self.send_command("estats", parameters="old") return await self.send_command("estats", parameters="old")
else: else:
return await self.send_command("estats") return await self.send_command("estats")
async def check(self, command) -> dict: async def check(self, command) -> dict:
"""API 'check' command."""
return await self.send_command("check", parameters=command) return await self.send_command("check", parameters=command)
async def failover_only(self, failover: bool) -> dict: async def failover_only(self, failover: bool) -> dict:
"""API 'failover-only' command."""
return await self.send_command("failover-only", parameters=failover) return await self.send_command("failover-only", parameters=failover)
async def coin(self) -> dict: async def coin(self) -> dict:
"""API 'coin' command."""
return await self.send_command("coin") return await self.send_command("coin")
async def debug(self, setting: str) -> dict: async def debug(self, setting: str) -> dict:
"""API 'debug' command."""
return await self.send_command("debug", parameters=setting) return await self.send_command("debug", parameters=setting)
async def setconfig(self, name: str, n: int) -> dict: async def setconfig(self, name: str, n: int) -> dict:
"""API 'setconfig' command."""
return await self.send_command("setconfig", parameters=f"{name}, {n}") return await self.send_command("setconfig", parameters=f"{name}, {n}")
async def usbstats(self) -> dict: async def usbstats(self) -> dict:
"""API 'usbstats' command."""
return await self.send_command("usbstats") return await self.send_command("usbstats")
async def pgaset(self, n: int, opt: str, val: int = None) -> dict: async def pgaset(self, n: int, opt: str, val: int = None) -> dict:
"""API 'pgaset' command."""
if val: if val:
return await self.send_command("pgaset", parameters=f"{n}, {opt}, {val}") return await self.send_command("pgaset", parameters=f"{n}, {opt}, {val}")
else: else:
return await self.send_command("pgaset", parameters=f"{n}, {opt}") return await self.send_command("pgaset", parameters=f"{n}, {opt}")
async def zero(self, which: str, value: bool) -> dict: async def zero(self, which: str, value: bool) -> dict:
"""API 'zero' command."""
return await self.send_command("zero", parameters=f"{which}, {value}") return await self.send_command("zero", parameters=f"{which}, {value}")
async def hotplug(self, n: int) -> dict: async def hotplug(self, n: int) -> dict:
"""API 'hotplug' command."""
return await self.send_command("hotplug", parameters=n) return await self.send_command("hotplug", parameters=n)
async def asc(self, n: int) -> dict: async def asc(self, n: int) -> dict:
"""API 'asc' command."""
return await self.send_command("asc", parameters=n) return await self.send_command("asc", parameters=n)
async def ascenable(self, n: int) -> dict: async def ascenable(self, n: int) -> dict:
"""API 'ascenable' command."""
return await self.send_command("ascenable", parameters=n) return await self.send_command("ascenable", parameters=n)
async def ascdisable(self, n: int) -> dict: async def ascdisable(self, n: int) -> dict:
"""API 'ascdisable' command."""
return await self.send_command("ascdisable", parameters=n) return await self.send_command("ascdisable", parameters=n)
async def ascidentify(self, n: int) -> dict: async def ascidentify(self, n: int) -> dict:
"""API 'ascidentify' command."""
return await self.send_command("ascidentify", parameters=n) return await self.send_command("ascidentify", parameters=n)
async def asccount(self) -> dict: async def asccount(self) -> dict:
"""API 'asccount' command."""
return await self.send_command("asccount") return await self.send_command("asccount")
async def ascset(self, n: int, opt: str, val: int = None) -> dict: async def ascset(self, n: int, opt: str, val: int = None) -> dict:
"""API 'ascset' command."""
if val: if val:
return await self.send_command("ascset", parameters=f"{n}, {opt}, {val}") return await self.send_command("ascset", parameters=f"{n}, {opt}, {val}")
else: else:
return await self.send_command("ascset", parameters=f"{n}, {opt}") return await self.send_command("ascset", parameters=f"{n}, {opt}")
async def lcd(self) -> dict: async def lcd(self) -> dict:
"""API 'lcd' command."""
return await self.send_command("lcd") return await self.send_command("lcd")
async def lockstats(self) -> dict: async def lockstats(self) -> dict:
"""API 'lockstats' command."""
return await self.send_command("lockstats") return await self.send_command("lockstats")

View File

@@ -6,8 +6,28 @@ class CGMinerAPI(BaseMinerAPI):
super().__init__(ip, port) super().__init__(ip, port)
async def version(self) -> dict: async def version(self) -> dict:
"""
API 'version' command.
Returns a dict containing version information.
"""
return await self.send_command("version") return await self.send_command("version")
async def config(self) -> dict:
"""
API 'config' command.
Returns a dict containing some miner configuration information:
ASC Count <- the number of ASCs
PGA Count <- the number of PGAs
Pool Count <- the number of Pools
Strategy <- the current pool strategy
Log Interval <- the interval of logging
Device Code <- list of compiled device drivers
OS <- the current operating system
"""
return await self.send_command("config")
async def summary(self) -> dict: async def summary(self) -> dict:
return await self.send_command("summary") return await self.send_command("summary")

View File

@@ -5,7 +5,7 @@ import toml
from config.bos import bos_config_convert, general_config_convert_bos from config.bos import bos_config_convert, general_config_convert_bos
class BOSminer(BaseMiner): class BOSMinerS9(BaseMiner):
def __init__(self, ip: str) -> None: def __init__(self, ip: str) -> None:
api = BOSMinerAPI(ip) api = BOSMinerAPI(ip)
super().__init__(ip, api) super().__init__(ip, api)
@@ -14,7 +14,7 @@ class BOSminer(BaseMiner):
self.pwd = 'admin' self.pwd = 'admin'
def __repr__(self) -> str: def __repr__(self) -> str:
return f"BOSminer: {str(self.ip)}" return f"S9 - BOSminer: {str(self.ip)}"
async def _get_ssh_connection(self) -> asyncssh.connect: async def _get_ssh_connection(self) -> asyncssh.connect:
"""Create a new asyncssh connection""" """Create a new asyncssh connection"""

View File

@@ -5,7 +5,7 @@ import toml
from config.bos import bos_config_convert, general_config_convert_bos from config.bos import bos_config_convert, general_config_convert_bos
class BOSminer(BaseMiner): class BOSminerX17(BaseMiner):
def __init__(self, ip: str) -> None: def __init__(self, ip: str) -> None:
api = BOSMinerAPI(ip) api = BOSMinerAPI(ip)
super().__init__(ip, api) super().__init__(ip, api)
@@ -14,7 +14,7 @@ class BOSminer(BaseMiner):
self.pwd = 'admin' self.pwd = 'admin'
def __repr__(self) -> str: def __repr__(self) -> str:
return f"BOSminer: {str(self.ip)}" return f"X17 - BOSminer: {str(self.ip)}"
async def _get_ssh_connection(self) -> asyncssh.connect: async def _get_ssh_connection(self) -> asyncssh.connect:
"""Create a new asyncssh connection""" """Create a new asyncssh connection"""

View File

@@ -0,0 +1,26 @@
from API.btminer import BTMinerAPI
from miners import BaseMiner
class BTMinerM20(BaseMiner):
def __init__(self, ip: str) -> None:
api = BTMinerAPI(ip)
super().__init__(ip, api)
def __repr__(self) -> str:
return f"M20 - BTMiner: {str(self.ip)}"
async def get_hostname(self) -> str:
return "BTMiner Unknown"
async def send_config(self):
return None # ignore for now
async def restart_backend(self) -> None:
return None
async def reboot(self) -> None:
return None
async def get_config(self) -> None:
return None

View File

@@ -0,0 +1,26 @@
from API.btminer import BTMinerAPI
from miners import BaseMiner
class BTMinerM21(BaseMiner):
def __init__(self, ip: str) -> None:
api = BTMinerAPI(ip)
super().__init__(ip, api)
def __repr__(self) -> str:
return f"M21 - BTMiner: {str(self.ip)}"
async def get_hostname(self) -> str:
return "BTMiner Unknown"
async def send_config(self):
return None # ignore for now
async def restart_backend(self) -> None:
return None
async def reboot(self) -> None:
return None
async def get_config(self) -> None:
return None

View File

@@ -0,0 +1,26 @@
from API.btminer import BTMinerAPI
from miners import BaseMiner
class BTMinerM30(BaseMiner):
def __init__(self, ip: str) -> None:
api = BTMinerAPI(ip)
super().__init__(ip, api)
def __repr__(self) -> str:
return f"M30 - BTMiner: {str(self.ip)}"
async def get_hostname(self) -> str:
return "BTMiner Unknown"
async def send_config(self):
return None # ignore for now
async def restart_backend(self) -> None:
return None
async def reboot(self) -> None:
return None
async def get_config(self) -> None:
return None

View File

@@ -0,0 +1,26 @@
from API.btminer import BTMinerAPI
from miners import BaseMiner
class BTMinerM31(BaseMiner):
def __init__(self, ip: str) -> None:
api = BTMinerAPI(ip)
super().__init__(ip, api)
def __repr__(self) -> str:
return f"M31 - BTMiner: {str(self.ip)}"
async def get_hostname(self) -> str:
return "BTMiner Unknown"
async def send_config(self):
return None # ignore for now
async def restart_backend(self) -> None:
return None
async def reboot(self) -> None:
return None
async def get_config(self) -> None:
return None

View File

@@ -0,0 +1,26 @@
from API.btminer import BTMinerAPI
from miners import BaseMiner
class BTMinerM32(BaseMiner):
def __init__(self, ip: str) -> None:
api = BTMinerAPI(ip)
super().__init__(ip, api)
def __repr__(self) -> str:
return f"M32 - BTMiner: {str(self.ip)}"
async def get_hostname(self) -> str:
return "BTMiner Unknown"
async def send_config(self):
return None # ignore for now
async def restart_backend(self) -> None:
return None
async def reboot(self) -> None:
return None
async def get_config(self) -> None:
return None

View File

@@ -1,10 +1,6 @@
import ipaddress import ipaddress
import asyncio import asyncio
from miners.miner_factory import MinerFactory from miners.miner_factory import MinerFactory
from miners.bmminer import BMMiner
from miners.bosminer import BOSminer
from miners.cgminer import CGMiner
from miners.unknown import UnknownMiner
PING_RETRIES: int = 3 PING_RETRIES: int = 3
PING_TIMEOUT: int = 3 PING_TIMEOUT: int = 3
@@ -19,6 +15,7 @@ class MinerNetwork:
self.mask = mask self.mask = mask
def get_network(self) -> ipaddress.ip_network: def get_network(self) -> ipaddress.ip_network:
"""Get the network using the information passed to the MinerNetwork or from cache."""
if self.network: if self.network:
return self.network return self.network
if not self.ip_addr: if not self.ip_addr:
@@ -31,7 +28,8 @@ class MinerNetwork:
subnet_mask = "24" subnet_mask = "24"
return ipaddress.ip_network(f"{default_gateway}/{subnet_mask}", strict=False) return ipaddress.ip_network(f"{default_gateway}/{subnet_mask}", strict=False)
async def scan_network_for_miners(self) -> None or list[BOSminer or BMMiner or CGMiner or UnknownMiner]: async def scan_network_for_miners(self) -> None or list:
"""Scan the network for miners, and use the miner factory to get correct types."""
local_network = self.get_network() local_network = self.get_network()
print(f"Scanning {local_network} for miners...") print(f"Scanning {local_network} for miners...")
scan_tasks = [] scan_tasks = []
@@ -49,6 +47,7 @@ class MinerNetwork:
@staticmethod @staticmethod
async def ping_miner(ip: ipaddress.ip_address) -> None or ipaddress.ip_address: async def ping_miner(ip: ipaddress.ip_address) -> None or ipaddress.ip_address:
"""Send ping requests to a miner."""
for i in range(PING_RETRIES): for i in range(PING_RETRIES):
connection_fut = asyncio.open_connection(str(ip), 4028) connection_fut = asyncio.open_connection(str(ip), 4028)
try: try: