-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatamanager.cpp
82 lines (71 loc) · 2.07 KB
/
datamanager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "datamanager.h"
#include <QDir>
#include <QDebug>
#include <QQmlContext>
#include <QProcess>
DataManager::DataManager(QObject *parent)
: QObject(parent)
{
// Khởi tạo dữ liệu mặc định
m_masterData = " ";
m_slaveData = " ";
enable = false;
spi_error = false;
readDataFromFile();
}
QString DataManager::masterData() const
{
return m_masterData;
}
QString DataManager::slaveData() const
{
return m_slaveData;
}
void DataManager::updateData()
{
readDataFromFile();
emit dataChanged();
}
void DataManager::runExecutable()
{
enable = true;
QProcess process;
QString executable = "D:/QT_DEVELOPMENT/Udemy_Intermediate/Read_Data_Mock_Project/build/Desktop_Qt_6_7_0_MSVC2019_64bit-Debug/test_spi.run";
if (!QFile::exists(executable)) {
qDebug() << "Executable file does not exist!";
return;
}
process.setProgram(executable);
bool started = process.startDetached();
if (!started) {
qDebug() << "Failed to start the executable!";
} else {
qDebug() << "Executable started successfully!";
}
}
void DataManager::readDataFromFile()
{
if(enable){
QString path = QDir::currentPath() + QDir::separator() + "data.txt";
path.replace("\\","/");
QFile file(path);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
if (line.startsWith("Master data send")) {
m_masterData = line.mid(QString("Master data send").length()).trimmed();
} else if (line.startsWith("Slave data send")) {
m_slaveData = line.mid(QString("Slave data send").length()).trimmed();
} else if (line.startsWith("Error!")){
spi_error = true;
}
if(line.isEmpty()){
m_masterData = "Empty";
m_slaveData = "Empty";
}
}
file.close();
}
}
}