LAPTOP-SNT8I5JK\Boounion
2024-12-02 aedb3b85fed48cb2cf0abb5fafa8e7591644c9f4
SourceCode/Bond/BondEq/DBManager/AxisManager.cpp
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,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;
}