LAPTOP-SNT8I5JK\Boounion
2025-06-06 7b3161e949bb32f91d5cebc9d52147198e360b6b
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include "stdafx.h"
#include "AxisManager.h"
#include <sstream>
#include <stdexcept>
#include <mutex>
 
// ¾²Ì¬³ÉÔ±³õʼ»¯
std::mutex AxisManager::m_mutex;
 
// »ñÈ¡µ¥ÀýʵÀý
AxisManager& AxisManager::getInstance() {
    static AxisManager instance;
    return instance;
}
 
AxisManager::AxisManager() : m_pDB(nullptr) {}
 
AxisManager::~AxisManager() {
    m_pDB = nullptr;
}
 
// ÉèÖÃÊý¾Ý¿âÁ¬½Ó
void AxisManager::setDatabase(BL::Database* db) {
    std::lock_guard<std::mutex> lock(m_mutex);
    m_pDB = db;
}
 
// ³õʼ»¯Öá±íºÍ¶¨Î»µã±í
bool AxisManager::initializeTables() {
    if (!m_pDB) {
        throw std::runtime_error("Database connection is not set.");
    }
 
    const std::string createAxesTableQuery = R"(
        CREATE TABLE IF NOT EXISTS axes (
            axis_id INTEGER PRIMARY KEY,
            axis_no TEXT NOT NULL,
            description TEXT NOT NULL,
            start_address TEXT,
            jog_distance REAL,
            manual_speed REAL,
            max_manual_speed REAL,
            min_manual_speed REAL,
            auto_speed REAL,
            max_auto_speed REAL,
            min_auto_speed REAL,
            acceleration_time REAL,
            deceleration_time REAL
        )
    )";
 
    const std::string createPositionsTableQuery = R"(
        CREATE TABLE IF NOT EXISTS positions (
            position_id INTEGER PRIMARY KEY AUTOINCREMENT,
            axis_id INTEGER NOT NULL,
            description TEXT,
            position_value REAL,
            plc_address TEXT,
            FOREIGN KEY (axis_id) REFERENCES axes(axis_id)
        )
    )";
 
    return m_pDB->executeQuery(createAxesTableQuery) && m_pDB->executeQuery(createPositionsTableQuery);
}
 
// ³õʼ»¯Ä¬ÈÏÊý¾Ý
bool AxisManager::initializeDefaultData() {
    if (!m_pDB) {
        throw std::runtime_error("Database connection is not set.");
    }
 
    for (int axisId = 1; axisId <= 12; ++axisId) {
        std::ostringstream axisQuery;
        axisQuery << "INSERT OR IGNORE INTO axes (axis_id, axis_no, description, start_address, jog_distance, manual_speed, "
            << "max_manual_speed, min_manual_speed, auto_speed, max_auto_speed, min_auto_speed, acceleration_time, deceleration_time) "
            << "VALUES (" << axisId << ", 'M" << axisId * 10 << "', 'Öá " << axisId << "', 'D" << (5090 + axisId * 2) << "', "
            << "0.5, 10.0, 20.0, 5.0, 15.0, 25.0, 10.0, 0.2, 0.3)";
        m_pDB->executeQuery(axisQuery.str());
    }
 
    for (int axisId = 1; axisId <= 12; ++axisId) {
        for (int positionIndex = 1; positionIndex <= 25; ++positionIndex) {
            std::ostringstream positionQuery;
            positionQuery << "INSERT OR IGNORE INTO positions (axis_id, description, position_value, plc_address) "
                << "VALUES (" << axisId << ", '¶¨Î»µã " << positionIndex << "', " << (positionIndex * 10.0) << ", "
                << "'D" << (5240 + positionIndex * 2) << "')";
            m_pDB->executeQuery(positionQuery.str());
        }
    }
 
    return true;
}
 
// Ìí¼Ó»ò¸üÐÂÖáÐÅÏ¢
bool AxisManager::saveAxis(int axisId, const std::string& axisNo, const std::string& description,
    const std::string& startAddress, double jogDistance, double manualSpeed,
    double maxManualSpeed, double minManualSpeed, double autoSpeed,
    double maxAutoSpeed, double minAutoSpeed, double accelerationTime,
    double decelerationTime) {
    if (!m_pDB) {
        throw std::runtime_error("Database connection is not set.");
    }
 
    std::ostringstream query;
    query << "INSERT INTO axes (axis_id, axis_no, description, start_address, jog_distance, manual_speed, "
        << "max_manual_speed, min_manual_speed, auto_speed, max_auto_speed, min_auto_speed, acceleration_time, deceleration_time) "
        << "VALUES (" << axisId << ", '" << axisNo << "', '" << description << "', '" << startAddress << "', "
        << jogDistance << ", " << manualSpeed << ", " << maxManualSpeed << ", " << minManualSpeed << ", " << autoSpeed
        << ", " << maxAutoSpeed << ", " << minAutoSpeed << ", " << accelerationTime << ", " << decelerationTime << ") "
        << "ON CONFLICT(axis_id) DO UPDATE SET "
        << "axis_no=excluded.axis_no, description=excluded.description, start_address=excluded.start_address, "
        << "jog_distance=excluded.jog_distance, manual_speed=excluded.manual_speed, max_manual_speed=excluded.max_manual_speed, "
        << "min_manual_speed=excluded.min_manual_speed, auto_speed=excluded.auto_speed, max_auto_speed=excluded.max_auto_speed, "
        << "min_auto_speed=excluded.min_auto_speed, acceleration_time=excluded.acceleration_time, deceleration_time=excluded.deceleration_time";
 
    std::lock_guard<std::mutex> lock(m_mutex);
    return m_pDB->executeQuery(query.str());
}
 
// »ñÈ¡µ¥¸öÖáÐÅÏ¢
std::vector<std::string> AxisManager::getAxis(int axisId) {
    if (!m_pDB) {
        throw std::runtime_error("Database connection is not set.");
    }
 
    std::ostringstream query;
    query << "SELECT * FROM axes WHERE axis_id = " << axisId;
 
    auto result = m_pDB->fetchResults(query.str());
    return !result.empty() ? result[0] : std::vector<std::string>();
}
 
// »ñÈ¡ËùÓÐÖáÐÅÏ¢
std::vector<std::vector<std::string>> AxisManager::getAllAxes() {
    if (!m_pDB) {
        throw std::runtime_error("Database connection is not set.");
    }
 
    return m_pDB->fetchResults("SELECT * FROM axes ORDER BY axis_id");
}
 
// É¾³ýÖ¸¶¨Öá
bool AxisManager::deleteAxis(int axisId) {
    if (!m_pDB) {
        throw std::runtime_error("Database connection is not set.");
    }
 
    std::ostringstream query;
    query << "DELETE FROM axes WHERE axis_id = " << axisId;
 
    std::lock_guard<std::mutex> lock(m_mutex);
    return m_pDB->executeQuery(query.str());
}
 
// Ìí¼Ó»ò¸üж¨Î»µã
bool AxisManager::savePosition(int axisId, const std::string& description, double positionValue, const std::string& plcAddress) {
    if (!m_pDB) {
        throw std::runtime_error("Database connection is not set.");
    }
 
    std::ostringstream query;
    query << "INSERT INTO positions (axis_id, description, position_value, plc_address) VALUES ("
        << axisId << ", '" << description << "', " << positionValue << ", '" << plcAddress << "') "
        << "ON CONFLICT(axis_id) DO UPDATE SET "
        << "description=excluded.description, position_value=excluded.position_value, plc_address=excluded.plc_address";
 
    std::lock_guard<std::mutex> lock(m_mutex);
    return m_pDB->executeQuery(query.str());
}
 
// »ñÈ¡ÖáµÄËùÓж¨Î»µã
std::vector<std::vector<std::string>> AxisManager::getPositions(int axisId, int pageNumber, int pageSize) {
    if (!m_pDB) {
        throw std::runtime_error("Database connection is not set.");
    }
 
    int offset = (pageNumber - 1) * pageSize;
    std::ostringstream query;
    query << "SELECT * FROM positions WHERE axis_id = " << axisId << " LIMIT " << pageSize << " OFFSET " << offset;
 
    return m_pDB->fetchResults(query.str());
}
 
// »ñÈ¡¶¨Î»µã×ÜÊý
int AxisManager::getTotalPositionCount(int axisId) {
    if (!m_pDB) {
        throw std::runtime_error("Database connection is not set.");
    }
 
    std::ostringstream query;
    query << "SELECT COUNT(*) FROM positions WHERE axis_id = " << axisId;
 
    auto result = m_pDB->fetchResults(query.str());
    return (!result.empty() && !result[0].empty()) ? std::stoi(result[0][0]) : 0;
}
 
// É¾³ýÖ¸¶¨¶¨Î»µã
bool AxisManager::deletePosition(int positionId) {
    if (!m_pDB) {
        throw std::runtime_error("Database connection is not set.");
    }
 
    std::ostringstream query;
    query << "DELETE FROM positions WHERE position_id = " << positionId;
 
    std::lock_guard<std::mutex> lock(m_mutex);
    return m_pDB->executeQuery(query.str());
}
 
// »ñÈ¡ËùÓеÄÖáID
std::vector<int> AxisManager::getUsedAxisIds() {
    if (!m_pDB) {
        throw std::runtime_error("Database connection is not set.");
    }
 
    std::vector<int> usedAxisIds;
    std::string query = "SELECT axis_id FROM axes ORDER BY axis_id";
    auto results = m_pDB->fetchResults(query);
 
    for (const auto& row : results) {
        if (!row.empty()) {
            usedAxisIds.push_back(std::stoi(row[0]));
        }
    }
 
    return usedAxisIds;
}
 
// »ñÈ¡ËùÓÐÖáµÄÖáNO
std::vector<std::string> AxisManager::getAllAxisNumbers() {
    if (!m_pDB) {
        throw std::runtime_error("Database connection is not set.");
    }
 
    std::vector<std::string> axisNumbers;
    std::string query = "SELECT axis_no FROM axes ORDER BY axis_id";
    auto results = m_pDB->fetchResults(query);
 
    for (const auto& row : results) {
        if (!row.empty()) {
            axisNumbers.push_back(row[0]);
        }
    }
 
    return axisNumbers;
}