Trying to get disconnecting to work reliably
This commit is contained in:
1 parent
54bd41903b
commit
aee1dff718
6 files changed
+71
-12
No files matched your search
@@ -33,6 +33,9 @@ class Protocol(ListenerAware, TransportListener):
|
||||
@state.setter
|
||||
def state(self, new_state):
|
||||
old_state = self._state
|
||||
if old_state == new_state:
|
||||
return
|
||||
|
||||
self._state = new_state
|
||||
|
||||
name = "_on_state_{}".format(new_state)
|
||||
@@ -44,8 +47,11 @@ class Protocol(ListenerAware, TransportListener):
|
||||
self.notify_listeners("on_protocol_state", self, old_state, new_state)
|
||||
|
||||
def connect(self, transport, transport_args=None, transport_kwargs=None):
|
||||
self.process_protocol_log("--- Protocol {} connecting via transport {}...".format(self.__class__.__name__,
|
||||
transport.__class__.__name__))
|
||||
if self.state not in (ProtocolState.DISCONNECTED, ProtocolState.DISCONNECTED_WITH_ERROR):
|
||||
raise ProtocolAlreadyConnectedError("Already connected, disconnect first")
|
||||
|
||||
self.process_protocol_log("--- Protocol {} connecting via transport {}...".format(self,
|
||||
transport))
|
||||
|
||||
if transport_args is None:
|
||||
transport_args = []
|
||||
@@ -60,7 +66,16 @@ class Protocol(ListenerAware, TransportListener):
|
||||
self.state = ProtocolState.CONNECTING
|
||||
|
||||
def disconnect(self):
|
||||
if self.state in (ProtocolState.DISCONNECTED, ProtocolState.DISCONNECTED_WITH_ERROR):
|
||||
raise ProtocolNotConnectedError("Already disconnected")
|
||||
|
||||
self.process_protocol_log("--- Protocol {} disconnecting from transport {}...".format(self,
|
||||
self._transport))
|
||||
|
||||
self._transport.unregister_listener(self)
|
||||
if self._transport.state == TransportState.CONNECTED:
|
||||
self._transport.disconnect()
|
||||
self.state = ProtocolState.DISCONNECTED
|
||||
|
||||
def process(self, job, position=0):
|
||||
if not job.can_process(self):
|
||||
@@ -146,6 +161,9 @@ class Protocol(ListenerAware, TransportListener):
|
||||
self._protocol_logger.info(message)
|
||||
self.notify_listeners("on_protocol_log", self, message)
|
||||
|
||||
def __str__(self):
|
||||
return self.__class__.__name__
|
||||
|
||||
class ProtocolState(object):
|
||||
CONNECTING = "connecting"
|
||||
CONNECTED = "connected"
|
||||
@@ -155,6 +173,11 @@ class ProtocolState(object):
|
||||
ERROR = "error"
|
||||
DISCONNECTED_WITH_ERROR = "disconnected_with_error"
|
||||
|
||||
class ProtocolAlreadyConnectedError(Exception):
|
||||
pass
|
||||
|
||||
class ProtocolNotConnectedError(Exception):
|
||||
pass
|
||||
|
||||
class FanControlProtocolMixin(object):
|
||||
|
||||
|
||||
@@ -128,6 +128,14 @@ class ReprapGcodeProtocol(Protocol, MotorControlProtocolMixin,
|
||||
transport_args=transport_args,
|
||||
transport_kwargs=transport_kwargs)
|
||||
|
||||
def disconnect(self):
|
||||
self._send_queue_active = False
|
||||
if self._temperature_poller:
|
||||
self._temperature_poller.cancel()
|
||||
self._temperature_poller = None
|
||||
|
||||
super(ReprapGcodeProtocol, self).disconnect()
|
||||
|
||||
def process(self, job, position=0):
|
||||
if isinstance(job, LocalGcodeStreamjob):
|
||||
self._internal_state["only_from_job"] = True
|
||||
|
||||
@@ -40,13 +40,15 @@ class Transport(ListenerAware):
|
||||
|
||||
def connect(self, **params):
|
||||
if self.state == TransportState.CONNECTED:
|
||||
raise AlreadyConnectedError("Already connected, disconnect first")
|
||||
raise TransportAlreadyConnectedError("Already connected, disconnect first")
|
||||
options = self.get_connection_options()
|
||||
param_dict = get_param_dict(params, options)
|
||||
self.create_connection(**param_dict)
|
||||
self.state = TransportState.CONNECTED
|
||||
|
||||
def disconnect(self):
|
||||
if self.state == TransportState.DISCONNECTED:
|
||||
raise TransportNotConnectedError("Already disconnected")
|
||||
self.drop_connection()
|
||||
self.state = TransportState.DISCONNECTED
|
||||
|
||||
@@ -71,17 +73,20 @@ class Transport(ListenerAware):
|
||||
def do_write(self, data):
|
||||
pass
|
||||
|
||||
def __str__(self):
|
||||
return self.__class__.__name__
|
||||
|
||||
|
||||
class TransportState(object):
|
||||
CONNECTED = "connected"
|
||||
DISCONNECTED = "disconnected"
|
||||
|
||||
|
||||
class NotConnectedError(Exception):
|
||||
class TransportNotConnectedError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class AlreadyConnectedError(Exception):
|
||||
class TransportAlreadyConnectedError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
@@ -132,11 +137,17 @@ class SeparatorAwareTransportWrapper(ListenerAware):
|
||||
def __getattr__(self, item):
|
||||
return getattr(self.transport, item)
|
||||
|
||||
def __str__(self):
|
||||
return "SeparatorAwareTransportWrapper({}, separator={})".format(self.transport, self.separator)
|
||||
|
||||
class LineAwareTransportWrapper(SeparatorAwareTransportWrapper):
|
||||
|
||||
def __init__(self, transport):
|
||||
SeparatorAwareTransportWrapper.__init__(self, transport, b"\n")
|
||||
|
||||
def __str__(self):
|
||||
return "LineAwareTransportWrapper({})".format(self.transport)
|
||||
|
||||
class PushingTransportWrapper(object):
|
||||
|
||||
def __init__(self, transport, name="pushingTransportReceiveLoop", timeout=None):
|
||||
@@ -158,20 +169,27 @@ class PushingTransportWrapper(object):
|
||||
self._receiver_thread.start()
|
||||
|
||||
def disconnect(self):
|
||||
self.transport.disconnect()
|
||||
self._receiver_active = False
|
||||
self.transport.disconnect()
|
||||
|
||||
def wait(self):
|
||||
self._receiver_thread.join()
|
||||
|
||||
def _receiver_loop(self):
|
||||
while self._receiver_active:
|
||||
data = self.transport.read(timeout=self.timeout)
|
||||
self.notify_listeners("on_transport_data_pushed", self, data)
|
||||
try:
|
||||
data = self.transport.read(timeout=self.timeout)
|
||||
self.notify_listeners("on_transport_data_pushed", self, data)
|
||||
except:
|
||||
if self._receiver_active:
|
||||
raise
|
||||
|
||||
def __getattr__(self, item):
|
||||
return getattr(self.transport, item)
|
||||
|
||||
def __str__(self):
|
||||
return "PushingTransportWrapper({})".format(self.transport)
|
||||
|
||||
class TransportListener(object):
|
||||
|
||||
def on_transport_log_sent_data(self, transport, data):
|
||||
|
||||
@@ -105,6 +105,8 @@ class SerialTransport(Transport):
|
||||
def close_connection(self):
|
||||
self._serial.close()
|
||||
|
||||
def __str__(self):
|
||||
return "SerialTransport"
|
||||
|
||||
class VirtualSerialTransport(SerialTransport):
|
||||
name = "Virtual Serial Connection"
|
||||
@@ -126,3 +128,6 @@ class VirtualSerialTransport(SerialTransport):
|
||||
raise ValueError("virtual_serial_factory is not callable")
|
||||
|
||||
self._serial = self.virtual_serial_factory()
|
||||
|
||||
def __str__(self):
|
||||
return "VirtualSerialTransport({})".format(self.virtual_serial_factory)
|
||||
@@ -37,6 +37,9 @@ class TcpTransport(Transport):
|
||||
self._socket = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
|
||||
self._socket.connect((host, port))
|
||||
|
||||
def drop_connection(self):
|
||||
self._socket.close()
|
||||
|
||||
def do_read(self, size=None, timeout=None):
|
||||
if size is None:
|
||||
size = 16
|
||||
@@ -45,8 +48,8 @@ class TcpTransport(Transport):
|
||||
def do_write(self, data):
|
||||
self._socket.sendall(data)
|
||||
|
||||
def close(self):
|
||||
self._socket.close()
|
||||
def __str__(self):
|
||||
return "TcpTransport"
|
||||
|
||||
if __name__ == "__main__":
|
||||
transport = LineAwareTransportWrapper(TcpTransport())
|
||||
|
||||
@@ -243,11 +243,13 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback, ProtocolListener,
|
||||
eventManager().fire(Events.DISCONNECTING)
|
||||
self.cancel_print()
|
||||
|
||||
if self._protocol is not None:
|
||||
if self._protocol is not None and self._protocol.state not in (ProtocolState.DISCONNECTED, ProtocolState.DISCONNECTED_WITH_ERROR):
|
||||
self._protocol.disconnect()
|
||||
self._protocol = None
|
||||
|
||||
if self._transport is not None:
|
||||
self._transport.disconnect()
|
||||
self._transport = None
|
||||
eventManager().fire(Events.DISCONNECTED)
|
||||
|
||||
def get_transport(self):
|
||||
@@ -450,7 +452,7 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback, ProtocolListener,
|
||||
"""
|
||||
Cancel the current printjob.
|
||||
"""
|
||||
if self._protocol is None:
|
||||
if self._protocol is None or not self._protocol.state in (ProtocolState.PRINTING, ProtocolState.PAUSED):
|
||||
return
|
||||
|
||||
# reset progress, height, print time
|
||||
|
||||
Reference in new issue
Block a user