Skip to content
This repository has been archived by the owner on May 2, 2023. It is now read-only.

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
macdylan committed Dec 16, 2020
1 parent d06bd28 commit 3e9cc8f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
7 changes: 4 additions & 3 deletions SM2GCodeWriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,10 @@ def write(self, stream, nodes, mode = MeshWriter.OutputMode.TextMode):
stream.write(header_buffer[2]) # Filament used
stream.write(header_buffer[3]) # Layer height
stream.write("\n\n;header_type: 3dp\n")
stream.write(";thumbnail: data:image/png;base64,")
stream.write(base64_bytes.decode("ascii"))
stream.write("\n")
if base64_bytes:
stream.write(";thumbnail: data:image/png;base64,")
stream.write(base64_bytes.decode("ascii"))
stream.write("\n")
stream.write(";file_total_lines: %s\n" % model_line_count)
stream.write(";estimated_time(s): %s\n" % estiTime)
stream.write(";nozzle_temperature(°C): %s\n" % printTemp)
Expand Down
41 changes: 31 additions & 10 deletions SM2OutputDeviceManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import socket
import requests
from io import StringIO
from PyQt5.QtCore import QTimer

from cura.CuraApplication import CuraApplication
from cura.PrinterOutput.NetworkedPrinterOutputDevice import NetworkedPrinterOutputDevice, AuthState
Expand Down Expand Up @@ -144,7 +145,7 @@ def _onAuthenticationStateChanged(self):
self._need_auth.hide()

def requestWrite(self, nodes, file_name=None, limit_mimetypes=False, file_handler=None, filter_by_machine=False, **kwargs) -> None:
if self._progress.visible or self._writing:
if self._progress.visible or self._need_auth.visible or self._writing:
return

self.writeStarted.emit(self)
Expand All @@ -162,15 +163,21 @@ def requestWrite(self, nodes, file_name=None, limit_mimetypes=False, file_handle

def _onWriteJobFinished(self, job):
self._writing = False
self._auth_token = self.connect()
self._startUpload()

def connect(self) -> str:
super().connect()
try:
conn = requests.post("http://" + self._address + self._api_prefix + "/connect",
data={"token": self._auth_token})
Logger.log("d", "/connect: %d from %s", conn.status_code, self._address)
if conn.status_code == 200:
return conn.json().get("token")
elif conn.status_code == 403 and self._auth_token:
# expired
self._auth_token = ""
return self.connect()
else:
Message(text="Please check the touchscreen and try again.", lifetime=10, dismissable=True).show()
return self._auth_token
Expand All @@ -182,17 +189,18 @@ def connect(self) -> str:
def check_status(self):
try:
conn = requests.get("http://" + self._address + self._api_prefix + "/status", params={"token": self._auth_token})
Logger.log("d", "check_status: %s", conn.status_code)
Logger.log("d", "/status: %d from %s", conn.status_code, self._address)
if conn.status_code == 200:
resp = conn.json()
status = resp.get("status", "UNKNOWN")

status = conn.json().get("status", "UNKNOWN")
Logger.log("d", "Printer status is %s" % status)
if status == "IDLE":
self.setConnectionState(ConnectionState.Connected)
elif status in ("RUNNING", "PAUSED", "STOPPED"):
self.setConnectionState(ConnectionState.Busy)
else:
self.setConnectionState(ConnectionState.Error)

self._setAuthState(AuthState.Authenticated)

if conn.status_code == 401:
Expand All @@ -205,7 +213,6 @@ def check_status(self):
self._setAuthState(AuthState.NotAuthenticated)

def _startUpload(self):
self._auth_token = self.connect()
Logger.log("d", "Token: %s", self._auth_token)
if not self._auth_token:
return
Expand All @@ -229,18 +236,18 @@ def _startUpload(self):
self._createFormPart("name=token", self._auth_token.encode()),
self._createFormPart("name=file; filename=\"{}\"".format(file_name), self._gcode_stream.getvalue().encode())
]
self._gcode_stream = StringIO()
self.postFormWithParts("/upload", parts,
on_finished=self._onUploadCompleted,
on_progress=self._onUploadProgress)
self._gcode_stream = StringIO()

def _onUploadCompleted(self, reply):
self._progress.hide()
if not reply.error():
Message(
title="Sent to {}".format(self._id),
text="Start print on the touchscreen.",
lifetime=6).show()
lifetime=0).show()
self.writeFinished.emit()
else:
Message(title="Error", text=reply.errorString(), lifetime=0, dismissable=True).show()
Expand Down Expand Up @@ -287,9 +294,23 @@ def __init__(self, device) -> None:
use_inactivity_timer = False
)
self._device = device
self.addAction("", "Continue", "", "")
self.actionTriggered.connect(self._onCheck)
self.setProgress(-1)
# self.addAction("", "Continue", "", "")
# self.actionTriggered.connect(self._onCheck)
self._gTimer = QTimer()
self._gTimer.setInterval(1.5 * 1000)
self._gTimer.timeout.connect(lambda: self._onCheck(None, None))
self.inactivityTimerStart.connect(self._startTimer)
self.inactivityTimerStop.connect(self._stopTimer)

def _startTimer(self):
if self._gTimer and not self._gTimer.isActive():
self._gTimer.start()

def _stopTimer(self):
if self._gTimer and self._gTimer.isActive():
self._gTimer.stop()

def _onCheck(self, messageId, actionId):
self._device.checkAndStartUpload()
self.hide()
# self.hide()

0 comments on commit 3e9cc8f

Please sign in to comment.