#include "stdafx.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;
|
}
|