added basic network functionality
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
|
import ipaddress
|
||||||
|
|
||||||
|
|
||||||
class APIError(Exception):
|
class APIError(Exception):
|
||||||
@@ -17,17 +18,17 @@ class APIError(Exception):
|
|||||||
|
|
||||||
|
|
||||||
class BaseMinerAPI:
|
class BaseMinerAPI:
|
||||||
def __init__(self, ip, port):
|
def __init__(self, ip: str, port: int):
|
||||||
self.port = port
|
self.port = port
|
||||||
self.ip = ip
|
self.ip = ipaddress.ip_address(ip)
|
||||||
|
|
||||||
async def multicommand(self, *commands: str) -> dict:
|
async def multicommand(self, *commands: str) -> dict:
|
||||||
command = "+".join(commands)
|
command = "+".join(commands)
|
||||||
return await self.send_command(command)
|
return await self.send_command(command)
|
||||||
|
|
||||||
async def send_command(self, command, parameters: dict = None) -> dict:
|
async def send_command(self, command: str, parameters: str = None) -> dict:
|
||||||
# get reader and writer streams
|
# get reader and writer streams
|
||||||
reader, writer = await asyncio.open_connection(self.ip, self.port)
|
reader, writer = await asyncio.open_connection(str(self.ip), self.port)
|
||||||
|
|
||||||
# create the command
|
# create the command
|
||||||
cmd = {"command": command}
|
cmd = {"command": command}
|
||||||
|
|||||||
@@ -48,19 +48,29 @@ class BOSMinerAPI(BaseMinerAPI):
|
|||||||
return await self.send_command("lcd")
|
return await self.send_command("lcd")
|
||||||
|
|
||||||
async def switchpool(self, n: int) -> dict:
|
async def switchpool(self, n: int) -> dict:
|
||||||
return await self.send_command("switchpool", parameters=n)
|
# BOS has not implemented this yet, they will in the future
|
||||||
|
return NotImplementedError
|
||||||
|
# return await self.send_command("switchpool", parameters=n)
|
||||||
|
|
||||||
async def enablepool(self, n: int) -> dict:
|
async def enablepool(self, n: int) -> dict:
|
||||||
return await self.send_command("enablepool", parameters=n)
|
# BOS has not implemented this yet, they will in the future
|
||||||
|
return NotImplementedError
|
||||||
|
# return await self.send_command("enablepool", parameters=n)
|
||||||
|
|
||||||
async def disablepool(self, n: int) -> dict:
|
async def disablepool(self, n: int) -> dict:
|
||||||
return await self.send_command("disablepool", parameters=n)
|
# BOS has not implemented this yet, they will in the future
|
||||||
|
return NotImplementedError
|
||||||
|
# return await self.send_command("disablepool", parameters=n)
|
||||||
|
|
||||||
async def addpool(self, url: str, username: str, password: str) -> dict:
|
async def addpool(self, url: str, username: str, password: str) -> dict:
|
||||||
return await self.send_command("addpool", parameters=f"{url}, {username}, {password}")
|
# BOS has not implemented this yet, they will in the future
|
||||||
|
return NotImplementedError
|
||||||
|
# return await self.send_command("addpool", parameters=f"{url}, {username}, {password}")
|
||||||
|
|
||||||
async def removepool(self, n: int) -> dict:
|
async def removepool(self, n: int) -> dict:
|
||||||
return await self.send_command("removepool", parameters=n)
|
# BOS has not implemented this yet, they will in the future
|
||||||
|
return NotImplementedError
|
||||||
|
# return await self.send_command("removepool", parameters=n)
|
||||||
|
|
||||||
async def fans(self) -> dict:
|
async def fans(self) -> dict:
|
||||||
return await self.send_command("fans")
|
return await self.send_command("fans")
|
||||||
@@ -78,4 +88,4 @@ class BOSMinerAPI(BaseMinerAPI):
|
|||||||
return await self.send_command("pause")
|
return await self.send_command("pause")
|
||||||
|
|
||||||
async def resume(self) -> dict:
|
async def resume(self) -> dict:
|
||||||
return await self.send_command("resume")
|
return await self.send_command("resume")
|
||||||
|
|||||||
12
main.py
12
main.py
@@ -1,13 +1,11 @@
|
|||||||
from API.bosminer import BOSMinerAPI
|
from API.bosminer import BOSMinerAPI
|
||||||
|
from network import MinerNetwork
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
bosminer = BOSMinerAPI("172.16.1.199")
|
miner_network = MinerNetwork("192.168.1.1")
|
||||||
data_normal = await bosminer.asccount()
|
await miner_network.scan_network_for_miners()
|
||||||
data_multi = await bosminer.multicommand("version", "config")
|
|
||||||
print(data_normal)
|
|
||||||
print(data_multi)
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
asyncio.get_event_loop().run_until_complete(main())
|
asyncio.get_event_loop().run_until_complete(main())
|
||||||
|
|||||||
59
network/__init__.py
Normal file
59
network/__init__.py
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
import netifaces
|
||||||
|
import ipaddress
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
PING_RETRIES: int = 3
|
||||||
|
PING_TIMEOUT: int = 1
|
||||||
|
|
||||||
|
|
||||||
|
class MinerNetwork:
|
||||||
|
def __init__(self, ip_addr=None):
|
||||||
|
self.network = None
|
||||||
|
self.ip_addr = ip_addr
|
||||||
|
self.connected_miners = {}
|
||||||
|
|
||||||
|
def get_network(self):
|
||||||
|
if self.network:
|
||||||
|
return self.network
|
||||||
|
gateways = netifaces.gateways()
|
||||||
|
if not self.ip_addr:
|
||||||
|
default_gateway = gateways['default'][netifaces.AF_INET][0]
|
||||||
|
else:
|
||||||
|
default_gateway = self.ip_addr
|
||||||
|
subnet_mask = netifaces.ifaddresses(gateways['default'][netifaces.AF_INET][1])[netifaces.AF_INET][0]['netmask']
|
||||||
|
return ipaddress.ip_network(f"{default_gateway}/{subnet_mask}", strict=False)
|
||||||
|
|
||||||
|
async def scan_network_for_miners(self):
|
||||||
|
local_network = self.get_network()
|
||||||
|
print(f"Scanning {local_network} for miners...")
|
||||||
|
scan_tasks = []
|
||||||
|
for host in local_network.hosts():
|
||||||
|
scan_tasks.append(self.ping_miner(host))
|
||||||
|
await asyncio.gather(*scan_tasks)
|
||||||
|
print(f"Found {len(self.connected_miners)} connected miners...")
|
||||||
|
|
||||||
|
async def ping_miner(self, ip: ipaddress.ip_address):
|
||||||
|
for i in range(PING_RETRIES):
|
||||||
|
connection_fut = asyncio.open_connection(str(ip), 4028)
|
||||||
|
try:
|
||||||
|
# get the read and write streams from the connection
|
||||||
|
reader, writer = await asyncio.wait_for(connection_fut, timeout=PING_TIMEOUT)
|
||||||
|
# immediately close connection, we know connection happened
|
||||||
|
writer.close()
|
||||||
|
# make sure the writer is closed
|
||||||
|
await writer.wait_closed()
|
||||||
|
# add miner to dict
|
||||||
|
self.connected_miners[ip] = {}
|
||||||
|
# ping was successful
|
||||||
|
return True
|
||||||
|
except asyncio.exceptions.TimeoutError:
|
||||||
|
# ping failed if we time out
|
||||||
|
continue
|
||||||
|
except ConnectionRefusedError:
|
||||||
|
# handle for other connection errors
|
||||||
|
print("Unknown error...")
|
||||||
|
# ping failed, likely with an exception
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
continue
|
||||||
|
return False
|
||||||
Reference in New Issue
Block a user