Minimal job queue in test code, first local file print

This commit is contained in:
Gina Häußge committed 2016-01-30 12:08:31 +01:00
1 parent cf02471db7
commit 89a37766df
2 files changed
+75 -21

No files matched your search

+52 -14
View File
@@ -865,8 +865,8 @@ class ReprapGcodeProtocol(Protocol, MotorControlProtocolMixin,
_timeout = float(p_match.group("value")) / 1000.0
elif s_match:
_timeout = float(s_match.group("value"))
# TODO needs timeout handling
#self._timeout = get_new_timeout("communication") + _timeout
self._internal_state["timeout"] = self._get_timeout("communication") + _timeout
##~~ command phase handlers
@@ -898,40 +898,78 @@ if __name__ == "__main__":
from octoprint.comm.protocol import ProtocolListener, FileAwareProtocolListener
from octoprint.job import PrintjobListener
import os
virtual_sd = "C:/Users/gina/AppData/Roaming/OctoPrint/virtualSd"
filename = "50mm-t~5.gco"
def virtual_serial_factory():
return VirtualPrinter(virtual_sd="C:/Users/gina/AppData/Roaming/OctoPrint/virtualSd")
return VirtualPrinter(virtual_sd=virtual_sd)
class CustomProtocolListener(ProtocolListener, FileAwareProtocolListener, PrintjobListener):
def __init__(self, protocol):
super(CustomProtocolListener, self).__init__()
self._job = None
self._queue = []
self._protocol = protocol
def add_job(self, job):
self._queue.append(job)
def get_next_job(self):
return self._queue.pop(0) if self._queue else None
def start_next_job(self):
job = self.get_next_job()
if job is None:
return
self._job = job
self._job.register_listener(self)
self._protocol.process(self._job)
def on_protocol_state(self, protocol, old_state, new_state):
if protocol != self._protocol:
return
print("State changed from {} to {}".format(old_state, new_state))
def on_protocol_temperature(self, protocol, temperatures):
if protocol != self._protocol:
return
print("Temperature update: {!r}".format(temperatures))
def on_protocol_sd_file_list(self, protocol, files):
if protocol != self._protocol:
return
print("Received files from printer:")
for f in files:
print("\t{} (Size {} bytes)".format(*f))
filename = "50mm-t~5.gco"
print("Going to print file {} from SD card".format(filename))
from octoprint.job import SDFilePrintjob
job = SDFilePrintjob(filename)
job.register_listener(self)
protocol.process(job)
self.start_next_job()
def on_protocol_log(self, protocol, data):
if protocol != self._protocol:
return
print(data)
def on_job_progress(self, job, progress, estimation):
print("Job progress: {}% (Total estimated time: {}min)".format(int(progress * 100) if progress is not None else "-",
int(estimation / 60) if estimation is not None else "-"))
def on_job_progress(self, job, progress, elapsed, estimated):
if job != self._job:
return
print("Job progress: {}% (Time: {} min / {} min)".format(int(progress * 100) if progress is not None else "0",
"{}:{}".format(int(elapsed / 60), int(elapsed % 60)) if elapsed is not None else "?",
"{}:{}".format(int(estimated / 60), int(estimated % 60)) if estimated is not None else "?"))
def on_job_done(self, job):
self.start_next_job()
transport = VirtualSerialTransport(virtual_serial_factory=virtual_serial_factory)
protocol = ReprapGcodeProtocol(ReprapGcodeFlavor)
protocol.register_listener(CustomProtocolListener())
protocol_listener = CustomProtocolListener(protocol)
protocol_listener.add_job(LocalGcodeFilePrintjob(os.path.join(virtual_sd, filename)))
protocol_listener.add_job(SDFilePrintjob(filename))
protocol.register_listener(protocol_listener)
transport.connect()
protocol.connect(transport)
+23 -7
View File
@@ -21,6 +21,10 @@ class Printjob(ListenerAware):
self._protocol = None
self._printer_profile = None
@property
def elapsed(self):
return time.time() - self._start if self._start is not None else None
def can_process(self, protocol):
return False
@@ -66,6 +70,9 @@ class Printjob(ListenerAware):
def process_job_failed(self):
self.notify_listeners("on_job_failed")
def process_job_progress(self):
self.notify_listeners("on_job_progress", self, self.get_progress(), self.elapsed, self.get_time_estimate())
class LocalFilePrintjob(Printjob):
@@ -86,6 +93,7 @@ class LocalFilePrintjob(Printjob):
if position > 0:
self._handle.seek(position)
self.process_job_started()
def get_next(self):
from octoprint.util import to_unicode
@@ -105,6 +113,7 @@ class LocalFilePrintjob(Printjob):
self.close()
processed = self.process_line(line)
self.process_job_progress()
return processed
except Exception as e:
self.close()
@@ -117,7 +126,7 @@ class LocalFilePrintjob(Printjob):
def get_progress(self):
if self._handle is None:
return 0.0
return self._handle.tell() / self._size
return float(self._handle.tell()) / float(self._size)
def close(self):
if self._handle is not None:
@@ -143,10 +152,16 @@ class LocalGcodeFilePrintjob(LocalFilePrintjob):
return LocalGcodeFilePrintjob in protocol.supported_jobs
def process_line(self, line):
processed = line
# strip comments
# TODO no dependency on protocol module
from octoprint.comm.protocol.gcode.util import strip_comment
# apply offsets
# strip line
processed = line.strip()
# strip comments
processed = strip_comment(processed)
# TODO apply offsets
# return result
return processed
@@ -200,15 +215,16 @@ class SDFilePrintjob(Printjob, FileAwareProtocolListener):
def on_protocol_sd_status(self, protocol, pos, total):
self._last_pos = pos
self._size = total
self.notify_listeners("on_job_progress", self, self.get_progress(), self.get_time_estimate())
self.process_job_progress()
def on_protocol_file_print_started(self, protocol, name, size):
self._size = size
self.process_job_started()
def on_protocol_file_print_done(self, protocol):
self._active = False
self._protocol.unregister_listener(self)
self.notify_listeners("on_job_done", self)
self.process_job_done()
def _query_status(self):
if self._protocol.can_send():
@@ -232,5 +248,5 @@ class PrintjobListener(object):
def on_job_failed(self, job):
pass
def on_job_progress(self, job, progress, estimation):
def on_job_progress(self, job, progress, elapsed, estimated):
pass