-
Notifications
You must be signed in to change notification settings - Fork 14
/
cassini.py
executable file
·212 lines (180 loc) · 7.64 KB
/
cassini.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!env python3
# -*- coding: utf-8 -*-
#
# Cassini
#
# Copyright (C) 2023 Vladimir Vukicevic
# License: MIT
#
import os
import pprint
import socket
import sys
import time
import asyncio
import logging
import argparse
from simple_mqtt_server import SimpleMQTTServer
from simple_http_server import SimpleHTTPServer
from saturn_printer import SaturnPrinter, PrintInfoStatus, CurrentStatus, FileStatus
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s,%(msecs)d %(levelname)s: %(message)s",
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()
mqtt_server_task = asyncio.create_task(mqtt.serve_forever())
return mqtt, mqtt.port, mqtt_server_task
async def create_http_server():
http = SimpleHTTPServer('0.0.0.0', 0)
await http.start()
http_server_task = asyncio.create_task(http.serve_forever())
return http, http.port, http_server_task
def do_status(printers):
for i, p in enumerate(printers):
attrs = p.desc['Data']['Attributes']
status = p.desc['Data']['Status']
print_info = status['PrintInfo']
file_info = status['FileTransferInfo']
print(f"{p.addr[0]}:")
print(f" {attrs['Name']} ({attrs['MachineName']})")
print(f" Machine Status: {CurrentStatus(status['CurrentStatus']).name}")
print(f" Print Status: {PrintInfoStatus(print_info['Status']).name}")
print(f" Layers: {print_info['CurrentLayer']}/{print_info['TotalLayer']}")
print(f" File: {print_info['Filename']}")
print(f" File Transfer Status: {FileStatus(file_info['Status']).name}")
def do_status_full(printers):
for i, p in enumerate(printers):
pprint.pprint(p.desc)
def do_watch(printer, interval=5, broadcast=None):
status = printer.status()
with alive_bar(total=status['totalLayers'], manual=True, elapsed=False, title=status['filename']) as bar:
while True:
printers = SaturnPrinter.find_printers(broadcast=broadcast)
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 create_servers():
mqtt, mqtt_port, mqtt_task = await create_mqtt_server()
http, http_port, http_task = await create_http_server()
return mqtt, http
async def do_print(printer, filename):
mqtt, http = await create_servers()
connected = await printer.connect(mqtt, http)
if not connected:
logging.error("Failed to connect to printer")
sys.exit(1)
result = await printer.print_file(filename)
if result:
logging.info("Print started")
else:
logging.error("Failed to start print")
sys.exit(1)
async def do_upload(printer, filename, start_printing=False):
if not os.path.exists(filename):
logging.error(f"{filename} does not exist")
sys.exit(1)
mqtt, http = await create_servers()
connected = await printer.connect(mqtt, http)
if not connected:
logging.error("Failed to connect to printer")
sys.exit(1)
#await printer.upload_file(filename, start_printing=start_printing)
upload_task = asyncio.create_task(printer.upload_file(filename, start_printing=start_printing))
# grab the first one, because we want the file size
basename = filename.split('\\')[-1].split('/')[-1]
file_size = os.path.getsize(filename)
with alive_bar(total=file_size, manual=True, elapsed=False, title=basename) as bar:
while True:
if printer.file_transfer_future is None:
await asyncio.sleep(0.1)
continue
progress = await printer.file_transfer_future
if progress[0] < 0:
logging.error("File upload failed!")
sys.exit(1)
bar(progress[0] / progress[1])
if progress[0] >= progress[1]:
break
await upload_task
def main():
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('--broadcast', help='Explicit broadcast IP address')
parser.add_argument('--debug', help='Enable debug logging', action='store_true')
subparsers = parser.add_subparsers(title="commands", dest="command", required=True)
parser_status = subparsers.add_parser('status', help='Discover and display status of all printers')
parser_status_full = subparsers.add_parser('status-full', help='Discover and display full status of all printers')
parser_watch = subparsers.add_parser('watch', help='Continuously update the status of the selected printer')
parser_watch.add_argument('--interval', type=int, help='Status update interval (seconds)', default=5)
parser_upload = subparsers.add_parser('upload', help='Upload a file to the printer')
parser_upload.add_argument('--start-printing', help='Start printing after upload is complete', action='store_true')
parser_upload.add_argument('filename', help='File to upload')
parser_print = subparsers.add_parser('print', help='Start printing a file already present on the printer')
parser_print.add_argument('filename', help='File to print')
parser_connect_mqtt = subparsers.add_parser('connect-mqtt', help='Connect printer to particular MQTT server')
parser_connect_mqtt.add_argument('address', help='MQTT host and port, e.g. "192.168.1.33:1883" or "mqtt.local:1883"')
args = parser.parse_args()
if args.debug:
logging.getLogger().setLevel(logging.DEBUG)
printers = []
printer = None
broadcast = args.broadcast
if args.printer:
printer = SaturnPrinter.find_printer(args.printer)
if printer is None:
logging.error(f"No response from printer {args.printer}")
sys.exit(1)
printers = [printer]
else:
printers = SaturnPrinter.find_printers(broadcast=broadcast)
if len(printers) == 0:
logging.error("No printers found on network")
sys.exit(1)
printer = printers[0]
if args.command == "status":
do_status(printers)
sys.exit(0)
if args.command == "status-full":
do_status_full(printers)
sys.exit(0)
if args.command == "connect-mqtt":
mqtt_host, mqtt_port = args.address.split(':')
try:
mqtt_host = socket.gethostbyname(mqtt_host)
except socket.gaierror:
pass
for p in printers:
p.connect_mqtt(mqtt_host, mqtt_port)
if args.command == "watch":
do_watch(printer, interval=args.interval, broadcast=broadcast)
sys.exit(0)
logging.info(f'Printer: {printer.describe()} ({printer.addr[0]})')
if printer.busy:
logging.error(f'Printer is busy (status: {printer.current_status})')
sys.exit(1)
if args.command == "upload":
asyncio.run(do_upload(printer, args.filename, start_printing=args.start_printing))
elif args.command == "print":
asyncio.run(do_print(printer, args.filename))
main()