mrDarker
2025-04-01 1bc4fecb9cc1641ed3ad0a2fda30766fc06fb13e
SourceCode/Bond/Servo/SECSRuntimeManager.cpp
@@ -104,8 +104,8 @@
        return 1;
    }
    // 构建删除所有数据的 SQL 语句
    std::string deleteSQL = "DELETE FROM " + tableName + ";";
    // 构建删除所有数据的 SQL 语句, 重置自增 ID
    std::string deleteSQL = "TRUNCATE TABLE " + tableName + ";";
    if (!m_pDB->executeQuery(deleteSQL)) {
        return 2;
    }
@@ -789,6 +789,157 @@
    }
}
// 添加 SystemEC 数据
int SECSRuntimeManager::addSystemEC(int nID, const std::string& sName, const std::string& sDataType, int nMinValue, int nMaxValue, int nDefaultVal, const std::string& sUnit, const std::string& sRemark, int nSystemID) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return 1; // 数据库未连接
    }
    if (isIDDuplicate(nID)) {
        return 2; // ID 重复
    }
    if (isNameDuplicate(sName)) {
        return 3; // Name 重复
    }
    // 构建插入 SQL 语句
    std::string insertSQL = "INSERT INTO SystemEC (ID, Name, DataType, MinValue, MaxValue, DefaultVal, Unit, Remark, SystemID) VALUES ("
        + std::to_string(nID) + ", '" + sName + "', '" + sDataType + "', "
        + (nMinValue >= 0 ? std::to_string(nMinValue) : "NULL") + ", "
        + (nMaxValue >= 0 ? std::to_string(nMaxValue) : "NULL") + ", "
        + (nDefaultVal >= 0 ? std::to_string(nDefaultVal) : "NULL") + ", "
        + ((sUnit == "NULL" || sUnit.empty()) ? "NULL" : "'" + sUnit + "'") + ", '"
        + sRemark + "', " + std::to_string(nSystemID) + ");";
    if (!m_pDB->executeQuery(insertSQL)) {
        return 4; // 插入失败
    }
    return 0; // 插入成功
}
// 查询指定 ID 的 SystemEC 数据
std::vector<std::vector<std::string>> SECSRuntimeManager::getSystemECByID(int nID) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return {};
    }
    std::string querySQL = "SELECT * FROM SystemEC WHERE ID = " + std::to_string(nID) + ";";
    return m_pDB->fetchResults(querySQL);
}
// 查询所有 SystemEC 数据
std::vector<std::vector<std::string>> SECSRuntimeManager::getAllSystemEC() {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return {};
    }
    std::string querySQL = "SELECT * FROM SystemEC;";
    return m_pDB->fetchResults(querySQL);
}
// 更新指定 ID 的 SystemEC 数据
int SECSRuntimeManager::updateSystemEC(int nID, int nNewID, const std::string& sName, const std::string& sDataType, int nMinValue, int nMaxValue, int nDefaultVal, const std::string& sUnit, const std::string& sRemark, int nSystemID) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return 1;
    }
    // 检查是否存在该 ID
    if (!isIDDuplicate(nID)) {
        return 2;
    }
    // 检查新的 ID 是否已存在
    if (isIDDuplicate(nNewID) && nNewID != nID) {
        return 3;
    }
    // 构建更新 SQL 语句
    std::string updateSQL = "UPDATE SystemEC SET ";
    bool firstField = true;
    if (nNewID > 0 && nNewID != nID) {
        updateSQL += "ID = " + std::to_string(nNewID);
        firstField = false;
    }
    if (!sName.empty()) {
        if (!firstField) updateSQL += ", ";
        updateSQL += "Name = '" + sName + "'";
        firstField = false;
    }
    if (!sDataType.empty()) {
        if (!firstField) updateSQL += ", ";
        updateSQL += "DataType = '" + sDataType + "'";
        firstField = false;
    }
    if (nMinValue >= 0) {
        if (!firstField) updateSQL += ", ";
        updateSQL += "MinValue = " + std::to_string(nMinValue);
        firstField = false;
    }
    if (nMaxValue >= 0) {
        if (!firstField) updateSQL += ", ";
        updateSQL += "MaxValue = " + std::to_string(nMaxValue);
        firstField = false;
    }
    if (nDefaultVal >= 0) {
        if (!firstField) updateSQL += ", ";
        updateSQL += "DefaultVal = " + std::to_string(nDefaultVal);
        firstField = false;
    }
    if (sUnit != "NULL" && !sUnit.empty()) {
        if (!firstField) updateSQL += ", ";
        updateSQL += "Unit = '" + sUnit + "'";
        firstField = false;
    }
    else if (sUnit == "NULL") {
        if (!firstField) updateSQL += ", ";
        updateSQL += "Unit = NULL";
        firstField = false;
    }
    if (!sRemark.empty()) {
        if (!firstField) updateSQL += ", ";
        updateSQL += "Remark = '" + sRemark + "'";
        firstField = false;
    }
    if (nSystemID > 0) {
        if (!firstField) updateSQL += ", ";
        updateSQL += "SystemID = " + std::to_string(nSystemID);
    }
    updateSQL += " WHERE ID = " + std::to_string(nID) + ";";
    if (!m_pDB->executeQuery(updateSQL)) {
        return 4;
    }
    return 0;
}
// 删除指定 ID 的 SystemEC 数据
int SECSRuntimeManager::deleteSystemECByID(int nID) {
    return deleteDataByID("SystemEC", nID);
}
// 删除所有 SystemEC 数据
int SECSRuntimeManager::deleteAllSystemEC() {
    return deleteAllDataFromTable("SystemEC");
}
// 初始化 EqpEC 表
void SECSRuntimeManager::initEqpECTable() {
    std::lock_guard<std::mutex> lock(m_mutex);
@@ -816,6 +967,168 @@
    }
}
// 添加 EqpEC 数据
int SECSRuntimeManager::addEqpEC(int nID, const std::string& sName, const std::string& sDataType, int nMinValue, int nMaxValue, int nDefaultValue, const std::string& sUnit, const std::string& sRemark, int nSeqNo, int nLength, int bCanUpdateByHost) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return 1; // 数据库未连接
    }
    if (isIDDuplicate(nID)) {
        return 2; // ID 重复
    }
    if (isNameDuplicate(sName)) {
        return 3; // Name 重复
    }
    // 构建插入 SQL 语句
    std::string insertSQL = "INSERT INTO EqpEC (ID, Name, DataType, MinValue, MaxValue, DefaultValue, Unit, Remark, SeqNo, Length, CanUpdateByHost) VALUES ("
        + std::to_string(nID) + ", '" + sName + "', '" + sDataType + "', "
        + (nMinValue >= 0 ? std::to_string(nMinValue) : "NULL") + ", "
        + (nMaxValue >= 0 ? std::to_string(nMaxValue) : "NULL") + ", "
        + (nDefaultValue >= 0 ? std::to_string(nDefaultValue) : "NULL") + ", "
        + ((sUnit == "NULL" || sUnit.empty()) ? "NULL" : "'" + sUnit + "'") + ", '"
        + sRemark + "', " + std::to_string(nSeqNo) + ", "
        + std::to_string(nLength) + ", " + std::to_string(bCanUpdateByHost) + ");";
    if (!m_pDB->executeQuery(insertSQL)) {
        return 4; // 插入失败
    }
    return 0; // 插入成功
}
// 查询指定 ID 的 EqpEC 数据
std::vector<std::vector<std::string>> SECSRuntimeManager::getEqpECByID(int nID) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return {};
    }
    std::string querySQL = "SELECT * FROM EqpEC WHERE ID = " + std::to_string(nID) + ";";
    return m_pDB->fetchResults(querySQL);
}
// 查询所有 EqpEC 数据
std::vector<std::vector<std::string>> SECSRuntimeManager::getAllEqpEC() {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return {};
    }
    std::string querySQL = "SELECT * FROM EqpEC;";
    return m_pDB->fetchResults(querySQL);
}
// 更新指定 ID 的 EqpEC 数据
int SECSRuntimeManager::updateEqpEC(int nID, int nNewID, const std::string& sName, const std::string& sDataType, int nMinValue, int nMaxValue, int nDefaultValue, const std::string& sUnit, const std::string& sRemark, int nSeqNo, int nLength, int bCanUpdateByHost) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return 1;
    }
    // 检查是否存在该 ID
    if (!isIDDuplicate(nID)) {
        return 2;
    }
    // 检查新的 ID 是否已存在
    if (isIDDuplicate(nNewID) && nNewID != nID) {
        return 3;
    }
    // 构建更新 SQL 语句
    std::string updateSQL = "UPDATE EqpEC SET ";
    bool firstField = true;
    if (nNewID > 0 && nNewID != nID) {
        updateSQL += "ID = " + std::to_string(nNewID);
        firstField = false;
    }
    if (!sName.empty()) {
        if (!firstField) updateSQL += ", ";
        updateSQL += "Name = '" + sName + "'";
        firstField = false;
    }
    if (!sDataType.empty()) {
        if (!firstField) updateSQL += ", ";
        updateSQL += "DataType = '" + sDataType + "'";
        firstField = false;
    }
    if (nMinValue >= 0) {
        if (!firstField) updateSQL += ", ";
        updateSQL += "MinValue = " + std::to_string(nMinValue);
        firstField = false;
    }
    if (nMaxValue >= 0) {
        if (!firstField) updateSQL += ", ";
        updateSQL += "MaxValue = " + std::to_string(nMaxValue);
        firstField = false;
    }
    if (nDefaultValue >= 0) {
        if (!firstField) updateSQL += ", ";
        updateSQL += "DefaultValue = " + std::to_string(nDefaultValue);
        firstField = false;
    }
    if (sUnit != "NULL" && !sUnit.empty()) {
        if (!firstField) updateSQL += ", ";
        updateSQL += "Unit = '" + sUnit + "'";
        firstField = false;
    }
    else if (sUnit == "NULL") {
        if (!firstField) updateSQL += ", ";
        updateSQL += "Unit = NULL";
        firstField = false;
    }
    if (!sRemark.empty()) {
        if (!firstField) updateSQL += ", ";
        updateSQL += "Remark = '" + sRemark + "'";
        firstField = false;
    }
    if (nSeqNo > 0) {
        if (!firstField) updateSQL += ", ";
        updateSQL += "SeqNo = " + std::to_string(nSeqNo);
    }
    if (nLength > 0) {
        if (!firstField) updateSQL += ", ";
        updateSQL += "Length = " + std::to_string(nLength);
    }
    if (bCanUpdateByHost >= 0) {
        if (!firstField) updateSQL += ", ";
        updateSQL += "CanUpdateByHost = " + std::to_string(bCanUpdateByHost);
    }
    updateSQL += " WHERE ID = " + std::to_string(nID) + ";";
    if (!m_pDB->executeQuery(updateSQL)) {
        return 4;
    }
    return 0;
}
// 删除指定 ID 的 EqpEC 数据
int SECSRuntimeManager::deleteEqpECByID(int nID) {
    return deleteDataByID("EqpEC", nID);
}
// 删除所有 EqpEC 数据
int SECSRuntimeManager::deleteAllEqpEC() {
    return deleteAllDataFromTable("EqpEC");
}
// 初始化 SystemEvent 表
void SECSRuntimeManager::initSystemEventTable() {
    std::lock_guard<std::mutex> lock(m_mutex);
@@ -836,6 +1149,110 @@
    }
}
// 添加 SystemEvent 数据
int SECSRuntimeManager::addSystemEvent(int nCEID, const std::string& sName, const std::string& sRemark, int nSystemID) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return 1; // 数据库未连接
    }
    // 构建 SQL 插入语句
    std::string insertSQL = "INSERT INTO SystemEvent (CEID, Name, Remark, SystemID) VALUES ("
        + std::to_string(nCEID) + ", '" + sName + "', '" + sRemark + "', " + std::to_string(nSystemID) + ");";
    if (!m_pDB->executeQuery(insertSQL)) {
        return 2; // 插入失败
    }
    return 0; // 插入成功
}
// 查询指定 CEID 的 SystemEvent 数据
std::vector<std::vector<std::string>> SECSRuntimeManager::getSystemEventByID(int nCEID) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return {};
    }
    std::string querySQL = "SELECT * FROM SystemEvent WHERE CEID = " + std::to_string(nCEID) + ";";
    return m_pDB->fetchResults(querySQL);
}
// 查询所有 SystemEvent 数据
std::vector<std::vector<std::string>> SECSRuntimeManager::getAllSystemEvents() {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return {};
    }
    std::string querySQL = "SELECT * FROM SystemEvent;";
    return m_pDB->fetchResults(querySQL);
}
// 更新指定 CEID 的 SystemEvent 数据
int SECSRuntimeManager::updateSystemEvent(int nCEID, int nNewCEID, const std::string& sName, const std::string& sRemark, int nSystemID) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return 1;
    }
    // 构建更新 SQL 语句
    std::string updateSQL = "UPDATE SystemEvent SET ";
    bool firstField = true;
    if (nNewCEID > 0 && nNewCEID != nCEID) {
        updateSQL += "CEID = " + std::to_string(nNewCEID);
        firstField = false;
    }
    if (!sName.empty()) {
        if (!firstField) updateSQL += ", ";
        updateSQL += "Name = '" + sName + "'";
        firstField = false;
    }
    if (!sRemark.empty()) {
        if (!firstField) updateSQL += ", ";
        updateSQL += "Remark = '" + sRemark + "'";
        firstField = false;
    }
    if (nSystemID > 0) {
        if (!firstField) updateSQL += ", ";
        updateSQL += "SystemID = " + std::to_string(nSystemID);
    }
    updateSQL += " WHERE CEID = " + std::to_string(nCEID) + ";";
    if (!m_pDB->executeQuery(updateSQL)) {
        return 2;
    }
    return 0;
}
// 删除指定 CEID 的 SystemEvent 数据
int SECSRuntimeManager::deleteSystemEventByID(int nCEID) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return 1; // 数据库未连接,返回 1
    }
    // 构建删除 SQL 语句
    std::string deleteSQL = "DELETE FROM SystemEvent WHERE CEID = " + std::to_string(nCEID) + ";";
    if (!m_pDB->executeQuery(deleteSQL)) {
        return 2; // 删除失败,返回 2
    }
    return 0; // 删除成功,返回 0
}
// 删除所有 SystemEvent 数据
int SECSRuntimeManager::deleteAllSystemEvents() {
    return deleteAllDataFromTable("SystemEvent");
}
// 初始化 EqpEvent 表
void SECSRuntimeManager::initEqpEventTable() {
    std::lock_guard<std::mutex> lock(m_mutex);
@@ -856,6 +1273,105 @@
    }
}
// 添加 EqpEvent 数据
int SECSRuntimeManager::addEqpEvent(const std::string& sName, const std::string& sRemark, int nBitNo) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return 1; // 数据库未连接
    }
    // 构建 SQL 插入语句
    std::string insertSQL = "INSERT INTO EqpEvent (Name, Remark, BitNo) VALUES ('"
        + sName + "', '" + sRemark + "', " + std::to_string(nBitNo) + ");";
    if (!m_pDB->executeQuery(insertSQL)) {
        return 2; // 插入失败
    }
    return 0; // 插入成功
}
// 查询指定 CEID 的 EqpEvent 数据
std::vector<std::vector<std::string>> SECSRuntimeManager::getEqpEventByID(int nCEID) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return {};
    }
    std::string querySQL = "SELECT * FROM EqpEvent WHERE CEID = " + std::to_string(nCEID) + ";";
    return m_pDB->fetchResults(querySQL);
}
// 查询所有 EqpEvent 数据
std::vector<std::vector<std::string>> SECSRuntimeManager::getAllEqpEvents() {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return {};
    }
    std::string querySQL = "SELECT * FROM EqpEvent;";
    return m_pDB->fetchResults(querySQL);
}
// 更新指定 CEID 的 EqpEvent 数据
int SECSRuntimeManager::updateEqpEvent(int nCEID, const std::string& sName, const std::string& sRemark, int nBitNo) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return 1;
    }
    // 构建更新 SQL 语句
    std::string updateSQL = "UPDATE EqpEvent SET ";
    bool firstField = true;
    if (!sName.empty()) {
        if (!firstField) updateSQL += ", ";
        updateSQL += "Name = '" + sName + "'";
        firstField = false;
    }
    if (!sRemark.empty()) {
        if (!firstField) updateSQL += ", ";
        updateSQL += "Remark = '" + sRemark + "'";
        firstField = false;
    }
    if (nBitNo >= 0) {
        if (!firstField) updateSQL += ", ";
        updateSQL += "BitNo = " + std::to_string(nBitNo);
    }
    updateSQL += " WHERE CEID = " + std::to_string(nCEID) + ";";
    if (!m_pDB->executeQuery(updateSQL)) {
        return 2;
    }
    return 0;
}
// 删除指定 CEID 的 EqpEvent 数据
int SECSRuntimeManager::deleteEqpEventByID(int nCEID) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return 1; // 数据库未连接,返回 1
    }
    // 构建删除 SQL 语句
    std::string deleteSQL = "DELETE FROM EqpEvent WHERE CEID = " + std::to_string(nCEID) + ";";
    if (!m_pDB->executeQuery(deleteSQL)) {
        return 2; // 删除失败,返回 2
    }
    return 0; // 删除成功,返回 0
}
// 删除所有 EqpEvent 数据
int SECSRuntimeManager::deleteAllEqpEvents() {
    return deleteAllDataFromTable("EqpEvent");
}
// 初始化 EventLink 表
void SECSRuntimeManager::initEventLinkTable() {
   std::lock_guard<std::mutex> lock(m_mutex);