bug: reformat, and fix miner listener.

This commit is contained in:
Upstream Data
2024-04-30 11:49:47 -06:00
parent 5971d9fd83
commit 05a4ae6f04
8 changed files with 124 additions and 112 deletions

View File

@@ -27,6 +27,7 @@ from pyasic.misc import merge_dicts
class MinerConfig: class MinerConfig:
"""Represents the configuration for a miner including pool configuration, """Represents the configuration for a miner including pool configuration,
fan mode, temperature settings, mining mode, and power scaling.""" fan mode, temperature settings, mining mode, and power scaling."""
pools: PoolConfig = field(default_factory=PoolConfig.default) pools: PoolConfig = field(default_factory=PoolConfig.default)
fan_mode: FanModeConfig = field(default_factory=FanModeConfig.default) fan_mode: FanModeConfig = field(default_factory=FanModeConfig.default)
temperature: TemperatureConfig = field(default_factory=TemperatureConfig.default) temperature: TemperatureConfig = field(default_factory=TemperatureConfig.default)
@@ -110,7 +111,7 @@ class MinerConfig:
} }
def as_boser(self, user_suffix: str = None) -> dict: def as_boser(self, user_suffix: str = None) -> dict:
""""Generates the configuration in the format suitable for BOSer.""" """ "Generates the configuration in the format suitable for BOSer."""
return { return {
**self.fan_mode.as_boser(), **self.fan_mode.as_boser(),
**self.temperature.as_boser(), **self.temperature.as_boser(),

View File

@@ -557,9 +557,7 @@ class MinerFactory:
if mtype == MinerTypes.ANTMINER: if mtype == MinerTypes.ANTMINER:
# could still be mara # could still be mara
auth = httpx.DigestAuth("root", "root") auth = httpx.DigestAuth("root", "root")
res = await self.send_web_command( res = await self.send_web_command(ip, "/kaonsu/v1/brief", auth=auth)
ip, "/kaonsu/v1/brief", auth=auth
)
if res is not None: if res is not None:
mtype = MinerTypes.MARATHON mtype = MinerTypes.MARATHON
return mtype return mtype

View File

@@ -21,22 +21,33 @@ class MinerListenerProtocol(asyncio.Protocol):
def __init__(self): def __init__(self):
self.responses = {} self.responses = {}
self.transport = None self.transport = None
self.new_miner = None
async def get_new_miner(self):
try:
while self.new_miner is None:
await asyncio.sleep(0)
return self.new_miner
finally:
self.new_miner = None
def connection_made(self, transport): def connection_made(self, transport):
self.transport = transport self.transport = transport
@staticmethod def datagram_received(self, data, _addr):
def datagram_received(data, _addr): if data == b"OK\x00\x00\x00\x00\x00\x00\x00\x00":
return
m = data.decode() m = data.decode()
if "," in m: if "," in m:
ip, mac = m.split(",") ip, mac = m.split(",")
if "/" in ip:
ip = ip.replace("[", "").split("/")[0]
else: else:
d = m[:-1].split("MAC") d = m[:-1].split("MAC")
ip = d[0][3:] ip = d[0][3:]
mac = d[1][1:] mac = d[1][1:]
new_miner = {"IP": ip, "MAC": mac.upper()} self.new_miner = {"IP": ip, "MAC": mac.upper()}
MinerListener().new_miner = new_miner
def connection_lost(self, _): def connection_lost(self, _):
pass pass
@@ -45,32 +56,32 @@ class MinerListenerProtocol(asyncio.Protocol):
class MinerListener: class MinerListener:
def __init__(self, bind_addr: str = "0.0.0.0"): def __init__(self, bind_addr: str = "0.0.0.0"):
self.found_miners = [] self.found_miners = []
self.new_miner = None self.stop = asyncio.Event()
self.stop = False
self.bind_addr = bind_addr self.bind_addr = bind_addr
async def listen(self): async def listen(self):
self.stop = False
loop = asyncio.get_running_loop() loop = asyncio.get_running_loop()
transport_14235, _ = await loop.create_datagram_endpoint( transport_14235, protocol_14235 = await loop.create_datagram_endpoint(
MinerListenerProtocol, local_addr=(self.bind_addr, 14235) MinerListenerProtocol, local_addr=(self.bind_addr, 14235)
) )
transport_8888, _ = await loop.create_datagram_endpoint( transport_8888, protocol_8888 = await loop.create_datagram_endpoint(
MinerListenerProtocol, local_addr=(self.bind_addr, 8888) MinerListenerProtocol, local_addr=(self.bind_addr, 8888)
) )
try:
while not self.stop.is_set():
tasks = [
asyncio.create_task(protocol_14235.get_new_miner()),
asyncio.create_task(protocol_8888.get_new_miner()),
]
await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
for t in tasks:
if t.done():
yield t.result()
while True: finally:
if self.new_miner: transport_14235.close()
yield self.new_miner transport_8888.close()
self.found_miners.append(self.new_miner)
self.new_miner = None
if self.stop:
transport_14235.close()
transport_8888.close()
break
await asyncio.sleep(0)
async def cancel(self): async def cancel(self):
self.stop = True self.stop = True

View File

@@ -46,9 +46,10 @@ _settings = { # defaults
ssl_cxt = httpx.create_ssl_context() ssl_cxt = httpx.create_ssl_context()
#this function configures socket options like SO_LINGER and returns an AsyncHTTPTransport instance to perform asynchronous HTTP requests
#using those options. # this function configures socket options like SO_LINGER and returns an AsyncHTTPTransport instance to perform asynchronous HTTP requests
#SO_LINGER controls what happens when you close a socket with unsent data - it allows specifying linger time for the data to be sent. # using those options.
# SO_LINGER controls what happens when you close a socket with unsent data - it allows specifying linger time for the data to be sent.
def transport(verify: Union[str, bool, SSLContext] = ssl_cxt): def transport(verify: Union[str, bool, SSLContext] = ssl_cxt):
l_onoff = 1 l_onoff = 1
l_linger = get("so_linger_time", 1000) l_linger = get("so_linger_time", 1000)

View File

@@ -9,6 +9,7 @@ class AntminerModernSSH(BaseSSH):
Args: Args:
ip (str): The IP address of the Antminer device. ip (str): The IP address of the Antminer device.
""" """
def __init__(self, ip: str): def __init__(self, ip: str):
super().__init__(ip) super().__init__(ip)
self.pwd = settings.get("default_antminer_ssh_password", "root") self.pwd = settings.get("default_antminer_ssh_password", "root")