Allow explicit --printers and --broadcast
This commit is contained in:
12
cassini.py
12
cassini.py
@@ -59,11 +59,11 @@ def do_status(printers):
|
|||||||
print(f" Print Status: {print_info['Status']} Layers: {print_info['CurrentLayer']}/{print_info['TotalLayer']} File: {print_info['Filename']}")
|
print(f" Print Status: {print_info['Status']} Layers: {print_info['CurrentLayer']}/{print_info['TotalLayer']} File: {print_info['Filename']}")
|
||||||
print(f" File Transfer Status: {file_info['Status']}")
|
print(f" File Transfer Status: {file_info['Status']}")
|
||||||
|
|
||||||
def do_watch(printer, interval=5):
|
def do_watch(printer, interval=5, broadcast=None):
|
||||||
status = printer.status()
|
status = printer.status()
|
||||||
with alive_bar(total=status['totalLayers'], manual=True, elapsed=False, title=status['filename']) as bar:
|
with alive_bar(total=status['totalLayers'], manual=True, elapsed=False, title=status['filename']) as bar:
|
||||||
while True:
|
while True:
|
||||||
printers = SaturnPrinter.find_printers()
|
printers = SaturnPrinter.find_printers(broadcast=broadcast)
|
||||||
if len(printers) > 0:
|
if len(printers) > 0:
|
||||||
status = printers[0].status()
|
status = printers[0].status()
|
||||||
pct = status['currentLayer'] / status['totalLayers']
|
pct = status['currentLayer'] / status['totalLayers']
|
||||||
@@ -125,6 +125,7 @@ async def do_upload(printer, filename, start_printing=False):
|
|||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(prog='cassini', description='ELEGOO Saturn printer control utility')
|
parser = argparse.ArgumentParser(prog='cassini', description='ELEGOO Saturn printer control utility')
|
||||||
parser.add_argument('-p', '--printer', help='ID of printer to target')
|
parser.add_argument('-p', '--printer', help='ID of printer to target')
|
||||||
|
parser.add_argument('--broadcast', help='Explicit broadcast IP address')
|
||||||
parser.add_argument('--debug', help='Enable debug logging', action='store_true')
|
parser.add_argument('--debug', help='Enable debug logging', action='store_true')
|
||||||
|
|
||||||
subparsers = parser.add_subparsers(title="commands", dest="command", required=True)
|
subparsers = parser.add_subparsers(title="commands", dest="command", required=True)
|
||||||
@@ -148,14 +149,15 @@ def main():
|
|||||||
|
|
||||||
printers = []
|
printers = []
|
||||||
printer = None
|
printer = None
|
||||||
|
broadcast = args.broadcast
|
||||||
if args.printer:
|
if args.printer:
|
||||||
printer = SaturnPrinter.find_printer(args.printer)
|
printer = SaturnPrinter.find_printer(args.printer)
|
||||||
printers = [printer]
|
|
||||||
if printer is None:
|
if printer is None:
|
||||||
logging.error(f"No response from printer {args.printer}")
|
logging.error(f"No response from printer {args.printer}")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
printers = [printer]
|
||||||
else:
|
else:
|
||||||
printers = SaturnPrinter.find_printers()
|
printers = SaturnPrinter.find_printers(broadcast=broadcast)
|
||||||
if len(printers) == 0:
|
if len(printers) == 0:
|
||||||
logging.error("No printers found on network")
|
logging.error("No printers found on network")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
@@ -166,7 +168,7 @@ def main():
|
|||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
if args.command == "watch":
|
if args.command == "watch":
|
||||||
do_watch(printer, interval=args.interval)
|
do_watch(printer, interval=args.interval, broadcast=broadcast)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
logging.info(f'Printer: {printer.describe()} ({printer.addr[0]})')
|
logging.info(f'Printer: {printer.describe()} ({printer.addr[0]})')
|
||||||
|
@@ -60,13 +60,15 @@ class SaturnPrinter:
|
|||||||
self.desc = None
|
self.desc = None
|
||||||
|
|
||||||
# Broadcast and find all printers, return array of SaturnPrinter objects
|
# Broadcast and find all printers, return array of SaturnPrinter objects
|
||||||
def find_printers(timeout=1):
|
def find_printers(timeout=1, broadcast=None):
|
||||||
|
if broadcast is None:
|
||||||
|
broadcast = '<broadcast>'
|
||||||
printers = []
|
printers = []
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
with sock:
|
with sock:
|
||||||
sock.settimeout(timeout)
|
sock.settimeout(timeout)
|
||||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, timeout)
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, timeout)
|
||||||
sock.sendto(b'M99999', ('<broadcast>', SATURN_UDP_PORT))
|
sock.sendto(b'M99999', (broadcast, SATURN_UDP_PORT))
|
||||||
|
|
||||||
now = time.time()
|
now = time.time()
|
||||||
while True:
|
while True:
|
||||||
@@ -85,10 +87,10 @@ class SaturnPrinter:
|
|||||||
# Find a specific printer at the given address, return a SaturnPrinter object
|
# Find a specific printer at the given address, return a SaturnPrinter object
|
||||||
# or None if no response is obtained
|
# or None if no response is obtained
|
||||||
def find_printer(addr, timeout=5):
|
def find_printer(addr, timeout=5):
|
||||||
printer = SaturnPrinter(addr, None)
|
printers = SaturnPrinter.find_printers(broadcast=addr)
|
||||||
if printer.refresh(timeout):
|
if len(printers) == 0 or printers[0].addr[0] != addr:
|
||||||
return printer
|
return None
|
||||||
return None
|
return printers[0]
|
||||||
|
|
||||||
# Refresh this SaturnPrinter with latest status
|
# Refresh this SaturnPrinter with latest status
|
||||||
def refresh(self, timeout=5):
|
def refresh(self, timeout=5):
|
||||||
|
Reference in New Issue
Block a user