added a progress bar when scanning using a generator for scan and get_miner

This commit is contained in:
UpstreamData
2021-12-22 16:46:11 -07:00
parent 66d2b63ae6
commit 06f274ba9c
5 changed files with 72 additions and 9 deletions

View File

@@ -23,12 +23,34 @@ async def update_ui_with_data(key, data, append=False):
data = window[key].get_text() + data
window[key].update(data)
async def update_prog_bar(amount):
window["progress"].Update(amount)
async def scan_network(network):
await update_ui_with_data("status", "Scanning")
miners = await network.scan_network_for_miners()
window["ip_list"].update([str(miner.ip) for miner in miners])
await update_ui_with_data("ip_count", str(len(miners)))
network_size = len(network)
miner_generator = network.scan_network_generator()
print(2*network_size)
window["progress"].Update(0, max=2*network_size)
progress_bar_len = 0
miners = []
async for miner in miner_generator:
if miner:
miners.append(miner)
progress_bar_len += 1
asyncio.create_task(update_prog_bar(progress_bar_len))
progress_bar_len += network_size-len(miners)
asyncio.create_task(update_prog_bar(progress_bar_len))
get_miner_genenerator = miner_factory.get_miner_generator(miners)
all_miners = []
async for found_miner in get_miner_genenerator:
all_miners.append(found_miner)
progress_bar_len += 1
asyncio.create_task(update_prog_bar(progress_bar_len))
window["ip_list"].update([str(miner.ip) for miner in all_miners])
await update_ui_with_data("ip_count", str(len(all_miners)))
await update_ui_with_data("status", "")

File diff suppressed because one or more lines are too long

View File

@@ -15,6 +15,15 @@ class MinerFactory:
def __init__(self):
self.miners = {}
async def get_miner_generator(self, found_ips):
loop = asyncio.get_event_loop()
scan_tasks = []
for miner in found_ips:
scan_tasks.append(loop.create_task(self.get_miner(miner)))
scanned = asyncio.as_completed(scan_tasks)
for miner in scanned:
yield await miner
async def get_miner(self, ip: ipaddress.ip_address) -> BOSminer or CGMiner or BMMiner or UnknownMiner:
"""Decide a miner type using the IP address of the miner."""
# check if the miner already exists in cache

View File

@@ -12,6 +12,9 @@ class MinerNetwork:
self.connected_miners = {}
self.mask = mask
def __len__(self):
return len([item for item in self.get_network().hosts()])
def get_network(self) -> ipaddress.ip_network:
"""Get the network using the information passed to the MinerNetwork or from cache."""
if self.network:
@@ -50,6 +53,23 @@ class MinerNetwork:
miners = await asyncio.gather(*create_miners_tasks)
return miners
async def scan_network_generator(self):
loop = asyncio.get_event_loop()
local_network = self.get_network()
scan_tasks = []
for host in local_network.hosts():
if len(scan_tasks) >= SCAN_THREADS:
scanned = asyncio.as_completed(scan_tasks)
scan_tasks = []
for miner in scanned:
yield await miner
scan_tasks.append(loop.create_task(self.ping_miner(host)))
scanned = asyncio.as_completed(scan_tasks)
for miner in scanned:
yield await miner
@staticmethod
async def ping_miner(ip: ipaddress.ip_address) -> None or ipaddress.ip_address:
for i in range(PING_RETRIES):

View File

@@ -71,5 +71,16 @@ async def restart_btminer_miner():
print(await miner.power_off())
async def scan_generator():
miners_all = []
miner_network = MinerNetwork('192.168.1.1', mask=22)
miners = miner_network.scan_network_generator()
async for miner in miners:
if miner:
miners_all.append(miner)
print(miners_all)
print(len(miners_all))
if __name__ == '__main__':
asyncio.new_event_loop().run_until_complete(restart_btminer_miner())
asyncio.new_event_loop().run_until_complete(scan_generator())