-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogressreader.py
More file actions
43 lines (35 loc) · 1.29 KB
/
progressreader.py
File metadata and controls
43 lines (35 loc) · 1.29 KB
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
# -*- coding: utf-8 -*-
import socket
import sys
import re
class ProgressReader:
def __init__(self, ip, port):
self.printer_address = (ip, port)
self.progress = 0
def get_progress(self):
return self.progress
def get_printer_address(self):
return self.printer_address
def update_progress(self):
printer_socket = socket.create_connection(address=self.printer_address, timeout=5)
# request control
printer_socket.send('~M601 S1\r\n'.encode())
printer_socket.recv(1024)
# request progress
printer_socket.send('~M27\r\n'.encode())
data = printer_socket.recv(1024)
info_result = data.decode()
# release control
printer_socket.send('~M602\r\n'.encode())
printer_socket.recv(1024)
printer_socket.close()
# structure response
regex_groups = re.search('([0-9].*)\/([0-9].*?)\\r', info_result).groups()
printed, total = regex_groups
self.progress = 0 if total == 0 else int(float(printed) / int(total) * 100)
def print_progress(self):
bar_len = 100
filled_len = self.progress
bar = '█' * filled_len + '-' * (bar_len - filled_len)
sys.stdout.write(f"[{bar}] {self.progress}%\r")
sys.stdout.flush()