1
0
forked from BRT/arc
arc/main.cpp
slava 1672216bdc Integrate device data management with database access
Added SQLite database support for managing device information and common data. Implemented DAOs for handling device and common data operations, such as inserting/updating devices and tracking the last scan time. Enhanced `DeviceScreener` to read device info, update database records, and take screenshots.
2025-04-17 22:10:11 +07:00

80 lines
3.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <QApplication>
#include <QThread>
#include "views/MyWindow.h"
#include <tesseract/baseapi.h>
#include "database/database_manager.h"
#include "database/dao/common_data_dao.h"
#include "services/device_screener.h"
void setupWorkerAndThread(QCoreApplication& app) {
auto* thread = new QThread;
auto* deviceScreenerWorker = new DeviceScreener;
// Перемещаем worker в новый поток thread.
// Это значит, что все слоты worker, вызываемые через connect(), теперь будут исполняться в этом фоне-потоке.
deviceScreenerWorker->moveToThread(thread);
// Когда поток запустится, автоматически вызовется worker->start() внутри фонового потока.
QObject::connect(thread, &QThread::started, deviceScreenerWorker, &DeviceScreener::start);
// 🔁 Эти 3 строки делают автоматическую зачистку:
// Когда worker закончит работу (emit finished()), мы:
// - останавливаем поток (thread->quit())
// - удаляем worker (deleteLater)
// - удаляем thread (deleteLater)
// Это защищает от утечек памяти и оставшихся потоков после завершения.
QObject::connect(deviceScreenerWorker, &DeviceScreener::finished, thread, &QThread::quit);
QObject::connect(deviceScreenerWorker, &DeviceScreener::finished, deviceScreenerWorker, &QObject::deleteLater);
QObject::connect(thread, &QThread::finished, thread, &QThread::deleteLater);
// Когда пользователь закрывает приложение (Ctrl+C, exit() и т.п.), мы вызываем worker->stop() — ты сам пишешь, что в этом методе.
//Обычно он просто ставит флаг m_running = false, чтобы остановить бесконечный цикл в start()
QObject::connect(&app, &QCoreApplication::aboutToQuit, [=]() {
deviceScreenerWorker->stop();
});
thread->start();
}
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
// Setup worker and thread
setupWorkerAndThread(app);
const QDateTime current = QDateTime::currentDateTimeUtc();
qDebug() << "Обновление lastScan:" << CommonDataDAO::updateLastScan(current);
const QDateTime savedTime = CommonDataDAO::getLastScan();
qDebug() << "Сохранившееся время:" << savedTime.toString();
//
// // tesseract, tesseract-lang
// QImage image("./images/img.png");
// if (image.isNull()) {
// qDebug() << "Cant load the image";
// return -1;
// }
//
// image = image.convertToFormat(QImage::Format_Grayscale8);
//
// tesseract::TessBaseAPI tess;
// if (tess.Init(nullptr, "rus")) {
// qDebug() << "Ошибка инициализации Tesseract с русским языком....";
// return -1;
// }
//
// tess.SetImage(image.bits(), image.width(), image.height(), 1, image.bytesPerLine());
//
// QString result = QString::fromUtf8(tess.GetUTF8Text());
// qDebug() << "Распознанный текст:" << result;
MyWindow window;
window.show();
return app.exec();
}