| ¶Ô±ÈÐÂÎļþ |
| | |
| | | #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; |
| | | } |