Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
QuinnDamerell committed Jan 24, 2025
1 parent 7ba5ac7 commit 238f7c3
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
7 changes: 4 additions & 3 deletions bambu_octoeverywhere/bambuclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ def _Publish(self, msg:dict) -> bool:
def _GetConnectionContextToTry(self) -> ConnectionContext:
# Increment and reset if it's too high.
# This will restart the process of trying cloud connect and falling back.
countReset = False
self.ConsecutivelyFailedConnectionAttempts += 1
if self.ConsecutivelyFailedConnectionAttempts > 6:
self.ConsecutivelyFailedConnectionAttempts = 0
Expand All @@ -395,10 +396,10 @@ def _GetConnectionContextToTry(self) -> ConnectionContext:
self.Logger.warning("We tried to connect via Bambu Cloud, but failed. We will try a local connection.")

# On the first few attempts, use the expected IP or the cloud config.
# The first attempt will always be attempt 1, since it's reset to 0 and incremented before connecting.
# Every time we reset the count, we will try a network scan to see if we can find the printer guessing it's IP might have changed.
# The IP can be empty, like if the docker container is used, in which case we should always search for the printer.
configIpOrHostname = self.Config.GetStr(Config.SectionCompanion, Config.CompanionKeyIpOrHostname, None)
if self.ConsecutivelyFailedConnectionAttempts < 4:
if countReset is False:
# If we aren't using a cloud connection or it failed, return the local hostname
if configIpOrHostname is not None and len(configIpOrHostname) > 0:
return self._GetLocalConnectionContext(configIpOrHostname)
Expand All @@ -408,7 +409,7 @@ def _GetConnectionContextToTry(self) -> ConnectionContext:
# Note we don't want to do this too often since it's CPU intensive and the printer might just be off.
# We use a lower thread count and delay before each action to reduce the required load.
self.Logger.info(f"Searching for your Bambu Lab printer {self.PrinterSn}")
ips = NetworkSearch.ScanForInstances_Bambu(self.Logger, self.LanAccessCode, self.PrinterSn, threadCount=5, delaySec=1.0)
ips = NetworkSearch.ScanForInstances_Bambu(self.Logger, self.LanAccessCode, self.PrinterSn, threadCount=25, delaySec=0.2)

# If we get an IP back, it is the printer.
# The scan above will only return an IP if the printer was successfully connected to, logged into, and fully authorized with the Access Token and Printer SN.
Expand Down
1 change: 0 additions & 1 deletion linux_host/networksearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def ScanForInstances_Bambu(logger:logging.Logger, accessCode:str, printerSn:str,
def callback(ip:str):
# TODO - This is a quick hack to slow down the scan so it doesn't eat a lot of CPU load on the device while the printer is off
# and the plugin is trying to find it. But it's important this scan also be fast, for the installer.
logger.Info("sleeping for "+str(delaySec)+" seconds")
time.sleep(delaySec)
return NetworkSearch.ValidateConnection_Bambu(logger, ip, accessCode, printerSn, portStr, timeoutSec=5)
# We want to return if any one IP is found, since there can only be one printer that will match the printer 100% correct.
Expand Down

0 comments on commit 238f7c3

Please sign in to comment.