From aedb3b85fed48cb2cf0abb5fafa8e7591644c9f4 Mon Sep 17 00:00:00 2001
From: LAPTOP-SNT8I5JK\Boounion <Chenluhua@qq.com>
Date: 星期一, 02 十二月 2024 08:53:06 +0800
Subject: [PATCH] Merge branch 'liuyang' into clh
---
SourceCode/Bond/BondEq/DBManager/AxisManager.cpp | 246 +++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 246 insertions(+), 0 deletions(-)
diff --git a/SourceCode/Bond/BondEq/DBManager/AxisManager.cpp b/SourceCode/Bond/BondEq/DBManager/AxisManager.cpp
new file mode 100644
index 0000000..e3e1ae5
--- /dev/null
+++ b/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;
+}
--
Gitblit v1.9.3