Skip to content

Commit

Permalink
Use realpath for port comparison
Browse files Browse the repository at this point in the history
Use realpath so we don't get fooled by synonyms (symbolic links).  Also,
add some more logging
  • Loading branch information
markwal committed Mar 11, 2016
1 parent c2b5330 commit 5ec52fa
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions octoprint_portlister/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# coding=utf-8
from __future__ import absolute_import

import os
from threading import Timer
import watchdog
from watchdog.observers import Observer

import octoprint.plugin
from octoprint.printer import get_connection_options
from octoprint.util import get_exception_string

class PortListEventHandler(watchdog.events.FileSystemEventHandler):
def __init__(self, parent):
Expand All @@ -18,7 +20,7 @@ def on_created(self, event):

class PortListerPlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.AssetPlugin, octoprint.plugin.SettingsPlugin):
def on_after_startup(self, *args, **kwargs):
self._logger.info("Port Lister %s %s", repr(args), repr(kwargs))
self._logger.info("Port Lister %s %s" % (repr(args), repr(kwargs)))
event_handler = PortListEventHandler(self)
self._observer = Observer()
self._observer.schedule(event_handler, "/dev", recursive=False)
Expand All @@ -28,10 +30,10 @@ def on_port_created(self, port, *args, **kwargs):
# if we're already connected ignore it
if self._printer.is_closed_or_error():
connection_options = get_connection_options()
self._logger.info("on_port_created connection_options %s", repr(connection_options))
self._logger.info("on_port_created connection_options %s" % (repr(connection_options)))

# is the new device in the port list? yes, tell the view model
self._logger.info("Checking if %s is in %s", port, repr(connection_options["ports"]))
self._logger.info("Checking if %s is in %s" % (port, repr(connection_options["ports"])))
if port in connection_options["ports"]:
self._plugin_manager.send_plugin_message(self._plugin_name, port)

Expand All @@ -42,7 +44,7 @@ def on_port_created(self, port, *args, **kwargs):
else:
self._logger.info("Not autoconnecting because autoconnect is turned off.")
else:
self._logger.warning("Won't autoconnect because %s isn't in %s", port, repr(connection_options["ports"]))
self._logger.warning("Won't autoconnect because %s isn't in %s" % (port, repr(connection_options["ports"])))
else:
self._logger.warning("Not auto connecting because printer is not closed nor in error state.")

Expand All @@ -52,12 +54,20 @@ def on_shutdown(self, *args, **kwargs):
self._observer.join()

def do_auto_connect(self, port, *args, **kwargs):
(autoport, baudrate) = self._settings.global_get(["serial", "port"]), self._settings.global_get_int(["serial", "baudrate"])
if autoport == port or autoport == "AUTO":
printer_profile = self._printer_profile_manager.get_default()
profile = printer_profile["id"] if "id" in printer_profile else "_default"
self._logger.info("Attempting to connect to %s at %d with profile %s", autoport, baudrate, repr(profile))
self._printer.connect(port=autoport, baudrate=baudrate, profile=profile)
try:
self._logger.info("do_auto_connect")
(autoport, baudrate) = self._settings.global_get(["serial", "port"]), self._settings.global_get_int(["serial", "baudrate"])
if autoport == "AUTO" or os.path.realpath(autoport) == os.path.realpath(port):
self._logger.info("realpath match")
printer_profile = self._printer_profile_manager.get_default()
profile = printer_profile["id"] if "id" in printer_profile else "_default"
self._logger.info("Attempting to connect to %s at %d with profile %s" % (autoport, baudrate, repr(profile)))
self._printer.connect(port=autoport, baudrate=baudrate, profile=profile)
else:
self._logger.info("realpath no match")
self._logger.info("Skipping auto connect on %s because it isn't %s" % (os.path.realpath(port), os.path.realpath(autoport)))
except:
self._logger.error("Exception in do_auto_connect %s", get_exception_string())

def get_settings_defaults(self, *args, **kwargs):
return dict(autoconnect_delay=20)
Expand Down

0 comments on commit 5ec52fa

Please sign in to comment.