Add watch command
This commit is contained in:
10
README.md
10
README.md
@@ -16,7 +16,8 @@ Copyright (C) 2023 Vladimir Vukicevic
|
||||
|
||||
## Usage
|
||||
|
||||
Python 3 is required. There are no other prerequisites.
|
||||
Python 3 is required. There are no other requirements, but you should
|
||||
install the `alive-progress` package for nicer progress bars (`pip3 install alive-progress`).
|
||||
|
||||
### Printer status
|
||||
|
||||
@@ -26,6 +27,13 @@ $ ./cassini.py status
|
||||
Status: 4 Layers: 27/1002
|
||||
```
|
||||
|
||||
### Watch live ptogress
|
||||
|
||||
```
|
||||
$ ./cassini.py watch [interval]
|
||||
_STL_B_Warriors_1_Sword_Combined_Supported.goo |███████████████████████████████████▉ ︎ | 90%
|
||||
```
|
||||
|
||||
### File transfer
|
||||
|
||||
```
|
||||
|
33
cassini.py
33
cassini.py
@@ -25,6 +25,21 @@ logging.basicConfig(
|
||||
datefmt="%H:%M:%S",
|
||||
)
|
||||
|
||||
try:
|
||||
from alive_progress import alive_bar
|
||||
except ImportError:
|
||||
logging.info("Run 'pip3 install alive-progress' for better progress bars")
|
||||
class alive_bar(object):
|
||||
def __init__(self, total, title, **kwargs):
|
||||
self.total = total
|
||||
self.title = title
|
||||
def __call__(self, x):
|
||||
print(f"{int(x*self.total)}/{self.total} {self.title}\r", end="")
|
||||
def __enter__(self):
|
||||
return self
|
||||
def __exit__(self, *args):
|
||||
print("\n")
|
||||
|
||||
async def create_mqtt_server():
|
||||
mqtt = SimpleMQTTServer('0.0.0.0', 0)
|
||||
await mqtt.start()
|
||||
@@ -47,6 +62,20 @@ def print_printer_status(printers):
|
||||
print(f"{i}: {attrs['Name']} ({attrs['MachineName']})")
|
||||
print(f" Status: {printInfo['Status']} Layers: {printInfo['CurrentLayer']}/{printInfo['TotalLayer']}")
|
||||
|
||||
def do_watch(interval):
|
||||
printers = SaturnPrinter.find_printers()
|
||||
status = printers[0].status()
|
||||
with alive_bar(total=status['totalLayers'], manual=True, elapsed=False, title=status['filename']) as bar:
|
||||
while True:
|
||||
printers = SaturnPrinter.find_printers()
|
||||
if len(printers) > 0:
|
||||
status = printers[0].status()
|
||||
pct = status['currentLayer'] / status['totalLayers']
|
||||
bar(pct)
|
||||
if pct >= 1.0:
|
||||
break
|
||||
time.sleep(interval)
|
||||
|
||||
async def main():
|
||||
cmd = None
|
||||
printers = SaturnPrinter.find_printers()
|
||||
@@ -63,6 +92,10 @@ async def main():
|
||||
if len(sys.argv) > 1:
|
||||
cmd = sys.argv[1]
|
||||
|
||||
if cmd == 'watch':
|
||||
do_watch(int(sys.argv[2]) if len(sys.argv) > 2 else 5)
|
||||
return
|
||||
|
||||
if cmd == 'status':
|
||||
print_printer_status(printers)
|
||||
return
|
||||
|
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@@ -0,0 +1 @@
|
||||
alive-progress==3.1.4
|
@@ -58,7 +58,7 @@ class SaturnPrinter:
|
||||
except socket.timeout:
|
||||
continue
|
||||
else:
|
||||
logging.debug(f'Found printer at {addr}')
|
||||
#logging.debug(f'Found printer at {addr}')
|
||||
pdata = json.loads(data.decode('utf-8'))
|
||||
printers.append(SaturnPrinter(addr, pdata))
|
||||
return printers
|
||||
@@ -95,6 +95,15 @@ class SaturnPrinter:
|
||||
attrs = self.desc['Data']['Attributes']
|
||||
return f"{attrs['Name']} ({attrs['MachineName']})"
|
||||
|
||||
def status(self):
|
||||
printinfo = self.desc['Data']['Status']['PrintInfo']
|
||||
return {
|
||||
'status': self.desc['Data']['Status']['CurrentStatus'],
|
||||
'filename': printinfo['Filename'],
|
||||
'currentLayer': printinfo['CurrentLayer'],
|
||||
'totalLayers': printinfo['TotalLayer']
|
||||
}
|
||||
|
||||
def send_command(self, cmdid, data=None):
|
||||
# generate 16-byte random identifier as a hex string
|
||||
hexstr = '%032x' % random.getrandbits(128)
|
||||
|
Reference in New Issue
Block a user