File indexing completed on 2024-05-12 04:41:11

0001 /* AtCore KDE Libary for 3D Printers
0002     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0003     SPDX-FileCopyrightText: 2016-2018 Chris Rizzitello <rizzitello@kde.org>
0004     SPDX-FileCopyrightText: 2016 Tomaz Canabrava <tcanabrava@kde.org>
0005     SPDX-FileCopyrightText: 2016-2018 Patrick José Pereira <patrickjp@kde.org>
0006     SPDX-FileCopyrightText: 2017 Lays Rodrigues <lays.rodrigues@kde.org>
0007 */
0008 
0009 #include <QLoggingCategory>
0010 #include <QString>
0011 
0012 #include "atcore.h"
0013 #include "marlinplugin.h"
0014 
0015 Q_LOGGING_CATEGORY(MARLIN_PLUGIN, "org.kde.atelier.core.firmware.marlin")
0016 
0017 QString MarlinPlugin::name() const
0018 {
0019     return QStringLiteral("Marlin");
0020 }
0021 
0022 bool MarlinPlugin::isSdSupported() const
0023 {
0024     return true;
0025 }
0026 
0027 MarlinPlugin::MarlinPlugin()
0028 {
0029     qCDebug(MARLIN_PLUGIN) << MarlinPlugin::name() << " plugin loaded!";
0030 }
0031 
0032 void MarlinPlugin::validateCommand(const QString &lastMessage)
0033 {
0034     if (lastMessage.contains(QStringLiteral("End file list"))) {
0035         core()->setReadingSdCardList(false);
0036     } else if (core()->isReadingSdCardList()) {
0037         // Below is to not add directories
0038         if (!lastMessage.endsWith(QChar::fromLatin1('/'))) {
0039             QString fileName = lastMessage;
0040             fileName.chop(fileName.length() - fileName.lastIndexOf(QChar::fromLatin1(' ')));
0041             core()->appendSdCardFileList(fileName);
0042         }
0043     } else {
0044         if (lastMessage.contains(QStringLiteral("SD card ok"))) {
0045             core()->setSdMounted(true);
0046         } else if (lastMessage.contains(QStringLiteral("SD init fail"))) {
0047             core()->setSdMounted(false);
0048         } else if (lastMessage.contains(QStringLiteral("Begin file list"))) {
0049             core()->setSdMounted(true);
0050             core()->clearSdCardFileList();
0051             core()->setReadingSdCardList(true);
0052         } else if (lastMessage.contains(QStringLiteral("SD printing byte"))) {
0053             if (core()->state() != AtCore::BUSY) {
0054                 // This should only happen if Attached to an Sd printing machine.
0055                 // Just tell the client were starting a job like normal.
0056                 // For this to work the client should check if sdCardPrintStatus()
0057                 // Upon the Connection to a known firmware with sdSupport
0058                 core()->setState(AtCore::STARTPRINT);
0059                 core()->setState(AtCore::BUSY);
0060             }
0061             QString temp = lastMessage;
0062             temp.replace(QStringLiteral("SD printing byte"), QString());
0063             qlonglong total = temp.mid(temp.lastIndexOf(QChar::fromLatin1('/')) + 1, temp.length() - temp.lastIndexOf(QChar::fromLatin1('/'))).toLongLong();
0064             if (total) {
0065                 temp.chop(temp.length() - temp.lastIndexOf(QChar::fromLatin1('/')));
0066                 qlonglong remaining = total - temp.toLongLong();
0067                 float progress = float(total - remaining) * 100 / float(total);
0068                 Q_EMIT core()->printProgressChanged(progress);
0069                 if (progress >= 100) {
0070                     core()->setState(AtCore::FINISHEDPRINT);
0071                     core()->setState(AtCore::IDLE);
0072                 }
0073             } else {
0074                 core()->setState(AtCore::FINISHEDPRINT);
0075                 core()->setState(AtCore::IDLE);
0076             }
0077         }
0078         if (lastMessage.contains(QStringLiteral("ok"))) {
0079             Q_EMIT readyForCommand();
0080         }
0081     }
0082 }