Skip to content

Commit

Permalink
Updating the bambu docker to make local mode more clear and perfered.
Browse files Browse the repository at this point in the history
  • Loading branch information
QuinnDamerell committed Dec 19, 2024
1 parent 98dc8ae commit 12a0f13
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 128 deletions.
22 changes: 20 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
]
},
{
"name": "Bambu Connect - P1S",
"name": "Bambu Connect - X1C",
"type": "debugpy",
"request": "launch",
"module": "bambu_octoeverywhere",
Expand All @@ -51,7 +51,7 @@
]
},
{
"name": "Bambu Connect - X1C",
"name": "Bambu Connect - P1S",
"type": "debugpy",
"request": "launch",
"module": "bambu_octoeverywhere",
Expand Down Expand Up @@ -129,5 +129,23 @@
"module": "octoprint_octoeverywhere",
"justMyCode": true
},
{
"name": "Docker Container Bootstrap & Run",
"type": "debugpy",
"request": "launch",
"module": "docker_octoeverywhere",
"justMyCode": false,
"env": {
"VENV_DIR": "/home/pi/octoeverywhere-env",
"REPO_DIR": "/home/pi/octoeverywhere",
"DATA_DIR": "/home/pi/.octoeverywhere-docker-bootstrap", // This path needs to be created.
//"SERIAL_NUMBER": "serial",
//"ACCESS_CODE": "test123"
//"PRINTER_IP": "127.0.0.1",
//"CONNECTION_MODE": "cloud",
//"BAMBU_CLOUD_ACCOUNT_EMAIL":"quinn@test.com",
//"BAMBU_CLOUD_ACCOUNT_PASSWORD": "test123",
},
},
]
}
35 changes: 21 additions & 14 deletions bambu_octoeverywhere/bambuclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ def _ClientWorker(self):
# We are connecting to Bambu Cloud, setup MQTT for it.
self.Client.tls_set(tls_version=ssl.PROTOCOL_TLS)
else:
# We are trying to connect to the printer locally, so configure mqtt for a LAN connection.
self.Logger.info("Trying to connect to printer via LAN...")
# We are trying to connect to the printer locally, so configure mqtt for a local connection.
self.Logger.info("Trying to connect to printer via local connection...")
self.Client.tls_set(tls_version=ssl.PROTOCOL_TLS, cert_reqs=ssl.CERT_NONE)
self.Client.tls_insecure_set(True)

Expand Down Expand Up @@ -380,20 +380,25 @@ def _GetConnectionContextToTry(self) -> ConnectionContext:
if self.ConsecutivelyFailedConnectionAttempts > 6:
self.ConsecutivelyFailedConnectionAttempts = 0

# Get the connection mode set by the user. This defaults to local, but the user can explicitly set it to either.
connectionMode = self.Config.GetStr(Config.SectionBambu, Config.BambuConnectionMode, Config.BambuConnectionModeDefault)
if connectionMode == Config.BambuConnectionModeValueCloud:
# If the mode is set to cloud, try to connect via it.
# If a context can't be created, there's something wrong with the account info
# or a Bambu service issue. Since we have the local info, we can try it as well.
cloudContext = self._TryToGetCloudConnectContext()
if cloudContext is not None:
return cloudContext
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.
# 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 we are using a Bambu cloud connection, try to return a connection object for it.
# We always try to do this for the first few attempts, since if it's setup as a Cloud connection, a LAN connection most likely won't work.
cloudContext = self._TryToGetCloudConnectContext()
if cloudContext is not None:
return cloudContext

# If we aren't using a cloud connection or it failed, return the LAN hostname
# 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._GetLanConnectionContext(configIpOrHostname)
return self._GetLocalConnectionContext(configIpOrHostname)

# If we fail too many times, try to scan for the printer on the local subnet, the IP could have changed.
# Since we 100% identify the printer by the access token and printer SN, we can try to scan for it.
Expand All @@ -408,13 +413,13 @@ def _GetConnectionContextToTry(self) -> ConnectionContext:
ip = ips[0]
self.Logger.info(f"We found a new IP for this printer. [{configIpOrHostname} -> {ip}] Updating the config and using it to connect.")
self.Config.SetStr(Config.SectionCompanion, Config.CompanionKeyIpOrHostname, ip)
return self._GetLanConnectionContext(ip)
return self._GetLocalConnectionContext(ip)

# If we don't find anything, just use the config IP.
return self._GetLanConnectionContext(configIpOrHostname)
return self._GetLocalConnectionContext(configIpOrHostname)


def _GetLanConnectionContext(self, ipOrHostname) -> ConnectionContext:
def _GetLocalConnectionContext(self, ipOrHostname) -> ConnectionContext:
# The username is always the same, we use the local LAN access token.
return ConnectionContext(False, ipOrHostname, "bblp", self.LanAccessCode)

Expand All @@ -436,7 +441,9 @@ def _TryToGetCloudConnectContext(self) -> ConnectionContext:
if accessTokenResult.Status == LoginStatus.BadUserNameOrPassword:
self.Logger.error("The email address or password is wrong. Re-run the Bambu Connect installer or use the docker files to update your email address and password.")
elif accessTokenResult.Status == LoginStatus.TwoFactorAuthEnabled:
self.Logger.error("To factor auth is enabled on this account. Bambu Lab doesn't allow us to support two factor auth, so it must be disabled on your account or LAN Only mode must be used on the printer.")
self.Logger.error("Two factor auth is enabled on this account. Bambu Lab doesn't allow us to support two factor auth, so it must be disabled on your account or the local connection mode.")
elif accessTokenResult.Status == LoginStatus.EmailCodeRequired:
self.Logger.error("This account requires an email code to login. Bambu Lab doesn't allow us to support this, so you must use the local connection mode.")
else:
self.Logger.error("Unknown error, we will try again later.")
self.Logger.error("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
Expand Down
3 changes: 2 additions & 1 deletion bambu_octoeverywhere/bambucloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class LoginStatus(Enum):
Success = 0 # This is the only successful value
TwoFactorAuthEnabled = 1
BadUserNameOrPassword = 2
UnknownError = 3
EmailCodeRequired = 3
UnknownError = 4


# The result of a get access token request.
Expand Down
24 changes: 23 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@ services:
- SERIAL_NUMBER=XXXXXXXXXXXXXXX
# Find using the printer's display or use https://octoeverywhere.com/s/bambu-ip
- PRINTER_IP=XXX.XXX.XXX.XXX

# Optionally: If you want to connect via the Bambu Cloud, you can specify the following environment variables.
# By default the plugin will use the local connection mode, which is preferred.
# Bambu Cloud might not work for all printers and account types due to limitations by Bambu Labs. :(
#
# If you use Bambu Cloud, you MUST disable 2 factor authentication, because Bambu does not allow us to support it.
# Your Bambu email address and password are KEPT LOCALLY, securely on disk, and are NEVER SENT to the OctoEverywhere service
# - BAMBU_CLOUD_ACCOUNT_EMAIL=quinn@test.com
# - BAMBU_CLOUD_ACCOUNT_PASSWORD=supersecretpassword
# - CONNECTION_MODE=cloud

volumes:
# This can also be an absolue path, e.g. /var/octoeverywhere/plugin/data or /c/users/name/plugin/data
# This can also be an absolute path, e.g. /var/octoeverywhere/plugin/data or /c/users/name/plugin/data
- ./data:/data

# Add as many printers as you want!
Expand All @@ -23,6 +34,17 @@ services:
# - SERIAL_NUMBER=XXXXXXXXXXXXXXX
# # Find using the printer's display or use https://octoeverywhere.com/s/bambu-ip
# - PRINTER_IP=XXX.XXX.XXX.XXX
#
# # Optionally: If you want to connect via the Bambu Cloud, you can specify the following environment variables.
# # By default the plugin will use the local connection mode, which is preferred.
# # Bambu Cloud might not work for all printers and account types due to limitations by Bambu Labs. :(
# #
# # If you use Bambu Cloud, you MUST disable 2 factor authentication, because Bambu does not allow us to support it.
# # Your Bambu email address and password are KEPT LOCALLY, securely on disk, and are NEVER SENT to the OctoEverywhere service
# # - BAMBU_CLOUD_ACCOUNT_EMAIL=quinn@test.com
# # - BAMBU_CLOUD_ACCOUNT_PASSWORD=supersecretpassword
# # - CONNECTION_MODE=cloud
#
# volumes:
# # Specify a path mapping for the required persistent storage
# - ./data:/data
Loading

0 comments on commit 12a0f13

Please sign in to comment.