diff --git a/src/octoprint/comm/protocol/__init__.py b/src/octoprint/comm/protocol/__init__.py index 8977cba75..463dbebab 100644 --- a/src/octoprint/comm/protocol/__init__.py +++ b/src/octoprint/comm/protocol/__init__.py @@ -8,13 +8,25 @@ __copyright__ = "Copyright (C) 2015 The OctoPrint Project - Released under terms import logging - class Protocol(object): + supported_jobs = [] + def __init__(self): self._logger = logging.getLogger(__name__) + self._state = ProtocolState.DISCONNECTED self._listeners = [] + self._job = None + + @property + def state(self): + return self._state + + @state.setter + def state(self, new_state): + self._state = new_state + def register_listener(self, listener): self._listeners.append(listener) @@ -27,8 +39,22 @@ class Protocol(object): def disconnect(self): pass - def process(self, job): - pass + def process(self, job, position=0): + if not job.can_process(self): + raise ValueError("Job {} cannot be processed with protocol {}".format(job, self)) + self._job = job + self._job.register_listener(self) + self._job.process(job, position=position) + + def pause_processing(self): + if self._job is None or self.state != ProtocolState.PRINTING: + return + self.state = ProtocolState.PAUSED + + def resume_processing(self): + if self._job is None or self.state != ProtocolState.PAUSED: + return + self.state = ProtocolState.PRINTING def move(self, x=None, y=None, z=None, e=None, feedrate=None, relative=False): pass @@ -56,6 +82,38 @@ class Protocol(object): for key, value in kwargs.items()])) )) + def on_job_started(self, job): + if job != self._job: + return + self.state = ProtocolState.PRINTING + + def on_job_done(self, job): + if job != self._job: + return + self._job = None + self.state = ProtocolState.CONNECTED + + def on_job_cancelled(self, job): + if job != self._job: + return + self._job = None + self.state = ProtocolState.CONNECTED + + def on_job_failed(self, job): + if job != self._job: + return + self._job = None + self.state = ProtocolState.CONNECTED + + +class ProtocolState(object): + CONNECTED = "connected" + DISCONNECTED = "disconnected" + PRINTING = "printing" + PAUSED = "paused" + ERROR = "error" + DISCONNECTED_WITH_ERROR = "disconnected with error" + class FanControlProtocolMixin(object): @@ -95,7 +153,7 @@ class FileAwareProtocolMixin(object): def list_files(self): pass - def start_file_print(self, name): + def start_file_print(self, name, position=0): pass def pause_file_print(self): @@ -104,6 +162,9 @@ class FileAwareProtocolMixin(object): def resume_file_print(self): pass + def get_file_print_status(self): + pass + class FileManagementProtocolMixin(FileAwareProtocolMixin): @@ -136,3 +197,9 @@ class FileAwareProtocolListener(object): def on_protocol_sd_status(self, name, pos, total): pass + + def on_protocol_file_print_started(self, name, size): + pass + + def on_protocol_file_print_done(self): + pass diff --git a/src/octoprint/comm/protocol/reprap/__init__.py b/src/octoprint/comm/protocol/reprap/__init__.py index e5ba03b16..578133e95 100644 --- a/src/octoprint/comm/protocol/reprap/__init__.py +++ b/src/octoprint/comm/protocol/reprap/__init__.py @@ -8,31 +8,33 @@ __copyright__ = "Copyright (C) 2015 The OctoPrint Project - Released under terms import re -from octoprint.comm.protocol import Protocol, FileStreamingProtocolMixin, MotorControlProtocolMixin, FanControlProtocolMixin +from octoprint.comm.protocol import Protocol, FileStreamingProtocolMixin, MotorControlProtocolMixin, FanControlProtocolMixin, ProtocolState from octoprint.comm.transport import LineAwareTransportWrapper, TransportListener, PushingTransportWrapper -from octoprint.util import to_unicode, dictview, CountedEvent +from octoprint.comm.protocol.util import TypedQueue, TypeAlreadyInQueue, GcodeCommand, process_gcode_line, strip_comment + +from octoprint.job import LocalGcodeFilePrintjob, SDFilePrintjob, LocalGcodeStreamjob + +from octoprint.util import to_unicode, protectedkeydict, CountedEvent import Queue as queue import threading - -regex_float_pattern = "[-+]?[0-9]*\.?[0-9]+" -regex_positive_float_pattern = "[+]?[0-9]*\.?[0-9]+" -regex_int_pattern = "\d+" - -regex_float = re.compile(regex_float_pattern) -"""Regex for a float value.""" - - class ReprapGcodeProtocol(Protocol, MotorControlProtocolMixin, FanControlProtocolMixin, FileStreamingProtocolMixin, TransportListener): + supported_jobs = [LocalGcodeFilePrintjob, LocalGcodeStreamjob, SDFilePrintjob] + + @staticmethod + def get_attributes_starting_with(flavor, prefix): + return [x for x in dir(flavor) if x.startswith(prefix)] + def __init__(self, flavor): Protocol.__init__(self) self.flavor = flavor - self._registered_messages = ["comm_ok", "comm_start", "comm_wait", "comm_resend"] \ - + [x for x in dir(self.flavor) if x.startswith("message_")] + self._registered_messages = self.get_attributes_starting_with(self.flavor, "comm_") \ + + self.get_attributes_starting_with(self.flavor, "message_") + self._error_messages = self.get_attributes_starting_with(self.flavor, "error_") self._transport = None @@ -40,9 +42,14 @@ class ReprapGcodeProtocol(Protocol, MotorControlProtocolMixin, FanControlProtoco # sd status sd_available=False, sd_files=[], - sd_files_temp=None + sd_files_temp=None, + + # resend status + resend_delta=None, + resend_linenumber=None, + resend_count=None, ) - self._state_view = dictview(self._internal_state) + self._protected_state = protectedkeydict(self._internal_state) self._command_queue = queue.Queue() self._clear_to_send = CountedEvent(max=10, name="comm.clear_to_send") @@ -50,9 +57,16 @@ class ReprapGcodeProtocol(Protocol, MotorControlProtocolMixin, FanControlProtoco self._current_linenumber = 1 self._last_lines = [] + self._last_communication_error = None + + self._gcode_hooks = dict(queuing=dict(), + queued=dict(), + sending=dict(), + sent=dict()) # mutexes self._line_mutex = threading.RLock() + self._send_queue_mutex = threading.RLock() # sending thread self._send_queue_active = True @@ -60,73 +74,86 @@ class ReprapGcodeProtocol(Protocol, MotorControlProtocolMixin, FanControlProtoco self.sending_thread.daemon = True self.sending_thread.start() + self._only_send_from_job = False + self._trigger_events_while_sending = True + def connect(self, transport): self._transport = transport + def process(self, job, position=0): + if isinstance(job, LocalGcodeStreamjob): + self._only_send_from_job = True + self._trigger_events_while_sending = False + else: + self._only_send_from_job = False + self._trigger_events_while_sending = True + + Protocol.process(job, position=position) + def move(self, x=None, y=None, z=None, e=None, feedrate=None, relative=False): commands = [self.flavor.command_move(x=x, y=y, z=z, e=e, f=feedrate)] if relative: - commands = [self.flavor.command_set_relative_positioning()] + commands + [self.flavor.command_set_absolute_positioning()] + commands = [self.flavor.command_set_relative_positioning()]\ + + commands\ + + [self.flavor.command_set_absolute_positioning()] - self._send(*commands) + self._send_commands(*commands) def home(self, x=False, y=False, z=False): - self._send(self.flavor.command_home(x=x, y=y, z=z)) + self._send_commands(self.flavor.command_home(x=x, y=y, z=z)) def set_feedrate_multiplier(self, multiplier): - self._send(self.flavor.command_set_feedrate_multiplier(multiplier)) + self._send_commands(self.flavor.command_set_feedrate_multiplier(multiplier)) def set_extrusion_multiplier(self, multiplier): - self._send(self.flavor.command.set_extrusion_multiplier(multiplier)) + self._send_commands(self.flavor.command.set_extrusion_multiplier(multiplier)) ##~~ MotorControlProtocolMixin def set_motors(self, enabled): - self._send(self.flavor.command.set_motors(enabled)) + self._send_commands(self.flavor.command.set_motors(enabled)) ##~~ FanControlProtocolMixin def set_fan_speed(self, speed): - self._send(self.flavor.command.set_fan_speed(speed)) + self._send_commands(self.flavor.command.set_fan_speed(speed)) ##~~ FileStreamingProtocolMixin def init_file_storage(self): - self._send(self.flavor.command_sd_init()) + self._send_commands(self.flavor.command_sd_init()) def list_files(self): - self._send(self.flavor.command_sd_refresh()) + self._send_commands(self.flavor.command_sd_refresh()) - def start_file_print(self, name): - self._send(self.flavor.command_sd_select_file(name), - self.flavor.command_sd_set_pos(0), - self.flavor.command_sd_start()) + def start_file_print(self, name, position=0): + self._send_commands(self.flavor.command_sd_select_file(name), + self.flavor.command_sd_set_pos(position), + self.flavor.command_sd_start()) def pause_file_print(self): - self._send(self.flavor.command_sd_pause()) + self._send_commands(self.flavor.command_sd_pause()) def resume_file_print(self): - self._send(self.flavor.command_sd_resume()) + self._send_commands(self.flavor.command_sd_resume()) def delete_file(self, name): - self._send(self.flavor.command_sd_delete(name)) + self._send_commands(self.flavor.command_sd_delete(name)) def record_file(self, name, job): - self._send(self.flavor.command_sd_begin_write(name)) + self._send_commands(self.flavor.command_sd_begin_write(name)) def stop_recording_file(self): - self._send(self.flavor.command_sd_end_write()) + self._send_commands(self.flavor.command_sd_end_write()) + + ##~~ Receiving def on_transport_data_received(self, transport, data): if transport != self._transport: return self._receive(data) - def _send(self, *commands): - for command in commands: - self._send_queue.put(str(command) + b"\n") - def _receive(self, data): line = to_unicode(data, encoding="ascii", errors="replace").strip() lower_line = line.lower() @@ -136,12 +163,16 @@ class ReprapGcodeProtocol(Protocol, MotorControlProtocolMixin, FanControlProtoco if not handler_method: continue - if getattr(self.flavor, message)(line, lower_line, self._state_view): + if getattr(self.flavor, message)(line, lower_line, self._protected_state): message_args = dict() parse_method = getattr(self.flavor, "parse_{}".format(message), None) if parse_method: - message_args.update(parse_method(line, lower_line, self._state_view)) + parse_result = parse_method(line, lower_line, self._protected_state) + if parse_result is None: + break + + message_args.update(parse_result) if not handler_method(**message_args): break @@ -149,6 +180,217 @@ class ReprapGcodeProtocol(Protocol, MotorControlProtocolMixin, FanControlProtoco # unknown message pass + def _on_comm_ok(self): + self._clear_to_send.set() + + if not self.state in (ProtocolState.PRINTING, ProtocolState.CONNECTED, ProtocolState.PAUSED): + return + + if self._internal_state["resend_delta"] is not None: + self._send_next_from_resend() + else: + self._continue_sending() + + def _on_comm_wait(self): + if self.state == ProtocolState.PRINTING: + self._on_comm_ok() + + def _on_comm_resend(self, linenumber): + if linenumber is None: + return + + last_communication_error = self._last_communication_error + self._last_communication_error = None + + resend_delta = self._current_linenumber - linenumber + + if last_communication_error is not None \ + and last_communication_error == "linenumber" \ + and linenumber == self._internal_state["resend_last_linenumber"] \ + and self._resend_delta is not None and self._internal_state["resend_count"] < self._internal_state["resend_delta"]: + self._logger.debug("Ignoring resend request for line {}, that still originates from lines we sent before we got the first resend request".format(linenumber)) + self._internal_state["resend_count"] += 1 + return + + self._internal_state["resend_delta"] = resend_delta + self._internal_state["resend_last_linenumber"] = linenumber + self._internal_state["resend_count"] = 0 + + if self._internal_state["resend_delta"] > len(self._last_lines) or len(self._last_lines) == 0 or self._internal_state["resend_delta"] < 0: + # TODO error because printer requested lines we don't have + pass + + + def _on_comm_start(self): + print("!!! start") + self._clear_to_send.set() + + def _on_comm_error(self, line, lower_line): + for message in self._error_messages: + handler_method = getattr(self, "_on_{}".format(message), None) + if not handler_method: + continue + + if getattr(self.flavor, message)(line, lower_line, self._protected_state): + message_args = dict() + + parse_method = getattr(self.flavor, "parse_{}".format(message), None) + if parse_method: + parse_result = parse_method(line, lower_line, self._protected_state) + if parse_result is None: + break + + message_args.update(parse_result) + + if not handler_method(**message_args): + break + else: + # unknown error message + pass + + def on_error_communication(self, error_type): + self._last_communication_error = error_type + + # TODO multiline error handling + + def _on_message_sd_init_ok(self): + self._internal_state["sd_available"] = True + self.list_files() + + def _on_message_sd_init_fail(self): + self._internal_state["sd_available"] = False + + def _on_message_sd_begin_file_list(self): + self._internal_state["sd_files_temp"] = [] + + def _on_message_sd_end_file_list(self): + self._internal_state["sd_files"] = self._internal_state["sd_files_temp"] + self._internal_state["sd_files_temp"] = None + self.notify_listeners("on_protocol_sd_file_list", self._internal_state["sd_files"]) + + def _on_message_sd_entry(self, name, size): + self._internal_state["sd_files_temp"].append((name, size)) + + def _on_message_sd_file_opened(self, name, size): + pass + + def _on_message_sd_file_selected(self): + pass + + def _on_message_sd_done_printing(self): + pass + + def _on_message_sd_printing_byte(self, current, total): + self.notify_listeners("on_protocol_sd_status", current, total) + + ##~~ Sending + + def _continue_sending(self): + if self.state == ProtocolState.PRINTING and self._job is not None: + if not self._send_from_queue(): + self._send_next_from_job() + elif self.state == ProtocolState.CONNECTED or self.state == ProtocolState.PAUSED: + self._send_from_queue() + + def _send_next_from_resend(self): + self._last_communication_error = None + + # Make sure we are only handling one sending job at a time + with self._send_queue_mutex: + resend_delta = self._internal_state["resend_delta"] + command = self._last_lines[-resend_delta] + linenumber = self._current_linenumber - resend_delta + + self._enqueue_for_sending(command, linenumber=linenumber) + + self._internal_state["resend_delta"] -= 1 + if self._internal_state["resend_delta"] <= 0: + self._internal_state["resend_delta"] = None + self._internal_state["resend_last_linenumber"] = None + self._internal_state["resend_count"] = 0 + + def _send_next_from_job(self): + with self._send_queue_mutex: + line = self._job.get_next() + if line is not None: + self._send_command(line) + + # TODO report progress + + def _send_from_queue(self): + # We loop here to make sure that if we do NOT send the first command + # from the queue, we'll send the second (if there is one). We do not + # want to get stuck here by throwing away commands. + while True: + if self._command_queue.empty() or self._only_send_from_job: + # no command queue or irrelevant command queue => return + return False + + entry = self._command_queue.get() + if isinstance(entry, tuple): + if not len(entry) == 2: + # something with that entry is broken, ignore it and fetch + # the next one + continue + cmd, cmd_type = entry + else: + cmd = entry + cmd_type = None + + if self._send_command(cmd, command_type=cmd_type): + # we actually did add this cmd to the send queue, so let's + # return, we are done here + return True + + def _send_commands(self, *commands): + for command in commands: + self._send_command(command) + + def _send_command(self, command, command_type=None): + if isinstance(command, GcodeCommand): + gcode = command + command = gcode.command + else: + gcode = None + + with self._send_queue_mutex: + if self._trigger_events_while_sending: + command, command_type, gcode = self._process_command_phase("queuing", command, command_type, gcode=gcode) + + if command is None: + # command is no more, return + return False + + # TODO gcode to event mapping + #if gcode and gcode.command in gcode_to_event: + # # if this is a gcode bound to an event, trigger that now + # self._eventbus.fire(gcode_to_event[gcode.command]) + + self._enqueue_for_sending(command, command_type=command_type) + + if self._trigger_events_while_sending: + self._process_command_phase("queued", command, command_type, gcode=gcode) + + return True + + def _enqueue_for_sending(self, command, command_type=None, linenumber=None): + """ + Enqueues a command and optional command_type and linenumber in the send queue. + + Arguments: + command (unicode): The command to send + command_type (unicode): An optional command type. There can only be + one command of a specified command type in the queue at any + given time. + linenumber (int): An optional line number to use for sending the + command. + """ + + try: + self._send_queue.put((command, linenumber, command_type)) + except TypeAlreadyInQueue as e: + self._logger.debug("Type already in queue: {}".format(e.type)) + def _send_loop(self): """ The send loop is reponsible of sending commands in ``self._send_queue`` over the line, if it is cleared for @@ -207,7 +449,7 @@ class ReprapGcodeProtocol(Protocol, MotorControlProtocolMixin, FanControlProtoco # now comes the part where we increase line numbers and send stuff - no turning back now command_requiring_checksum = gcode is not None and gcode.command in self.flavor.checksum_requiring_commands command_allowing_checksum = gcode is not None or self.flavor.unknown_with_checksum - checksum_enabled = self.flavor.always_send_checksum or (self.isPrinting() and not self.flavor.never_send_checksum) + checksum_enabled = self.flavor.always_send_checksum or (self.state == ProtocolState.PRINTING and not self.flavor.never_send_checksum) command_to_send = command.encode("ascii", errors="replace") if command_requiring_checksum or (command_allowing_checksum and checksum_enabled): @@ -328,215 +570,7 @@ class ReprapGcodeProtocol(Protocol, MotorControlProtocolMixin, FanControlProtoco def _add_to_last_line(self, command): self._last_lines.append(command) - def _on_comm_ok(self): - self._clear_to_send.set() - def _on_comm_wait(self): - print("!!! wait") - - def _on_comm_resend(self): - print("!!! resend") - - def _on_comm_start(self): - print("!!! start") - - def _on_message_sd_init_ok(self): - self._internal_state["sd_available"] = True - self.list_files() - - def _on_message_sd_init_fail(self): - self._internal_state["sd_available"] = False - - def _on_message_sd_begin_file_list(self): - self._internal_state["sd_files_temp"] = [] - - def _on_message_sd_end_file_list(self): - self._internal_state["sd_files"] = self._internal_state["sd_files_temp"] - self._internal_state["sd_files_temp"] = None - self.notify_listeners("on_protocol_sd_file_list", self._internal_state["sd_files"]) - - def _on_message_sd_entry(self, name, size): - self._internal_state["sd_files_temp"].append((name, size)) - -class GcodeCommand(object): - - known_float_attributes = ("x", "y", "z", "e", "s", "p", "r") - known_int_attributes = ("f", "t", "n") - known_attributes = known_float_attributes + known_int_attributes - - command_regex = re.compile("^\s*((?P[GM](?P\d+))|(?PT(?P\d+))|(?PF(?P\d+)))") - - argument_pattern = "\s*([{float_args}]{float}|[{int_args}]{int})".format(float_args="".join(map(lambda x: x.upper(), known_float_attributes)), - int_args="".join(map(lambda x: x.upper(), known_int_attributes)), - float=regex_float_pattern, - int=regex_int_pattern) - argument_regex = re.compile(argument_pattern) - param_regex = re.compile("^[GMT]\d+\s+(.*?)$") - - @staticmethod - def from_line(line): - """ - >>> gcode = GcodeCommand.from_line("M30 some_file.gco") - >>> gcode.command - 'M30' - >>> gcode.param - 'some_file.gco' - >>> gcode = GcodeCommand.from_line("G28 X0 Y0") - >>> gcode.command - 'G28' - >>> gcode.x - 0.0 - >>> gcode.y - 0.0 - >>> gcode = GcodeCommand.from_line("M104 S220.0 T1") - >>> gcode.command - 'M104' - >>> gcode.s - 220.0 - >>> gcode.t - 1 - """ - - line = line.strip() - command = "" - args = {"original": line} - match = GcodeCommand.command_regex.match(line) - if match is None: - args["unknown"] = True - else: - if match.group("GM"): - command = match.group("GM") - - matched_args = GcodeCommand.argument_regex.findall(line) - if not matched_args: - param_match = GcodeCommand.param_regex.match(line) - if param_match is not None: - args["param"] = param_match.group(1) - else: - for arg in matched_args: - key = arg[0].lower() - if key in GcodeCommand.known_int_attributes: - value = int(arg[1:]) - elif key in GcodeCommand.known_float_attributes: - value = float(arg[1:]) - else: - value = str(arg[1:]) - args[key] = value - elif match.group("T"): - command = "T" - args["tool"] = match.group("tool") - elif match.group("F"): - command = "F" - args["f"] = match.group("feedrate") - - return GcodeCommand(command, **args) - - def __init__(self, command, **kwargs): - self.command = command - self.x = None - self.y = None - self.z = None - self.e = None - self.s = None - self.p = None - self.r = None - self.f = None - self.t = None - self.n = None - - self.tool = None - self.original = None - self.param = None - - self.progress = None - self.callback = None - - self.unknown = False - - for key, value in kwargs.iteritems(): - if key in GcodeCommand.known_attributes + ("tool", "original", "param", "progress", "callback", "unknown"): - self.__setattr__(key, value) - - def __repr__(self): - return "GcodeCommand(\"{str}\",progress={progress})".format(str=str(self), progress=self.progress) - - def __str__(self): - if self.original is not None: - return self.original - else: - attr = [] - for key in GcodeCommand.known_attributes: - value = self.__getattribute__(key) - if value is not None: - if key in GcodeCommand.known_int_attributes: - attr.append("%s%d" % (key.upper(), value)) - elif key in GcodeCommand.known_float_attributes: - attr.append("%s%f" % (key.upper(), value)) - else: - attr.append("%s%r" % (key.upper(), value)) - attributeStr = " ".join(attr) - return "%s%s%s" % (self.command.upper(), " " + attributeStr if attributeStr else "", " " + self.param if self.param else "") - -class TypedQueue(queue.Queue): - - def __init__(self, maxsize=0): - queue.Queue.__init__(self, maxsize=maxsize) - self._lookup = [] - - def _put(self, item): - if isinstance(item, tuple) and len(item) == 3: - cmd, line, cmd_type = item - if cmd_type is not None: - if cmd_type in self._lookup: - raise TypeAlreadyInQueue(cmd_type, "Type {cmd_type} is already in queue".format(**locals())) - else: - self._lookup.append(cmd_type) - - queue.Queue._put(self, item) - - def _get(self): - item = queue.Queue._get(self) - - if isinstance(item, tuple) and len(item) == 3: - cmd, line, cmd_type = item - if cmd_type is not None and cmd_type in self._lookup: - self._lookup.remove(cmd_type) - - return item - - -class TypeAlreadyInQueue(Exception): - def __init__(self, t, *args, **kwargs): - Exception.__init__(self, *args, **kwargs) - self.type = t - - -def strip_comment(line): - if not ";" in line: - # shortcut - return line - - escaped = False - result = [] - for c in line: - if c == ";" and not escaped: - break - result += c - escaped = (c == "\\") and not escaped - return "".join(result) - -def process_gcode_line(line, offsets=None, current_tool=None): - line = strip_comment(line).strip() - if not len(line): - return None - - #if offsets is not None: - # line = apply_temperature_offsets(line, offsets, current_tool=current_tool) - - return line - - -""" if __name__ == "__main__": from octoprint.comm.transport.serialtransport import VirtualSerialTransport from octoprint.comm.protocol.reprap.virtual import VirtualPrinter @@ -567,3 +601,4 @@ if __name__ == "__main__": if __name__ == "__main__": gcode = GcodeCommand.from_line("M104 S220.4 T2") +""" diff --git a/src/octoprint/comm/protocol/reprap/flavors/__init__.py b/src/octoprint/comm/protocol/reprap/flavors/__init__.py index 95c5fb8be..ca73db094 100644 --- a/src/octoprint/comm/protocol/reprap/flavors/__init__.py +++ b/src/octoprint/comm/protocol/reprap/flavors/__init__.py @@ -8,7 +8,7 @@ __copyright__ = "Copyright (C) 2015 The OctoPrint Project - Released under terms import re -from octoprint.comm.protocol.reprap import GcodeCommand, regex_positive_float_pattern +from octoprint.comm.protocol.util import GcodeCommand, regex_positive_float_pattern class ReprapGcodeFlavor(object): @@ -37,6 +37,15 @@ class ReprapGcodeFlavor(object): regex_sd_file_opened = re.compile("File opened:\s*(?P.*?)(\s+Size:\s*(?P[0-9]+))?") + regex_sd_printing_byte = re.compile("(?P[0-9]*)/(?P[0-9]*)") + """Regex matching SD printing status reports. + + Groups will be as follows: + + * ``current``: current byte position in file being printed + * ``total``: total size of file being printed + """ + ##~~ Message matchers @classmethod @@ -55,17 +64,21 @@ class ReprapGcodeFlavor(object): def comm_resend(cls, line, lower_line, state): return lower_line.startswith("resend") or lower_line.startswith("rs") + @classmethod + def comm_debug(cls, line, lower_line, state): + return line.startswith("//") + @classmethod def comm_error(cls, line, lower_line, state): return line.startswith("Error:") or line.startswith("!!") @classmethod - def comm_error_multiline(cls, line, lower_line, state): + def error_multiline(cls, line, lower_line, state): return False @classmethod - def comm_error_communication(cls, line, lower_line, state): - return False + def error_communication(cls, line, lower_line, state): + return "line number" in lower_line or "checksum" in lower_line or "format error" in lower_line or "expected line" in lower_line @classmethod def message_temperature(cls, line, lower_line, state): @@ -101,7 +114,7 @@ class ReprapGcodeFlavor(object): @classmethod def message_sd_not_printing(cls, line, lower_line, state): - return "no sd printing" in lower_line + return "not sd printing" in lower_line @classmethod def message_sd_done_printing(cls, line, lower_line, state): @@ -121,6 +134,22 @@ class ReprapGcodeFlavor(object): ##~~ Message parsers + @classmethod + def parse_comm_error(cls, line, lower_line, state): + return dict(line=line, lower_line=lower_line) + + @classmethod + def parse_error_communication(cls, line, lower_line, state): + if "line number" in lower_line or "expected line" in lower_line: + error_type = "linenumber" + elif "checksum" in lower_line: + error_type = "checksum" + else: + error_type = "other" + + state["last_communication_error"] = error_type + return dict(error_type=error_type) + @classmethod def parse_comm_resend(cls, line, lower_line, state): line_to_resend = None @@ -138,8 +167,36 @@ class ReprapGcodeFlavor(object): @classmethod def parse_message_sd_entry(cls, line, lower_line, state): - name, size = line.split(" ", 2) - return dict(name=name, size=int(size)) + fileinfo = lower_line.rsplit(None, 1) + if len(fileinfo) > 1: + # we might have extended file information here, so let's split filename and size and try to make them a bit nicer + filename, size = fileinfo + try: + size = int(size) + except ValueError: + # whatever that was, it was not an integer, so we'll just use the whole line as filename and set size to None + filename = lower_line + size = None + else: + # no extended file information, so only the filename is there and we set size to None + filename = lower_line + size = None + + from octoprint.util import filter_non_ascii + + if filter_non_ascii(filename): + return None + else: + if not filename.startswith("/"): + # file from the root of the sd -- we'll prepend a / + filename = "/" + filename + + return dict(name=filename, size=int(size)) + + @classmethod + def parse_message_sd_printing_byte(cls, line, lower_line, state): + match = cls.regex_sd_printing_byte.match(lower_line) + return dict(current=match.group("current"), total=match.group("total")) ##~~ Commands diff --git a/src/octoprint/comm/protocol/reprap/flavors/repetier.py b/src/octoprint/comm/protocol/reprap/flavors/repetier.py index 6be5a217e..392a1cb32 100644 --- a/src/octoprint/comm/protocol/reprap/flavors/repetier.py +++ b/src/octoprint/comm/protocol/reprap/flavors/repetier.py @@ -8,6 +8,33 @@ __copyright__ = "Copyright (C) 2015 The OctoPrint Project - Released under terms from octoprint.comm.protocol.reprap.flavors import ReprapGcodeFlavor +import logging + class RepetierFlavor(ReprapGcodeFlavor): + key = "repetier" + always_send_checksum = True + + logger = logging.getLogger(__name__) + + identical_resends_countdown = 5 + + ##~~ Preprocessors, returning True stops further processing by the protocol + + @classmethod + def preprocess_comm_resend(cls, linenumber, state): + if state.get("resend_swallow_repetitions", False) \ + and state.get("resend_swallow_repetitions_counter", 0) \ + and linenumber == state["resend_last_linenumber"] \ + and state["resend_swallow_repetitions_counter"] > 0: + cls.logger.debug("Ignoring resend request for line {}, that is " + "probably a repetition sent by the firmware to " + "ensure it arrives, not a real request" + .format(linenumber)) + state["resend_swallow_repetitions_counter"] -= 1 + return True + + state["resend_swallow_repetitions_counter"] = cls.identical_resends_countdown + + diff --git a/src/octoprint/comm/protocol/util.py b/src/octoprint/comm/protocol/util.py new file mode 100644 index 000000000..7dc54311b --- /dev/null +++ b/src/octoprint/comm/protocol/util.py @@ -0,0 +1,191 @@ + +import re +import Queue as queue + +regex_float_pattern = "[-+]?[0-9]*\.?[0-9]+" +regex_positive_float_pattern = "[+]?[0-9]*\.?[0-9]+" +regex_int_pattern = "\d+" + +regex_float = re.compile(regex_float_pattern) +"""Regex for a float value.""" + + +class GcodeCommand(object): + + known_float_attributes = ("x", "y", "z", "e", "s", "p", "r") + known_int_attributes = ("f", "t", "n") + known_attributes = known_float_attributes + known_int_attributes + + command_regex = re.compile("^\s*((?P[GM](?P\d+))|(?PT(?P\d+))|(?PF(?P\d+)))") + + argument_pattern = "\s*([{float_args}]{float}|[{int_args}]{int})".format(float_args="".join(map(lambda x: x.upper(), known_float_attributes)), + int_args="".join(map(lambda x: x.upper(), known_int_attributes)), + float=regex_float_pattern, + int=regex_int_pattern) + argument_regex = re.compile(argument_pattern) + param_regex = re.compile("^[GMT]\d+\s+(.*?)$") + + @staticmethod + def from_line(line): + """ + >>> gcode = GcodeCommand.from_line("M30 some_file.gco") + >>> gcode.command + 'M30' + >>> gcode.param + 'some_file.gco' + >>> gcode = GcodeCommand.from_line("G28 X0 Y0") + >>> gcode.command + 'G28' + >>> gcode.x + 0.0 + >>> gcode.y + 0.0 + >>> gcode = GcodeCommand.from_line("M104 S220.0 T1") + >>> gcode.command + 'M104' + >>> gcode.s + 220.0 + >>> gcode.t + 1 + """ + + if isinstance(line, GcodeCommand): + return line + + line = line.strip() + command = "" + args = {"original": line} + match = GcodeCommand.command_regex.match(line) + if match is None: + args["unknown"] = True + else: + if match.group("GM"): + command = match.group("GM") + + matched_args = GcodeCommand.argument_regex.findall(line) + if not matched_args: + param_match = GcodeCommand.param_regex.match(line) + if param_match is not None: + args["param"] = param_match.group(1) + else: + for arg in matched_args: + key = arg[0].lower() + if key in GcodeCommand.known_int_attributes: + value = int(arg[1:]) + elif key in GcodeCommand.known_float_attributes: + value = float(arg[1:]) + else: + value = str(arg[1:]) + args[key] = value + elif match.group("T"): + command = "T" + args["tool"] = match.group("tool") + elif match.group("F"): + command = "F" + args["f"] = match.group("feedrate") + + return GcodeCommand(command, **args) + + def __init__(self, command, **kwargs): + self.command = command + self.x = None + self.y = None + self.z = None + self.e = None + self.s = None + self.p = None + self.r = None + self.f = None + self.t = None + self.n = None + + self.tool = None + self.original = None + self.param = None + + self.progress = None + self.callback = None + + self.unknown = False + + for key, value in kwargs.iteritems(): + if key in GcodeCommand.known_attributes + ("tool", "original", "param", "progress", "callback", "unknown"): + self.__setattr__(key, value) + + def __repr__(self): + return "GcodeCommand(\"{str}\",progress={progress})".format(str=str(self), progress=self.progress) + + def __str__(self): + if self.original is not None: + return self.original + else: + attr = [] + for key in GcodeCommand.known_attributes: + value = self.__getattribute__(key) + if value is not None: + if key in GcodeCommand.known_int_attributes: + attr.append("%s%d" % (key.upper(), value)) + elif key in GcodeCommand.known_float_attributes: + attr.append("%s%f" % (key.upper(), value)) + else: + attr.append("%s%r" % (key.upper(), value)) + attributeStr = " ".join(attr) + return "%s%s%s" % (self.command.upper(), " " + attributeStr if attributeStr else "", " " + self.param if self.param else "") + +class TypedQueue(queue.Queue): + + def __init__(self, maxsize=0): + queue.Queue.__init__(self, maxsize=maxsize) + self._lookup = [] + + def _put(self, item): + if isinstance(item, tuple) and len(item) == 3: + cmd, line, cmd_type = item + if cmd_type is not None: + if cmd_type in self._lookup: + raise TypeAlreadyInQueue(cmd_type, "Type {cmd_type} is already in queue".format(**locals())) + else: + self._lookup.append(cmd_type) + + queue.Queue._put(self, item) + + def _get(self): + item = queue.Queue._get(self) + + if isinstance(item, tuple) and len(item) == 3: + cmd, line, cmd_type = item + if cmd_type is not None and cmd_type in self._lookup: + self._lookup.remove(cmd_type) + + return item + + +class TypeAlreadyInQueue(Exception): + def __init__(self, t, *args, **kwargs): + Exception.__init__(self, *args, **kwargs) + self.type = t + + +def strip_comment(line): + if not ";" in line: + # shortcut + return line + + escaped = False + result = [] + for c in line: + if c == ";" and not escaped: + break + result += c + escaped = (c == "\\") and not escaped + return "".join(result) + +def process_gcode_line(line, offsets=None, current_tool=None): + line = strip_comment(line).strip() + if not len(line): + return None + + #if offsets is not None: + # line = apply_temperature_offsets(line, offsets, current_tool=current_tool) + + return line diff --git a/src/octoprint/comm/transport/serialtransport.py b/src/octoprint/comm/transport/serialtransport.py index 0a20fa18a..d3a4a51ff 100644 --- a/src/octoprint/comm/transport/serialtransport.py +++ b/src/octoprint/comm/transport/serialtransport.py @@ -9,6 +9,7 @@ from octoprint.comm.transport import Transport, LineAwareTransportWrapper, Pushi from octoprint.comm.transport.parameters import TextType, IntegerType, SuggestionType, ConstantNameType import serial +import logging class SerialTransport(Transport): name = "Serial Connection" @@ -69,9 +70,11 @@ class SerialTransport(Transport): + [ConstantNameType(port.device, port.description) for port in ports] def __init__(self): - super(Transport, self).__init__() + super(SerialTransport, self).__init__() self._serial = None + self._logger = logging.getLogger(__name__) + def create_connection(self, port="AUTO", baudrate=0): self._serial = serial.Serial(port=port, baudrate=baudrate) @@ -79,10 +82,13 @@ class SerialTransport(Transport): self._serial.close() def read(self, size=None): - return self._serial.read(size=size) + result = self._serial.read(size=size) + print("<<< {!r}".format(result)) + return result def write(self, data): self._serial.write(data) + print(">>> {!r}".format(data)) def close_connection(self): self._serial.close() diff --git a/src/octoprint/job/__init__.py b/src/octoprint/job/__init__.py new file mode 100644 index 000000000..f381d9dd6 --- /dev/null +++ b/src/octoprint/job/__init__.py @@ -0,0 +1,256 @@ +from __future__ import absolute_import, unicode_literals + + +import logging +import os +import time + +from octoprint.comm.protocol import FileAwareProtocolListener + +from abc import ABCMeta, abstractmethod, abstractproperty + +class Printjob(object): + __metaclass__ = ABCMeta + + def __init__(self): + self._logger = logging.getLogger(__name__) + self._start = None + self._protocol = None + self._printer_profile = None + + self._listeners = [] + + def register_listener(self, listener): + self._listeners.append(listener) + + def unregister_listener(self, listener): + self._listeners.remove(listener) + + def can_process(self, protocol): + return False + + def process(self, protocol, position=0): + self._start = time.time() + self._protocol = protocol + + def cancel(self): + pass + + def get_next(self): + return None + + def get_time_estimate(self, lost_time=0): + if self._start is None: + return None + + progress = self.get_progress() + if not progress: + return None + + spent_time = time.time() - self._start - lost_time + return spent_time / progress + + def get_progress(self): + return 0.0 + + def can_get_content(self): + return False + + def get_content_generator(self): + return None + + def process_job_started(self): + self.notify_listeners("on_job_started", self) + + def process_job_done(self): + self.notify_listeners("on_job_done", self) + + def process_job_cancelled(self): + self.notify_listeners("on_job_cancelled", self) + + def process_job_failed(self): + self.notify_listeners("on_job_failed") + + def notify_listeners(self, name, *args, **kwargs): + for listener in self._listeners: + method = getattr(listener, name, None) + if not method: + continue + + try: + method(*args, **kwargs) + except: + self._logger.exception("Exception while calling {} on printjob listener {}".format( + "{}({})".format(name, ", ".join(list(args) + ["{}={}".format(key, value) + for key, value in kwargs.items()])) + )) + + +class LocalFilePrintjob(Printjob): + + def __init__(self, path, encoding="utf-8"): + Printjob.__init__(self) + + self._path = path + self._encoding = encoding + self._size = os.stat(path).st_size + + self._handle = None + + def process(self, protocol, position=0): + Printjob.process(self, protocol, position=position) + + from octoprint.util import bom_aware_open + self._handle = bom_aware_open(self._path, encoding=self._encoding, errors="replace") + + if position > 0: + self._handle.seek(position) + + def get_next(self): + from octoprint.util import to_unicode + + if self._handle is None: + raise ValueError("File {} is not open for reading" % self._path) + + try: + processed = None + while processed is None: + if self._handle is None: + # file got closed just now + return None + line = to_unicode(self._handle.readline()) + if not line: + self.notify_listeners("on_job_done", self) + self.close() + processed = self.process_line(line) + + return processed + except Exception as e: + self.close() + self._logger.exception("Exception while processing line") + raise e + + def process_line(self, line): + return line + + def get_progress(self): + if self._handle is None: + return 0.0 + return self._handle.tell() / self._size + + def close(self): + if self._handle is not None: + try: + self._handle.close() + except: + pass + self._handle = None + + def can_get_content(self): + return True + + def get_content_generator(self): + from octoprint.util import bom_aware_open + with bom_aware_open(self._path, encoding=self._encoding, error="replace") as f: + for line in f.readline(): + yield line + + +class LocalGcodeFilePrintjob(LocalFilePrintjob): + + def can_process(self, protocol): + return LocalGcodeFilePrintjob in protocol.supported_jobs + + def process_line(self, line): + processed = line + # strip comments + + # apply offsets + + # return result + return processed + + +class LocalGcodeStreamjob(LocalGcodeFilePrintjob): + + def can_process(self, protocol): + from octoprint.comm.protocol import FileStreamingProtocolMixin + return LocalGcodeStreamjob in protocol.supported_jobs and isinstance(protocol, FileStreamingProtocolMixin) + + def process_line(self, line): + # we do not change anything for sd file streaming + return line + + +class SDFilePrintjob(Printjob, FileAwareProtocolListener): + + def __init__(self, filename, status_interval=2.0): + Printjob.__init__(self) + self._filename = filename + self._status_interval = status_interval + + self._status_timer = None + self._active = False + + self._size = None + self._last_pos = None + + def can_process(self, protocol): + from octoprint.comm.protocol import FileAwareProtocolMixin + return SDFilePrintjob in protocol.supported_jobs and isinstance(protocol, FileAwareProtocolMixin) + + def process(self, protocol, position=0): + Printjob.process(self, protocol, position=position) + + self._protocol.register_listener(self) + self._protocol.start_file_print(self._filename, position=position) + self._active = True + self._last_pos = position + + from octoprint.util import RepeatedTimer + self._status_timer = RepeatedTimer(self._status_interval, self._query_status, condition=self._query_active) + self._status_timer.start() + + def get_progress(self): + if self._size is None or self._last_pos is None: + return None + return self._last_pos / self._size + + def on_protocol_sd_file_list(self, files): + pass + + def on_protocol_sd_status(self, name, pos, total): + if name != self._filename: + return + self._last_pos = pos + + def on_protocol_file_print_started(self, name, size): + if name != self._filename: + return + self._size = size + + def on_protocol_file_print_done(self): + self._active = False + self._protocol.unregister_listener(self) + self.notify_listeners("on_job_done", self) + + def _query_status(self): + self._protocol.get_file_print_status() + + def _query_active(self): + return self._active + + +class PrintjobListener(object): + + def on_job_started(self, job): + pass + + def on_job_done(self, job): + pass + + def on_job_cancelled(self, job): + pass + + def on_job_failed(self, job): + pass diff --git a/src/octoprint/util/__init__.py b/src/octoprint/util/__init__.py index 3167b9f3d..33cbb0c61 100644 --- a/src/octoprint/util/__init__.py +++ b/src/octoprint/util/__init__.py @@ -986,3 +986,61 @@ class dictview(collections.Mapping): value = copy.copy(value) return value + +class protectedkeydict(collections.MutableMapping): + """ + >>> my_dict = dict(a="a") + >>> protected = protectedkeydict(my_dict) + >>> protected["a"] + 'a' + >>> protected["a"] = "b" + Traceback (most recent call last): + ... + KeyError: "Cannot set protected key 'a'" + >>> protected["b"] = "b" + >>> protected["b"] = "b2" + >>> del protected["a"] + Traceback (most recent call last): + ... + KeyError: "Cannot delete protected key 'a'" + >>> del protected["b"] + """ + + def __init__(self, data, copy=False, deepcopy=False): + self.data = data + self.copy = copy + self.deepcopy = deepcopy + self.protected_keys = data.keys() + + def __len__(self): + return len(self.data) + + def __iter__(self): + return iter(self.data) + + def __getitem__(self, key): + return self._get_item(key) + + def __setitem__(self, key, value): + if key in self.protected_keys: + raise KeyError("Cannot set protected key {!r}".format(key)) + self.data[key] = value + + def __delitem__(self, key): + if key in self.protected_keys: + raise KeyError("Cannot delete protected key {!r}".format(key)) + + def _get_item(self, key): + if key not in self.protected_keys: + return self.data[key] + else: + import copy + + value = self.data[key] + + if self.deepcopy: + value = copy.deepcopy(value) + elif self.copy: + value = copy.copy(value) + + return value